···11-import React from 'react'
22-import {
33- Statsig,
44- StatsigProvider,
55- useGate as useStatsigGate,
66-} from 'statsig-react'
77-import {useSession} from '../../state/session'
88-import {sha256} from 'js-sha256'
99-1010-const statsigOptions = {
1111- environment: {
1212- tier: process.env.NODE_ENV === 'development' ? 'development' : 'production',
1313- },
1414- // Don't block on waiting for network. The fetched config will kick in on next load.
1515- // This ensures the UI is always consistent and doesn't update mid-session.
1616- // Note this makes cold load (no local storage) and private mode return `false` for all gates.
1717- initTimeoutMs: 1,
1818-}
1919-2020-export function logEvent(
2121- eventName: string,
2222- value?: string | number | null,
2323- metadata?: Record<string, string> | null,
2424-) {
2525- Statsig.logEvent(eventName, value, metadata)
2626-}
2727-2828-export function useGate(gateName: string) {
2929- const {isLoading, value} = useStatsigGate(gateName)
3030- if (isLoading) {
3131- // This should not happen because of waitForInitialization={true}.
3232- console.error('Did not expected isLoading to ever be true.')
3333- }
3434- return value
3535-}
3636-3737-function toStatsigUser(did: string | undefined) {
3838- let userID: string | undefined
3939- if (did) {
4040- userID = sha256(did)
4141- }
4242- return {userID}
4343-}
4444-4545-export function Provider({children}: {children: React.ReactNode}) {
4646- const {currentAccount} = useSession()
4747- const currentStatsigUser = React.useMemo(
4848- () => toStatsigUser(currentAccount?.did),
4949- [currentAccount?.did],
5050- )
5151-5252- React.useEffect(() => {
5353- function refresh() {
5454- // Intentionally refetching the config using the JS SDK rather than React SDK
5555- // so that the new config is stored in cache but isn't used during this session.
5656- // It will kick in for the next reload.
5757- Statsig.updateUser(currentStatsigUser)
5858- }
5959- const id = setInterval(refresh, 3 * 60e3 /* 3 min */)
6060- return () => clearInterval(id)
6161- }, [currentStatsigUser])
6262-6363- return (
6464- <StatsigProvider
6565- sdkKey="client-SXJakO39w9vIhl3D44u8UupyzFl4oZ2qPIkjwcvuPsV"
6666- mountKey={currentStatsigUser.userID}
6767- user={currentStatsigUser}
6868- // This isn't really blocking due to short initTimeoutMs above.
6969- // However, it ensures `isLoading` is always `false`.
7070- waitForInitialization={true}
7171- options={statsigOptions}>
7272- {children}
7373- </StatsigProvider>
7474- )
7575-}