Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / components / hooks / useGetCalendarInfo.ts
blobbb15f67d621a1e793ca22efea921a645bdef8615
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 { useGetDecryptedPassphraseAndCalendarKeys } from '@proton/calendar/calendarBootstrap/keys';
7 import { getMemberAndAddress } from '@proton/shared/lib/calendar/members';
8 import type { GetCalendarInfo } from '@proton/shared/lib/interfaces/hooks/GetCalendarInfo';
10 export const useGetCalendarInfo = (): GetCalendarInfo => {
11     const getCalendarBootstrap = useGetCalendarBootstrap();
12     const getAddresses = useGetAddresses();
13     const getDecryptedPassphraseAndCalendarKeys = useGetDecryptedPassphraseAndCalendarKeys();
14     const getAddressKeys = useGetAddressKeys();
16     return useCallback(
17         async (calendarID: string) => {
18             const [{ Members, CalendarSettings: calendarSettings }, Addresses] = await Promise.all([
19                 getCalendarBootstrap(calendarID),
20                 getAddresses(),
21             ]);
23             const [{ ID: memberID }, { ID: addressID }] = getMemberAndAddress(Addresses, Members);
25             const [{ decryptedCalendarKeys, decryptedPassphrase, passphraseID }, addressKeys] = await Promise.all([
26                 getDecryptedPassphraseAndCalendarKeys(calendarID),
27                 getAddressKeys(addressID),
28             ]);
30             return {
31                 memberID,
32                 addressID,
33                 addressKeys,
34                 calendarKeys: decryptedCalendarKeys,
35                 calendarSettings,
36                 passphrase: decryptedPassphrase,
37                 passphraseID,
38             };
39         },
40         [getCalendarBootstrap, getAddresses, getDecryptedPassphraseAndCalendarKeys, getAddressKeys]
41     );
44 export default useGetCalendarInfo;