fix: update feature flags on update (#4613)

This commit is contained in:
Zach Pomerantz 2022-09-13 09:03:59 -07:00 committed by GitHub
parent 7848ad86bd
commit a6c1c49f98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,5 +1,4 @@
import { useAtom } from 'jotai' import { atomWithStorage, useAtomValue, useUpdateAtom } from 'jotai/utils'
import { atomWithStorage, useAtomValue } from 'jotai/utils'
import { createContext, ReactNode, useCallback, useContext } from 'react' import { createContext, ReactNode, useCallback, useContext } from 'react'
export { FeatureFlag } from './flags/featureFlags' export { FeatureFlag } from './flags/featureFlags'
@ -23,14 +22,16 @@ export function useFeatureFlagsContext(): FeatureFlagsContextType {
export const featureFlagSettings = atomWithStorage<Record<string, string>>('featureFlags', {}) export const featureFlagSettings = atomWithStorage<Record<string, string>>('featureFlags', {})
export function useUpdateFlag() { export function useUpdateFlag() {
const [featureFlags, setFeatureFlags] = useAtom(featureFlagSettings) const setFeatureFlags = useUpdateAtom(featureFlagSettings)
return useCallback( return useCallback(
(featureFlag: string, option: string) => { (featureFlag: string, option: string) => {
featureFlags[featureFlag] = option setFeatureFlags((featureFlags) => ({
setFeatureFlags(featureFlags) ...featureFlags,
[featureFlag]: option,
}))
}, },
[featureFlags, setFeatureFlags] [setFeatureFlags]
) )
} }