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
|
|
|
|
2021-10-29 17:51:43 +05: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';
|
2023-06-04 21:30:15 +02:00
|
|
|
import { Box, useMediaQuery } from '@mui/material';
|
2021-10-24 21:34:38 +05:30
|
|
|
import { useTheme } from '@mui/material/styles';
|
|
|
|
|
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-30 16:34:09 +03:30
|
|
|
import ExtensionIcon from '@mui/icons-material/Extension';
|
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-06-26 20:40:35 +02:00
|
|
|
import { useLocation, useNavigate } from 'react-router-dom';
|
2022-11-09 18:09:15 +01:00
|
|
|
import { createPortal } from 'react-dom';
|
2023-06-05 01:42:39 +02:00
|
|
|
import { NavbarItem } from '@/typings';
|
2023-10-28 00:32:02 +02:00
|
|
|
import { NavBarContext } from '@/components/context/NavbarContext';
|
|
|
|
|
import { ExtensionOutlinedIcon } from '@/components/util/CustomExtensionOutlinedIcon';
|
|
|
|
|
import { DesktopSideBar } from '@/components/navbar/navigation/DesktopSideBar';
|
|
|
|
|
import { MobileBottomBar } from '@/components/navbar/navigation/MobileBottomBar';
|
2023-06-26 20:40:35 +02:00
|
|
|
import { useHistory } from '@/util/useHistory';
|
2020-12-24 15:02:03 +03:30
|
|
|
|
2021-10-24 21:34:38 +05:30
|
|
|
const navbarItems: Array<NavbarItem> = [
|
|
|
|
|
{
|
|
|
|
|
path: '/library',
|
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
|
|
|
},
|
|
|
|
|
{
|
2021-10-24 21:34:38 +05:30
|
|
|
path: '/updates',
|
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
|
|
|
},
|
|
|
|
|
{
|
2021-10-30 16:34:09 +03:30
|
|
|
path: '/extensions',
|
2023-03-23 13:59:32 +01:00
|
|
|
title: 'extension.title',
|
2021-10-30 17:29:27 +03:30
|
|
|
SelectedIconComponent: ExtensionIcon,
|
|
|
|
|
IconComponent: ExtensionOutlinedIcon,
|
2021-10-30 16:34:09 +03:30
|
|
|
show: 'desktop',
|
2023-02-06 10:06:33 +01:00
|
|
|
},
|
|
|
|
|
{
|
2021-10-31 19:15:13 +03:30
|
|
|
path: '/sources',
|
2023-03-23 13:59:32 +01:00
|
|
|
title: 'source.title',
|
2021-10-31 19:15:13 +03:30
|
|
|
SelectedIconComponent: ExploreIcon,
|
|
|
|
|
IconComponent: ExploreOutlinedIcon,
|
|
|
|
|
show: 'desktop',
|
2023-02-06 10:06:33 +01:00
|
|
|
},
|
|
|
|
|
{
|
2021-10-26 14:57:11 +03:30
|
|
|
path: '/browse',
|
2023-03-23 13:59:32 +01:00
|
|
|
title: 'global.label.browse',
|
2021-10-30 17:29:27 +03:30
|
|
|
SelectedIconComponent: ExploreIcon,
|
|
|
|
|
IconComponent: ExploreOutlinedIcon,
|
2021-10-30 16:34:09 +03:30
|
|
|
show: 'mobile',
|
2023-02-06 10:06:33 +01:00
|
|
|
},
|
|
|
|
|
{
|
2021-10-26 12:13:37 +03:30
|
|
|
path: '/downloads',
|
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
|
|
|
},
|
|
|
|
|
{
|
2021-10-24 21:34:38 +05:30
|
|
|
path: '/settings',
|
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() {
|
2023-06-08 14:25:24 +02:00
|
|
|
const { title, action, override, defaultBackTo: backToUrl } = useContext(NavBarContext);
|
2021-10-30 16:10:34 +03:30
|
|
|
|
2021-10-24 21:34:38 +05:30
|
|
|
const theme = useTheme();
|
2023-06-04 21:30:15 +02:00
|
|
|
const navigate = useNavigate();
|
2023-06-26 20:40:35 +02:00
|
|
|
const { pathname, ...location } = useLocation();
|
|
|
|
|
const history = useHistory();
|
2021-01-23 01:20:16 +03:30
|
|
|
|
2021-10-30 16:10:34 +03:30
|
|
|
const isMobileWidth = useMediaQuery(theme.breakpoints.down('sm'));
|
2023-06-04 21:30:15 +02:00
|
|
|
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;
|
|
|
|
|
|
2023-03-11 18:05:58 +01:00
|
|
|
let navbar: JSX.Element | null = null;
|
2021-10-30 15:19:49 +03:30
|
|
|
if (isMobileWidth) {
|
2021-10-30 16:34:09 +03:30
|
|
|
if (isMainRoute) {
|
2023-02-08 18:19:26 +01:00
|
|
|
navbar = <MobileBottomBar navBarItems={navbarItems.filter((it) => it.show !== 'desktop')} />;
|
2021-10-30 15:19:49 +03:30
|
|
|
}
|
|
|
|
|
} else {
|
2021-10-30 16:34:09 +03:30
|
|
|
navbar = <DesktopSideBar navBarItems={navbarItems.filter((it) => it.show !== 'mobile')} />;
|
2021-10-30 15:19:49 +03:30
|
|
|
}
|
|
|
|
|
|
2022-11-21 01:33:45 +01:00
|
|
|
const handleBack = () => {
|
2023-06-26 20:40:35 +02:00
|
|
|
const isLastPageInHistory = location.key === 'default';
|
|
|
|
|
const wasPreviousPageReader = history[history.length - 2]?.match(/\/manga\/[0-9]+\/chapter\/[0-9]+.*/g);
|
|
|
|
|
|
|
|
|
|
if (isLastPageInHistory || wasPreviousPageReader) {
|
|
|
|
|
navigate(backToUrl ?? '');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-04 21:30:15 +02:00
|
|
|
navigate(-1);
|
2022-11-21 01:33:45 +01:00
|
|
|
};
|
|
|
|
|
|
2020-12-24 15:02:03 +03:30
|
|
|
return (
|
2021-10-30 18:41:53 +03:30
|
|
|
<Box sx={{ flexGrow: 1 }}>
|
2022-11-26 13:51:56 +01:00
|
|
|
<AppBar position="fixed" color="default">
|
2021-09-09 17:51:22 +04:30
|
|
|
<Toolbar>
|
2022-11-21 01:33:45 +01:00
|
|
|
{!isMainRoute && (
|
|
|
|
|
<IconButton
|
2023-06-26 20:40:35 +02:00
|
|
|
component="button"
|
2022-11-21 01:33:45 +01:00
|
|
|
edge="start"
|
|
|
|
|
sx={{ marginRight: theme.spacing(2) }}
|
|
|
|
|
color="inherit"
|
|
|
|
|
aria-label="menu"
|
|
|
|
|
disableRipple
|
|
|
|
|
size="large"
|
|
|
|
|
onClick={handleBack}
|
|
|
|
|
>
|
|
|
|
|
<ArrowBack />
|
|
|
|
|
</IconButton>
|
|
|
|
|
)}
|
2023-02-06 10:06:33 +01:00
|
|
|
<Typography
|
|
|
|
|
variant={isMobileWidth ? 'h6' : 'h5'}
|
|
|
|
|
sx={{ flexGrow: 1 }}
|
|
|
|
|
noWrap
|
|
|
|
|
textOverflow="ellipsis"
|
|
|
|
|
>
|
2021-09-09 17:51:22 +04:30
|
|
|
{title}
|
|
|
|
|
</Typography>
|
|
|
|
|
{action}
|
2022-11-09 18:09:15 +01:00
|
|
|
<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
|
|
|
);
|
|
|
|
|
}
|
2022-11-09 18:09:15 +01:00
|
|
|
|
|
|
|
|
interface INavbarToolbarProps {
|
2023-02-06 10:06:33 +01:00
|
|
|
children?: React.ReactNode;
|
2022-11-09 18:09:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const NavbarToolbar: React.FC<INavbarToolbarProps> = ({ children }) => {
|
|
|
|
|
const container = document.getElementById('navbarToolbar');
|
|
|
|
|
if (!container) return null;
|
|
|
|
|
|
|
|
|
|
return createPortal(children, container);
|
|
|
|
|
};
|