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';
24 hasCalendarToGenerate?: boolean;
25 hasHolidaysCalendarToGenerate?: boolean;
26 calendars?: VisualCalendar[];
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();
44 const run = async () => {
45 const addresses = await getAddresses();
48 await setupCalendarKeys({
55 if (hasCalendarToGenerate) {
56 // we create a personal calendar
65 if (hasHolidaysCalendarToGenerate) {
66 // we create a public holidays calendar. If we fail, we do it silently
68 const directory = await getHolidaysDirectory();
69 const visibleDirectory = directory.filter(({ Hidden }) => !Hidden);
70 const languageTags = getBrowserLanguageTags();
71 const holidaysCalendar = getSuggestedHolidaysCalendar(
77 if (!holidaysCalendar) {
78 throw new Error('Skip creating holidays calendar');
81 setupHolidaysCalendarHelper({
83 color: getRandomAccentColor(),
94 await Promise.all(promises);
99 getCalendars({ cache: CacheType.None }),
100 getCalendarUserSettings({ cache: CacheType.None }),
114 return <StandardLoadErrorPage />;
117 return <LoaderPage />;
120 export default CalendarSetupContainer;