Fix resuming last read page

Regression 856e928c44
This commit is contained in:
schroda
2025-02-20 13:16:39 +01:00
parent e3fc8f4195
commit ce2281a8b9
2 changed files with 16 additions and 10 deletions

View File

@@ -7,12 +7,16 @@
*/ */
import { useEffect } from 'react'; import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom'; import { useLocation, useNavigate } from 'react-router-dom';
import { Chapters } from '@/modules/chapter/services/Chapters.ts'; import { Chapters } from '@/modules/chapter/services/Chapters.ts';
import { DirectionOffset } from '@/Base.types.ts'; import { DirectionOffset } from '@/Base.types.ts';
import { requestManager } from '@/lib/requests/RequestManager.ts'; import { requestManager } from '@/lib/requests/RequestManager.ts';
import { GetChaptersReaderQuery } from '@/lib/graphql/generated/graphql.ts'; import { GetChaptersReaderQuery } from '@/lib/graphql/generated/graphql.ts';
import { IReaderSettings, ReaderStateChapters } from '@/modules/reader/types/Reader.types.ts'; import {
IReaderSettings,
ReaderOpenChapterLocationState,
ReaderStateChapters,
} from '@/modules/reader/types/Reader.types.ts';
import { READER_STATE_CHAPTERS_DEFAULTS } from '@/modules/reader/contexts/state/ReaderStateChaptersContext.tsx'; import { READER_STATE_CHAPTERS_DEFAULTS } from '@/modules/reader/contexts/state/ReaderStateChaptersContext.tsx';
export const useReaderSetChaptersState = ( export const useReaderSetChaptersState = (
@@ -24,13 +28,17 @@ export const useReaderSetChaptersState = (
shouldSkipDupChapters: IReaderSettings['shouldSkipDupChapters'], shouldSkipDupChapters: IReaderSettings['shouldSkipDupChapters'],
) => { ) => {
const navigate = useNavigate(); const navigate = useNavigate();
const locationState = useLocation<ReaderOpenChapterLocationState>().state;
const { updateInitialChapter } = locationState ?? {};
const finalInitialChapter = updateInitialChapter ? undefined : initialChapter;
useEffect(() => { useEffect(() => {
const newMangaChapters = chaptersResponse.data?.chapters.nodes; const newMangaChapters = chaptersResponse.data?.chapters.nodes;
const newCurrentChapter = newMangaChapters const newCurrentChapter = newMangaChapters
? (newMangaChapters[newMangaChapters.length - chapterSourceOrder] ?? null) ? (newMangaChapters[newMangaChapters.length - chapterSourceOrder] ?? null)
: undefined; : undefined;
const newInitialChapter = initialChapter ?? newCurrentChapter; const newInitialChapter = finalInitialChapter ?? newCurrentChapter;
const newChapterForDuplicatesHandling = chapterForDuplicatesHandling ?? newCurrentChapter; const newChapterForDuplicatesHandling = chapterForDuplicatesHandling ?? newCurrentChapter;
const nextChapter = const nextChapter =
@@ -61,10 +69,10 @@ export const useReaderSetChaptersState = (
return newMangaChapters; return newMangaChapters;
})(); })();
const hasInitialChapterChanged = newInitialChapter != null && newInitialChapter.id !== initialChapter?.id; const hasInitialChapterChanged = newInitialChapter != null && newInitialChapter.id !== finalInitialChapter?.id;
if (hasInitialChapterChanged) { if (hasInitialChapterChanged) {
navigate('', { replace: true }); navigate('', { replace: true, state: { ...locationState, updateInitialChapter: undefined } });
} }
setReaderStateChapters((prevState) => { setReaderStateChapters((prevState) => {
@@ -94,5 +102,5 @@ export const useReaderSetChaptersState = (
: prevState.visibleChapters, : prevState.visibleChapters,
}; };
}); });
}, [chaptersResponse.data?.chapters.nodes, chapterSourceOrder, shouldSkipDupChapters, initialChapter]); }, [chaptersResponse.data?.chapters.nodes, chapterSourceOrder, shouldSkipDupChapters, finalInitialChapter]);
}; };

View File

@@ -8,7 +8,7 @@
import Box from '@mui/material/Box'; import Box from '@mui/material/Box';
import { memo, useEffect, useLayoutEffect, useRef, useState } from 'react'; import { memo, useEffect, useLayoutEffect, useRef, useState } from 'react';
import { useLocation, useParams } from 'react-router-dom'; import { useParams } from 'react-router-dom';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useDefaultReaderSettings } from '@/modules/reader/services/ReaderSettingsMetadata.ts'; import { useDefaultReaderSettings } from '@/modules/reader/services/ReaderSettingsMetadata.ts';
import { useNavBarContext } from '@/modules/navigation-bar/contexts/NavbarContext.tsx'; import { useNavBarContext } from '@/modules/navigation-bar/contexts/NavbarContext.tsx';
@@ -33,7 +33,6 @@ import { ReaderHotkeys } from '@/modules/reader/components/ReaderHotkeys.tsx';
import { import {
IReaderSettings, IReaderSettings,
IReaderSettingsWithDefaultFlag, IReaderSettingsWithDefaultFlag,
ReaderOpenChapterLocationState,
ReaderStateChapters, ReaderStateChapters,
TReaderAutoScrollContext, TReaderAutoScrollContext,
TReaderStateMangaContext, TReaderStateMangaContext,
@@ -113,7 +112,6 @@ const BaseReader = ({
cancelAutoScroll: TReaderAutoScrollContext['cancel']; cancelAutoScroll: TReaderAutoScrollContext['cancel'];
}) => { }) => {
const { t } = useTranslation(); const { t } = useTranslation();
const { updateInitialChapter } = useLocation<ReaderOpenChapterLocationState>().state ?? {};
const scrollElementRef = useRef<HTMLDivElement | null>(null); const scrollElementRef = useRef<HTMLDivElement | null>(null);
@@ -192,7 +190,7 @@ const BaseReader = ({
useReaderSetChaptersState( useReaderSetChaptersState(
chaptersResponse, chaptersResponse,
chapterSourceOrder, chapterSourceOrder,
updateInitialChapter ? undefined : initialChapter, initialChapter,
chapterForDuplicatesHandling, chapterForDuplicatesHandling,
setReaderStateChapters, setReaderStateChapters,
shouldSkipDupChapters, shouldSkipDupChapters,