From 52203c18ba0100fe48c2ecdfbb5ab458f459c541 Mon Sep 17 00:00:00 2001 From: Valter Martinek Date: Sun, 20 Nov 2022 22:35:46 +0100 Subject: [PATCH 1/3] Enable passing backLink through location state. Explicitly handle cases for BACK --- src/components/MangaCard.tsx | 9 +++-- src/components/manga/ChapterCard.tsx | 3 +- src/components/manga/ResumeFAB.tsx | 3 +- src/components/navbar/DefaultNavBar.tsx | 44 ++++++++++++++----------- src/components/navbar/ReaderNavBar.tsx | 14 ++++++-- src/screens/Reader.tsx | 4 +-- src/screens/Updates.tsx | 2 +- src/util/useBackTo.ts | 24 ++++++++++++++ 8 files changed, 72 insertions(+), 31 deletions(-) create mode 100644 src/util/useBackTo.ts diff --git a/src/components/MangaCard.tsx b/src/components/MangaCard.tsx index c70f102fab..b5c22c8036 100644 --- a/src/components/MangaCard.tsx +++ b/src/components/MangaCard.tsx @@ -9,12 +9,13 @@ import React from 'react'; import Card from '@mui/material/Card'; import CardActionArea from '@mui/material/CardActionArea'; import Typography from '@mui/material/Typography'; -import { Link } from 'react-router-dom'; +import { Link, useLocation } from 'react-router-dom'; import { Avatar, CardContent, Grid } from '@mui/material'; import useLocalStorage from 'util/useLocalStorage'; import SpinnerImage from 'components/util/SpinnerImage'; import { Box, styled } from '@mui/system'; import { useLibraryOptionsContext } from 'components/context/LibraryOptionsContext'; +import { BACK } from 'util/useBackTo'; const BottomGradient = styled('div')({ position: 'absolute', @@ -88,12 +89,14 @@ const MangaCard = React.forwardRef((props: IProps, ref) const [useCache] = useLocalStorage('useCache', true); const [ItemWidth] = useLocalStorage('ItemWidth', 300); + const mangaLinkTo = { pathname: `/manga/${id}/`, state: { backLink: BACK } }; + if (gridLayout !== 2) { const colomns = Math.round(dimensions / ItemWidth); return ( // @ts-ignore gridsize type isnt allowed to be a decimal but it works fine - + ((props: IProps, ref) } return ( - + = (props: IProps) => { }} > {index === 1 ? 'Start' : 'Resume' } diff --git a/src/components/navbar/DefaultNavBar.tsx b/src/components/navbar/DefaultNavBar.tsx index 7ff9f03ad9..e977087220 100644 --- a/src/components/navbar/DefaultNavBar.tsx +++ b/src/components/navbar/DefaultNavBar.tsx @@ -23,12 +23,13 @@ import GetAppIcon from '@mui/icons-material/GetApp'; import GetAppOutlinedIcon from '@mui/icons-material/GetAppOutlined'; import SettingsIcon from '@mui/icons-material/Settings'; import ArrowBack from '@mui/icons-material/ArrowBack'; -import { useHistory } from 'react-router-dom'; +import { Link, useHistory } from 'react-router-dom'; import NavBarContext from 'components/context/NavbarContext'; import DarkTheme from 'components/context/DarkTheme'; import ExtensionOutlinedIcon from 'components/util/CustomExtensionOutlinedIcon'; import { Box } from '@mui/system'; import { createPortal } from 'react-dom'; +import useBackTo from 'util/useBackTo'; import DesktopSideBar from './navigation/DesktopSideBar'; import MobileBottomBar from './navigation/MobileBottomBar'; @@ -80,10 +81,11 @@ const navbarItems: Array = [ export default function DefaultNavBar() { const { title, action, override } = useContext(NavBarContext); + const backTo = useBackTo(); const { darkTheme } = useContext(DarkTheme); const theme = useTheme(); - const history = useHistory(); + const history = useHistory<{ backLink?: string }>(); const isMobileWidth = useMediaQuery(theme.breakpoints.down('sm')); const isMainRoute = navbarItems.some(({ path }) => path === history.location.pathname); @@ -100,28 +102,30 @@ export default function DefaultNavBar() { navbar = it.show !== 'mobile')} />; } + const handleBack = () => { + if (backTo.url != null) return; + history.goBack(); + }; + return ( - { - !navbarItems.some(({ path }) => path === history.location.pathname) - && ( - (history.length === 1 ? history.push('/library') : history.goBack())} - size="large" - > - - - ) - } + {!isMainRoute && ( + + + + )} {title} diff --git a/src/components/navbar/ReaderNavBar.tsx b/src/components/navbar/ReaderNavBar.tsx index d80a53d737..3476e40e2a 100644 --- a/src/components/navbar/ReaderNavBar.tsx +++ b/src/components/navbar/ReaderNavBar.tsx @@ -27,6 +27,7 @@ import ListItemSecondaryAction from '@mui/material/ListItemSecondaryAction'; import Collapse from '@mui/material/Collapse'; import Button from '@mui/material/Button'; import { styled } from '@mui/system'; +import useBackTo from 'util/useBackTo'; const Root = styled('div')(({ theme }) => ({ top: 0, @@ -129,6 +130,7 @@ interface IProps { export default function ReaderNavBar(props: IProps) { const history = useHistory(); + const backTo = useBackTo(); const { settings, setSettings, manga, chapter, curPage, @@ -167,6 +169,12 @@ export default function ReaderNavBar(props: IProps) { }; }, [handleScroll]);// handleScroll changes on every render + const handleClose = () => { + if (backTo.back) history.goBack(); + else if (backTo.url) history.push(backTo.url); + else history.push(`/manga/${manga.id}`); + }; + return ( <> history.push('..')} + onClick={handleClose} size="large" sx={{ mr: -1 }} > @@ -315,7 +323,7 @@ export default function ReaderNavBar(props: IProps) { && (