Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / components / hooks / useRecoveryNotification.ts
blobf8a33ef7eb8f8c9a447819c8cb1b3ac7af09884f
1 import { c } from 'ttag';
3 import { useAddresses } from '@proton/account/addresses/hooks';
4 import { useUser } from '@proton/account/user/hooks';
5 import { ThemeColor } from '@proton/colors';
6 import useIsRecoveryFileAvailable from '@proton/components/hooks/recoveryFile/useIsRecoveryFileAvailable';
7 import useHasOutdatedRecoveryFile from '@proton/components/hooks/useHasOutdatedRecoveryFile';
8 import { FeatureCode, useFeature } from '@proton/features';
9 import { MNEMONIC_STATUS } from '@proton/shared/lib/interfaces';
10 import { getLikelyHasKeysToReactivate } from '@proton/shared/lib/keys/getInactiveKeys';
12 import useSecurityCenter from '../components/drawer/views/SecurityCenter/useSecurityCenter';
13 import { getOverallStatus } from '../containers/recovery/getOverallStatus';
14 import useIsDataRecoveryAvailable from './useIsDataRecoveryAvailable';
15 import useIsMnemonicAvailable from './useIsMnemonicAvailable';
16 import useIsSentinelUser from './useIsSentinelUser';
17 import useRecoveryStatus from './useRecoveryStatus';
19 const useRecoveryNotification = (
20     isLessInvasive: boolean,
21     isQuickSettings?: boolean,
22     canDisplayNewSentinelSettings?: boolean
23 ): { path: string; text: string; color: ThemeColor } | undefined => {
24     const [user] = useUser();
25     const [addresses, loadingAddresses] = useAddresses();
26     const isSecurityCenterEnabled = useSecurityCenter();
27     const alreadyDisplayedInSecurityCenter = isSecurityCenterEnabled && isQuickSettings;
29     const [{ accountRecoveryStatus, dataRecoveryStatus, mnemonicIsSet }, loadingRecoveryStatus] = useRecoveryStatus();
31     const [isRecoveryFileAvailable, loadingIsRecoveryFileAvailable] = useIsRecoveryFileAvailable();
32     const [isMnemonicAvailable, loadingIsMnemonicAvailable] = useIsMnemonicAvailable();
33     const [isDataRecoveryAvailable, loadingIsDataRecoveryAvailable] = useIsDataRecoveryAvailable();
34     const hasOutdatedRecoveryFile = useHasOutdatedRecoveryFile();
35     const [{ isSentinelUser, hasMnemonic }, loadingIsSentinelUser] = useIsSentinelUser();
37     const { feature: hasDismissedRecoverDataCard } = useFeature(FeatureCode.DismissedRecoverDataCard);
38     const hasKeysToReactivate = getLikelyHasKeysToReactivate(user, addresses);
40     const overallStatus = getOverallStatus({ accountRecoveryStatus, dataRecoveryStatus, isDataRecoveryAvailable });
42     const loading =
43         loadingRecoveryStatus ||
44         loadingIsRecoveryFileAvailable ||
45         loadingIsMnemonicAvailable ||
46         loadingIsDataRecoveryAvailable ||
47         loadingIsSentinelUser ||
48         loadingAddresses;
49     if (loading) {
50         return;
51     }
53     if (!isDataRecoveryAvailable) {
54         return;
55     }
57     const hasOutdatedMnemonic = user.MnemonicStatus === MNEMONIC_STATUS.OUTDATED;
58     if (isMnemonicAvailable && hasOutdatedMnemonic) {
59         return {
60             path: '/recovery#data',
61             text: c('Action').t`Update recovery phrase`,
62             color: ThemeColor.Danger,
63         };
64     }
66     if (isRecoveryFileAvailable && hasOutdatedRecoveryFile && !alreadyDisplayedInSecurityCenter) {
67         return {
68             path: '/recovery#data',
69             text: c('Action').t`Update recovery file`,
70             color: ThemeColor.Danger,
71         };
72     }
74     if (isLessInvasive) {
75         return;
76     }
78     if (hasKeysToReactivate && hasDismissedRecoverDataCard?.Value === false) {
79         return {
80             path: '/recovery?action=recover-data',
81             text: c('Action').t`Unlock data`,
82             color: ThemeColor.Danger,
83         };
84     }
85     // keeps account recovery notification dot consistent with mail security center for new recovery settings for sentinel users
86     if (isSentinelUser && canDisplayNewSentinelSettings && !alreadyDisplayedInSecurityCenter) {
87         if (!hasMnemonic) {
88             return {
89                 path: '/recovery#data',
90                 text: c('Action').t`Update recovery phrase`,
91                 color: ThemeColor.Danger,
92             };
93         }
94         return;
95     }
97     if (mnemonicIsSet || overallStatus === 'complete') {
98         return;
99     }
101     const mnemonicCanBeSet =
102         user.MnemonicStatus === MNEMONIC_STATUS.ENABLED || user.MnemonicStatus === MNEMONIC_STATUS.PROMPT;
103     if (isMnemonicAvailable && mnemonicCanBeSet && !alreadyDisplayedInSecurityCenter) {
104         return {
105             path: '/recovery?action=generate-recovery-phrase',
106             text: c('Action').t`Set recovery phrase`,
107             color: ThemeColor.Warning,
108         };
109     }
111     if (!alreadyDisplayedInSecurityCenter) {
112         return {
113             path: '/recovery',
114             text: c('Action').t`Activate recovery`,
115             color: ThemeColor.Warning,
116         };
117     }
120 export default useRecoveryNotification;