w-funnel/src/lib/funnel/unleash/UnleashProvider.tsx
dev.daminik00 ea381ea399 ab test
2025-10-30 01:56:59 +01:00

54 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client";
import { FlagProvider } from "@unleash/proxy-client-react";
import { type ReactNode, useMemo } from "react";
interface UnleashProviderProps {
children: ReactNode;
userId?: string;
sessionId?: string;
}
/**
* Unleash Provider для воронок
* Инициализирует Unleash клиент с настройками для AB тестирования
*/
export function UnleashProvider({
children,
userId,
sessionId,
}: UnleashProviderProps) {
// Проверяем наличие необходимых env переменных
const unleashUrl = process.env.NEXT_PUBLIC_UNLEASH_URL;
const unleashClientKey = process.env.NEXT_PUBLIC_UNLEASH_CLIENT_KEY;
// Создаем config с useMemo ДО условного return
const config = useMemo(() => ({
url: unleashUrl || "",
clientKey: unleashClientKey || "",
refreshInterval: 15,
appName: "witlab-funnel",
context: {
userId: userId,
sessionId: sessionId || userId || "anonymous",
properties: {},
},
}), [unleashUrl, unleashClientKey, userId, sessionId]);
// Если настройки не заданы, просто возвращаем children без Unleash
if (!unleashUrl || !unleashClientKey) {
if (process.env.NODE_ENV === "development") {
console.warn(
"[Unleash] Missing configuration. Set NEXT_PUBLIC_UNLEASH_URL and NEXT_PUBLIC_UNLEASH_CLIENT_KEY to enable AB testing."
);
}
return <>{children}</>;
}
return (
<FlagProvider config={config}>
{children}
</FlagProvider>
);
}