Add feature gate

This commit is contained in:
Eric Bailey
2024-12-16 18:34:33 -06:00
parent 8934aa108e
commit a706918e5a
2 changed files with 27 additions and 4 deletions
+1
View File
@@ -3,3 +3,4 @@ export type Gate =
| 'debug_show_feedcontext' // DISABLED DUE TO EME
| 'post_feed_lang_window' // DISABLED DUE TO EME
| 'remove_show_latest_button'
| 'trending_topics_beta'
+26 -4
View File
@@ -1,5 +1,6 @@
import React from 'react'
import {useGate} from '#/lib/statsig/statsig'
import {useServiceConfigQuery} from '#/state/queries/service-config'
import {device} from '#/storage'
@@ -12,19 +13,40 @@ const Context = React.createContext<Context>({
})
export function Provider({children}: React.PropsWithChildren<{}>) {
const gate = useGate()
// refetches at most every minute
const {data: config, isLoading: isInitialLoad} = useServiceConfigQuery()
const ctx = React.useMemo<Context>(() => {
// previously cached value
/*
* While loading, use cached value
*/
const cachedEnabled = device.get(['trendingBetaEnabled'])
if (isInitialLoad) {
return {enabled: Boolean(cachedEnabled)}
}
/*
* Doing an extra check here to reduce hits to statsig. If it's disabled on
* the server, we can exit early.
*/
const enabled = Boolean(config?.topicsEnabled)
if (!enabled) {
// cache for next reload
device.set(['trendingBetaEnabled'], enabled)
return {enabled: false}
}
/*
* Service is enabled, but also check statsig in case we're rolling back.
*/
const gateEnabled = gate('trending_topics_beta')
const _enabled = enabled && gateEnabled
// update cache
device.set(['trendingBetaEnabled'], enabled)
return {enabled}
}, [isInitialLoad, config])
device.set(['trendingBetaEnabled'], _enabled)
return {enabled: _enabled}
}, [isInitialLoad, config, gate])
return <Context.Provider value={ctx}>{children}</Context.Provider>
}