Feature/improve back navigation button (#636)

* Improve detection of last page in history

* Use "useBackButton" in reader
This commit is contained in:
schroda
2024-03-04 20:40:14 +01:00
committed by GitHub
parent f0fc31ceac
commit 224df1300f
2 changed files with 17 additions and 19 deletions

View File

@@ -25,6 +25,8 @@ import Collapse from '@mui/material/Collapse';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { AllowedMetadataValueTypes, ChapterOffset, IReaderSettings, TChapter, TManga } from '@/typings'; import { AllowedMetadataValueTypes, ChapterOffset, IReaderSettings, TChapter, TManga } from '@/typings';
import { ReaderSettingsOptions } from '@/components/reader/ReaderSettingsOptions'; import { ReaderSettingsOptions } from '@/components/reader/ReaderSettingsOptions';
import { useBackButton } from '@/util/useBackButton.ts';
import { useSetDefaultBackTo } from '@/components/context/NavbarContext.tsx';
const Root = styled('div')({ const Root = styled('div')({
zIndex: 10, zIndex: 10,
@@ -137,6 +139,9 @@ export function ReaderNavBar(props: IProps) {
const { settings, setSettingValue, manga, chapter, curPage, scrollToPage, openNextChapter, retrievingNextChapter } = const { settings, setSettingValue, manga, chapter, curPage, scrollToPage, openNextChapter, retrievingNextChapter } =
props; props;
const handleBack = useBackButton();
useSetDefaultBackTo(`/manga/${manga.id}`);
const [drawerOpen, setDrawerOpen] = useState(settings.staticNav || prevDrawerOpen); const [drawerOpen, setDrawerOpen] = useState(settings.staticNav || prevDrawerOpen);
const [updateDrawerOnRender, setUpdateDrawerOnRender] = useState(true); const [updateDrawerOnRender, setUpdateDrawerOnRender] = useState(true);
const [hideOpenButton, setHideOpenButton] = useState(settings.staticNav || prevDrawerOpen); const [hideOpenButton, setHideOpenButton] = useState(settings.staticNav || prevDrawerOpen);
@@ -188,19 +193,6 @@ export function ReaderNavBar(props: IProps) {
}; };
}, [handleScroll]); // handleScroll changes on every render }, [handleScroll]); // handleScroll changes on every render
const handleClose = () => {
const isLastPageInHistory = location.key === 'default';
if (isLastPageInHistory) {
navigate(`/manga/${manga.id}`);
return;
}
// this works because opening previous/next chapter will replace the current history element.
// in case this gets changed this has to be updated
navigate(-1);
};
return ( return (
<Root> <Root>
<Slide direction="right" in={drawerOpen} timeout={200} appear={false} mountOnEnter unmountOnExit> <Slide direction="right" in={drawerOpen} timeout={200} appear={false} mountOnEnter unmountOnExit>
@@ -233,7 +225,7 @@ export function ReaderNavBar(props: IProps) {
color="inherit" color="inherit"
aria-label="menu" aria-label="menu"
disableRipple disableRipple
onClick={handleClose} onClick={handleBack}
size="large" size="large"
sx={{ mr: -1 }} sx={{ mr: -1 }}
> >

View File

@@ -18,14 +18,20 @@ export const useBackButton = () => {
const { defaultBackTo: backToUrl } = useContext(NavBarContext); const { defaultBackTo: backToUrl } = useContext(NavBarContext);
return () => { return () => {
const isLastPageInHistory = location.key === 'default'; const isHistoryEmpty = !history.length;
const wasPreviousPageReader = history[history.length - 2]?.match(/\/manga\/[0-9]+\/chapter\/[0-9]+.*/g); const isLastPageInHistoryCurrentPage = history.length === 1 && history[0] === location.pathname;
if (isLastPageInHistory || wasPreviousPageReader) { const canNavigateBack = !isHistoryEmpty && !isLastPageInHistoryCurrentPage;
navigate(backToUrl ?? ''); if (canNavigateBack) {
navigate(-1);
return; return;
} }
navigate(-1); if (backToUrl) {
navigate(backToUrl);
return;
}
navigate('/library');
}; };
}; };