Extract app page history from nav bar context

This commit is contained in:
schroda
2025-01-21 20:26:03 +01:00
parent 9b883a5170
commit 480eaf5a08
7 changed files with 50 additions and 25 deletions

View File

@@ -27,6 +27,7 @@ import { ReaderContextProvider } from '@/modules/reader/contexts/ReaderContextPr
import { DIRECTION_TO_CACHE } from '@/modules/theme/ThemeDirectionCache.ts'; import { DIRECTION_TO_CACHE } from '@/modules/theme/ThemeDirectionCache.ts';
import { AppHotkeysProvider } from '@/modules/hotkeys/contexts/AppHotkeysProvider.tsx'; import { AppHotkeysProvider } from '@/modules/hotkeys/contexts/AppHotkeysProvider.tsx';
import { SnackbarWithDescription } from '@/modules/core/components/snackbar/SnackbarWithDescription.tsx'; import { SnackbarWithDescription } from '@/modules/core/components/snackbar/SnackbarWithDescription.tsx';
import { AppPageHistoryContextProvider } from '@/modules/core/contexts/AppPageHistoryContextProvider.tsx';
interface Props { interface Props {
children: React.ReactNode; children: React.ReactNode;
@@ -93,21 +94,23 @@ export const AppContext: React.FC<Props> = ({ children }) => {
<QueryParamProvider adapter={ReactRouter6Adapter}> <QueryParamProvider adapter={ReactRouter6Adapter}>
<LibraryOptionsContextProvider> <LibraryOptionsContextProvider>
<NavBarContextProvider> <NavBarContextProvider>
<ActiveDeviceContextProvider> <AppPageHistoryContextProvider>
<SnackbarProvider <ActiveDeviceContextProvider>
Components={{ <SnackbarProvider
default: SnackbarWithDescription, Components={{
info: SnackbarWithDescription, default: SnackbarWithDescription,
success: SnackbarWithDescription, info: SnackbarWithDescription,
warning: SnackbarWithDescription, success: SnackbarWithDescription,
error: SnackbarWithDescription, warning: SnackbarWithDescription,
}} error: SnackbarWithDescription,
> }}
<ReaderContextProvider> >
<AppHotkeysProvider>{children}</AppHotkeysProvider> <ReaderContextProvider>
</ReaderContextProvider> <AppHotkeysProvider>{children}</AppHotkeysProvider>
</SnackbarProvider> </ReaderContextProvider>
</ActiveDeviceContextProvider> </SnackbarProvider>
</ActiveDeviceContextProvider>
</AppPageHistoryContextProvider>
</NavBarContextProvider> </NavBarContextProvider>
</LibraryOptionsContextProvider> </LibraryOptionsContextProvider>
</QueryParamProvider> </QueryParamProvider>

View File

@@ -0,0 +1,13 @@
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { createContext, useContext } from 'react';
export const AppPageHistoryContext = createContext<string[]>([]);
export const useAppPageHistoryContext = () => useContext(AppPageHistoryContext);

View File

@@ -0,0 +1,17 @@
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import React from 'react';
import { AppPageHistoryContext } from '@/modules/core/contexts/AppPageHistoryContext.tsx';
import { useHistory } from '@/modules/core/hooks/useHistory.ts';
export const AppPageHistoryContextProvider = ({ children }: { children: React.ReactNode }) => {
const history = useHistory();
return <AppPageHistoryContext.Provider value={history}>{children}</AppPageHistoryContext.Provider>;
};

View File

@@ -8,15 +8,15 @@
import { useLocation, useNavigate } from 'react-router-dom'; import { useLocation, useNavigate } from 'react-router-dom';
import { useCallback } from 'react'; import { useCallback } from 'react';
import { useNavBarContext } from '@/modules/navigation-bar/contexts/NavbarContext.tsx';
import { AppRoutes } from '@/modules/core/AppRoute.constants.ts'; import { AppRoutes } from '@/modules/core/AppRoute.constants.ts';
import { useAppPageHistoryContext } from '@/modules/core/contexts/AppPageHistoryContext.tsx';
const PAGES_TO_IGNORE: readonly RegExp[] = [/\/manga\/[0-9]+\/chapter\/[0-9]+/g]; 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 } = useNavBarContext(); const history = useAppPageHistoryContext();
return useCallback(() => { return useCallback(() => {
const isHistoryEmpty = !history.length; const isHistoryEmpty = !history.length;

View File

@@ -24,8 +24,6 @@ export interface NavbarItem {
} }
export type NavbarContextType = { export type NavbarContextType = {
history: string[];
// AppBar title // AppBar title
title: string | React.ReactNode; title: string | React.ReactNode;
setTitle: (title: NavbarContextType['title'], browserTitle?: string) => void; setTitle: (title: NavbarContextType['title'], browserTitle?: string) => void;

View File

@@ -8,7 +8,6 @@
import React, { useCallback, useMemo, useState } from 'react'; import React, { useCallback, useMemo, useState } from 'react';
import { NavBarContext } from '@/modules/navigation-bar/contexts/NavbarContext.tsx'; import { NavBarContext } from '@/modules/navigation-bar/contexts/NavbarContext.tsx';
import { useHistory } from '@/modules/core/hooks/useHistory.ts';
import { useLocalStorage } from '@/modules/core/hooks/useStorage.tsx'; import { useLocalStorage } from '@/modules/core/hooks/useStorage.tsx';
import { INavbarOverride } from '@/modules/navigation-bar/NavigationBar.types.ts'; import { INavbarOverride } from '@/modules/navigation-bar/NavigationBar.types.ts';
@@ -29,8 +28,6 @@ export function NavBarContextProvider({ children }: IProps) {
const [readerNavBarWidth, setReaderNavBarWidth] = useState(0); const [readerNavBarWidth, setReaderNavBarWidth] = useState(0);
const [bottomBarHeight, setBottomBarHeight] = useState(0); const [bottomBarHeight, setBottomBarHeight] = useState(0);
const history = useHistory();
const updateTitle = useCallback( const updateTitle = useCallback(
(newTitle: string | React.ReactNode, browserTitle: string = typeof newTitle === 'string' ? newTitle : '') => { (newTitle: string | React.ReactNode, browserTitle: string = typeof newTitle === 'string' ? newTitle : '') => {
document.title = `${browserTitle} - Suwayomi`; document.title = `${browserTitle} - Suwayomi`;
@@ -41,7 +38,6 @@ export function NavBarContextProvider({ children }: IProps) {
const value = useMemo( const value = useMemo(
() => ({ () => ({
history,
title, title,
setTitle: updateTitle, setTitle: updateTitle,
appBarHeight, appBarHeight,
@@ -60,7 +56,6 @@ export function NavBarContextProvider({ children }: IProps) {
setBottomBarHeight, setBottomBarHeight,
}), }),
[ [
history,
title, title,
updateTitle, updateTitle,
appBarHeight, appBarHeight,

View File

@@ -11,7 +11,6 @@ import React, { useContext } from 'react';
import { NavbarContextType } from '@/modules/navigation-bar/NavigationBar.types.ts'; import { NavbarContextType } from '@/modules/navigation-bar/NavigationBar.types.ts';
export const NavBarContext = React.createContext<NavbarContextType>({ export const NavBarContext = React.createContext<NavbarContextType>({
history: [],
title: 'Suwayomi', title: 'Suwayomi',
setTitle: (): void => {}, setTitle: (): void => {},
appBarHeight: 0, appBarHeight: 0,