From 64ac4a54fd6d62357fe2090dcaab765b903b2cc7 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Sat, 19 Oct 2024 16:34:51 +0200 Subject: [PATCH] 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 --- src/modules/core/hooks/useBackButton.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/modules/core/hooks/useBackButton.ts b/src/modules/core/hooks/useBackButton.ts index 0c7173ea..cbb2acb5 100644 --- a/src/modules/core/hooks/useBackButton.ts +++ b/src/modules/core/hooks/useBackButton.ts @@ -7,19 +7,21 @@ */ import { useLocation, useNavigate } from 'react-router-dom'; -import { useContext } from 'react'; -import { NavBarContext } from '@/modules/navigation-bar/contexts/NavbarContext.tsx'; +import { useNavBarContext } from '@/modules/navigation-bar/contexts/NavbarContext.tsx'; + +const PAGES_TO_IGNORE: readonly RegExp[] = [/\/manga\/[0-9]+\/chapter\/[0-9]+/g]; export const useBackButton = () => { const navigate = useNavigate(); const location = useLocation(); - const { history } = useContext(NavBarContext); + const { history } = useNavBarContext(); return () => { const isHistoryEmpty = !history.length; 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) { navigate(-1); return;