Files
suwayomi-material-you-webui/src/modules/navigation-bar/components/DefaultNavBar.tsx

169 lines
6.5 KiB
TypeScript
Raw Normal View History

/*
* 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
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
2021-01-26 23:32:12 +03:30
2025-01-21 20:34:17 +01:00
import { useCallback, 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';
import ArrowBack from '@mui/icons-material/ArrowBack';
import { useLocation } from 'react-router-dom';
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 16:09:34 +02:00
import { useBackButton } from '@/modules/core/hooks/useBackButton.ts';
import { useGetOptionForDirection } from '@/modules/theme/services/ThemeCreator.ts';
2024-10-05 23:22:42 +02:00
import { MediaQuery } from '@/modules/core/utils/MediaQuery.tsx';
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';
import { MobileBottomBar } from '@/modules/navigation-bar/components/MobileBottomBar.tsx';
2025-01-21 20:34:17 +01:00
import { useNavBarContext } from '@/modules/navigation-bar/contexts/NavbarContext.tsx';
2025-03-22 01:16:38 +01:00
import { useMetadataServerSettings } from '@/modules/settings/services/ServerSettingsMetadata.ts';
import { NAVIGATION_BAR_ITEMS } from '@/modules/navigation-bar/NavigationBar.constants.ts';
import { NavigationBarUtil } from '@/modules/navigation-bar/NavigationBar.util.ts';
2023-10-28 00:32:02 +02:00
export function DefaultNavBar() {
const { title, action, override, isCollapsed, setIsCollapsed, setAppBarHeight, navBarWidth, setNavBarWidth } =
2025-01-21 20:34:17 +01:00
useNavBarContext();
2021-10-30 16:10:34 +03:30
2024-09-06 16:59:27 +02:00
const theme = useTheme();
const getOptionForDirection = useGetOptionForDirection();
const { pathname } = useLocation();
const handleBack = useBackButton();
2024-05-23 02:37:40 +02:00
const isMobileWidth = MediaQuery.useIsMobileWidth();
2024-07-11 17:22:52 +02:00
2025-03-22 01:16:38 +01:00
const {
settings: { hideHistory },
} = useMetadataServerSettings();
2024-07-11 17:22:52 +02:00
const appBarRef = useRef<HTMLDivElement | null>(null);
useResizeObserver(
appBarRef,
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 isMainRoute = NAVIGATION_BAR_ITEMS.some(({ path, show }) => {
if (isMobileWidth && show === 'desktop') {
return false;
}
if (!isMobileWidth && show === 'mobile') {
return false;
}
return path === pathname;
});
const actualNavBarWidth = isMobileWidth || isCollapsed ? 0 : navBarWidth;
2024-07-11 17:22:52 +02:00
const visibleNavBarItems = useMemo(
2025-03-22 01:16:38 +01:00
() =>
NavigationBarUtil.filterItems(NAVIGATION_BAR_ITEMS, {
hideHistory,
hideBoth: false,
hideDesktop: isMobileWidth,
hideMobile: !isMobileWidth,
}),
2025-03-22 01:16:38 +01:00
[isMobileWidth, hideHistory],
2024-07-11 17:22:52 +02:00
);
const NavBarComponent = useMemo(() => (isMobileWidth ? MobileBottomBar : DesktopSideBar), [isMobileWidth]);
const navBar = useMemo(
() => <NavBarComponent navBarItems={visibleNavBarItems} />,
[NavBarComponent, visibleNavBarItems],
);
useLayoutEffect(() => {
if (!isMobileWidth) {
// do not reset navbar width to prevent grid from jumping due to the changing grid item width
return;
}
setNavBarWidth(0);
}, [isMobileWidth]);
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,
pt: 'env(safe-area-inset-top)',
2024-07-11 17:22:52 +02:00
width: `calc(100% - ${actualNavBarWidth}px)`,
zIndex: theme.zIndex.drawer,
2024-07-11 17:22:52 +02:00
}}
>
<Toolbar sx={{ position: 'relative' }}>
{!isMobileWidth && (
<Stack
sx={{
position: 'absolute',
left: 0,
width: `calc(${navBarWidth}px + env(safe-area-inset-left))`,
2024-07-11 17:22:52 +02:00
...(!isCollapsed && { display: 'none' }),
alignItems: 'center',
}}
>
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>
)}
2024-07-11 17:22:52 +02:00
<Stack
sx={{
ml: `${isCollapsed ? navBarWidth : 0}px`,
width: `calc(100% - (${isCollapsed ? navBarWidth : 0}px + env(safe-area-inset-left)))`,
2024-07-11 17:22:52 +02:00
flexDirection: 'row',
alignItems: 'center',
}}
>
2024-07-11 17:22:52 +02:00
{!isMainRoute && (
<IconButton
edge="start"
component="button"
sx={{ marginRight: 2 }}
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
);
}