diff --git a/src/app/[funnelId]/layout.tsx b/src/app/[funnelId]/layout.tsx
index e01bf34..67254e4 100644
--- a/src/app/[funnelId]/layout.tsx
+++ b/src/app/[funnelId]/layout.tsx
@@ -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 (
-
+
-
+
);
}
diff --git a/src/lib/funnel/unleash/UnleashProvider.tsx b/src/lib/funnel/unleash/UnleashProvider.tsx
index 2d02e45..c000eb9 100644
--- a/src/lib/funnel/unleash/UnleashProvider.tsx
+++ b/src/lib/funnel/unleash/UnleashProvider.tsx
@@ -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 {children};
+ return (
+
+ {children}
+
+ );
}
diff --git a/src/lib/funnel/unleash/UnleashSessionProvider.tsx b/src/lib/funnel/unleash/UnleashSessionProvider.tsx
new file mode 100644
index 0000000..5f63eb1
--- /dev/null
+++ b/src/lib/funnel/unleash/UnleashSessionProvider.tsx
@@ -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(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 (
+
+ {children}
+
+ );
+}
diff --git a/src/lib/funnel/unleash/index.ts b/src/lib/funnel/unleash/index.ts
index 863b45c..4995a0e 100644
--- a/src/lib/funnel/unleash/index.ts
+++ b/src/lib/funnel/unleash/index.ts
@@ -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";