Simplify chapter util types

This commit is contained in:
schroda
2025-05-24 21:27:28 +02:00
parent 61471ae214
commit ecca993c54
2 changed files with 17 additions and 13 deletions

View File

@@ -34,16 +34,16 @@ export type ChapterIdInfo = Pick<ChapterType, 'id'>;
export type ChapterMangaInfo = Pick<ChapterType, 'mangaId'>; export type ChapterMangaInfo = Pick<ChapterType, 'mangaId'>;
export type ChapterDownloadInfo = ChapterIdInfo & Pick<ChapterType, 'isDownloaded'>; export type ChapterDownloadInfo = Pick<ChapterType, 'isDownloaded'>;
export type ChapterBookmarkInfo = ChapterIdInfo & Pick<ChapterType, 'isBookmarked'>; export type ChapterBookmarkInfo = Pick<ChapterType, 'isBookmarked'>;
export type ChapterReadInfo = ChapterIdInfo & Pick<ChapterType, 'isRead'>; export type ChapterReadInfo = Pick<ChapterType, 'isRead'>;
export type ChapterNumberInfo = ChapterIdInfo & Pick<ChapterType, 'chapterNumber'>; export type ChapterNumberInfo = Pick<ChapterType, 'chapterNumber'>;
export type ChapterSourceOrderInfo = ChapterIdInfo & Pick<ChapterType, 'sourceOrder'>; export type ChapterSourceOrderInfo = Pick<ChapterType, 'sourceOrder'>;
export type ChapterScanlatorInfo = ChapterIdInfo & Pick<ChapterType, 'scanlator'>; export type ChapterScanlatorInfo = Pick<ChapterType, 'scanlator'>;
export type ChapterRealUrlInfo = Pick<ChapterType, 'realUrl'>; export type ChapterRealUrlInfo = Pick<ChapterType, 'realUrl'>;

View File

@@ -31,6 +31,7 @@ import {
ChapterAction, ChapterAction,
ChapterBookmarkInfo, ChapterBookmarkInfo,
ChapterDownloadInfo, ChapterDownloadInfo,
ChapterIdInfo,
ChapterMangaInfo, ChapterMangaInfo,
ChapterNumberInfo, ChapterNumberInfo,
ChapterReadInfo, ChapterReadInfo,
@@ -118,12 +119,12 @@ export class Chapters {
return chapters.filter(Chapters.isDownloaded); return chapters.filter(Chapters.isDownloaded);
} }
static isDownloadable<Chapter extends ChapterDownloadInfo>(chapter: Chapter): boolean { static isDownloadable<Chapter extends ChapterIdInfo & ChapterDownloadInfo>(chapter: Chapter): boolean {
const downloadStatus = Chapters.getDownloadStatusFromCache(chapter.id); const downloadStatus = Chapters.getDownloadStatusFromCache(chapter.id);
return !Chapters.isDownloaded(chapter) && (!downloadStatus || downloadStatus.state === DownloadState.Error); return !Chapters.isDownloaded(chapter) && (!downloadStatus || downloadStatus.state === DownloadState.Error);
} }
static getDownloadable<Chapter extends ChapterDownloadInfo>(chapters: Chapter[]): Chapter[] { static getDownloadable<Chapter extends ChapterIdInfo & ChapterDownloadInfo>(chapters: Chapter[]): Chapter[] {
return chapters.filter(this.isDownloadable); return chapters.filter(this.isDownloadable);
} }
@@ -199,7 +200,7 @@ export class Chapters {
} }
static async markAsRead( static async markAsRead(
chapters: (ChapterDownloadInfo & ChapterBookmarkInfo)[], chapters: (ChapterIdInfo & ChapterDownloadInfo & ChapterBookmarkInfo)[],
wasManuallyMarkedAsRead: boolean = false, wasManuallyMarkedAsRead: boolean = false,
trackProgressMangaId?: MangaIdInfo['id'], trackProgressMangaId?: MangaIdInfo['id'],
): Promise<void> { ): Promise<void> {
@@ -276,7 +277,7 @@ export class Chapters {
? { ? {
wasManuallyMarkedAsRead: boolean; wasManuallyMarkedAsRead: boolean;
trackProgressMangaId?: MangaIdInfo['id']; trackProgressMangaId?: MangaIdInfo['id'];
chapters: (ChapterDownloadInfo & ChapterBookmarkInfo & ChapterReadInfo)[]; chapters: (ChapterIdInfo & ChapterDownloadInfo & ChapterBookmarkInfo & ChapterReadInfo)[];
} }
: { : {
wasManuallyMarkedAsRead?: never; wasManuallyMarkedAsRead?: never;
@@ -316,7 +317,10 @@ export class Chapters {
.flat(); .flat();
} }
static removeDuplicates<T extends ChapterScanlatorInfo & ChapterNumberInfo>(currentChapter: T, chapters: T[]): T[] { static removeDuplicates<T extends ChapterIdInfo & ChapterScanlatorInfo & ChapterNumberInfo>(
currentChapter: T,
chapters: T[],
): T[] {
const chapterNumberToChapters = Object.groupBy(chapters, ({ chapterNumber }) => chapterNumber); const chapterNumberToChapters = Object.groupBy(chapters, ({ chapterNumber }) => chapterNumber);
const uniqueChapters = Object.values(chapterNumberToChapters).map( const uniqueChapters = Object.values(chapterNumberToChapters).map(
@@ -333,7 +337,7 @@ export class Chapters {
.filter((chapter): chapter is T => !!chapter); .filter((chapter): chapter is T => !!chapter);
} }
static getNextChapter<Chapter extends ChapterScanlatorInfo & ChapterNumberInfo & ChapterReadInfo>( static getNextChapter<Chapter extends ChapterIdInfo & ChapterScanlatorInfo & ChapterNumberInfo & ChapterReadInfo>(
currentChapter: Chapter, currentChapter: Chapter,
chapters: Chapter[], chapters: Chapter[],
{ {
@@ -350,7 +354,7 @@ export class Chapters {
return nextChapters.slice(sliceStartIndex, sliceEndIndex)[0]; return nextChapters.slice(sliceStartIndex, sliceEndIndex)[0];
} }
static getNextChapters<Chapter extends ChapterScanlatorInfo & ChapterNumberInfo & ChapterReadInfo>( static getNextChapters<Chapter extends ChapterIdInfo & ChapterScanlatorInfo & ChapterNumberInfo & ChapterReadInfo>(
fromChapter: Chapter, fromChapter: Chapter,
chapters: Chapter[], chapters: Chapter[],
{ {