Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / applications / calendar / src / app / containers / setup / CalendarSetupContainer.tsx
blob63db2fea4bf77cffef909506958c52af2b4161c2
1 import { useEffect, useState } from 'react';
3 import { useGetAddressKeys } from '@proton/account/addressKeys/hooks';
4 import { useGetAddresses } from '@proton/account/addresses/hooks';
5 import { useGetCalendarUserSettings } from '@proton/calendar/calendarUserSettings/hooks';
6 import { useGetCalendars } from '@proton/calendar/calendars/hooks';
7 import { useGetHolidaysDirectory } from '@proton/calendar/holidaysDirectory/hooks';
8 import { LoaderPage, StandardLoadErrorPage, useApi, useEventManager } from '@proton/components';
9 import { CacheType } from '@proton/redux-utilities';
10 import { getSilentApi } from '@proton/shared/lib/api/helpers/customConfig';
11 import setupCalendarHelper from '@proton/shared/lib/calendar/crypto/keys/setupCalendarHelper';
12 import { setupCalendarKeys } from '@proton/shared/lib/calendar/crypto/keys/setupCalendarKeys';
13 import setupHolidaysCalendarHelper from '@proton/shared/lib/calendar/crypto/keys/setupHolidaysCalendarHelper';
14 import { getSuggestedHolidaysCalendar } from '@proton/shared/lib/calendar/holidaysCalendar/holidaysCalendar';
15 import { getRandomAccentColor } from '@proton/shared/lib/colors';
16 import { getTimezone } from '@proton/shared/lib/date/timezone';
17 import { traceError } from '@proton/shared/lib/helpers/sentry';
18 import { languageCode } from '@proton/shared/lib/i18n';
19 import { getBrowserLanguageTags } from '@proton/shared/lib/i18n/helper';
20 import type { VisualCalendar } from '@proton/shared/lib/interfaces/calendar';
21 import noop from '@proton/utils/noop';
23 interface Props {
24     hasCalendarToGenerate?: boolean;
25     hasHolidaysCalendarToGenerate?: boolean;
26     calendars?: VisualCalendar[];
27     onDone: () => void;
30 const CalendarSetupContainer = ({ hasCalendarToGenerate, hasHolidaysCalendarToGenerate, calendars, onDone }: Props) => {
31     const { call } = useEventManager();
32     const getAddresses = useGetAddresses();
33     const getAddressKeys = useGetAddressKeys();
34     const getHolidaysDirectory = useGetHolidaysDirectory();
35     const getCalendars = useGetCalendars();
36     const getCalendarUserSettings = useGetCalendarUserSettings();
38     const normalApi = useApi();
39     const silentApi = getSilentApi(normalApi);
41     const [error, setError] = useState();
43     useEffect(() => {
44         const run = async () => {
45             const addresses = await getAddresses();
47             if (calendars) {
48                 await setupCalendarKeys({
49                     api: silentApi,
50                     calendars,
51                     getAddressKeys,
52                 });
53             } else {
54                 const promises = [];
55                 if (hasCalendarToGenerate) {
56                     // we create a personal calendar
57                     promises.push(
58                         setupCalendarHelper({
59                             api: silentApi,
60                             addresses,
61                             getAddressKeys,
62                         })
63                     );
64                 }
65                 if (hasHolidaysCalendarToGenerate) {
66                     // we create a public holidays calendar. If we fail, we do it silently
67                     try {
68                         const directory = await getHolidaysDirectory();
69                         const visibleDirectory = directory.filter(({ Hidden }) => !Hidden);
70                         const languageTags = getBrowserLanguageTags();
71                         const holidaysCalendar = getSuggestedHolidaysCalendar(
72                             visibleDirectory,
73                             getTimezone(),
74                             languageCode,
75                             languageTags
76                         );
77                         if (!holidaysCalendar) {
78                             throw new Error('Skip creating holidays calendar');
79                         }
80                         promises.push(
81                             setupHolidaysCalendarHelper({
82                                 holidaysCalendar,
83                                 color: getRandomAccentColor(),
84                                 notifications: [],
85                                 addresses,
86                                 getAddressKeys,
87                                 api: silentApi,
88                             }).catch(noop)
89                         );
90                     } catch (e) {
91                         noop();
92                     }
93                 }
94                 await Promise.all(promises);
95             }
97             await call();
98             await Promise.all([
99                 getCalendars({ cache: CacheType.None }),
100                 getCalendarUserSettings({ cache: CacheType.None }),
101             ]);
102         };
103         run()
104             .then(() => {
105                 onDone();
106             })
107             .catch((e) => {
108                 setError(e);
109                 traceError(e);
110             });
111     }, []);
113     if (error) {
114         return <StandardLoadErrorPage />;
115     }
117     return <LoaderPage />;
120 export default CalendarSetupContainer;