Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / components / hooks / useDeviceRecovery.ts
blob3522fbe610baa102bd278a102787e695006c307d
1 import { useEffect } from 'react';
3 import { useGetAddresses } from '@proton/account/addresses/hooks';
4 import { useGetUser } from '@proton/account/user/hooks';
5 import { useGetUserKeys, useUserKeys } from '@proton/account/userKeys/hooks';
6 import { useGetUserSettings, useUserSettings } from '@proton/account/userSettings/hooks';
7 import useIsRecoveryFileAvailable from '@proton/components/hooks/recoveryFile/useIsRecoveryFileAvailable';
8 import useAuthentication from '@proton/components/hooks/useAuthentication';
9 import useEventManager from '@proton/components/hooks/useEventManager';
10 import { getIsDeviceRecoveryEnabled, syncDeviceRecovery } from '@proton/shared/lib/recoveryFile/deviceRecovery';
11 import noop from '@proton/utils/noop';
13 import useApi from './useApi';
14 import useConfig from './useConfig';
16 export const useIsDeviceRecoveryEnabled = () => {
17     const [userSettings] = useUserSettings();
18     const authentication = useAuthentication();
19     return getIsDeviceRecoveryEnabled(userSettings, authentication);
22 export const useIsDeviceRecoveryAvailable = () => {
23     const [recoveryFileAvailable, loading] = useIsRecoveryFileAvailable();
24     return [recoveryFileAvailable, loading];
27 export const useDeviceRecovery = () => {
28     const [userKeys] = useUserKeys();
29     const getUserKeys = useGetUserKeys();
30     const getUser = useGetUser();
31     const getAddresses = useGetAddresses();
32     const getUserSettings = useGetUserSettings();
33     const authentication = useAuthentication();
34     const { call } = useEventManager();
35     const api = useApi();
36     const { APP_NAME } = useConfig();
38     useEffect(() => {
39         if (!userKeys?.length) {
40             return;
41         }
42         const abortController = new AbortController();
43         const run = async () => {
44             const [user, userKeys, addresses, userSettings] = await Promise.all([
45                 getUser(),
46                 getUserKeys(),
47                 getAddresses(),
48                 getUserSettings(),
49             ]);
50             if (!userKeys) {
51                 return;
52             }
53             const result = await syncDeviceRecovery({
54                 api,
55                 user,
56                 userKeys,
57                 addresses,
58                 userSettings,
59                 appName: APP_NAME,
60                 signal: abortController.signal,
61                 authentication,
62             });
63             if (result) {
64                 await call();
65             }
66         };
67         run().catch(noop);
68         return () => {
69             abortController.abort();
70         };
71         // In lieu of better logic, this is trying to catch user keys that get reactivated.
72     }, [userKeys?.length]);