Merge branch 'DRVDOC-1260' into 'main'
[ProtonMail-WebClient.git] / packages / components / containers / keyTransparency / useKTActivation.ts
blobff3eb776bd35a68b2ea5f296645aecac12af8f6c
1 import { useCallback, useEffect, useState } from 'react';
3 import useAuthentication from '@proton/components/hooks/useAuthentication';
4 import useConfig from '@proton/components/hooks/useConfig';
5 import { useGetMailSettings } from '@proton/mail/mailSettings/hooks';
6 import type { APP_NAMES } from '@proton/shared/lib/constants';
7 import { APPS } from '@proton/shared/lib/constants';
8 import type { MailSettings } from '@proton/shared/lib/interfaces';
9 import { KeyTransparencyActivation } from '@proton/shared/lib/interfaces';
10 import { KEY_TRANSPARENCY_SETTING } from '@proton/shared/lib/mail/mailSettings';
11 import { useFlag, useGetFlag } from '@proton/unleash';
13 import { KtFeatureEnum, isKTActive } from './ktStatus';
15 const getKTFlag = ({ logOnly, showUI }: { logOnly: boolean; showUI: boolean }): KtFeatureEnum => {
16     if (showUI) {
17         return KtFeatureEnum.ENABLE_UI;
18     }
19     if (logOnly) {
20         return KtFeatureEnum.ENABLE_CORE;
21     }
22     return KtFeatureEnum.DISABLE;
25 const getKTActivation = (
26     featureFlag: KtFeatureEnum,
27     appName: APP_NAMES,
28     mailSettings?: MailSettings
29 ): KeyTransparencyActivation => {
30     if (!featureFlag) {
31         return KeyTransparencyActivation.DISABLED;
32     }
33     if (!isKTActive(featureFlag)) {
34         return KeyTransparencyActivation.DISABLED;
35     }
36     if (featureFlag == KtFeatureEnum.ENABLE_CORE) {
37         return KeyTransparencyActivation.LOG_ONLY;
38     }
39     if (featureFlag == KtFeatureEnum.ENABLE_UI) {
40         if (appName === APPS.PROTONMAIL) {
41             if (mailSettings?.KT === KEY_TRANSPARENCY_SETTING.ENABLED) {
42                 return KeyTransparencyActivation.SHOW_UI;
43             } else {
44                 return KeyTransparencyActivation.LOG_ONLY;
45             }
46         }
47         return KeyTransparencyActivation.SHOW_UI;
48     }
49     return KeyTransparencyActivation.DISABLED;
52 export const useGetKTActivation = (): (() => Promise<KeyTransparencyActivation>) => {
53     const { APP_NAME: appName } = useConfig();
54     const authentication = useAuthentication();
55     const getMailSettings = useGetMailSettings();
56     const getFlag = useGetFlag();
58     return useCallback(async () => {
59         const logOnly = getFlag('KeyTransparencyLogOnly');
60         const showUI = getFlag('KeyTransparencyShowUI');
61         const featureFlag = getKTFlag({ logOnly, showUI });
62         const mailSettings = Boolean(authentication.UID) ? await getMailSettings() : undefined;
63         return getKTActivation(featureFlag, appName, mailSettings);
64     }, []);
67 const useKTActivation = (): KeyTransparencyActivation => {
68     const showUI = useFlag('KeyTransparencyShowUI');
69     const logOnly = useFlag('KeyTransparencyLogOnly');
70     const getKTActivation = useGetKTActivation();
71     const [ktActivation, setKTActivation] = useState(KeyTransparencyActivation.DISABLED);
73     useEffect(() => {
74         const run = async () => {
75             setKTActivation(await getKTActivation());
76         };
77         void run();
78     }, [showUI, logOnly]);
80     return ktActivation;
83 export default useKTActivation;