-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from deriv-com/ako/add-gb-feature-flag
Ako/ add gb feature flag
- Loading branch information
Showing
5 changed files
with
85 additions
and
1 deletion.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { Analytics } from '@deriv-com/analytics'; | ||
import useIsGrowthbookIsLoaded from './useIsGrowthbookLoaded'; | ||
|
||
interface UseGrowthbookGetFeatureValueArgs<T> { | ||
defaultValue?: T; | ||
featureFlag: string; | ||
} | ||
|
||
const useGrowthbookGetFeatureValue = <T extends boolean | string>({ | ||
defaultValue, | ||
featureFlag, | ||
}: UseGrowthbookGetFeatureValueArgs<T>) => { | ||
const resolvedDefaultValue: T = defaultValue !== undefined ? defaultValue : (false as T); | ||
const [featureFlagValue, setFeatureFlagValue] = useState( | ||
Analytics?.getFeatureValue(featureFlag, resolvedDefaultValue) ?? resolvedDefaultValue | ||
); | ||
const isGBLoaded = useIsGrowthbookIsLoaded(); | ||
|
||
useEffect(() => { | ||
if (isGBLoaded) { | ||
if (Analytics?.getInstances()?.ab) { | ||
const setFeatureValue = () => { | ||
const value = Analytics?.getFeatureValue(featureFlag, resolvedDefaultValue); | ||
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, isGBLoaded]; | ||
}; | ||
|
||
export default useGrowthbookGetFeatureValue; |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { Analytics } from '@deriv-com/analytics'; | ||
|
||
const useIsGrowthbookIsLoaded = () => { | ||
const [isGBLoaded, setIsGBLoaded] = useState(false); | ||
|
||
useEffect(() => { | ||
let checksCounter = 0; | ||
const analyticsInterval: NodeJS.Timeout = setInterval(() => { | ||
// Check if the analytics instance is available for 10 seconds before setting the feature flag value | ||
if (checksCounter > 20) { | ||
// If the analytics instance is not available after 10 seconds, clear the interval | ||
clearInterval(analyticsInterval); | ||
return; | ||
} | ||
checksCounter += 1; | ||
if (Analytics?.getInstances()?.ab) { | ||
setIsGBLoaded(true); | ||
clearInterval(analyticsInterval); | ||
} | ||
}, 500); | ||
|
||
return () => { | ||
clearInterval(analyticsInterval); | ||
}; | ||
}, []); | ||
|
||
return isGBLoaded; | ||
}; | ||
|
||
export default useIsGrowthbookIsLoaded; |