1 import { useEffect } from 'react';
3 import { useUserSettings } from '@proton/account/userSettings/hooks';
6 getVersionCookieIsValid,
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 = () => {
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);
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
32 const canUpdate = earlyAccessScope.feature && 'Value' in earlyAccessScope.feature;
34 const update = async (earlyAccessEnabled: boolean) => {
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
41 updateVersionCookie(earlyAccessEnabled ? earlyAccessScopeValue : undefined, earlyAccessScope.feature);
44 await withLoadingUpdate(api(updateEarlyAccess({ EarlyAccess: Number(earlyAccessEnabled) })));
47 const normalizedVersionCookieAtLoad = getVersionCookieIsValid(versionCookieAtLoad, earlyAccessScope.feature)
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;
58 if (hasInboxDesktopFeature('EarlyAccess')) {
59 invokeInboxDesktopIPC({ type: 'earlyAccess', payload: targetEnvironment });
61 }, [targetEnvironment]);
64 value: Boolean(userSettings.EarlyAccess),
65 scope: earlyAccessScopeValue,
70 environmentIsDesynchronized,
72 currentEnvironment: versionCookieAtLoad,
76 export default useEarlyAccess;