Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / components / hooks / useIsSentinelUser.ts
blob6226f74e61755ad0dc12be0b5a254025e15f069a
1 import { useUser } from '@proton/account/user/hooks';
2 import { useUserSettings } from '@proton/account/userSettings/hooks';
3 import { MNEMONIC_STATUS, SETTINGS_PROTON_SENTINEL_STATE } from '@proton/shared/lib/interfaces';
5 import useIsMnemonicAvailable from './useIsMnemonicAvailable';
7 const useIsSentinelUser = () => {
8     const [userSettings, loadingUserSettings] = useUserSettings();
9     const [isMnemonicAvailable, loadingIsMnemonicAvailable] = useIsMnemonicAvailable();
10     const [user] = useUser();
12     const isSentinelUser = userSettings?.HighSecurity?.Value === SETTINGS_PROTON_SENTINEL_STATE.ENABLED;
14     const hasEmail = !!userSettings?.Email?.Value;
15     const hasEmailVerified = !!userSettings?.Email?.Status;
16     const hasEmailEnabled = !!userSettings?.Email?.Reset;
17     const hasPhone = !!userSettings?.Phone?.Value;
18     const hasPhoneVerified = !!userSettings?.Phone?.Status;
19     const hasPhoneEnabled = !!userSettings?.Phone?.Reset;
21     const hasVerifiedandDisabledEmail = hasEmail && hasEmailVerified && !hasEmailEnabled;
22     const hasVerifiedandDisabledPhone = hasPhone && hasPhoneVerified && !hasPhoneEnabled;
23     const hasRecoveryMethod = hasVerifiedandDisabledEmail || hasVerifiedandDisabledPhone;
24     const hasMnemonic = isMnemonicAvailable && user.MnemonicStatus === MNEMONIC_STATUS.SET;
26     const getSentinelRecoveryStatus = () => {
27         if (isSentinelUser && hasMnemonic && hasRecoveryMethod) {
28             return 'complete';
29         }
30         if (isSentinelUser && hasMnemonic) {
31             return 'intermediate';
32         }
33         if (isSentinelUser && !hasMnemonic && ((hasEmail && hasEmailEnabled) || (hasPhone && hasPhoneEnabled))) {
34             return 'insecure';
35         }
37         return 'incomplete';
38     };
40     const sentinelRecoveryStatus = getSentinelRecoveryStatus();
41     return [
42         {
43             isSentinelUser,
44             sentinelRecoveryStatus,
45             hasMnemonic,
46         },
47         loadingUserSettings || !userSettings || loadingIsMnemonicAvailable,
48     ] as const;
51 export default useIsSentinelUser;