Streamline route location state

This commit is contained in:
schroda
2026-05-27 23:59:58 +02:00
parent 29bb60c28b
commit e973872539
13 changed files with 103 additions and 44 deletions

View File

@@ -383,7 +383,7 @@ export enum ReaderResumeMode {
LAST_READ,
}
export interface ReaderOpenChapterLocationState {
export interface RouteStateReader {
resumeMode?: ReaderResumeMode;
updateInitialChapter?: boolean;
}

View File

@@ -11,11 +11,7 @@ import { useLocation, useNavigate } from 'react-router-dom';
import { Chapters } from '@/features/chapter/services/Chapters.ts';
import type { requestManager } from '@/lib/requests/RequestManager.ts';
import type { GetChaptersReaderQuery } from '@/lib/graphql/generated/graphql.ts';
import type {
IReaderSettings,
ReaderOpenChapterLocationState,
ReaderStateChapters,
} from '@/features/reader/Reader.types.ts';
import type { IReaderSettings, RouteStateReader, ReaderStateChapters } from '@/features/reader/Reader.types.ts';
import { filterChapters } from '@/features/chapter/utils/ChapterList.util.tsx';
import type { ChapterListFilterOptions } from '@/features/chapter/Chapter.types.ts';
import { getReaderChapterFromCache } from '@/features/reader/Reader.utils.ts';
@@ -24,6 +20,7 @@ import { getReaderChaptersStore } from '@/features/reader/stores/ReaderStore.ts'
import { STABLE_EMPTY_OBJECT } from '@/base/Base.constants.ts';
import { READER_DEFAULT_CHAPTERS_STATE } from '@/features/reader/stores/ReaderChaptersStore.ts';
import { AppRoutes } from '@/base/AppRoute.constants.ts';
export const useReaderSetChaptersState = (
chaptersResponse: ReturnType<typeof requestManager.useGetMangaChapters<GetChaptersReaderQuery>>,
@@ -36,7 +33,7 @@ export const useReaderSetChaptersState = (
chapterListOptions: ChapterListFilterOptions,
) => {
const navigate = useNavigate();
const locationState = useLocation<ReaderOpenChapterLocationState>().state;
const locationState = useLocation<RouteStateReader>().state;
const { updateInitialChapter } = locationState ?? STABLE_EMPTY_OBJECT;
const finalInitialChapter = updateInitialChapter ? undefined : initialChapter;
@@ -77,7 +74,10 @@ export const useReaderSetChaptersState = (
const hasInitialChapterChanged = newInitialChapter != null && newInitialChapter.id !== finalInitialChapter?.id;
if (hasInitialChapterChanged) {
navigate('', { replace: true, state: { ...locationState, updateInitialChapter: undefined } });
navigate('', {
replace: true,
state: { ...locationState, ...AppRoutes.reader.state({ updateInitialChapter: undefined }) },
});
}
getReaderChaptersStore().setReaderStateChapters((prevState) => {

View File

@@ -13,7 +13,7 @@ import { useTheme } from '@mui/material/styles';
import { t } from '@lingui/core/macro';
import type { ChapterIdInfo, TChapterReader } from '@/features/chapter/Chapter.types.ts';
import { Chapters } from '@/features/chapter/services/Chapters.ts';
import type { IReaderSettings, ReaderOpenChapterLocationState } from '@/features/reader/Reader.types.ts';
import type { IReaderSettings, RouteStateReader } from '@/features/reader/Reader.types.ts';
import { ReaderExitMode, ReaderOverlayMode, ReadingDirection, ReadingMode } from '@/features/reader/Reader.types.ts';
import { requestManager } from '@/lib/requests/RequestManager.ts';
import { MANGA_META_FIELDS } from '@/lib/graphql/manga/MangaFragments.ts';
@@ -82,7 +82,7 @@ export class ReaderService {
return ReaderService.chapterUpdateQueues.get(id)!;
}
static navigateToChapter(chapter: TChapterReader, state?: ReaderOpenChapterLocationState): void {
static navigateToChapter(chapter: TChapterReader, state?: RouteStateReader): void {
ReactRouter.navigate(Chapters.getReaderUrl(chapter), {
replace: true,
state,

View File

@@ -12,7 +12,7 @@ import Stack from '@mui/material/Stack';
import { useTheme } from '@mui/material/styles';
import { useLocation } from 'react-router-dom';
import { useMergedRef } from '@mantine/hooks';
import type { ReaderOpenChapterLocationState } from '@/features/reader/Reader.types.ts';
import type { RouteStateReader } from '@/features/reader/Reader.types.ts';
import { PageInViewportType, ReaderResumeMode, ReadingDirection, ReadingMode } from '@/features/reader/Reader.types.ts';
import { MediaQuery } from '@/base/utils/MediaQuery.tsx';
import { ReaderControls } from '@/features/reader/services/ReaderControls.ts';
@@ -50,6 +50,7 @@ import {
useReaderPagesStore,
useReaderSettingsStore,
} from '@/features/reader/stores/ReaderStore.ts';
import { STABLE_EMPTY_OBJECT } from '@/base/Base.constants.ts';
const READING_MODE_TO_IN_VIEWPORT_TYPE: Record<ReadingMode, PageInViewportType> = {
[ReadingMode.SINGLE_PAGE]: PageInViewportType.X,
@@ -109,9 +110,7 @@ const BaseReaderViewer = ({
isStaticNav: state.isStaticNav,
}));
const safeAreaInset = useReaderSettingsStore('safeAreaInset');
const { resumeMode = ReaderResumeMode.START } = useLocation<ReaderOpenChapterLocationState>().state ?? {
resumeMode: ReaderResumeMode.START,
};
const { resumeMode = ReaderResumeMode.START } = useLocation<RouteStateReader>().state ?? STABLE_EMPTY_OBJECT;
const scrollElementRef = useRef<HTMLDivElement | null>(null);
const mergedRef = useMergedRef(ref, scrollElementRef);