-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a4f074f
commit d3ab08d
Showing
2 changed files
with
28 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
{ | ||
"editor.codeActionsOnSave": { | ||
"source.fixAll.eslint": "explicit" | ||
}, | ||
"editor.codeActionsOnSave": { "source.fixAll.eslint": true }, | ||
"editor.defaultFormatter": "esbenp.prettier-vscode", | ||
"editor.formatOnSave": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,41 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { Analytics } from '@deriv-com/analytics'; | ||
import useIsGrowthbookIsLoaded from '../useIsGrowthbookLoaded'; | ||
import useIsGrowthbookIsLoaded from './useIsGrowthbookLoaded'; | ||
|
||
type featureValueTypes = Record<string, boolean> | boolean | string | []; | ||
|
||
interface UseGrowthbookGetFeatureValueArgs { | ||
defaultValue?: featureValueTypes; | ||
featureFlag: string; | ||
defaultValue?: featureValueTypes; | ||
featureFlag: string; | ||
} | ||
|
||
const useGrowthbookGetFeatureValue = <T,>({ | ||
defaultValue, | ||
featureFlag, | ||
defaultValue, | ||
featureFlag, | ||
}: UseGrowthbookGetFeatureValueArgs): [T, boolean] => { | ||
const resolvedDefaultValue: featureValueTypes = defaultValue !== undefined ? defaultValue : false; | ||
const [featureFlagValue, setFeatureFlagValue] = useState<T>( | ||
(Analytics?.getFeatureValue(featureFlag, resolvedDefaultValue) ?? resolvedDefaultValue) as T, | ||
); | ||
const isGBLoaded = useIsGrowthbookIsLoaded(); | ||
const resolvedDefaultValue: featureValueTypes = defaultValue !== undefined ? defaultValue : false; | ||
const [featureFlagValue, setFeatureFlagValue] = useState<T>( | ||
(Analytics?.getFeatureValue(featureFlag, resolvedDefaultValue) ?? resolvedDefaultValue) as T | ||
); | ||
const isGBLoaded = useIsGrowthbookIsLoaded(); | ||
|
||
useEffect(() => { | ||
if (isGBLoaded) { | ||
if (Analytics?.getInstances()?.ab) { | ||
const setFeatureValue = () => { | ||
const value = Analytics?.getFeatureValue(featureFlag, resolvedDefaultValue) as T; | ||
setFeatureFlagValue(value); | ||
}; | ||
setFeatureValue(); | ||
Analytics?.getInstances()?.ab?.GrowthBook?.setRenderer(() => { | ||
// this will be called whenever the feature flag value changes and acts as a event listener | ||
setFeatureValue(); | ||
}); | ||
} | ||
} | ||
}, [isGBLoaded, resolvedDefaultValue, featureFlag]); | ||
useEffect(() => { | ||
if (isGBLoaded) { | ||
if (Analytics?.getInstances()?.ab) { | ||
const setFeatureValue = () => { | ||
const value = Analytics?.getFeatureValue(featureFlag, resolvedDefaultValue) as T; | ||
setFeatureFlagValue(value); | ||
}; | ||
setFeatureValue(); | ||
Analytics?.getInstances()?.ab?.GrowthBook?.setRenderer(() => { | ||
// this will be called whenever the feature flag value changes and acts as a event listener | ||
setFeatureValue(); | ||
}); | ||
} | ||
} | ||
}, [isGBLoaded, resolvedDefaultValue, featureFlag]); | ||
|
||
return [featureFlagValue as T, isGBLoaded]; | ||
return [featureFlagValue as T, isGBLoaded]; | ||
}; | ||
|
||
export default useGrowthbookGetFeatureValue; | ||
export default useGrowthbookGetFeatureValue; |