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 => {
17 return KtFeatureEnum.ENABLE_UI;
20 return KtFeatureEnum.ENABLE_CORE;
22 return KtFeatureEnum.DISABLE;
25 const getKTActivation = (
26 featureFlag: KtFeatureEnum,
28 mailSettings?: MailSettings
29 ): KeyTransparencyActivation => {
31 return KeyTransparencyActivation.DISABLED;
33 if (!isKTActive(featureFlag)) {
34 return KeyTransparencyActivation.DISABLED;
36 if (featureFlag == KtFeatureEnum.ENABLE_CORE) {
37 return KeyTransparencyActivation.LOG_ONLY;
39 if (featureFlag == KtFeatureEnum.ENABLE_UI) {
40 if (appName === APPS.PROTONMAIL) {
41 if (mailSettings?.KT === KEY_TRANSPARENCY_SETTING.ENABLED) {
42 return KeyTransparencyActivation.SHOW_UI;
44 return KeyTransparencyActivation.LOG_ONLY;
47 return KeyTransparencyActivation.SHOW_UI;
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);
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);
74 const run = async () => {
75 setKTActivation(await getKTActivation());
78 }, [showUI, logOnly]);
83 export default useKTActivation;