Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / applications / calendar / src / app / helpers / timezoneSuggestion.ts
blobbba3ccba6d319aa61b4200643507391881ec5b3d
1 import { getSHA256String } from '@proton/shared/lib/helpers/crypto';
3 const DAY_IN_MILLISECONDS = 86400000;
4 const LOCALSTORAGE_TZ_KEY = 'tzSuggestion';
6 export const getTimezoneSuggestionKey = async (userID: string) => {
7     return getSHA256String(`${LOCALSTORAGE_TZ_KEY}${userID}`);
8 };
10 export const getLastTimezoneSuggestion = (key: string) => {
11     const defaultDate = new Date(2000, 0, 1);
12     try {
13         const lastSuggestion = parseInt(`${window.localStorage.getItem(key)}`, 10);
14         const date = new Date(lastSuggestion);
15         if (Number.isNaN(+date)) {
16             return defaultDate;
17         }
18         return date;
19     } catch (e: any) {
20         return defaultDate;
21     }
24 export const saveLastTimezoneSuggestion = (key: string) => {
25     try {
26         const timestamp = +new Date();
27         window.localStorage.setItem(key, `${timestamp}`);
28         // eslint-disable-next-line no-empty
29     } catch (e: any) {}
32 export const canAskTimezoneSuggestion = (key: string) => {
33     return Date.now() - +getLastTimezoneSuggestion(key) >= DAY_IN_MILLISECONDS;