2025-01-18 22:41:54 +01:00
|
|
|
/*
|
|
|
|
|
* 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 { useEffect } from 'react';
|
2025-02-20 13:16:39 +01:00
|
|
|
import { useLocation, useNavigate } from 'react-router-dom';
|
2025-01-18 22:41:54 +01:00
|
|
|
import { Chapters } from '@/modules/chapter/services/Chapters.ts';
|
|
|
|
|
import { DirectionOffset } from '@/Base.types.ts';
|
|
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
|
|
|
|
import { GetChaptersReaderQuery } from '@/lib/graphql/generated/graphql.ts';
|
2025-02-20 13:16:39 +01:00
|
|
|
import {
|
|
|
|
|
IReaderSettings,
|
|
|
|
|
ReaderOpenChapterLocationState,
|
|
|
|
|
ReaderStateChapters,
|
|
|
|
|
} from '@/modules/reader/types/Reader.types.ts';
|
2025-01-28 02:16:00 +01:00
|
|
|
import { READER_STATE_CHAPTERS_DEFAULTS } from '@/modules/reader/contexts/state/ReaderStateChaptersContext.tsx';
|
2025-01-18 22:41:54 +01:00
|
|
|
|
|
|
|
|
export const useReaderSetChaptersState = (
|
|
|
|
|
chaptersResponse: ReturnType<typeof requestManager.useGetMangaChapters<GetChaptersReaderQuery>>,
|
|
|
|
|
chapterSourceOrder: number,
|
2025-01-28 02:16:00 +01:00
|
|
|
initialChapter: ReaderStateChapters['initialChapter'],
|
2025-02-01 20:03:02 +01:00
|
|
|
chapterForDuplicatesHandling: ReaderStateChapters['chapterForDuplicatesHandling'],
|
2025-01-18 22:41:54 +01:00
|
|
|
setReaderStateChapters: ReaderStateChapters['setReaderStateChapters'],
|
|
|
|
|
shouldSkipDupChapters: IReaderSettings['shouldSkipDupChapters'],
|
|
|
|
|
) => {
|
2025-02-11 02:09:28 +01:00
|
|
|
const navigate = useNavigate();
|
2025-02-20 13:16:39 +01:00
|
|
|
const locationState = useLocation<ReaderOpenChapterLocationState>().state;
|
|
|
|
|
|
|
|
|
|
const { updateInitialChapter } = locationState ?? {};
|
|
|
|
|
const finalInitialChapter = updateInitialChapter ? undefined : initialChapter;
|
2025-02-11 02:09:28 +01:00
|
|
|
|
2025-01-18 22:41:54 +01:00
|
|
|
useEffect(() => {
|
|
|
|
|
const newMangaChapters = chaptersResponse.data?.chapters.nodes;
|
|
|
|
|
const newCurrentChapter = newMangaChapters
|
|
|
|
|
? (newMangaChapters[newMangaChapters.length - chapterSourceOrder] ?? null)
|
|
|
|
|
: undefined;
|
2025-02-20 13:16:39 +01:00
|
|
|
const newInitialChapter = finalInitialChapter ?? newCurrentChapter;
|
2025-02-01 20:03:02 +01:00
|
|
|
const newChapterForDuplicatesHandling = chapterForDuplicatesHandling ?? newCurrentChapter;
|
2025-01-18 22:41:54 +01:00
|
|
|
|
2025-01-28 02:16:00 +01:00
|
|
|
const nextChapter =
|
|
|
|
|
newMangaChapters &&
|
|
|
|
|
newCurrentChapter &&
|
|
|
|
|
Chapters.getNextChapter(newCurrentChapter, newMangaChapters, {
|
|
|
|
|
offset: DirectionOffset.NEXT,
|
|
|
|
|
skipDupe: shouldSkipDupChapters,
|
|
|
|
|
skipDupeChapter: newChapterForDuplicatesHandling,
|
|
|
|
|
});
|
|
|
|
|
const previousChapter =
|
|
|
|
|
newMangaChapters &&
|
|
|
|
|
newCurrentChapter &&
|
|
|
|
|
Chapters.getNextChapter(newCurrentChapter, newMangaChapters, {
|
|
|
|
|
offset: DirectionOffset.PREVIOUS,
|
|
|
|
|
skipDupe: shouldSkipDupChapters,
|
|
|
|
|
skipDupeChapter: newChapterForDuplicatesHandling,
|
|
|
|
|
});
|
2025-02-05 20:05:36 +01:00
|
|
|
const newChapters = (() => {
|
|
|
|
|
if (!newMangaChapters || !newChapterForDuplicatesHandling) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (shouldSkipDupChapters) {
|
|
|
|
|
return Chapters.removeDuplicates(newChapterForDuplicatesHandling, newMangaChapters);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newMangaChapters;
|
|
|
|
|
})();
|
2025-01-28 02:16:00 +01:00
|
|
|
|
2025-02-20 13:16:39 +01:00
|
|
|
const hasInitialChapterChanged = newInitialChapter != null && newInitialChapter.id !== finalInitialChapter?.id;
|
2025-01-28 02:16:00 +01:00
|
|
|
|
2025-02-11 02:09:28 +01:00
|
|
|
if (hasInitialChapterChanged) {
|
2025-02-20 13:16:39 +01:00
|
|
|
navigate('', { replace: true, state: { ...locationState, updateInitialChapter: undefined } });
|
2025-02-11 02:09:28 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-28 02:16:00 +01:00
|
|
|
setReaderStateChapters((prevState) => {
|
|
|
|
|
const hasCurrentChapterChanged = newCurrentChapter?.id !== prevState.currentChapter?.id;
|
|
|
|
|
return {
|
|
|
|
|
...prevState,
|
|
|
|
|
mangaChapters: newMangaChapters ?? [],
|
2025-02-05 20:05:36 +01:00
|
|
|
chapters: newChapters,
|
2025-01-28 02:16:00 +01:00
|
|
|
initialChapter: newInitialChapter,
|
|
|
|
|
chapterForDuplicatesHandling: newChapterForDuplicatesHandling,
|
|
|
|
|
currentChapter: newCurrentChapter,
|
|
|
|
|
nextChapter,
|
|
|
|
|
previousChapter,
|
|
|
|
|
isCurrentChapterReady: hasCurrentChapterChanged ? false : prevState.isCurrentChapterReady,
|
|
|
|
|
visibleChapters: hasInitialChapterChanged
|
|
|
|
|
? {
|
|
|
|
|
...READER_STATE_CHAPTERS_DEFAULTS.visibleChapters,
|
|
|
|
|
lastLeadingChapterSourceOrder:
|
|
|
|
|
newInitialChapter?.sourceOrder ??
|
|
|
|
|
READER_STATE_CHAPTERS_DEFAULTS.visibleChapters.lastLeadingChapterSourceOrder,
|
|
|
|
|
lastTrailingChapterSourceOrder:
|
|
|
|
|
newInitialChapter?.sourceOrder ??
|
|
|
|
|
READER_STATE_CHAPTERS_DEFAULTS.visibleChapters.lastTrailingChapterSourceOrder,
|
2025-02-10 01:05:35 +01:00
|
|
|
isLeadingChapterPreloadMode: false,
|
|
|
|
|
isTrailingChapterPreloadMode: false,
|
2025-01-28 02:16:00 +01:00
|
|
|
}
|
|
|
|
|
: prevState.visibleChapters,
|
|
|
|
|
};
|
2025-01-18 22:41:54 +01:00
|
|
|
});
|
2025-02-20 13:16:39 +01:00
|
|
|
}, [chaptersResponse.data?.chapters.nodes, chapterSourceOrder, shouldSkipDupChapters, finalInitialChapter]);
|
2025-01-18 22:41:54 +01:00
|
|
|
};
|