Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / applications / drive / src / app / components / revisions / getCategorizedRevisions.ts
blobe2cdb9a73504f5c5412db77d854186f70c8e2080
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);
18         switch (true) {
19             case isToday(dateStart):
20                 const todayRevisions = result.get('today');
21                 result.set(
22                     'today',
23                     todayRevisions
24                         ? { title: todayRevisions.title, list: [...todayRevisions.list, revision] }
25                         : { title: c('Info').t`Today`, list: [revision] }
26                 );
27                 break;
28             case isYesterday(dateStart):
29                 const yesterdayRevisions = result.get('yesterday');
30                 result.set(
31                     'yesterday',
32                     yesterdayRevisions
33                         ? { title: yesterdayRevisions.title, list: [...yesterdayRevisions.list, revision] }
34                         : { title: c('Info').t`Yesterday`, list: [revision] }
35                 );
36                 break;
37             case isThisWeek(dateStart):
38                 const dayOfWeek = dateStart.getDay();
39                 const dayOfWeekRevisions = result.get(`d${dayOfWeek}`);
40                 result.set(
41                     `d${dayOfWeek}`,
42                     dayOfWeekRevisions
43                         ? { title: dayOfWeekRevisions.title, list: [...dayOfWeekRevisions.list, revision] }
44                         : {
45                               title: new Intl.DateTimeFormat(localeCode, { weekday: 'long' }).format(dateStart),
46                               list: [revision],
47                           }
48                 );
49                 break;
50             case isLastWeek(dateStart):
51                 const lastWeekRevisions = result.get('last-week');
52                 result.set(
53                     'last-week',
54                     lastWeekRevisions
55                         ? { title: lastWeekRevisions.title, list: [...lastWeekRevisions.list, revision] }
56                         : { title: c('Info').t`Last week`, list: [revision] }
57                 );
58                 break;
59             case isThisYear(dateStart):
60                 const monthOfYear = dateStart.getMonth();
61                 const monthOfYearRevisions = result.get(`m${monthOfYear}`);
62                 result.set(
63                     `m${monthOfYear}`,
64                     monthOfYearRevisions
65                         ? {
66                               title: monthOfYearRevisions.title,
67                               list: [...monthOfYearRevisions.list, revision],
68                           }
69                         : {
70                               title: new Intl.DateTimeFormat(localeCode, { month: 'long' }).format(dateStart),
71                               list: [revision],
72                           }
73                 );
74                 break;
75             default:
76                 const year = dateStart.getFullYear().toString();
77                 const yearRevisions = result.get(year);
78                 result.set(
79                     year,
80                     yearRevisions
81                         ? { title: yearRevisions.title, list: [...yearRevisions.list, revision] }
82                         : {
83                               title: new Intl.DateTimeFormat(localeCode, { year: 'numeric' }).format(dateStart),
84                               list: [revision],
85                           }
86                 );
87                 break;
88         }
90         return result;
91     }, new Map());