Extract "reader chapters store slice" into file

This commit is contained in:
schroda
2025-09-20 17:58:51 +02:00
parent 9f5db5496f
commit ed53aff071
5 changed files with 68 additions and 63 deletions

View File

@@ -21,7 +21,8 @@ import { ChapterListOptions } from '@/features/chapter/Chapter.types.ts';
import { getReaderChapterFromCache } from '@/features/reader/Reader.utils.ts'; import { getReaderChapterFromCache } from '@/features/reader/Reader.utils.ts';
import { DirectionOffset } from '@/base/Base.types.ts'; import { DirectionOffset } from '@/base/Base.types.ts';
import { getReaderStore } from '@/features/reader/stores/ReaderStore.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 = ( export const useReaderSetChaptersState = (
chaptersResponse: ReturnType<typeof requestManager.useGetMangaChapters<GetChaptersReaderQuery>>, chaptersResponse: ReturnType<typeof requestManager.useGetMangaChapters<GetChaptersReaderQuery>>,

View 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,
};
}),
},
});

View File

@@ -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'>;
};

View File

@@ -18,9 +18,7 @@ import {
createReaderAutoScrollStoreSlice, createReaderAutoScrollStoreSlice,
ReaderAutoScrollStoreSlice, ReaderAutoScrollStoreSlice,
} from '@/features/reader/auto-scroll/ReaderAutoScrollStore.ts'; } from '@/features/reader/auto-scroll/ReaderAutoScrollStore.ts';
import { IReaderSettingsWithDefaultFlag, ReaderStateChapters } from '@/features/reader/Reader.types.ts'; import { IReaderSettingsWithDefaultFlag } 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 { DEFAULT_READER_SETTINGS_WITH_DEFAULT_FLAG } from '@/features/reader/settings/ReaderSettingsMetadata.ts'; import { DEFAULT_READER_SETTINGS_WITH_DEFAULT_FLAG } from '@/features/reader/settings/ReaderSettingsMetadata.ts';
import { import {
createReaderProgressBarStoreSlice, createReaderProgressBarStoreSlice,
@@ -31,12 +29,10 @@ import {
ReaderTapZoneStoreSlice, ReaderTapZoneStoreSlice,
} from '@/features/reader/tap-zones/ReaderTapZoneStore.tsx'; } from '@/features/reader/tap-zones/ReaderTapZoneStore.tsx';
import { createReaderPagesStoreSlice, ReaderPagesStoreSlice } from '@/features/reader/stores/ReaderPagesStore.ts'; import { createReaderPagesStoreSlice, ReaderPagesStoreSlice } from '@/features/reader/stores/ReaderPagesStore.ts';
import {
interface ReaderChaptersStoreSlice { createReaderChaptersStoreSlice,
chapters: ReaderStateChapters & { ReaderChaptersStoreSlice,
reset: () => void; } from '@/features/reader/stores/ReaderChaptersStore.ts';
};
}
interface ReaderStore interface ReaderStore
extends ReaderOverlayStoreSlice, extends ReaderOverlayStoreSlice,
@@ -67,30 +63,6 @@ const DEFAULT_STATE = {
}, },
} satisfies Pick<ReaderStore, 'manga'> & { scrollbar: Pick<ReaderStore['scrollbar'], 'xSize' | 'ySize'> }; } 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>()( export const useReaderStore = create<ReaderStore>()(
immer((set, get, store) => ({ immer((set, get, store) => ({
...DEFAULT_STATE, ...DEFAULT_STATE,

View File

@@ -43,7 +43,8 @@ import { LoadingPlaceholder } from '@/base/components/feedback/LoadingPlaceholde
import { ReaderInfiniteScrollUpdateChapter } from '@/features/reader/infinite-scroll/ReaderInfiniteScrollUpdateChapter.tsx'; import { ReaderInfiniteScrollUpdateChapter } from '@/features/reader/infinite-scroll/ReaderInfiniteScrollUpdateChapter.tsx';
import { useResizeObserver } from '@/base/hooks/useResizeObserver.tsx'; import { useResizeObserver } from '@/base/hooks/useResizeObserver.tsx';
import { ChapterIdInfo } from '@/features/chapter/Chapter.types.ts'; 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 = ({ const BaseReaderChapterViewer = ({
currentPageIndex, currentPageIndex,