2021-10-24 21:34:38 +05:30
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2023-05-18 13:17:41 +02:00
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
2021-10-24 21:34:38 +05:30
|
|
|
|
2024-07-11 17:22:52 +02:00
|
|
|
import Drawer from '@mui/material/Drawer';
|
|
|
|
|
import List from '@mui/material/List';
|
2024-04-14 15:50:12 +02:00
|
|
|
import ListItemIcon from '@mui/material/ListItemIcon';
|
2024-07-11 17:22:52 +02:00
|
|
|
import ListItemText from '@mui/material/ListItemText';
|
2023-03-23 13:59:32 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-07-11 17:22:52 +02:00
|
|
|
import { useLocation } from 'react-router-dom';
|
|
|
|
|
import IconButton from '@mui/material/IconButton';
|
|
|
|
|
import ChevronLeftIcon from '@mui/icons-material/ChevronLeft';
|
|
|
|
|
import ChevronRightIcon from '@mui/icons-material/ChevronRight';
|
|
|
|
|
import Divider from '@mui/material/Divider';
|
|
|
|
|
import { styled, useTheme } from '@mui/material/styles';
|
|
|
|
|
import { useCallback, useEffect, useMemo, useRef } from 'react';
|
|
|
|
|
import Box from '@mui/material/Box';
|
|
|
|
|
import ListItem from '@mui/material/ListItem';
|
|
|
|
|
import Tooltip from '@mui/material/Tooltip';
|
|
|
|
|
import { NavbarItem } from '@/typings.ts';
|
|
|
|
|
import { ListItemLink } from '@/components/util/ListItemLink.tsx';
|
|
|
|
|
import { getOptionForDirection } from '@/theme.ts';
|
|
|
|
|
import { useNavBarContext } from '@/components/context/NavbarContext.tsx';
|
|
|
|
|
import { useResizeObserver } from '@/util/useResizeObserver.tsx';
|
|
|
|
|
|
|
|
|
|
const DrawerHeader = styled('div')(({ theme }) => ({
|
2021-10-29 17:51:43 +05:30
|
|
|
display: 'flex',
|
2024-07-11 17:22:52 +02:00
|
|
|
alignItems: 'center',
|
|
|
|
|
justifyContent: 'flex-end',
|
|
|
|
|
padding: theme.spacing(0, 1),
|
|
|
|
|
// necessary for content to be below app bar
|
|
|
|
|
...theme.mixins.toolbar,
|
2021-10-24 21:34:38 +05:30
|
|
|
}));
|
|
|
|
|
|
2024-07-11 17:22:52 +02:00
|
|
|
const NavigationBarItem = ({ path, title, IconComponent, SelectedIconComponent }: NavbarItem) => {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
2021-10-24 21:34:38 +05:30
|
|
|
const location = useLocation();
|
2024-07-11 17:22:52 +02:00
|
|
|
const { isCollapsed } = useNavBarContext();
|
2021-11-08 04:30:31 +05:30
|
|
|
const theme = useTheme();
|
2021-10-30 17:29:27 +03:30
|
|
|
|
2024-07-11 17:22:52 +02:00
|
|
|
const isActive = path === location.pathname;
|
|
|
|
|
const Icon = isActive ? SelectedIconComponent : IconComponent;
|
|
|
|
|
|
|
|
|
|
const { listItemProps, listItemIconProps } = useMemo(
|
|
|
|
|
() => ({
|
|
|
|
|
listItemProps: isCollapsed ? { p: 0.5, display: 'flex', flexDirection: 'column' } : {},
|
|
|
|
|
listItemIconProps: isCollapsed ? { justifyContent: 'center' } : {},
|
|
|
|
|
}),
|
|
|
|
|
[isCollapsed],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<ListItemLink selected={!isCollapsed && isActive} sx={{ p: 0, m: 0 }} to={path}>
|
|
|
|
|
<Tooltip title={t(title)} placement="right">
|
|
|
|
|
<ListItem sx={listItemProps}>
|
|
|
|
|
<ListItemIcon sx={listItemIconProps}>
|
|
|
|
|
<Icon sx={{ color: isActive ? 'primary.main' : undefined }} />
|
|
|
|
|
</ListItemIcon>
|
|
|
|
|
<ListItemText
|
|
|
|
|
primary={t(title)}
|
|
|
|
|
sx={{ maxWidth: '100%' }}
|
|
|
|
|
primaryTypographyProps={{
|
|
|
|
|
sx: {
|
|
|
|
|
maxWidth: '100%',
|
|
|
|
|
overflow: 'hidden',
|
|
|
|
|
textOverflow: 'ellipsis',
|
|
|
|
|
...(isCollapsed ? theme.typography.caption : {}),
|
|
|
|
|
color: isActive ? 'primary.main' : undefined,
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</ListItem>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
</ListItemLink>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const MIN_WIDTH_COLLAPSED = undefined;
|
|
|
|
|
const MAX_WIDTH_COLLAPSED = 120;
|
|
|
|
|
const MIN_WIDTH_EXTENDED = 240;
|
|
|
|
|
const MAX_WIDTH_EXTENDED = 400;
|
|
|
|
|
|
|
|
|
|
export const DesktopSideBar = ({ navBarItems }: { navBarItems: NavbarItem[] }) => {
|
|
|
|
|
const { isCollapsed, setIsCollapsed, navBarWidth, setNavBarWidth } = useNavBarContext();
|
|
|
|
|
|
|
|
|
|
const ref = useRef<HTMLDivElement | null>(null);
|
|
|
|
|
useResizeObserver(
|
|
|
|
|
ref,
|
2024-07-17 22:08:31 +02:00
|
|
|
useCallback(() => setNavBarWidth(ref.current?.clientWidth ?? 0), [ref.current]),
|
2024-07-11 17:22:52 +02:00
|
|
|
);
|
2024-07-13 15:35:34 +02:00
|
|
|
useEffect(() => () => setNavBarWidth(0), []);
|
2021-10-30 17:29:27 +03:30
|
|
|
|
2021-10-24 21:34:38 +05:30
|
|
|
return (
|
2024-07-11 17:22:52 +02:00
|
|
|
<Drawer variant="permanent" sx={{ width: navBarWidth }}>
|
|
|
|
|
<Box
|
|
|
|
|
ref={ref}
|
|
|
|
|
sx={{
|
|
|
|
|
minWidth: isCollapsed ? MIN_WIDTH_COLLAPSED : MIN_WIDTH_EXTENDED,
|
|
|
|
|
maxWidth: isCollapsed ? MAX_WIDTH_COLLAPSED : MAX_WIDTH_EXTENDED,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<DrawerHeader>
|
|
|
|
|
<IconButton onClick={() => setIsCollapsed(true)}>
|
|
|
|
|
{getOptionForDirection(<ChevronLeftIcon />, <ChevronRightIcon />)}
|
|
|
|
|
</IconButton>
|
|
|
|
|
</DrawerHeader>
|
|
|
|
|
<Divider />
|
|
|
|
|
<List sx={{ p: 1 }} dense={isCollapsed}>
|
|
|
|
|
{navBarItems.map((navBarItem) => (
|
|
|
|
|
<NavigationBarItem key={navBarItem.path} {...navBarItem} />
|
|
|
|
|
))}
|
|
|
|
|
</List>
|
|
|
|
|
</Box>
|
|
|
|
|
</Drawer>
|
2021-10-24 21:34:38 +05:30
|
|
|
);
|
2024-07-11 17:22:52 +02:00
|
|
|
};
|