Files
suwayomi-material-you-webui/src/features/reader/hooks/useReaderSetChaptersState.ts

152 lines
6.7 KiB
TypeScript
Raw Normal View History

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, useMemo, useRef } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
2025-08-15 22:02:58 +02:00
import { Chapters } from '@/features/chapter/services/Chapters.ts';
2025-01-18 22:41:54 +01:00
import { requestManager } from '@/lib/requests/RequestManager.ts';
import { GetChaptersReaderQuery } from '@/lib/graphql/generated/graphql.ts';
import {
IReaderSettings,
ReaderOpenChapterLocationState,
ReaderStateChapters,
2025-08-16 02:02:13 +02:00
} from '@/features/reader/Reader.types.ts';
2025-08-15 22:02:58 +02:00
import { filterChapters } from '@/features/chapter/utils/ChapterList.util.tsx';
import { ChapterListOptions } from '@/features/chapter/Chapter.types.ts';
2025-08-16 02:02:13 +02:00
import { getReaderChapterFromCache } from '@/features/reader/Reader.utils.ts';
import { DirectionOffset } from '@/base/Base.types.ts';
import { getReaderChaptersStore } from '@/features/reader/stores/ReaderStore.ts';
import { READER_DEFAULT_CHAPTERS_STATE } from '@/features/reader/stores/ReaderChaptersStore.ts';
2025-01-18 22:41:54 +01:00
export const useReaderSetChaptersState = (
chaptersResponse: ReturnType<typeof requestManager.useGetMangaChapters<GetChaptersReaderQuery>>,
chapterSourceOrder: number,
mangaChapters: ReaderStateChapters['mangaChapters'],
2025-01-28 02:16:00 +01:00
initialChapter: ReaderStateChapters['initialChapter'],
chapterForDuplicatesHandling: ReaderStateChapters['chapterForDuplicatesHandling'],
2025-01-18 22:41:54 +01:00
shouldSkipDupChapters: IReaderSettings['shouldSkipDupChapters'],
shouldSkipFilteredChapters: IReaderSettings['shouldSkipFilteredChapters'],
chapterListOptions: ChapterListOptions,
2025-01-18 22:41:54 +01:00
) => {
const navigate = useNavigate();
const locationState = useLocation<ReaderOpenChapterLocationState>().state;
const { updateInitialChapter } = locationState ?? {};
const finalInitialChapter = updateInitialChapter ? undefined : initialChapter;
const newMangaChapters = chaptersResponse.data?.chapters.nodes;
const newCurrentChapter = useMemo(
() => (newMangaChapters ? (newMangaChapters[newMangaChapters.length - chapterSourceOrder] ?? null) : undefined),
[newMangaChapters, chapterSourceOrder],
);
const newInitialChapter = finalInitialChapter ?? newCurrentChapter;
const newChapterForDuplicatesHandling = chapterForDuplicatesHandling ?? newCurrentChapter;
const filteredChapters = useMemo(() => {
if (!newMangaChapters) {
return newMangaChapters;
}
return shouldSkipFilteredChapters
? filterChapters(mangaChapters ?? newMangaChapters, chapterListOptions)
: newMangaChapters;
}, [newMangaChapters, shouldSkipFilteredChapters, mangaChapters, chapterListOptions]);
const uniqueChapters = useMemo(() => {
if (!filteredChapters || !newChapterForDuplicatesHandling) {
return filteredChapters;
}
return shouldSkipDupChapters
? Chapters.removeDuplicates(newChapterForDuplicatesHandling, filteredChapters)
: filteredChapters;
}, [filteredChapters, shouldSkipDupChapters, newChapterForDuplicatesHandling]);
const visibleChapters = useMemo(() => {
if (!uniqueChapters) {
return [];
}
return uniqueChapters.map((chapter) => getReaderChapterFromCache(chapter.id)!);
}, [uniqueChapters]);
const prevVisibleChaptersRef = useRef<typeof visibleChapters>([]);
2025-01-18 22:41:54 +01:00
useEffect(() => {
// Check if visibleChapters actually changed by comparing chapter IDs
const hasChaptersChanged =
visibleChapters.length !== prevVisibleChaptersRef.current.length ||
visibleChapters.some((chapter, index) => chapter.id !== prevVisibleChaptersRef.current[index]?.id);
// Use the previous reference if chapters haven't actually changed
const stableVisibleChapters = hasChaptersChanged ? visibleChapters : prevVisibleChaptersRef.current;
// Update the ref for next comparison
if (hasChaptersChanged) {
prevVisibleChaptersRef.current = visibleChapters;
}
2025-01-28 02:16:00 +01:00
const nextChapter =
newCurrentChapter &&
Chapters.getNextChapter(newCurrentChapter, stableVisibleChapters, {
2025-01-28 02:16:00 +01:00
offset: DirectionOffset.NEXT,
});
const previousChapter =
newCurrentChapter &&
Chapters.getNextChapter(newCurrentChapter, stableVisibleChapters, {
2025-01-28 02:16:00 +01:00
offset: DirectionOffset.PREVIOUS,
});
const hasInitialChapterChanged = newInitialChapter != null && newInitialChapter.id !== finalInitialChapter?.id;
2025-01-28 02:16:00 +01:00
if (hasInitialChapterChanged) {
navigate('', { replace: true, state: { ...locationState, updateInitialChapter: undefined } });
}
getReaderChaptersStore().setReaderStateChapters((prevState) => {
2025-01-28 02:16:00 +01:00
const hasCurrentChapterChanged = newCurrentChapter?.id !== prevState.currentChapter?.id;
2025-01-28 02:16:00 +01:00
return {
...prevState,
mangaChapters: prevState.mangaChapters ?? newMangaChapters,
chapters: stableVisibleChapters,
2025-01-28 02:16:00 +01:00
initialChapter: newInitialChapter,
chapterForDuplicatesHandling: newChapterForDuplicatesHandling,
currentChapter: newCurrentChapter,
nextChapter,
previousChapter,
isCurrentChapterReady: hasCurrentChapterChanged ? false : prevState.isCurrentChapterReady,
visibleChapters: hasInitialChapterChanged
? {
...READER_DEFAULT_CHAPTERS_STATE.chapters.visibleChapters,
2025-01-28 02:16:00 +01:00
lastLeadingChapterSourceOrder:
newInitialChapter?.sourceOrder ??
READER_DEFAULT_CHAPTERS_STATE.chapters.visibleChapters.lastLeadingChapterSourceOrder,
2025-01-28 02:16:00 +01:00
lastTrailingChapterSourceOrder:
newInitialChapter?.sourceOrder ??
READER_DEFAULT_CHAPTERS_STATE.chapters.visibleChapters.lastTrailingChapterSourceOrder,
isLeadingChapterPreloadMode: false,
isTrailingChapterPreloadMode: false,
// do not set "scrollIntoView" to "true" for the initial render
scrollIntoView: !!prevState.initialChapter,
2025-01-28 02:16:00 +01:00
}
: prevState.visibleChapters,
};
2025-01-18 22:41:54 +01:00
});
}, [
visibleChapters,
newCurrentChapter,
newInitialChapter,
finalInitialChapter,
newMangaChapters,
newChapterForDuplicatesHandling,
locationState,
]);
2025-01-18 22:41:54 +01:00
};