Add "groupByDate" to "Chapters"

This commit is contained in:
schroda
2025-03-10 00:26:31 +01:00
parent 86525ec2a2
commit e9b42db10f
4 changed files with 43 additions and 34 deletions

View File

@@ -26,6 +26,7 @@ import { ReaderOpenChapterLocationState, ReaderResumeMode } from '@/modules/read
import { AppRoutes } from '@/modules/core/AppRoute.constants.ts';
import { getErrorMessage } from '@/lib/HelperFunctions.ts';
import { DOWNLOAD_TYPE_FIELDS } from '@/lib/graphql/fragments/DownloadFragments.ts';
import { epochToDate, getDateString } from '@/util/DateHelper.ts';
export type ChapterAction = 'download' | 'delete' | 'bookmark' | 'unbookmark' | 'mark_as_read' | 'mark_as_unread';
@@ -455,4 +456,19 @@ export class Chapters {
updateInitialChapter,
};
}
/**
* Returns the chapters grouped by the passed key representing a timestamp.
*
* The timestamp gets mapped to a string via {@link getDateString}
*/
static groupByDate<
T extends Pick<ChapterType, 'lastReadAt'> | Pick<ChapterType, 'fetchedAt'> | Pick<ChapterType, 'uploadDate'>,
K extends keyof ExtractCommon<OmitNotMatching<ChapterType, 'lastReadAt' | 'fetchedAt' | 'uploadDate'>, T>,
>(chapters: T[], key: K): Record<string, T[]> {
return Object.groupBy(chapters, (chapter) => getDateString(epochToDate(Number(chapter[key])))) as Record<
string,
T[]
>;
}
}