UNPKG

@xo-union/tk-component-header-nav

Version:
73 lines 3.37 kB
import _URLSearchParams from "@babel/runtime-corejs3/core-js/url-search-params"; import _globalThis from "@babel/runtime-corejs3/core-js/global-this"; import React from "react"; import Flipper, { determineExperimentBucket } from "@tkww/orion-web-sdk-experimentation"; import experimentConfigs from "./experimentConfigs.js"; const flipperApiKey = (() => { if (process.env.UNION_FLIPPER_ENV) { if (process.env.UNION_FLIPPER_ENV !== "qa" && process.env.UNION_FLIPPER_ENV !== "production") { throw new Error(`Expected UNION_FLIPPER_ENV to be 'qa' or 'production'.`); } console.warn('Using UNION_FLIPPER_ENV could result in a misconfigured environment. It is best that you use UNION_DEPLOYMENT_ENV for all your Union services integrations.'); return process.env.UNION_FLIPPER_ENV; } if (process.env.UNION_DEPLOYMENT_ENV) { if (process.env.UNION_DEPLOYMENT_ENV !== "qa" && process.env.UNION_DEPLOYMENT_ENV !== "production") { throw new Error(`Expected UNION_DEPLOYMENT_ENV to be 'qa' or 'production'.`); } return process.env.UNION_DEPLOYMENT_ENV; } if (false) { console.warn("Expected UNION_DEPLOYMENT_ENV to be 'qa' or 'production'. NODE_ENV fallback will be removed in a future version."); return process.env.NODE_ENV === 'production' ? 'production' : 'qa'; } throw new Error("Expected UNION_DEPLOYMENT_ENV to be either 'qa' or 'production'."); })() === 'production' ? 'e8d65bf456cc4d0dbc553b22e75b09d5' : '046212cf79267fa192885d3660ee65a8'; const useRenderRef = val => { const ref = React.useRef(val); ref.current = val; return ref; }; const useURLQueryState = (initialValue, key) => { var _globalThis$location; const [state, setState] = React.useState(initialValue); const searchParams = new _URLSearchParams(_globalThis === null || _globalThis === void 0 ? void 0 : (_globalThis$location = _globalThis.location) === null || _globalThis$location === void 0 ? void 0 : _globalThis$location.search); return [searchParams.get(key) || state, setState, searchParams.has(key)]; }; process.env.APP_ENV = process.env.APP_ENV ?? process.env.UNION_DEPLOYMENT_ENV ?? process.env.NODE_ENV; const useOfflineExperiment = (experimentName, _ref) => { let { enabled } = _ref; const [variant, setVariant, isForced] = useURLQueryState(null, `experiment-${experimentName}`); const configRef = React.useRef(experimentConfigs[experimentName]); const enabledRef = useRenderRef(enabled); React.useEffect(() => { if (!enabledRef.current || isForced) { return; } if (_globalThis.analytics) { _globalThis.analytics.ready(async () => { const flipper = new Flipper({ apiKey: flipperApiKey }); const anonId = _globalThis.analytics.user().anonymousId(); const theVariant = determineExperimentBucket(configRef.current, anonId); setVariant(theVariant.variant_name); try { await flipper.reportAssignment({ experimentId: configRef.current.experiment_id, bucketingId: anonId, variantId: theVariant.variant_id }); } catch (err) { /* eslint-disable no-console */ console.warn('Error while trying to report assignment to flipper'); console.error(err); } }); } }, []); return variant; }; export default useOfflineExperiment;