Ignore reader when going back in app history

Navigation back through the apps back button should not open the reader again.
This is mostly a preparation for being able to open the manga page from the reader, regardless of if it was the previous page or not, in the future, in that case, the reader will be in the history stack and would be incorrectly opened through the back button
This commit is contained in:
schroda
2024-10-19 16:34:51 +02:00
parent 39dea49203
commit 64ac4a54fd

View File

@@ -7,19 +7,21 @@
*/ */
import { useLocation, useNavigate } from 'react-router-dom'; import { useLocation, useNavigate } from 'react-router-dom';
import { useContext } from 'react'; import { useNavBarContext } from '@/modules/navigation-bar/contexts/NavbarContext.tsx';
import { NavBarContext } from '@/modules/navigation-bar/contexts/NavbarContext.tsx';
const PAGES_TO_IGNORE: readonly RegExp[] = [/\/manga\/[0-9]+\/chapter\/[0-9]+/g];
export const useBackButton = () => { export const useBackButton = () => {
const navigate = useNavigate(); const navigate = useNavigate();
const location = useLocation(); const location = useLocation();
const { history } = useContext(NavBarContext); const { history } = useNavBarContext();
return () => { return () => {
const isHistoryEmpty = !history.length; const isHistoryEmpty = !history.length;
const isLastPageInHistoryCurrentPage = history.length === 1 && history[0] === location.pathname; const isLastPageInHistoryCurrentPage = history.length === 1 && history[0] === location.pathname;
const ignorePreviousPage = history.length && PAGES_TO_IGNORE.some((page) => !!history.slice(-2)[0].match(page));
const canNavigateBack = !isHistoryEmpty && !isLastPageInHistoryCurrentPage; const canNavigateBack = !ignorePreviousPage && !isHistoryEmpty && !isLastPageInHistoryCurrentPage;
if (canNavigateBack) { if (canNavigateBack) {
navigate(-1); navigate(-1);
return; return;