Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / components / hooks / mail / useShortDomainAddress.ts
blobaa4e431cf296972706340fe9319a290100fab0bd
1 import { useGetAddresses } from '@proton/account/addresses/hooks';
2 import { useProtonDomains } from '@proton/account/protonDomains/hooks';
3 import { useUser } from '@proton/account/user/hooks';
4 import { useGetUserKeys } from '@proton/account/userKeys/hooks';
5 import useKTVerifier from '@proton/components/containers/keyTransparency/useKTVerifier';
6 import { orderAddress, setupAddress } from '@proton/shared/lib/api/addresses';
7 import { DEFAULT_KEYGEN_TYPE, KEYGEN_CONFIGS } from '@proton/shared/lib/constants';
8 import { type Address, type ApiResponse } from '@proton/shared/lib/interfaces';
9 import { missingKeysSelfProcess } from '@proton/shared/lib/keys';
10 import noop from '@proton/utils/noop';
12 import useApi from '../useApi';
13 import useAuthentication from '../useAuthentication';
14 import useEventManager from '../useEventManager';
16 const useShortDomainAddress = () => {
17     const api = useApi();
18     const [user, loadingUser] = useUser();
19     const shortDomain = `${user.Name}@pm.me`;
20     const [{ premiumDomains }, loadingProtonDomains] = useProtonDomains();
21     const getAddresses = useGetAddresses();
22     const authentication = useAuthentication();
23     const { call } = useEventManager();
24     const getUserKeys = useGetUserKeys();
25     const { keyTransparencyVerify, keyTransparencyCommit } = useKTVerifier(api, async () => user);
27     return {
28         loadingDependencies: loadingProtonDomains || loadingUser,
29         shortDomainAddress: shortDomain,
30         hasShortDomain: (addresses: Address[]) => addresses.some(({ Email }) => Email === shortDomain),
31         createShortDomainAddress: async ({
32             setDefault,
33             addressSignature,
34         }: {
35             /** Set short domain as default address after creation */
36             setDefault: boolean;
37             addressSignature?: string;
38         }) => {
39             const [Domain = ''] = premiumDomains;
40             const addresses = await getAddresses();
42             // Early return if the address already exists
43             if (addresses.some(({ Email }) => Email === shortDomain)) {
44                 return;
45             }
47             // Create address
48             const [{ DisplayName = '', Signature = '' } = {}] = addresses || [];
49             const { Address } = await api<ApiResponse & { Address: Address }>(
50                 setupAddress({
51                     Domain,
52                     DisplayName: DisplayName || '', // DisplayName can be null
53                     Signature: addressSignature ?? Signature ?? '', // Signature can be null
54                 })
55             );
56             const userKeys = await getUserKeys();
57             await missingKeysSelfProcess({
58                 api,
59                 userKeys,
60                 addresses,
61                 addressesToGenerate: [Address],
62                 password: authentication.getPassword(),
63                 keyGenConfig: KEYGEN_CONFIGS[DEFAULT_KEYGEN_TYPE],
64                 onUpdate: noop,
65                 keyTransparencyVerify,
66             });
67             await keyTransparencyCommit(userKeys);
69             if (setDefault) {
70                 // Default address is the first one in the list so we need to reorder the addresses
71                 await api(orderAddress([Address.ID, ...addresses.map(({ ID }) => ID)]));
72             }
74             // Call event manager to ensure all the UI is up to date
75             await call();
77             // Return newly created address
78             return Address;
79         },
80     };
83 export default useShortDomainAddress;