2021-03-26 04:17:02 +04:30
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
2021-01-26 23:32:12 +03:30
|
|
|
* 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-01-26 23:32:12 +03:30
|
|
|
|
2024-10-24 17:30:24 +02:00
|
|
|
import { useCallback, useContext, useLayoutEffect, useMemo, useRef } from 'react';
|
2021-09-09 17:51:22 +04:30
|
|
|
import AppBar from '@mui/material/AppBar';
|
|
|
|
|
import Toolbar from '@mui/material/Toolbar';
|
|
|
|
|
import Typography from '@mui/material/Typography';
|
|
|
|
|
import IconButton from '@mui/material/IconButton';
|
2021-10-24 21:34:38 +05:30
|
|
|
import CollectionsBookmarkIcon from '@mui/icons-material/CollectionsBookmark';
|
2021-10-30 17:29:27 +03:30
|
|
|
import CollectionsOutlinedBookmarkIcon from '@mui/icons-material/CollectionsBookmarkOutlined';
|
2021-10-24 21:34:38 +05:30
|
|
|
import NewReleasesIcon from '@mui/icons-material/NewReleases';
|
2021-10-30 17:29:27 +03:30
|
|
|
import NewReleasesOutlinedIcon from '@mui/icons-material/NewReleasesOutlined';
|
2021-10-24 21:34:38 +05:30
|
|
|
import ExploreIcon from '@mui/icons-material/Explore';
|
2021-10-30 17:29:27 +03:30
|
|
|
import ExploreOutlinedIcon from '@mui/icons-material/ExploreOutlined';
|
2021-10-24 21:34:38 +05:30
|
|
|
import GetAppIcon from '@mui/icons-material/GetApp';
|
2021-10-30 17:29:27 +03:30
|
|
|
import GetAppOutlinedIcon from '@mui/icons-material/GetAppOutlined';
|
2021-10-24 21:34:38 +05:30
|
|
|
import SettingsIcon from '@mui/icons-material/Settings';
|
2021-10-26 13:49:23 +05:30
|
|
|
import ArrowBack from '@mui/icons-material/ArrowBack';
|
2023-11-12 03:21:26 +01:00
|
|
|
import { useLocation } from 'react-router-dom';
|
2024-04-26 12:53:31 +02:00
|
|
|
import ArrowForwardIcon from '@mui/icons-material/ArrowForward';
|
2024-07-11 17:22:52 +02:00
|
|
|
import MenuIcon from '@mui/icons-material/Menu';
|
|
|
|
|
import Stack from '@mui/material/Stack';
|
2024-09-06 16:59:27 +02:00
|
|
|
import { useTheme } from '@mui/material/styles';
|
2024-10-05 22:17:23 +02:00
|
|
|
import { NavBarContext } from '@/modules/navigation-bar/contexts/NavbarContext.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { useBackButton } from '@/modules/core/hooks/useBackButton.ts';
|
2024-12-11 13:59:50 +01:00
|
|
|
import { useGetOptionForDirection } from '@/modules/theme/services/ThemeCreator.ts';
|
2024-10-05 23:22:42 +02:00
|
|
|
import { MediaQuery } from '@/modules/core/utils/MediaQuery.tsx';
|
2024-10-05 22:17:23 +02:00
|
|
|
import { DesktopSideBar } from '@/modules/navigation-bar/components/DesktopSideBar.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { useResizeObserver } from '@/modules/core/hooks/useResizeObserver.tsx';
|
2024-10-05 22:17:23 +02:00
|
|
|
import { MobileBottomBar } from '@/modules/navigation-bar/components/MobileBottomBar.tsx';
|
|
|
|
|
import { NavbarItem } from '@/modules/navigation-bar/NavigationBar.types.ts';
|
2024-12-11 20:10:59 +01:00
|
|
|
import { AppRoutes } from '@/modules/core/AppRoute.constants.ts';
|
2020-12-24 15:02:03 +03:30
|
|
|
|
2021-10-24 21:34:38 +05:30
|
|
|
const navbarItems: Array<NavbarItem> = [
|
|
|
|
|
{
|
2024-12-11 20:10:59 +01:00
|
|
|
path: AppRoutes.library.path,
|
2023-03-23 13:59:32 +01:00
|
|
|
title: 'library.title',
|
2021-10-30 17:29:27 +03:30
|
|
|
SelectedIconComponent: CollectionsBookmarkIcon,
|
|
|
|
|
IconComponent: CollectionsOutlinedBookmarkIcon,
|
2021-10-30 16:34:09 +03:30
|
|
|
show: 'both',
|
2023-02-06 10:06:33 +01:00
|
|
|
},
|
|
|
|
|
{
|
2024-12-11 20:10:59 +01:00
|
|
|
path: AppRoutes.updates.path,
|
2023-03-23 13:59:32 +01:00
|
|
|
title: 'updates.title',
|
2021-10-30 17:29:27 +03:30
|
|
|
SelectedIconComponent: NewReleasesIcon,
|
|
|
|
|
IconComponent: NewReleasesOutlinedIcon,
|
2021-10-30 16:34:09 +03:30
|
|
|
show: 'both',
|
2023-02-06 10:06:33 +01:00
|
|
|
},
|
|
|
|
|
{
|
2024-12-11 20:10:59 +01:00
|
|
|
path: AppRoutes.browse.path,
|
2023-03-23 13:59:32 +01:00
|
|
|
title: 'global.label.browse',
|
2021-10-30 17:29:27 +03:30
|
|
|
SelectedIconComponent: ExploreIcon,
|
|
|
|
|
IconComponent: ExploreOutlinedIcon,
|
2024-01-02 23:32:49 +01:00
|
|
|
show: 'both',
|
2023-02-06 10:06:33 +01:00
|
|
|
},
|
|
|
|
|
{
|
2024-12-11 20:10:59 +01:00
|
|
|
path: AppRoutes.downloads.path,
|
2023-03-23 13:59:32 +01:00
|
|
|
title: 'download.title',
|
2021-10-30 17:29:27 +03:30
|
|
|
SelectedIconComponent: GetAppIcon,
|
|
|
|
|
IconComponent: GetAppOutlinedIcon,
|
2021-10-30 16:34:09 +03:30
|
|
|
show: 'both',
|
2023-02-06 10:06:33 +01:00
|
|
|
},
|
|
|
|
|
{
|
2024-12-11 20:10:59 +01:00
|
|
|
path: AppRoutes.settings.path,
|
2023-03-23 13:59:32 +01:00
|
|
|
title: 'settings.title',
|
2021-10-30 17:29:27 +03:30
|
|
|
SelectedIconComponent: SettingsIcon,
|
2021-10-24 21:34:38 +05:30
|
|
|
IconComponent: SettingsIcon,
|
2021-10-30 16:34:09 +03:30
|
|
|
show: 'both',
|
2021-10-24 21:34:38 +05:30
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2023-10-28 00:32:02 +02:00
|
|
|
export function DefaultNavBar() {
|
2024-07-29 17:22:49 +02:00
|
|
|
const { title, action, override, isCollapsed, setIsCollapsed, setAppBarHeight, navBarWidth, setNavBarWidth } =
|
2024-07-11 17:22:52 +02:00
|
|
|
useContext(NavBarContext);
|
2021-10-30 16:10:34 +03:30
|
|
|
|
2024-09-06 16:59:27 +02:00
|
|
|
const theme = useTheme();
|
2024-09-30 02:27:10 +02:00
|
|
|
const getOptionForDirection = useGetOptionForDirection();
|
2023-11-12 03:21:26 +01:00
|
|
|
const { pathname } = useLocation();
|
|
|
|
|
const handleBack = useBackButton();
|
2021-01-23 01:20:16 +03:30
|
|
|
|
2024-05-23 02:37:40 +02:00
|
|
|
const isMobileWidth = MediaQuery.useIsMobileWidth();
|
2023-06-04 21:30:15 +02:00
|
|
|
const isMainRoute = navbarItems.some(({ path }) => path === pathname);
|
2021-01-23 01:20:16 +03:30
|
|
|
|
2024-07-11 17:22:52 +02:00
|
|
|
const actualNavBarWidth = isMobileWidth || isCollapsed ? 0 : navBarWidth;
|
|
|
|
|
|
|
|
|
|
const appBarRef = useRef<HTMLDivElement | null>(null);
|
|
|
|
|
useResizeObserver(
|
|
|
|
|
appBarRef,
|
2024-12-15 22:37:11 +01:00
|
|
|
useCallback(() => {
|
|
|
|
|
setAppBarHeight(appBarRef.current?.clientHeight ?? 0);
|
|
|
|
|
}, [appBarRef.current]),
|
2024-07-11 17:22:52 +02:00
|
|
|
);
|
2024-10-24 17:30:24 +02:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
if (!override.status) {
|
|
|
|
|
setAppBarHeight(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return () => setAppBarHeight(0);
|
|
|
|
|
}, [override.status]);
|
2024-07-11 17:22:52 +02:00
|
|
|
|
|
|
|
|
const activeNavBar: NavbarItem['show'] = isMobileWidth ? 'mobile' : 'desktop';
|
|
|
|
|
const visibleNavBarItems = useMemo(
|
|
|
|
|
() => navbarItems.filter(({ show }) => ['both', activeNavBar].includes(show)),
|
|
|
|
|
[isMobileWidth],
|
|
|
|
|
);
|
|
|
|
|
const NavBarComponent = useMemo(() => (isMobileWidth ? MobileBottomBar : DesktopSideBar), [isMobileWidth]);
|
|
|
|
|
|
2024-07-29 17:22:49 +02:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
if (!isMobileWidth) {
|
|
|
|
|
// do not reset navbar width to prevent grid from jumping due to the changing grid item width
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setNavBarWidth(0);
|
|
|
|
|
}, [isMobileWidth]);
|
|
|
|
|
|
2024-07-11 17:22:52 +02:00
|
|
|
const navBar = useMemo(
|
|
|
|
|
() => <NavBarComponent navBarItems={visibleNavBarItems} />,
|
|
|
|
|
[NavBarComponent, visibleNavBarItems],
|
|
|
|
|
);
|
|
|
|
|
|
2021-10-30 16:10:34 +03:30
|
|
|
// Allow default navbar to be overrided
|
2021-10-30 15:19:49 +03:30
|
|
|
if (override.status) return override.value;
|
|
|
|
|
|
2020-12-24 15:02:03 +03:30
|
|
|
return (
|
2024-07-11 17:22:52 +02:00
|
|
|
<>
|
|
|
|
|
<AppBar
|
|
|
|
|
ref={appBarRef}
|
|
|
|
|
sx={{
|
|
|
|
|
position: 'fixed',
|
|
|
|
|
marginLeft: actualNavBarWidth,
|
2024-12-15 22:37:11 +01:00
|
|
|
pt: 'env(safe-area-inset-top)',
|
2024-07-11 17:22:52 +02:00
|
|
|
width: `calc(100% - ${actualNavBarWidth}px)`,
|
2024-09-09 21:37:07 +02:00
|
|
|
zIndex: theme.zIndex.drawer,
|
2024-07-11 17:22:52 +02:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Toolbar sx={{ position: 'relative' }}>
|
|
|
|
|
{!isMobileWidth && (
|
|
|
|
|
<Stack
|
|
|
|
|
sx={{
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
left: 0,
|
2024-12-15 22:37:11 +01:00
|
|
|
width: `calc(${navBarWidth}px + env(safe-area-inset-left))`,
|
2024-07-11 17:22:52 +02:00
|
|
|
...(!isCollapsed && { display: 'none' }),
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
}}
|
2022-11-21 01:33:45 +01:00
|
|
|
>
|
2024-09-06 16:59:27 +02:00
|
|
|
<IconButton aria-label="open drawer" onClick={() => setIsCollapsed(false)} color="inherit">
|
2024-07-11 17:22:52 +02:00
|
|
|
<MenuIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Stack>
|
2022-11-21 01:33:45 +01:00
|
|
|
)}
|
2024-07-11 17:22:52 +02:00
|
|
|
<Stack
|
|
|
|
|
sx={{
|
|
|
|
|
ml: `${isCollapsed ? navBarWidth : 0}px`,
|
2024-12-15 22:37:11 +01:00
|
|
|
width: `calc(100% - (${isCollapsed ? navBarWidth : 0}px + env(safe-area-inset-left)))`,
|
2024-07-11 17:22:52 +02:00
|
|
|
flexDirection: 'row',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
}}
|
2023-02-06 10:06:33 +01:00
|
|
|
>
|
2024-07-11 17:22:52 +02:00
|
|
|
{!isMainRoute && (
|
|
|
|
|
<IconButton
|
|
|
|
|
edge="start"
|
|
|
|
|
component="button"
|
|
|
|
|
sx={{ marginRight: 2 }}
|
|
|
|
|
size="large"
|
|
|
|
|
aria-label="menu"
|
|
|
|
|
onClick={handleBack}
|
2024-09-06 16:59:27 +02:00
|
|
|
color="inherit"
|
2024-07-11 17:22:52 +02:00
|
|
|
>
|
|
|
|
|
{getOptionForDirection(<ArrowBack />, <ArrowForwardIcon />)}
|
|
|
|
|
</IconButton>
|
|
|
|
|
)}
|
2024-09-03 17:15:47 +02:00
|
|
|
<Typography
|
|
|
|
|
variant="h5"
|
|
|
|
|
component="h1"
|
|
|
|
|
noWrap
|
|
|
|
|
sx={{
|
|
|
|
|
textOverflow: 'ellipsis',
|
|
|
|
|
flexGrow: 1,
|
|
|
|
|
}}
|
|
|
|
|
>
|
2024-07-11 17:22:52 +02:00
|
|
|
{title}
|
|
|
|
|
</Typography>
|
|
|
|
|
{action}
|
|
|
|
|
</Stack>
|
2021-09-09 17:51:22 +04:30
|
|
|
</Toolbar>
|
|
|
|
|
</AppBar>
|
2024-07-11 17:22:52 +02:00
|
|
|
{!isMobileWidth || isMainRoute ? navBar : null}
|
|
|
|
|
</>
|
2020-12-24 15:02:03 +03:30
|
|
|
);
|
|
|
|
|
}
|