22/* eslint-disable @typescript-eslint/no-unsafe-return */
33/* eslint-disable @typescript-eslint/no-unsafe-assignment */
44/* eslint-disable @typescript-eslint/no-unsafe-member-access */
5- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
65// @ts -expect-error TODO: remove once Growthbook is correctly typed and export
76import { GrowthBook , GrowthBookProvider } from '@growthbook/growthbook-react'
8- import type { ReactNode } from 'react'
9- import { useCallback , useEffect , useMemo } from 'react'
10- import type { GrowthBookType } from './types'
7+ import { type ReactNode , useCallback , useEffect , useMemo } from 'react'
8+ import type { Attributes , GrowthBookType , LoadConfig } from './types'
119
1210export type ToolConfig = {
1311 apiHost : string
@@ -18,57 +16,60 @@ export type ToolConfig = {
1816export type TrackingCallback = (
1917 experiment : { key : string } ,
2018 result : { key : string } ,
21- ) => null
19+ ) => void
20+
21+ // TODO: use type from growthbook when it's typed will be export correctly
22+ // export type TrackingCallback = NonNullable<Context['trackingCallback']>
2223
2324export type AbTestProviderProps = {
2425 children : ReactNode
25- anonymousId : string
2626 config : ToolConfig
2727 trackingCallback : TrackingCallback
28- errorCallback : ( error : string ) => null
28+ errorCallback : ( error : Error | string ) => void
29+ attributes : Attributes
30+ loadConfig ?: LoadConfig
2931}
3032
3133const getGrowthBookInstance = ( {
3234 config : { apiHost, clientKey, enableDevMode } ,
33- anonymousId ,
35+ attributes ,
3436 trackingCallback,
3537} : {
3638 config : ToolConfig
37- anonymousId : string
39+ attributes : Attributes
3840 trackingCallback : TrackingCallback
3941} ) : GrowthBookType =>
4042 new GrowthBook ( {
4143 apiHost,
4244 clientKey,
4345 enableDevMode,
44- attributes : {
45- anonymousId,
46- userId : undefined ,
47- organizationId : undefined ,
48- organizationType : undefined ,
49- } ,
46+ attributes,
5047 trackingCallback,
5148 } )
49+
50+ const defaultLoadConfig = {
51+ autoRefresh : false ,
52+ timeout : 500 ,
53+ }
54+
5255export const AbTestProvider = ( {
5356 children,
5457 config,
55- anonymousId,
5658 trackingCallback,
5759 errorCallback,
60+ attributes,
61+ loadConfig = defaultLoadConfig ,
5862} : AbTestProviderProps ) => {
5963 const growthbook : GrowthBookType = useMemo (
60- ( ) => getGrowthBookInstance ( { config, anonymousId , trackingCallback } ) ,
61- [ trackingCallback , config , anonymousId ] ,
64+ ( ) => getGrowthBookInstance ( { config, attributes , trackingCallback } ) ,
65+ [ trackingCallback , config , attributes ] ,
6266 )
6367
6468 const loadFeature = useCallback ( async ( ) => {
6569 if ( config . clientKey ) {
66- await growthbook . loadFeatures ( {
67- autoRefresh : false ,
68- timeout : 500 ,
69- } )
70+ await growthbook . loadFeatures ( loadConfig )
7071 }
71- } , [ growthbook , config ] )
72+ } , [ growthbook , config , loadConfig ] )
7273
7374 useEffect ( ( ) => {
7475 loadFeature ( ) . catch ( errorCallback )
0 commit comments