Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / components / hooks / useCalendarShareInvitationActions.ts
blob9ed7df159545ed118f5d378de9de16f9807f21bc
1 import { useCallback } from 'react';
3 import { c } from 'ttag';
5 import { useGetAddressKeys } from '@proton/account/addressKeys/hooks';
6 import { useGetAddresses } from '@proton/account/addresses/hooks';
7 import useApi from '@proton/components/hooks/useApi';
8 import useEventManager from '@proton/components/hooks/useEventManager';
9 import useGetEncryptionPreferences from '@proton/components/hooks/useGetEncryptionPreferences';
10 import useNotifications from '@proton/components/hooks/useNotifications';
11 import { getSilentApi } from '@proton/shared/lib/api/helpers/customConfig';
12 import {
13     acceptCalendarShareInvitation,
14     rejectCalendarShareInvitation,
15 } from '@proton/shared/lib/calendar/sharing/shareProton/shareProton';
16 import { canonicalizeInternalEmail } from '@proton/shared/lib/helpers/email';
17 import type { CalendarMemberInvitation } from '@proton/shared/lib/interfaces/calendar';
19 const useCalendarShareInvitationActions = () => {
20     const getAddresses = useGetAddresses();
21     const api = useApi();
22     const { call } = useEventManager();
23     const getAddressKeys = useGetAddressKeys();
24     const getEncryptionPreferences = useGetEncryptionPreferences();
25     const { createNotification } = useNotifications();
27     const accept = useCallback(
28         async ({
29             invitation,
30             skipSignatureVerification,
31             onFinish,
32             onError,
33         }: {
34             invitation: CalendarMemberInvitation;
35             skipSignatureVerification?: boolean;
36             onFinish?: () => void;
37             onError: (e: Error) => void;
38         }) => {
39             const {
40                 CalendarID: calendarID,
41                 Email: invitedEmail,
42                 Passphrase: armoredPassphrase,
43                 Calendar: { SenderEmail: senderEmail, Name: calendarName },
44                 Signature: armoredSignature,
45             } = invitation;
46             const addresses = await getAddresses();
47             const canonicalizedInvitedEmail = canonicalizeInternalEmail(invitedEmail);
48             const addressID = addresses.find(
49                 ({ Email }) => canonicalizeInternalEmail(Email) === canonicalizedInvitedEmail
50             )?.ID;
52             if (!addressID) {
53                 const text = 'Own address not found';
54                 createNotification({ type: 'error', text });
55                 throw new Error(text);
56             }
57             try {
58                 const accepted = await acceptCalendarShareInvitation({
59                     addressID,
60                     calendarID,
61                     armoredPassphrase,
62                     armoredSignature,
63                     senderEmail,
64                     getAddressKeys,
65                     getEncryptionPreferences,
66                     skipSignatureVerification,
67                     api: getSilentApi(api),
68                 });
69                 createNotification({
70                     type: 'success',
71                     text: c('Notification in shared calendar modal').t`Joined ${calendarName} (${senderEmail})`,
72                 });
73                 await call();
74                 onFinish?.();
75                 return accepted;
76             } catch (e: any) {
77                 if (!(e instanceof Error)) {
78                     onError(new Error('Unknown error'));
79                 }
80                 onError(e);
81             }
82         },
83         [api, getAddressKeys]
84     );
86     const reject = useCallback(
87         async ({
88             invitation,
89             onFinish,
90             onError,
91         }: {
92             invitation: CalendarMemberInvitation;
93             onFinish?: () => void;
94             onError: (e: Error) => void;
95         }) => {
96             const { CalendarID: calendarID, Email: invitedEmail } = invitation;
97             const canonicalizedInvitedEmail = canonicalizeInternalEmail(invitedEmail);
98             const addresses = await getAddresses();
99             const addressID = addresses.find(
100                 ({ Email }) => canonicalizeInternalEmail(Email) === canonicalizedInvitedEmail
101             )?.ID;
103             if (!addressID) {
104                 const text = 'Own address not found';
105                 createNotification({ type: 'error', text });
106                 throw new Error(text);
107             }
109             if (!addressID) {
110                 const text = 'Own address not found';
111                 createNotification({ type: 'error', text });
112                 throw new Error(text);
113             }
114             try {
115                 await rejectCalendarShareInvitation({
116                     addressID,
117                     calendarID,
118                     api: getSilentApi(api),
119                 });
120                 createNotification({
121                     type: 'success',
122                     text: c('Notification in shared calendar modal').t`Calendar invitation declined`,
123                 });
124                 await call();
125                 onFinish?.();
126             } catch (e: any) {
127                 if (!(e instanceof Error)) {
128                     onError(new Error('Unknown error'));
129                 }
130                 onError(e);
131             }
132         },
133         [api]
134     );
136     return { accept, reject };
139 export default useCalendarShareInvitationActions;