diff --git a/native/components/feature-flags-provider.react.js b/native/components/feature-flags-provider.react.js --- a/native/components/feature-flags-provider.react.js +++ b/native/components/feature-flags-provider.react.js @@ -1,5 +1,6 @@ // @flow +import AsyncStorage from '@react-native-async-storage/async-storage'; import * as React from 'react'; import { Platform } from 'react-native'; @@ -14,17 +15,19 @@ type FeatureFlagsContextType = { +configuration: FeatureFlagsConfiguration, - +loaded: boolean, + +loadedFromService: boolean, }; const defaultContext = { configuration: {}, - loaded: false, + loadedFromService: false, }; const FeatureFlagsContext: React.Context = React.createContext(defaultContext); +const featureFlagsStorageKey = 'FeatureFlags'; + type Props = { +children: React.Node, }; @@ -33,6 +36,31 @@ const isStaff = useIsCurrentUserStaff(); const [featuresConfig, setFeaturesConfig] = React.useState(defaultContext); + + React.useEffect(() => { + (async () => { + if (featuresConfig.loadedFromService) { + return; + } + + const persistedFeaturesConfig = await AsyncStorage.getItem( + featureFlagsStorageKey, + ); + if (!persistedFeaturesConfig) { + return; + } + + setFeaturesConfig(config => + config.loadedFromService + ? config + : { + configuration: JSON.parse(persistedFeaturesConfig), + loadedFromService: false, + }, + ); + })(); + }, [featuresConfig.loadedFromService]); + React.useEffect(() => { (async () => { try { @@ -47,8 +75,12 @@ } setFeaturesConfig({ configuration, - loaded: true, + loadedFromService: true, }); + await AsyncStorage.setItem( + featureFlagsStorageKey, + JSON.stringify(configuration), + ); } catch (e) { console.error('Feature flag retrieval failed:', e); }