1 import { fromUnixTime, isThisWeek, isThisYear, isToday, isYesterday } from 'date-fns';
2 import { c } from 'ttag';
4 import { dateLocale } from '@proton/shared/lib/i18n';
5 import isLastWeek from '@proton/utils/isLastWeek';
7 import type { DriveFileRevision } from '../../store';
9 type RevisionCategory = { title: string; list: DriveFileRevision[] };
10 export type CategorizedRevisions = Map<string, RevisionCategory>;
11 export function getCategorizedRevisions(
12 revisions: DriveFileRevision[],
13 localeCode = dateLocale.code
14 ): CategorizedRevisions {
15 return revisions.reduce<CategorizedRevisions>((result, revision) => {
16 const dateStart = fromUnixTime(revision.createTime);
19 case isToday(dateStart):
20 const todayRevisions = result.get('today');
24 ? { title: todayRevisions.title, list: [...todayRevisions.list, revision] }
25 : { title: c('Info').t`Today`, list: [revision] }
28 case isYesterday(dateStart):
29 const yesterdayRevisions = result.get('yesterday');
33 ? { title: yesterdayRevisions.title, list: [...yesterdayRevisions.list, revision] }
34 : { title: c('Info').t`Yesterday`, list: [revision] }
37 case isThisWeek(dateStart):
38 const dayOfWeek = dateStart.getDay();
39 const dayOfWeekRevisions = result.get(`d${dayOfWeek}`);
43 ? { title: dayOfWeekRevisions.title, list: [...dayOfWeekRevisions.list, revision] }
45 title: new Intl.DateTimeFormat(localeCode, { weekday: 'long' }).format(dateStart),
50 case isLastWeek(dateStart):
51 const lastWeekRevisions = result.get('last-week');
55 ? { title: lastWeekRevisions.title, list: [...lastWeekRevisions.list, revision] }
56 : { title: c('Info').t`Last week`, list: [revision] }
59 case isThisYear(dateStart):
60 const monthOfYear = dateStart.getMonth();
61 const monthOfYearRevisions = result.get(`m${monthOfYear}`);
66 title: monthOfYearRevisions.title,
67 list: [...monthOfYearRevisions.list, revision],
70 title: new Intl.DateTimeFormat(localeCode, { month: 'long' }).format(dateStart),
76 const year = dateStart.getFullYear().toString();
77 const yearRevisions = result.get(year);
81 ? { title: yearRevisions.title, list: [...yearRevisions.list, revision] }
83 title: new Intl.DateTimeFormat(localeCode, { year: 'numeric' }).format(dateStart),