Make react router routing possible outside components
This commit is contained in:
14
src/App.tsx
14
src/App.tsx
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
import CssBaseline from '@mui/material/CssBaseline';
|
import CssBaseline from '@mui/material/CssBaseline';
|
||||||
import React, { useEffect, useLayoutEffect } from 'react';
|
import React, { useEffect, useLayoutEffect } from 'react';
|
||||||
import { Navigate, Outlet, Route, Routes, useLocation } from 'react-router-dom';
|
import { Navigate, Outlet, Route, Routes, useLocation, useNavigate } from 'react-router-dom';
|
||||||
import { loadErrorMessages, loadDevMessages } from '@apollo/client/dev';
|
import { loadErrorMessages, loadDevMessages } from '@apollo/client/dev';
|
||||||
import { loadable } from 'react-lazily/loadable';
|
import { loadable } from 'react-lazily/loadable';
|
||||||
import Box from '@mui/material/Box';
|
import Box from '@mui/material/Box';
|
||||||
@@ -32,6 +32,7 @@ import { LoginPage } from '@/features/authentication/screens/LoginPage.tsx';
|
|||||||
import { AuthGuard } from '@/features/authentication/components/AuthGuard.tsx';
|
import { AuthGuard } from '@/features/authentication/components/AuthGuard.tsx';
|
||||||
import { SearchParam } from '@/base/Base.types.ts';
|
import { SearchParam } from '@/base/Base.types.ts';
|
||||||
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
||||||
|
import { ReactRouter } from '@/lib/react-router/ReactRouter.ts';
|
||||||
|
|
||||||
const { Browse } = loadable(() => import('@/features/browse/screens/Browse.tsx'), lazyLoadFallback);
|
const { Browse } = loadable(() => import('@/features/browse/screens/Browse.tsx'), lazyLoadFallback);
|
||||||
const { DownloadQueue } = loadable(() => import('@/features/downloads/screens/DownloadQueue.tsx'), lazyLoadFallback);
|
const { DownloadQueue } = loadable(() => import('@/features/downloads/screens/DownloadQueue.tsx'), lazyLoadFallback);
|
||||||
@@ -143,6 +144,16 @@ const BackgroundSubscriptions = () => {
|
|||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const ReactRouterSetter = () => {
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
ReactRouter.setNavigateFn(navigate);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
const PrivateRoutes = () => {
|
const PrivateRoutes = () => {
|
||||||
const { isAuthRequired, accessToken, refreshToken } = useSessionContext();
|
const { isAuthRequired, accessToken, refreshToken } = useSessionContext();
|
||||||
|
|
||||||
@@ -291,6 +302,7 @@ export const App: React.FC = () => (
|
|||||||
<WebUIUpdateChecker />
|
<WebUIUpdateChecker />
|
||||||
<InitialBackgroundRequests />
|
<InitialBackgroundRequests />
|
||||||
<BackgroundSubscriptions />
|
<BackgroundSubscriptions />
|
||||||
|
<ReactRouterSetter />
|
||||||
<CssBaseline enableColorScheme />
|
<CssBaseline enableColorScheme />
|
||||||
<AuthGuard>
|
<AuthGuard>
|
||||||
<Box sx={{ display: 'flex' }}>
|
<Box sx={{ display: 'flex' }}>
|
||||||
|
|||||||
@@ -166,8 +166,6 @@ export class ReaderControls {
|
|||||||
) => void {
|
) => void {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
const openChapter = ReaderService.useNavigateToChapter();
|
|
||||||
|
|
||||||
return useCallback(
|
return useCallback(
|
||||||
(offset, doTransitionCheck = true, scrollIntoView = true) => {
|
(offset, doTransitionCheck = true, scrollIntoView = true) => {
|
||||||
const {
|
const {
|
||||||
@@ -250,7 +248,7 @@ export class ReaderControls {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
openChapter(chapterToOpen, {
|
ReaderService.navigateToChapter(chapterToOpen, {
|
||||||
resumeMode: getReaderOpenChapterResumeMode(
|
resumeMode: getReaderOpenChapterResumeMode(
|
||||||
isSpecificChapterMode || !keepRenderedChapters,
|
isSpecificChapterMode || !keepRenderedChapters,
|
||||||
isPreviousChapter,
|
isPreviousChapter,
|
||||||
@@ -264,7 +262,7 @@ export class ReaderControls {
|
|||||||
|
|
||||||
doOpenChapter().catch(defaultPromiseErrorHandler('ReaderControls#useOpenChapter'));
|
doOpenChapter().catch(defaultPromiseErrorHandler('ReaderControls#useOpenChapter'));
|
||||||
},
|
},
|
||||||
[t, openChapter],
|
[t],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ import { FALLBACK_MANGA } from '@/features/manga/Manga.constants.ts';
|
|||||||
import { getMetadataKey } from '@/features/metadata/Metadata.utils.ts';
|
import { getMetadataKey } from '@/features/metadata/Metadata.utils.ts';
|
||||||
import { DirectionOffset } from '@/base/Base.types.ts';
|
import { DirectionOffset } from '@/base/Base.types.ts';
|
||||||
import { getReaderStore, useReaderStore } from '@/features/reader/stores/ReaderStore.ts';
|
import { getReaderStore, useReaderStore } from '@/features/reader/stores/ReaderStore.ts';
|
||||||
|
import { ReactRouter } from '@/lib/react-router/ReactRouter.ts';
|
||||||
|
|
||||||
const DIRECTION_TO_INVERTED: Record<Direction, Direction> = {
|
const DIRECTION_TO_INVERTED: Record<Direction, Direction> = {
|
||||||
ltr: 'rtl',
|
ltr: 'rtl',
|
||||||
@@ -71,15 +72,11 @@ export class ReaderService {
|
|||||||
return ReaderService.chapterUpdateQueues.get(id)!;
|
return ReaderService.chapterUpdateQueues.get(id)!;
|
||||||
}
|
}
|
||||||
|
|
||||||
static useNavigateToChapter(): (chapter: TChapterReader, state?: ReaderOpenChapterLocationState) => void {
|
static navigateToChapter(chapter: TChapterReader, state?: ReaderOpenChapterLocationState): void {
|
||||||
const navigate = useNavigate();
|
ReactRouter.navigate(Chapters.getReaderUrl(chapter), {
|
||||||
|
|
||||||
return useCallback((chapter, state) => {
|
|
||||||
navigate(Chapters.getReaderUrl(chapter), {
|
|
||||||
replace: true,
|
replace: true,
|
||||||
state,
|
state,
|
||||||
});
|
});
|
||||||
}, []);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static downloadAhead(
|
static downloadAhead(
|
||||||
|
|||||||
29
src/lib/react-router/ReactRouter.ts
Normal file
29
src/lib/react-router/ReactRouter.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* 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 { NavigateFunction, NavigateOptions, To } from 'react-router-dom';
|
||||||
|
import { noOp } from '@/lib/HelperFunctions.ts';
|
||||||
|
|
||||||
|
export class ReactRouter {
|
||||||
|
private static navigateFn: NavigateFunction = noOp;
|
||||||
|
|
||||||
|
static setNavigateFn(navigate: NavigateFunction) {
|
||||||
|
this.navigateFn = navigate;
|
||||||
|
}
|
||||||
|
|
||||||
|
static navigate(delta: number): void;
|
||||||
|
static navigate(to: To, options?: NavigateOptions): void;
|
||||||
|
static navigate(toOrDelta: number | To, options?: NavigateOptions): void {
|
||||||
|
if (typeof toOrDelta === 'number') {
|
||||||
|
this.navigateFn(toOrDelta);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.navigateFn(toOrDelta, options);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user