Skip to content

Commit

Permalink
Merge pull request #386 from MetaMask/improve-ld-support
Browse files Browse the repository at this point in the history
fix: use an experiment flags context and evaluate it only once and fl…
  • Loading branch information
kevin-chassagne authored Jun 10, 2024
2 parents de90b26 + 07c1cff commit e05ed1a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
34 changes: 34 additions & 0 deletions src/Context/ExperimentFlagsContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useLDClient } from 'gatsby-plugin-launchdarkly'
import React, { createContext, useContext, useEffect, useState } from 'react'

const ExperimentFlagsContext = createContext({
experimentFlags: {},
})

export const useExperimentFlags = () => useContext(ExperimentFlagsContext)

export const ExperimentFlagsProvider = ({ children }) => {
const [experimentFlags, setExperimentFlags] = useState('')
const ldClient = useLDClient()

useEffect(() => {
if (!ldClient) {
return
}

const init = async () => {
await ldClient.waitUntilReady()
const flagsValue = ldClient.allFlags()
setExperimentFlags(flagsValue)
ldClient.flush()
}

init()
}, [ldClient])

return (
<ExperimentFlagsContext.Provider value={{ experimentFlags }}>
{children}
</ExperimentFlagsContext.Provider>
)
}
9 changes: 6 additions & 3 deletions src/components/ClientSideWrapper/Wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ContextClientSide from '../../Context/ContextClientSide'
import MetaMaskContextProvider from '../../Context/MetaMaskContextProvider'
import { DEFAULT_LOCALE, LOCALES_TRANSLATE } from '../../lib/config.mjs'
import { useLocation } from '@reach/router'
import { ExperimentFlagsProvider } from '../../Context/ExperimentFlagsContext'

const ClientSideWrapper = ({ children }) => {
const location = useLocation()
Expand Down Expand Up @@ -47,9 +48,11 @@ const ClientSideWrapper = ({ children }) => {
}
return (
<MetaMaskContextProvider>
<ContextClientSide.Provider value={valueContext}>
{children}
</ContextClientSide.Provider>
<ExperimentFlagsProvider>
<ContextClientSide.Provider value={valueContext}>
{children}
</ContextClientSide.Provider>
</ExperimentFlagsProvider>
</MetaMaskContextProvider>
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Contentful/ContentfulModuleContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const ContentfulModuleContainer = props => {
const showLocaleProvidersOnBuyCryptoPage = useFeatureFlag({
componentName: 'PopupRegionSelector',
componentId: contentful_id,
flagName: 'showLocaleProvidersOnBuyCryptoPage',
flagName: 'show-locale-providers-on-buy-crypto-page',
elementRef,
})

Expand Down
2 changes: 1 addition & 1 deletion src/components/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const StyledHeader = props => {
const showLanguageSelector = useFeatureFlag({
componentName: 'Header',
componentId: contentfulId,
flagName: 'showLanguageSelector',
flagName: 'show-language-selector',
elementRef: languageSelectorRef,
})

Expand Down
15 changes: 8 additions & 7 deletions src/hooks/useFeatureFlag.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useFlags, useLDClient } from 'gatsby-plugin-launchdarkly'
import { useLDClient } from 'gatsby-plugin-launchdarkly'
import { useEffect, useRef, useState } from 'react'
import { useExperimentFlags } from '../Context/ExperimentFlagsContext'

export const useFeatureFlag = ({
componentName,
Expand All @@ -13,7 +14,7 @@ export const useFeatureFlag = ({
componentId,
}

const flags = useFlags()
const { experimentFlags } = useExperimentFlags()
const ldClient = useLDClient()
const hasDataLayerUpdatedRef = useRef(false)
const [flagValue, setFlagValue] = useState(null)
Expand Down Expand Up @@ -47,10 +48,10 @@ export const useFeatureFlag = ({
}

useEffect(() => {
if (!ldClient) return

setFlagValue(flags[flagName])
}, [ldClient])
if (experimentFlags?.[flagName]) {
setFlagValue(experimentFlags[flagName])
}
}, [experimentFlags, flagName])

useEffect(() => {
if (!ldClient) return
Expand Down Expand Up @@ -79,5 +80,5 @@ export const useFeatureFlag = ({
}
}, [ldClient, flagValue])

return flags[flagName]
return experimentFlags?.[flagName]
}

0 comments on commit e05ed1a

Please sign in to comment.