mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
Refactor app layouts and common components
This commit is contained in:
parent
6add573df6
commit
44678a61c2
22 changed files with 353 additions and 262 deletions
45
src/components/ListItemLink.tsx
Normal file
45
src/components/ListItemLink.tsx
Normal file
|
@ -0,0 +1,45 @@
|
|||
import ListItemButton, { ListItemButtonBaseProps } from '@mui/material/ListItemButton';
|
||||
import React, { FC } from 'react';
|
||||
import { Link, useLocation, useSearchParams } from 'react-router-dom';
|
||||
|
||||
interface ListItemLinkProps extends ListItemButtonBaseProps {
|
||||
to: string
|
||||
}
|
||||
|
||||
const isMatchingParams = (routeParams: URLSearchParams, currentParams: URLSearchParams) => {
|
||||
for (const param of routeParams) {
|
||||
if (currentParams.get(param[0]) !== param[1]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
const ListItemLink: FC<ListItemLinkProps> = ({
|
||||
children,
|
||||
to,
|
||||
...params
|
||||
}) => {
|
||||
const location = useLocation();
|
||||
const [ searchParams ] = useSearchParams();
|
||||
|
||||
const [ toPath, toParams ] = to.split('?');
|
||||
// eslint-disable-next-line compat/compat
|
||||
const toSearchParams = new URLSearchParams(`?${toParams}`);
|
||||
|
||||
const selected = location.pathname === toPath && (!toParams || isMatchingParams(toSearchParams, searchParams));
|
||||
|
||||
return (
|
||||
<ListItemButton
|
||||
component={Link}
|
||||
to={to}
|
||||
selected={selected}
|
||||
{...params}
|
||||
>
|
||||
{children}
|
||||
</ListItemButton>
|
||||
);
|
||||
};
|
||||
|
||||
export default ListItemLink;
|
Loading…
Add table
Add a link
Reference in a new issue