Files
suwayomi-material-you-webui/src/components/navbar/DefaultNavBar.tsx

139 lines
4.9 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
import React, { useContext } 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 Box from '@mui/material/Box';
import CollectionsBookmarkIcon from '@mui/icons-material/CollectionsBookmark';
2021-10-30 17:29:27 +03:30
import CollectionsOutlinedBookmarkIcon from '@mui/icons-material/CollectionsBookmarkOutlined';
import NewReleasesIcon from '@mui/icons-material/NewReleases';
2021-10-30 17:29:27 +03:30
import NewReleasesOutlinedIcon from '@mui/icons-material/NewReleasesOutlined';
import ExploreIcon from '@mui/icons-material/Explore';
2021-10-30 17:29:27 +03:30
import ExploreOutlinedIcon from '@mui/icons-material/ExploreOutlined';
import GetAppIcon from '@mui/icons-material/GetApp';
2021-10-30 17:29:27 +03:30
import GetAppOutlinedIcon from '@mui/icons-material/GetAppOutlined';
import SettingsIcon from '@mui/icons-material/Settings';
import ArrowBack from '@mui/icons-material/ArrowBack';
import { useLocation } from 'react-router-dom';
import { createPortal } from 'react-dom';
import ArrowForwardIcon from '@mui/icons-material/ArrowForward';
import { NavbarItem } from '@/typings';
2023-10-28 00:32:02 +02:00
import { NavBarContext } from '@/components/context/NavbarContext';
import { DesktopSideBar } from '@/components/navbar/navigation/DesktopSideBar';
import { MobileBottomBar } from '@/components/navbar/navigation/MobileBottomBar';
import { useBackButton } from '@/util/useBackButton.ts';
import { getOptionForDirection } from '@/theme.ts';
2024-05-23 02:37:40 +02:00
import { MediaQuery } from '@/lib/ui/MediaQuery.tsx';
2020-12-24 15:02:03 +03:30
const navbarItems: Array<NavbarItem> = [
{
path: '/library',
title: 'library.title',
2021-10-30 17:29:27 +03:30
SelectedIconComponent: CollectionsBookmarkIcon,
IconComponent: CollectionsOutlinedBookmarkIcon,
show: 'both',
},
{
path: '/updates',
title: 'updates.title',
2021-10-30 17:29:27 +03:30
SelectedIconComponent: NewReleasesIcon,
IconComponent: NewReleasesOutlinedIcon,
show: 'both',
},
{
path: '/browse',
title: 'global.label.browse',
2021-10-30 17:29:27 +03:30
SelectedIconComponent: ExploreIcon,
IconComponent: ExploreOutlinedIcon,
show: 'both',
},
{
path: '/downloads',
title: 'download.title',
2021-10-30 17:29:27 +03:30
SelectedIconComponent: GetAppIcon,
IconComponent: GetAppOutlinedIcon,
show: 'both',
},
{
path: '/settings',
title: 'settings.title',
2021-10-30 17:29:27 +03:30
SelectedIconComponent: SettingsIcon,
IconComponent: SettingsIcon,
show: 'both',
},
];
2023-10-28 00:32:02 +02:00
export function DefaultNavBar() {
const { title, action, override } = useContext(NavBarContext);
2021-10-30 16:10:34 +03:30
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();
const isMainRoute = navbarItems.some(({ path }) => path === pathname);
2021-01-23 01:20:16 +03:30
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;
let navbar: JSX.Element | null = null;
2021-10-30 15:19:49 +03:30
if (isMobileWidth) {
if (isMainRoute) {
navbar = <MobileBottomBar navBarItems={navbarItems.filter((it) => it.show !== 'desktop')} />;
2021-10-30 15:19:49 +03:30
}
} else {
navbar = <DesktopSideBar navBarItems={navbarItems.filter((it) => it.show !== 'mobile')} />;
2021-10-30 15:19:49 +03:30
}
2020-12-24 15:02:03 +03:30
return (
2021-10-30 18:41:53 +03:30
<Box sx={{ flexGrow: 1 }}>
<AppBar position="fixed" color="default">
2021-09-09 17:51:22 +04:30
<Toolbar>
{!isMainRoute && (
<IconButton
component="button"
edge="start"
2024-05-23 02:37:40 +02:00
sx={{ marginRight: 2 }}
color="inherit"
aria-label="menu"
size="large"
onClick={handleBack}
>
{getOptionForDirection(<ArrowBack />, <ArrowForwardIcon />)}
</IconButton>
)}
<Typography
variant={isMobileWidth ? 'h6' : 'h5'}
sx={{ flexGrow: 1 }}
noWrap
textOverflow="ellipsis"
>
2021-09-09 17:51:22 +04:30
{title}
</Typography>
{action}
<div id="navbarToolbar" />
2021-09-09 17:51:22 +04:30
</Toolbar>
</AppBar>
2021-10-30 15:19:49 +03:30
{navbar}
2021-10-30 18:41:53 +03:30
</Box>
2020-12-24 15:02:03 +03:30
);
}
interface INavbarToolbarProps {
children?: React.ReactNode;
}
export const NavbarToolbar: React.FC<INavbarToolbarProps> = ({ children }) => {
const container = document.getElementById('navbarToolbar');
if (!container) return null;
return createPortal(children, container);
};