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();
36 const { APP_NAME } = useConfig();
39 if (!userKeys?.length) {
42 const abortController = new AbortController();
43 const run = async () => {
44 const [user, userKeys, addresses, userSettings] = await Promise.all([
53 const result = await syncDeviceRecovery({
60 signal: abortController.signal,
69 abortController.abort();
71 // In lieu of better logic, this is trying to catch user keys that get reactivated.
72 }, [userKeys?.length]);