UNPKG

@scaleway/use-growthbook

Version:

Utility package to expose AB test tool

60 lines (59 loc) 1.62 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const jsxRuntime = require("react/jsx-runtime"); const growthbookReact = require("@growthbook/growthbook-react"); const react = require("react"); const defaultLoadConfig = { autoRefresh: false, timeout: 500, skipCache: false }; const getGrowthBookInstance = ({ config: { apiHost, clientKey, enableDevMode, backgroundSync, subscribeToChanges }, trackingCallback }) => new growthbookReact.GrowthBook({ apiHost, clientKey, enableDevMode, trackingCallback, backgroundSync, subscribeToChanges }); const AbTestProvider = ({ children, config, trackingCallback, errorCallback, attributes, loadConfig }) => { const growthbook = react.useMemo( () => getGrowthBookInstance({ config, trackingCallback }), [trackingCallback, config] ); const loadFeature = react.useCallback(async () => { if (config.clientKey) { await growthbook.loadFeatures(loadConfig ?? defaultLoadConfig); } }, [growthbook, config, loadConfig]); react.useEffect(() => { loadFeature().catch(errorCallback); }, [loadFeature, errorCallback]); react.useEffect(() => { const currentAttributes = growthbook.getAttributes(); if (currentAttributes !== attributes) { growthbook.setAttributes({ ...currentAttributes, ...attributes }).catch(errorCallback); } }, [attributes, growthbook, errorCallback]); return /* @__PURE__ */ jsxRuntime.jsx(growthbookReact.GrowthBookProvider, { growthbook, children }); }; exports.AbTestProvider = AbTestProvider;