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';
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();
22 const { call } = useEventManager();
23 const getAddressKeys = useGetAddressKeys();
24 const getEncryptionPreferences = useGetEncryptionPreferences();
25 const { createNotification } = useNotifications();
27 const accept = useCallback(
30 skipSignatureVerification,
34 invitation: CalendarMemberInvitation;
35 skipSignatureVerification?: boolean;
36 onFinish?: () => void;
37 onError: (e: Error) => void;
40 CalendarID: calendarID,
42 Passphrase: armoredPassphrase,
43 Calendar: { SenderEmail: senderEmail, Name: calendarName },
44 Signature: armoredSignature,
46 const addresses = await getAddresses();
47 const canonicalizedInvitedEmail = canonicalizeInternalEmail(invitedEmail);
48 const addressID = addresses.find(
49 ({ Email }) => canonicalizeInternalEmail(Email) === canonicalizedInvitedEmail
53 const text = 'Own address not found';
54 createNotification({ type: 'error', text });
55 throw new Error(text);
58 const accepted = await acceptCalendarShareInvitation({
65 getEncryptionPreferences,
66 skipSignatureVerification,
67 api: getSilentApi(api),
71 text: c('Notification in shared calendar modal').t`Joined ${calendarName} (${senderEmail})`,
77 if (!(e instanceof Error)) {
78 onError(new Error('Unknown error'));
86 const reject = useCallback(
92 invitation: CalendarMemberInvitation;
93 onFinish?: () => void;
94 onError: (e: Error) => void;
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
104 const text = 'Own address not found';
105 createNotification({ type: 'error', text });
106 throw new Error(text);
110 const text = 'Own address not found';
111 createNotification({ type: 'error', text });
112 throw new Error(text);
115 await rejectCalendarShareInvitation({
118 api: getSilentApi(api),
122 text: c('Notification in shared calendar modal').t`Calendar invitation declined`,
127 if (!(e instanceof Error)) {
128 onError(new Error('Unknown error'));
136 return { accept, reject };
139 export default useCalendarShareInvitationActions;