Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / components / hooks / useEarlyAccess.ts
blob51a85638d7a47b95ce95246111f0878c0dcb0d1b
1 import { useEffect } from 'react';
3 import { useUserSettings } from '@proton/account/userSettings/hooks';
4 import {
5     getTargetEnvironment,
6     getVersionCookieIsValid,
7     updateVersionCookie,
8     versionCookieAtLoad,
9 } from '@proton/components/helpers/versionCookie';
10 import { FeatureCode, useFeature } from '@proton/features';
11 import { useLoading } from '@proton/hooks';
12 import { updateEarlyAccess } from '@proton/shared/lib/api/settings';
13 import { hasInboxDesktopFeature, invokeInboxDesktopIPC } from '@proton/shared/lib/desktop/ipcHelpers';
15 import useApi from './useApi';
17 const useEarlyAccess = () => {
18     const api = useApi();
19     const earlyAccessScope = useFeature(FeatureCode.EarlyAccessScope);
20     const { feature: { Value: maybeEarlyAccess, DefaultValue } = {} } = earlyAccessScope;
21     const [loadingUpdate, withLoadingUpdate] = useLoading();
22     const [userSettings, userSettingsLoading] = useUserSettings();
24     const earlyAccessScopeValue = maybeEarlyAccess || DefaultValue;
25     const hasLoaded = !(userSettingsLoading || earlyAccessScope.loading);
27     /*
28      * Shouldn't be able to call update without the request for the EarlyAccessScope
29      * feature to have completed since the environment is set based on it should
30      * earlyAccessEnabled be true
31      */
32     const canUpdate = earlyAccessScope.feature && 'Value' in earlyAccessScope.feature;
34     const update = async (earlyAccessEnabled: boolean) => {
35         /*
36          * Can't update the cookie without the request for the EarlyAccessScope
37          * feature to have completed since the environment is set based on it should
38          * earlyAccessEnabled be true
39          */
40         if (canUpdate) {
41             updateVersionCookie(earlyAccessEnabled ? earlyAccessScopeValue : undefined, earlyAccessScope.feature);
42         }
44         await withLoadingUpdate(api(updateEarlyAccess({ EarlyAccess: Number(earlyAccessEnabled) })));
45     };
47     const normalizedVersionCookieAtLoad = getVersionCookieIsValid(versionCookieAtLoad, earlyAccessScope.feature)
48         ? versionCookieAtLoad
49         : undefined;
51     const targetEnvironment = getTargetEnvironment(earlyAccessScope.feature, Boolean(userSettings.EarlyAccess));
53     const currentEnvironmentMatchesTargetEnvironment = normalizedVersionCookieAtLoad === targetEnvironment;
54     const environmentIsDesynchronized = hasLoaded && !currentEnvironmentMatchesTargetEnvironment;
55     const loading = earlyAccessScope.loading || loadingUpdate;
57     useEffect(() => {
58         if (hasInboxDesktopFeature('EarlyAccess')) {
59             invokeInboxDesktopIPC({ type: 'earlyAccess', payload: targetEnvironment });
60         }
61     }, [targetEnvironment]);
63     return {
64         value: Boolean(userSettings.EarlyAccess),
65         scope: earlyAccessScopeValue,
66         canUpdate,
67         update,
68         loading,
69         loadingUpdate,
70         environmentIsDesynchronized,
71         targetEnvironment,
72         currentEnvironment: versionCookieAtLoad,
73     };
76 export default useEarlyAccess;