Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / components / hooks / useGetCalendarEventRaw.ts
blob4123947462ef62fa2a97abba67031bf96667e64e
1 import { useCallback } from 'react';
3 import { useGetAddressKeys } from '@proton/account/addressKeys/hooks';
4 import { useGetAddresses } from '@proton/account/addresses/hooks';
5 import { useGetCalendarBootstrap } from '@proton/calendar/calendarBootstrap/hooks';
6 import { useGetCalendarKeys } from '@proton/calendar/calendarBootstrap/keys';
7 import { getIsAutoAddedInvite } from '@proton/shared/lib/calendar/apiModels';
8 import { getAuthorPublicKeysMap, withNormalizedAuthors } from '@proton/shared/lib/calendar/author';
9 import { getCalendarEventDecryptionKeys } from '@proton/shared/lib/calendar/crypto/keys/helpers';
10 import { readCalendarEvent, readSessionKeys } from '@proton/shared/lib/calendar/deserialize';
11 import type { SimpleMap } from '@proton/shared/lib/interfaces';
12 import type { CalendarEvent } from '@proton/shared/lib/interfaces/calendar';
13 import type { ContactEmail } from '@proton/shared/lib/interfaces/contacts';
14 import type { GetCalendarEventRaw } from '@proton/shared/lib/interfaces/hooks/GetCalendarEventRaw';
16 import useGetVerificationPreferences from './useGetVerificationPreferences';
18 const useGetCalendarEventRaw = (contactEmailsMap: SimpleMap<ContactEmail>): GetCalendarEventRaw => {
19     const getCalendarKeys = useGetCalendarKeys();
20     const getCalendarBootstrap = useGetCalendarBootstrap();
21     const getAddresses = useGetAddresses();
22     const getAddressKeys = useGetAddressKeys();
23     const getVerificationPreferences = useGetVerificationPreferences();
25     return useCallback(
26         async (Event: CalendarEvent) => {
27             const {
28                 CalendarID,
29                 ID,
30                 SharedEvents,
31                 CalendarEvents,
32                 AttendeesEvents,
33                 Attendees,
34                 Notifications,
35                 Color,
36                 FullDay,
37             } = Event;
38             const encryptingAddressID = getIsAutoAddedInvite(Event) ? Event.AddressID : undefined;
39             const addresses = await getAddresses();
41             const [privateKeys, publicKeysMap, { CalendarSettings: calendarSettings }] = await Promise.all([
42                 getCalendarEventDecryptionKeys({ calendarEvent: Event, getAddressKeys, getCalendarKeys }),
43                 getAuthorPublicKeysMap({
44                     event: Event,
45                     getVerificationPreferences,
46                     contactEmailsMap,
47                 }),
48                 getCalendarBootstrap(CalendarID),
49             ]);
50             const [sharedSessionKey, calendarSessionKey] = await readSessionKeys({
51                 calendarEvent: Event,
52                 privateKeys,
53             });
54             return readCalendarEvent({
55                 event: {
56                     SharedEvents: withNormalizedAuthors(SharedEvents),
57                     CalendarEvents: withNormalizedAuthors(CalendarEvents),
58                     AttendeesEvents: withNormalizedAuthors(AttendeesEvents),
59                     Attendees,
60                     Notifications,
61                     FullDay,
62                     CalendarID,
63                     ID,
64                     Color,
65                 },
66                 publicKeysMap,
67                 sharedSessionKey,
68                 calendarSessionKey,
69                 calendarSettings,
70                 addresses,
71                 encryptingAddressID,
72             });
73         },
74         [getAddresses, getAddressKeys, getCalendarKeys, contactEmailsMap]
75     );
78 export default useGetCalendarEventRaw;