1 import { c } from 'ttag';
3 import type { useGetAddressKeys } from '@proton/components/hooks';
5 import { setupCalendar } from '../../../api/calendars';
6 import type { Api } from '../../../interfaces';
7 import type { CalendarSetupResponse, CalendarWithOwnMembers } from '../../../interfaces/calendar';
8 import { getPrimaryKey } from '../../../keys';
9 import { generateCalendarKeyPayload } from './calendarKeys';
10 import { isCalendarSetupData } from './helpers';
12 export const setupCalendarKey = async ({
19 getAddressKeys: ReturnType<typeof useGetAddressKeys>;
23 const { privateKey, publicKey } = getPrimaryKey(await getAddressKeys(addressID)) || {};
25 if (!privateKey || !publicKey) {
26 throw new Error(c('Error').t`Primary address key is not decrypted`);
29 const calendarKeyPayload = await generateCalendarKeyPayload({
35 if (!isCalendarSetupData(calendarKeyPayload)) {
36 throw new Error(c('Error').t`Missing key packet`);
39 return api<CalendarSetupResponse>(setupCalendar(calendarID, calendarKeyPayload));
42 export const setupCalendarKeys = async ({
48 calendars: CalendarWithOwnMembers[];
49 getAddressKeys: ReturnType<typeof useGetAddressKeys>;
52 calendars.map(async ({ ID: calendarID, Members }) => {
53 const addressID = Members[0]?.AddressID;
57 return setupCalendarKey({ calendarID, api, getAddressKeys, addressID });