Merge branch 'fix/isloading-photos' into 'main'
[ProtonMail-WebClient.git] / packages / components / containers / desktop / freeTrial / shouldDisplayReminder.ts
blobe02269f3a90cbf219c36f3fa35ac2df971cd0f38
1 import { differenceInDays, startOfDay } from 'date-fns';
3 import {
4     FIRST_REMINDER_DAYS,
5     SECOND_REMINDER_DAYS,
6     THIRD_REMINDER_DAYS,
7 } from '@proton/components/containers/desktop/freeTrial/constants';
8 import type { InboxDesktopFreeTrialReminders } from '@proton/shared/lib/desktop/desktopTypes';
10 export const shouldDisplay = (
11     today: Date,
12     differenceToTest: number,
13     expirationDay: Date,
14     alreadyDisplayed?: boolean
15 ) => {
16     const endDate = new Date(expirationDay);
17     const daysDifference = differenceInDays(endDate, today);
18     return daysDifference === differenceToTest && !alreadyDisplayed;
21 export const shouldDisplayReminder = (trialEndDate: Date, remindFlag: InboxDesktopFreeTrialReminders) => {
22     const startOfToday = startOfDay(new Date());
24     const firstReminder = shouldDisplay(startOfToday, FIRST_REMINDER_DAYS, trialEndDate, remindFlag?.first);
25     const secondReminder = shouldDisplay(startOfToday, SECOND_REMINDER_DAYS, trialEndDate, remindFlag?.second);
26     const thirdReminder = shouldDisplay(startOfToday, THIRD_REMINDER_DAYS, trialEndDate, remindFlag?.third);
28     return firstReminder || secondReminder || thirdReminder;