import { updateEarlyAccess } from '@proton/shared/lib/api/settings' ; import { deleteCookie , getCookie , setCookie } from '@proton/shared/lib/helpers/cookies' ; import useFeature from './useFeature' ; import useApi from './useApi' ; import useLoading from './useLoading' ; import useUserSettings from './useUserSettings' ; import { Feature , FeatureCode } from '../containers/features' ; export type Environment = 'alpha' | 'beta' ; export const getVersionCookieIsValid = ( versionCookie , earlyAccessScope ) => versionCookie === undefined || earlyAccessScope ?. Options ?. includes ( versionCookie ) ; export const getTargetEnvironment = ( earlyAccessScope , earlyAccessUserSetting ) : Environment | undefined => { if ( ! earlyAccessScope || ! earlyAccessUserSetting ) { return ; } return earlyAccessScope . Value ; } ; export const versionCookieAtLoad = getCookie ( 'Version' ) as Environment | undefined ; export const updateVersionCookie = ( environment , earlyAccessScopeFeature ) => { if ( environment ) { setCookie ( { cookieName : 'Version' , cookieValue : environment , expirationDate : 'max' , path : '/' , } ) ; } if ( ! getVersionCookieIsValid ( getCookie ( 'Version' ) as Environment | undefined , earlyAccessScopeFeature ) ) { return ; } if ( ! environment ) { deleteCookie ( 'Version' ) ; } } ; const useEarlyAccess = ( ) => { const api = useApi ( ) ; const earlyAccessScope = useFeature ( FeatureCode . EarlyAccessScope ) ; const { feature : { Value : maybeEarlyAccess , DefaultValue } = { } } = earlyAccessScope ; const [ loadingUpdate , withLoadingUpdate ] = useLoading ( ) ; const [ userSettings , userSettingsLoading ] = useUserSettings ( ) ; const earlyAccessScopeValue = maybeEarlyAccess || DefaultValue ; const hasLoaded = ! ( userSettingsLoading || earlyAccessScope . loading ) ; const canUpdate = earlyAccessScope . feature && 'Value' in earlyAccessScope . feature ; const update = async ( earlyAccessEnabled ) => { if ( canUpdate ) { updateVersionCookie ( earlyAccessEnabled ? earlyAccessScopeValue : undefined , earlyAccessScope . feature ) ; } await withLoadingUpdate ( api ( updateEarlyAccess ( { EarlyAccess : Number ( earlyAccessEnabled ) } ) ) ) ; } ; const normalizedVersionCookieAtLoad = getVersionCookieIsValid ( versionCookieAtLoad , earlyAccessScope . feature ) ? versionCookieAtLoad : undefined ; const targetEnvironment = getTargetEnvironment ( earlyAccessScope . feature , Boolean ( userSettings . EarlyAccess ) ) ; const currentEnvironmentMatchesTargetEnvironment = normalizedVersionCookieAtLoad === targetEnvironment ; const environmentIsDesynchronized = hasLoaded && ! currentEnvironmentMatchesTargetEnvironment ; const loading = earlyAccessScope . loading || loadingUpdate ; return { value : Boolean ( userSettings . EarlyAccess ) , scope : earlyAccessScopeValue , canUpdate , update , loading , loadingUpdate , environmentIsDesynchronized , targetEnvironment , currentEnvironment : versionCookieAtLoad , } ; } ; export default useEarlyAccess ;