commit
f7eaee0f9c
@ -1,7 +1,7 @@
|
||||
import type { ReactNode } from "react";
|
||||
import { notFound } from "next/navigation";
|
||||
import { PixelsProvider } from "@/components/providers/PixelsProvider";
|
||||
import { UnleashProvider } from "@/lib/funnel/unleash";
|
||||
import { UnleashSessionProvider } from "@/lib/funnel/unleash";
|
||||
import type { FunnelDefinition } from "@/lib/funnel/types";
|
||||
import { BAKED_FUNNELS } from "@/lib/funnel/bakedFunnels";
|
||||
import { IS_FULL_SYSTEM_BUILD } from "@/lib/runtime/buildVariant";
|
||||
@ -72,7 +72,7 @@ export default async function FunnelLayout({
|
||||
}
|
||||
|
||||
return (
|
||||
<UnleashProvider>
|
||||
<UnleashSessionProvider funnelId={funnelId}>
|
||||
<PixelsProvider
|
||||
googleAnalyticsId={funnel.meta.googleAnalyticsId}
|
||||
yandexMetrikaId={funnel.meta.yandexMetrikaId}
|
||||
@ -83,6 +83,6 @@ export default async function FunnelLayout({
|
||||
</TrialVariantSelectionProvider>
|
||||
</PaymentPlacementProvider>
|
||||
</PixelsProvider>
|
||||
</UnleashProvider>
|
||||
</UnleashSessionProvider>
|
||||
);
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { FlagProvider } from "@unleash/proxy-client-react";
|
||||
import type { ReactNode } from "react";
|
||||
import { type ReactNode, useMemo } from "react";
|
||||
|
||||
interface UnleashProviderProps {
|
||||
children: ReactNode;
|
||||
@ -22,6 +22,19 @@ export function UnleashProvider({
|
||||
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") {
|
||||
@ -32,17 +45,11 @@ export function UnleashProvider({
|
||||
return <>{children}</>;
|
||||
}
|
||||
|
||||
const config = {
|
||||
url: unleashUrl,
|
||||
clientKey: unleashClientKey,
|
||||
refreshInterval: 15,
|
||||
appName: "witlab-funnel",
|
||||
context: {
|
||||
userId: userId,
|
||||
sessionId: sessionId || userId || "anonymous",
|
||||
properties: {},
|
||||
},
|
||||
};
|
||||
console.log("[UnleashProvider] Initializing with sessionId:", sessionId || userId || "anonymous");
|
||||
|
||||
return <FlagProvider config={config}>{children}</FlagProvider>;
|
||||
return (
|
||||
<FlagProvider config={config}>
|
||||
{children}
|
||||
</FlagProvider>
|
||||
);
|
||||
}
|
||||
|
||||
55
src/lib/funnel/unleash/UnleashSessionProvider.tsx
Normal file
55
src/lib/funnel/unleash/UnleashSessionProvider.tsx
Normal file
@ -0,0 +1,55 @@
|
||||
"use client";
|
||||
|
||||
import { type ReactNode, useEffect, useState } from "react";
|
||||
import { UnleashProvider } from "./UnleashProvider";
|
||||
import { useSession } from "@/hooks/session/useSession";
|
||||
|
||||
interface UnleashSessionProviderProps {
|
||||
children: ReactNode;
|
||||
funnelId: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Создает сессию и инициализирует Unleash с sessionId
|
||||
*/
|
||||
export function UnleashSessionProvider({
|
||||
children,
|
||||
funnelId,
|
||||
}: UnleashSessionProviderProps) {
|
||||
const [sessionId, setSessionId] = useState<string | null>(null);
|
||||
const [isReady, setIsReady] = useState(false);
|
||||
const { createSession } = useSession({ funnelId });
|
||||
|
||||
useEffect(() => {
|
||||
const initSession = async () => {
|
||||
const localStorageKey = `${funnelId}_sessionId`;
|
||||
let sid = localStorage.getItem(localStorageKey);
|
||||
|
||||
if (!sid) {
|
||||
console.log(`[UnleashSessionProvider] Creating new session...`);
|
||||
const result = await createSession();
|
||||
sid = result.sessionId;
|
||||
}
|
||||
|
||||
if (sid) {
|
||||
console.log(`[UnleashSessionProvider] Using sessionId: ${sid}`);
|
||||
setSessionId(sid);
|
||||
}
|
||||
|
||||
setIsReady(true);
|
||||
};
|
||||
|
||||
initSession();
|
||||
}, [funnelId, createSession]);
|
||||
|
||||
// Показываем loading пока создается сессия
|
||||
if (!isReady || !sessionId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<UnleashProvider sessionId={sessionId}>
|
||||
{children}
|
||||
</UnleashProvider>
|
||||
);
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
export { UnleashProvider } from "./UnleashProvider";
|
||||
export { UnleashSessionProvider } from "./UnleashSessionProvider";
|
||||
export { UnleashContextProvider, useUnleashContext } from "./UnleashContext";
|
||||
export { useUnleash, checkUnleashVariant } from "./useUnleash";
|
||||
export { sendUnleashImpression, clearUnleashImpressions } from "./sendImpression";
|
||||
|
||||
Loading…
Reference in New Issue
Block a user