-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: statsig integration PoC (#747)
- Loading branch information
1 parent
0ffe236
commit ffa2f97
Showing
12 changed files
with
95 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,39 @@ | ||
import { StatsigClient } from '@statsig/js-client'; | ||
import { StatsigProvider, useStatsigClient } from '@statsig/react-bindings'; | ||
import { useEffect, useMemo, useState } from 'react'; | ||
|
||
export enum StatSigFlags { | ||
// When adding a flag here, make sure to add an analytics tracker in useAnalytics.ts | ||
ffSkipMigration = 'ff_skip_migration', | ||
} | ||
import { StatSigFlags, StatsigConfigType } from '@/types/statsig'; | ||
import { StatsigClient } from '@statsig/js-client'; | ||
import { | ||
StatsigProvider as StatsigProviderInternal, | ||
useStatsigClient, | ||
} from '@statsig/react-bindings'; | ||
|
||
export const StatSigProvider = ({ children }: { children: React.ReactNode }) => { | ||
const client = new StatsigClient(`${import.meta.env.VITE_STATSIG_CLIENT_KEY}`, { | ||
// TODO: fill in with ip address | ||
}); | ||
client.initializeSync(); | ||
import { initStatsig } from '@/lib/statsig'; | ||
|
||
return <StatsigProvider client={client}> {children} </StatsigProvider>; | ||
export const StatsigProvider = ({ children }: { children: React.ReactNode }) => { | ||
const [client, setClient] = useState<StatsigClient | null>(null); | ||
useEffect(() => { | ||
const setAsyncClient = async () => { | ||
const statsigClient = await initStatsig(); | ||
setClient(statsigClient); | ||
}; | ||
setAsyncClient(); | ||
}, [initStatsig]); | ||
// if no client, render without provider until a client exists | ||
if (!client) return <div>{children}</div>; | ||
return <StatsigProviderInternal client={client}> {children} </StatsigProviderInternal>; | ||
}; | ||
|
||
export const useStatSigGateValue = (gate: StatSigFlags) => { | ||
export const useStatsigGateValue = (gate: StatSigFlags) => { | ||
const { checkGate } = useStatsigClient(); | ||
return checkGate(gate); | ||
}; | ||
|
||
export const useAllStatsigGateValues = () => { | ||
const { checkGate } = useStatsigClient(); | ||
const allGateValues = useMemo(() => { | ||
return Object.values(StatSigFlags).reduce((acc, gate) => { | ||
return { ...acc, [gate]: checkGate(gate) }; | ||
}, {} as StatsigConfigType); | ||
}, []); | ||
return allGateValues; | ||
}; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { StatsigClient } from '@statsig/js-client'; | ||
|
||
let statsigClient: StatsigClient; | ||
|
||
export const initStatsig = async () => { | ||
if (statsigClient) return statsigClient; | ||
statsigClient = new StatsigClient( | ||
import.meta.env.VITE_STATSIG_CLIENT_KEY, | ||
{}, | ||
{ | ||
disableLogging: import.meta.env.VITE_DISABLE_STATSIG, | ||
disableStorage: import.meta.env.VITE_DISABLE_STATSIG, | ||
} | ||
); | ||
await statsigClient.initializeAsync(); | ||
return statsigClient; | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type StatsigConfigType = Record<StatSigFlags, boolean>; | ||
|
||
export enum StatSigFlags { | ||
ffSkipMigration = 'ff_skip_migration', | ||
} |