Extract "reader chapters store slice" into file
This commit is contained in:
@@ -21,7 +21,8 @@ import { ChapterListOptions } from '@/features/chapter/Chapter.types.ts';
|
||||
import { getReaderChapterFromCache } from '@/features/reader/Reader.utils.ts';
|
||||
import { DirectionOffset } from '@/base/Base.types.ts';
|
||||
import { getReaderStore } from '@/features/reader/stores/ReaderStore.ts';
|
||||
import { READER_DEFAULT_CHAPTERS_STATE } from '@/features/reader/stores/ReaderStore.constants.ts';
|
||||
|
||||
import { READER_DEFAULT_CHAPTERS_STATE } from '@/features/reader/stores/ReaderChaptersStore.ts';
|
||||
|
||||
export const useReaderSetChaptersState = (
|
||||
chaptersResponse: ReturnType<typeof requestManager.useGetMangaChapters<GetChaptersReaderQuery>>,
|
||||
|
||||
59
src/features/reader/stores/ReaderChaptersStore.ts
Normal file
59
src/features/reader/stores/ReaderChaptersStore.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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 { ImmerStateCreator } from '@/lib/zustand/Zustand.types.ts';
|
||||
import { ReaderStateChapters } from '@/features/reader/Reader.types.ts';
|
||||
|
||||
export interface ReaderChaptersStoreSlice {
|
||||
chapters: ReaderStateChapters & {
|
||||
reset: () => void;
|
||||
};
|
||||
}
|
||||
|
||||
export const READER_DEFAULT_CHAPTERS_STATE = {
|
||||
chapters: {
|
||||
chapters: [],
|
||||
isCurrentChapterReady: false,
|
||||
visibleChapters: {
|
||||
leading: 0,
|
||||
trailing: 0,
|
||||
lastLeadingChapterSourceOrder: 99999,
|
||||
lastTrailingChapterSourceOrder: -1,
|
||||
isLeadingChapterPreloadMode: true,
|
||||
isTrailingChapterPreloadMode: true,
|
||||
scrollIntoView: false,
|
||||
resumeMode: undefined,
|
||||
},
|
||||
},
|
||||
} satisfies {
|
||||
chapters: Omit<ReaderStateChapters, 'setReaderStateChapters'>;
|
||||
};
|
||||
|
||||
export const createReaderChaptersStoreSlice = <T extends ReaderChaptersStoreSlice>(
|
||||
...[set, get]: Parameters<ImmerStateCreator<T>>
|
||||
): ReaderChaptersStoreSlice => ({
|
||||
chapters: {
|
||||
...READER_DEFAULT_CHAPTERS_STATE.chapters,
|
||||
reset: () => set(() => ({ chapters: { ...get().chapters, ...READER_DEFAULT_CHAPTERS_STATE.chapters } })),
|
||||
setReaderStateChapters: (state) =>
|
||||
set((draft) => {
|
||||
if (typeof state === 'function') {
|
||||
draft.chapters = {
|
||||
...get().chapters,
|
||||
...state(get().chapters),
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
draft.chapters = {
|
||||
...get().chapters,
|
||||
...state,
|
||||
};
|
||||
}),
|
||||
},
|
||||
});
|
||||
@@ -1,28 +0,0 @@
|
||||
/*
|
||||
* 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 { ReaderStateChapters } from '@/features/reader/Reader.types.ts';
|
||||
|
||||
export const READER_DEFAULT_CHAPTERS_STATE = {
|
||||
chapters: {
|
||||
chapters: [],
|
||||
isCurrentChapterReady: false,
|
||||
visibleChapters: {
|
||||
leading: 0,
|
||||
trailing: 0,
|
||||
lastLeadingChapterSourceOrder: 99999,
|
||||
lastTrailingChapterSourceOrder: -1,
|
||||
isLeadingChapterPreloadMode: true,
|
||||
isTrailingChapterPreloadMode: true,
|
||||
scrollIntoView: false,
|
||||
resumeMode: undefined,
|
||||
},
|
||||
},
|
||||
} satisfies {
|
||||
chapters: Omit<ReaderStateChapters, 'setReaderStateChapters'>;
|
||||
};
|
||||
@@ -18,9 +18,7 @@ import {
|
||||
createReaderAutoScrollStoreSlice,
|
||||
ReaderAutoScrollStoreSlice,
|
||||
} from '@/features/reader/auto-scroll/ReaderAutoScrollStore.ts';
|
||||
import { IReaderSettingsWithDefaultFlag, ReaderStateChapters } from '@/features/reader/Reader.types.ts';
|
||||
import { ImmerStateCreator } from '@/lib/zustand/Zustand.types.ts';
|
||||
import { READER_DEFAULT_CHAPTERS_STATE } from '@/features/reader/stores/ReaderStore.constants.ts';
|
||||
import { IReaderSettingsWithDefaultFlag } from '@/features/reader/Reader.types.ts';
|
||||
import { DEFAULT_READER_SETTINGS_WITH_DEFAULT_FLAG } from '@/features/reader/settings/ReaderSettingsMetadata.ts';
|
||||
import {
|
||||
createReaderProgressBarStoreSlice,
|
||||
@@ -31,12 +29,10 @@ import {
|
||||
ReaderTapZoneStoreSlice,
|
||||
} from '@/features/reader/tap-zones/ReaderTapZoneStore.tsx';
|
||||
import { createReaderPagesStoreSlice, ReaderPagesStoreSlice } from '@/features/reader/stores/ReaderPagesStore.ts';
|
||||
|
||||
interface ReaderChaptersStoreSlice {
|
||||
chapters: ReaderStateChapters & {
|
||||
reset: () => void;
|
||||
};
|
||||
}
|
||||
import {
|
||||
createReaderChaptersStoreSlice,
|
||||
ReaderChaptersStoreSlice,
|
||||
} from '@/features/reader/stores/ReaderChaptersStore.ts';
|
||||
|
||||
interface ReaderStore
|
||||
extends ReaderOverlayStoreSlice,
|
||||
@@ -67,30 +63,6 @@ const DEFAULT_STATE = {
|
||||
},
|
||||
} satisfies Pick<ReaderStore, 'manga'> & { scrollbar: Pick<ReaderStore['scrollbar'], 'xSize' | 'ySize'> };
|
||||
|
||||
const createReaderChaptersStoreSlice = <T extends ReaderChaptersStoreSlice>(
|
||||
...[set, get]: Parameters<ImmerStateCreator<T>>
|
||||
): ReaderChaptersStoreSlice => ({
|
||||
chapters: {
|
||||
...READER_DEFAULT_CHAPTERS_STATE.chapters,
|
||||
reset: () => set(() => ({ chapters: { ...get().chapters, ...READER_DEFAULT_CHAPTERS_STATE.chapters } })),
|
||||
setReaderStateChapters: (state) =>
|
||||
set((draft) => {
|
||||
if (typeof state === 'function') {
|
||||
draft.chapters = {
|
||||
...get().chapters,
|
||||
...state(get().chapters),
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
draft.chapters = {
|
||||
...get().chapters,
|
||||
...state,
|
||||
};
|
||||
}),
|
||||
},
|
||||
});
|
||||
|
||||
export const useReaderStore = create<ReaderStore>()(
|
||||
immer((set, get, store) => ({
|
||||
...DEFAULT_STATE,
|
||||
|
||||
@@ -43,7 +43,8 @@ import { LoadingPlaceholder } from '@/base/components/feedback/LoadingPlaceholde
|
||||
import { ReaderInfiniteScrollUpdateChapter } from '@/features/reader/infinite-scroll/ReaderInfiniteScrollUpdateChapter.tsx';
|
||||
import { useResizeObserver } from '@/base/hooks/useResizeObserver.tsx';
|
||||
import { ChapterIdInfo } from '@/features/chapter/Chapter.types.ts';
|
||||
import { READER_DEFAULT_PAGES_STATE } from '@/features/reader/stores/ReaderStore.constants.ts';
|
||||
|
||||
import { READER_DEFAULT_PAGES_STATE } from '@/features/reader/stores/ReaderPagesStore.ts';
|
||||
|
||||
const BaseReaderChapterViewer = ({
|
||||
currentPageIndex,
|
||||
|
||||
Reference in New Issue
Block a user