Split up chapter list options type
This commit is contained in:
@@ -11,14 +11,22 @@ import type { ChapterReaderFieldsFragment, DownloadStatusFieldsFragment } from '
|
|||||||
|
|
||||||
export type ChapterSortMode = 'fetchedAt' | 'source' | 'chapterNumber' | 'uploadedAt';
|
export type ChapterSortMode = 'fetchedAt' | 'source' | 'chapterNumber' | 'uploadedAt';
|
||||||
|
|
||||||
export interface ChapterListOptions {
|
export interface ChapterListFilterOptions {
|
||||||
unread: NullAndUndefined<boolean>;
|
unread: NullAndUndefined<boolean>;
|
||||||
downloaded: NullAndUndefined<boolean>;
|
downloaded: NullAndUndefined<boolean>;
|
||||||
bookmarked: NullAndUndefined<boolean>;
|
bookmarked: NullAndUndefined<boolean>;
|
||||||
|
excludedScanlators: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ChapterListSortOptions {
|
||||||
reverse: boolean;
|
reverse: boolean;
|
||||||
sortBy: ChapterSortMode;
|
sortBy: ChapterSortMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ChapterListFilterSortOptions extends ChapterListFilterOptions, ChapterListSortOptions {}
|
||||||
|
|
||||||
|
export interface ChapterListOptions extends ChapterListFilterSortOptions {
|
||||||
showChapterNumber: boolean;
|
showChapterNumber: boolean;
|
||||||
excludedScanlators: string[];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TChapterReader = ChapterReaderFieldsFragment;
|
export type TChapterReader = ChapterReaderFieldsFragment;
|
||||||
|
|||||||
@@ -11,7 +11,10 @@ import type { ChapterType } from '@/lib/graphql/generated/graphql-base.types.ts'
|
|||||||
import type {
|
import type {
|
||||||
ChapterBookmarkInfo,
|
ChapterBookmarkInfo,
|
||||||
ChapterDownloadInfo,
|
ChapterDownloadInfo,
|
||||||
|
ChapterListFilterOptions,
|
||||||
|
ChapterListFilterSortOptions,
|
||||||
ChapterListOptions,
|
ChapterListOptions,
|
||||||
|
ChapterListSortOptions,
|
||||||
ChapterNumberInfo,
|
ChapterNumberInfo,
|
||||||
ChapterReadInfo,
|
ChapterReadInfo,
|
||||||
ChapterScanlatorInfo,
|
ChapterScanlatorInfo,
|
||||||
@@ -63,10 +66,7 @@ function scanlatorFilter(excludedScanlators: string[], { scanlator }: ChapterSca
|
|||||||
}
|
}
|
||||||
|
|
||||||
type TChapterSort = ChapterSourceOrderInfo & ChapterNumberInfo & Pick<ChapterType, 'fetchedAt' | 'uploadDate'>;
|
type TChapterSort = ChapterSourceOrderInfo & ChapterNumberInfo & Pick<ChapterType, 'fetchedAt' | 'uploadDate'>;
|
||||||
const sortChapters = <T extends TChapterSort>(
|
const sortChapters = <T extends TChapterSort>(chapters: T[], { sortBy, reverse }: ChapterListSortOptions): T[] => {
|
||||||
chapters: T[],
|
|
||||||
{ sortBy, reverse }: Pick<ChapterListOptions, 'sortBy' | 'reverse'>,
|
|
||||||
): T[] => {
|
|
||||||
const sortedChapters: T[] = [...chapters];
|
const sortedChapters: T[] = [...chapters];
|
||||||
|
|
||||||
switch (sortBy) {
|
switch (sortBy) {
|
||||||
@@ -96,7 +96,7 @@ const sortChapters = <T extends TChapterSort>(
|
|||||||
type TChapterFilter = ChapterReadInfo & ChapterDownloadInfo & ChapterBookmarkInfo & ChapterScanlatorInfo;
|
type TChapterFilter = ChapterReadInfo & ChapterDownloadInfo & ChapterBookmarkInfo & ChapterScanlatorInfo;
|
||||||
export function filterChapters<Chapters extends TChapterFilter>(
|
export function filterChapters<Chapters extends TChapterFilter>(
|
||||||
chapters: Chapters[],
|
chapters: Chapters[],
|
||||||
options: ChapterListOptions,
|
options: ChapterListFilterOptions,
|
||||||
): Chapters[] {
|
): Chapters[] {
|
||||||
return chapters.filter(
|
return chapters.filter(
|
||||||
(chp) =>
|
(chp) =>
|
||||||
@@ -109,14 +109,14 @@ export function filterChapters<Chapters extends TChapterFilter>(
|
|||||||
|
|
||||||
export function filterAndSortChapters<Chapters extends TChapterSort & TChapterFilter>(
|
export function filterAndSortChapters<Chapters extends TChapterSort & TChapterFilter>(
|
||||||
chapters: Chapters[],
|
chapters: Chapters[],
|
||||||
options: ChapterListOptions,
|
options: ChapterListFilterSortOptions,
|
||||||
): Chapters[] {
|
): Chapters[] {
|
||||||
const filtered = filterChapters(chapters, options);
|
const filtered = filterChapters(chapters, options);
|
||||||
|
|
||||||
return sortChapters(filtered, options);
|
return sortChapters(filtered, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const isFilterActive = (options: ChapterListOptions) => {
|
export const isFilterActive = (options: ChapterListFilterOptions) => {
|
||||||
const { unread, downloaded, bookmarked, excludedScanlators } = options;
|
const { unread, downloaded, bookmarked, excludedScanlators } = options;
|
||||||
return unread != null || downloaded != null || bookmarked != null || !!excludedScanlators.length;
|
return unread != null || downloaded != null || bookmarked != null || !!excludedScanlators.length;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import type {
|
|||||||
ReaderStateChapters,
|
ReaderStateChapters,
|
||||||
} from '@/features/reader/Reader.types.ts';
|
} from '@/features/reader/Reader.types.ts';
|
||||||
import { filterChapters } from '@/features/chapter/utils/ChapterList.util.tsx';
|
import { filterChapters } from '@/features/chapter/utils/ChapterList.util.tsx';
|
||||||
import type { ChapterListOptions } from '@/features/chapter/Chapter.types.ts';
|
import type { ChapterListFilterOptions } 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 { getReaderChaptersStore } from '@/features/reader/stores/ReaderStore.ts';
|
import { getReaderChaptersStore } from '@/features/reader/stores/ReaderStore.ts';
|
||||||
@@ -33,7 +33,7 @@ export const useReaderSetChaptersState = (
|
|||||||
chapterForDuplicatesHandling: ReaderStateChapters['chapterForDuplicatesHandling'],
|
chapterForDuplicatesHandling: ReaderStateChapters['chapterForDuplicatesHandling'],
|
||||||
shouldSkipDupChapters: IReaderSettings['shouldSkipDupChapters'],
|
shouldSkipDupChapters: IReaderSettings['shouldSkipDupChapters'],
|
||||||
shouldSkipFilteredChapters: IReaderSettings['shouldSkipFilteredChapters'],
|
shouldSkipFilteredChapters: IReaderSettings['shouldSkipFilteredChapters'],
|
||||||
chapterListOptions: ChapterListOptions,
|
chapterListOptions: ChapterListFilterOptions,
|
||||||
) => {
|
) => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const locationState = useLocation<ReaderOpenChapterLocationState>().state;
|
const locationState = useLocation<ReaderOpenChapterLocationState>().state;
|
||||||
|
|||||||
Reference in New Issue
Block a user