Last active
June 18, 2020 21:20
-
-
Save frehner/c6fa439b458e5151dcaa0e07e174a038 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import React from 'react' | |
import { | |
OptimizelyFeature as OriginalOptimizelyFeature, | |
useFeature as useFeatureOriginal, | |
} from '@optimizely/react-sdk' | |
export function OptimizelyFeature({ feature, children, ...restProps }) { | |
return ( | |
<OriginalOptimizelyFeature feature={feature} {...restProps}> | |
{(isEnabled, ...rest) => { | |
let isEnabledFinal = isEnabled | |
const storageSetting = localStorage.getItem(feature) | |
if (storageSetting) { | |
// allows the local override to always win out, even if the env has the toggle on | |
isEnabledFinal = storageSetting === 'true' | |
} | |
return children(isEnabledFinal, ...rest) | |
}} | |
</OriginalOptimizelyFeature> | |
) | |
} | |
export function useFeature(...data) { | |
const [isEnabled, ...rest] = useFeatureOriginal(...data) | |
let isEnabledFinal = isEnabled | |
const storageSetting = localStorage.getItem(data[0]) | |
if (storageSetting) { | |
// allows the local override to always win out, even if the env has the toggle on | |
isEnabledFinal = storageSetting === 'true' | |
} | |
return [isEnabledFinal, ...rest] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment