diff options
author | dan <dan.abramov@gmail.com> | 2024-03-06 05:55:34 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-06 05:55:34 +0000 |
commit | eb298d2e60a0ddf26ebaf8f27373418bbf7769e3 (patch) | |
tree | 223bdfdeae951ccbda10c2a47eda0794191c4df0 /src/lib/statsig/statsig.web.tsx | |
parent | 26fc0cf66d0cde33e8105495785a1ce4248fb9f7 (diff) | |
download | voidsky-eb298d2e60a0ddf26ebaf8f27373418bbf7769e3.tar.zst |
Initial feature gating and A/B testing integration (#3122)
* Add statsig dependency * Add SDK provider * Move to separate file, add tier and hashing * Disable local storage for now * Add initial gate testing fixture * Fork for web just in case * More WIP * wip * Rm test gate * Add shim on native * Clarify
Diffstat (limited to 'src/lib/statsig/statsig.web.tsx')
-rw-r--r-- | src/lib/statsig/statsig.web.tsx | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/lib/statsig/statsig.web.tsx b/src/lib/statsig/statsig.web.tsx new file mode 100644 index 000000000..6508131c4 --- /dev/null +++ b/src/lib/statsig/statsig.web.tsx @@ -0,0 +1,51 @@ +import React from 'react' +import {StatsigProvider, useGate as useStatsigGate} from 'statsig-react' +import {useSession} from '../../state/session' +import {sha256} from 'js-sha256' + +const statsigOptions = { + environment: { + tier: process.env.NODE_ENV === 'development' ? 'development' : 'production', + }, + // Don't block on waiting for network. The fetched config will kick in on next load. + // This ensures the UI is always consistent and doesn't update mid-session. + // Note this makes cold load (no local storage) and private mode return `false` for all gates. + initTimeoutMs: 1, +} + +export function useGate(gateName: string) { + const {isLoading, value} = useStatsigGate(gateName) + if (isLoading) { + // This should not happen because of waitForInitialization={true}. + console.error('Did not expected isLoading to ever be true.') + } + return value +} + +function toStatsigUser(did: string | undefined) { + let userID: string | undefined + if (did) { + userID = sha256(did) + } + return {userID} +} + +export function Provider({children}: {children: React.ReactNode}) { + const {currentAccount} = useSession() + const currentStatsigUser = React.useMemo( + () => toStatsigUser(currentAccount?.did), + [currentAccount?.did], + ) + return ( + <StatsigProvider + sdkKey="client-SXJakO39w9vIhl3D44u8UupyzFl4oZ2qPIkjwcvuPsV" + mountKey={currentStatsigUser.userID} + user={currentStatsigUser} + // This isn't really blocking due to short initTimeoutMs above. + // However, it ensures `isLoading` is always `false`. + waitForInitialization={true} + options={statsigOptions}> + {children} + </StatsigProvider> + ) +} |