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';
|
|
|
|
|
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';
|
|
|
|
|
import { IReaderSettings, ReaderStateChapters } from '@/modules/reader/types/Reader.types.ts';
|
|
|
|
|
|
|
|
|
|
export const useReaderSetChaptersState = (
|
|
|
|
|
chaptersResponse: ReturnType<typeof requestManager.useGetMangaChapters<GetChaptersReaderQuery>>,
|
|
|
|
|
chapterSourceOrder: number,
|
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'],
|
|
|
|
|
) => {
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const newMangaChapters = chaptersResponse.data?.chapters.nodes;
|
|
|
|
|
const newCurrentChapter = newMangaChapters
|
|
|
|
|
? (newMangaChapters[newMangaChapters.length - chapterSourceOrder] ?? null)
|
|
|
|
|
: undefined;
|
2025-02-01 20:03:02 +01:00
|
|
|
const newChapterForDuplicatesHandling = chapterForDuplicatesHandling ?? newCurrentChapter;
|
2025-01-18 22:41:54 +01:00
|
|
|
|
|
|
|
|
setReaderStateChapters({
|
|
|
|
|
mangaChapters: newMangaChapters ?? [],
|
|
|
|
|
chapters:
|
2025-02-01 20:03:02 +01:00
|
|
|
newChapterForDuplicatesHandling && newMangaChapters
|
|
|
|
|
? Chapters.removeDuplicates(newChapterForDuplicatesHandling, newMangaChapters)
|
2025-01-18 22:41:54 +01:00
|
|
|
: [],
|
2025-02-01 20:03:02 +01:00
|
|
|
chapterForDuplicatesHandling: newChapterForDuplicatesHandling,
|
2025-01-18 22:41:54 +01:00
|
|
|
currentChapter: newCurrentChapter,
|
|
|
|
|
nextChapter:
|
|
|
|
|
newMangaChapters &&
|
|
|
|
|
newCurrentChapter &&
|
|
|
|
|
Chapters.getNextChapter(newCurrentChapter, newMangaChapters, {
|
|
|
|
|
offset: DirectionOffset.NEXT,
|
|
|
|
|
skipDupe: shouldSkipDupChapters,
|
2025-02-01 20:03:02 +01:00
|
|
|
skipDupeChapter: newChapterForDuplicatesHandling,
|
2025-01-18 22:41:54 +01:00
|
|
|
}),
|
|
|
|
|
previousChapter:
|
|
|
|
|
newMangaChapters &&
|
|
|
|
|
newCurrentChapter &&
|
|
|
|
|
Chapters.getNextChapter(newCurrentChapter, newMangaChapters, {
|
|
|
|
|
offset: DirectionOffset.PREVIOUS,
|
|
|
|
|
skipDupe: shouldSkipDupChapters,
|
2025-02-01 20:03:02 +01:00
|
|
|
skipDupeChapter: newChapterForDuplicatesHandling,
|
2025-01-18 22:41:54 +01:00
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
}, [chaptersResponse.data?.chapters.nodes, chapterSourceOrder, shouldSkipDupChapters]);
|
|
|
|
|
};
|