w-funnel/src/lib/funnel/unleash/UnleashSessionProvider.tsx
dev.daminik00 a75ac7e8c8 session
2025-12-24 02:06:11 +03:00

56 lines
1.5 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 { type ReactNode, useEffect, useState, useRef } from "react";
import { UnleashProvider } from "./UnleashProvider";
import { useSession } from "@/hooks/session/useSession";
interface UnleashSessionProviderProps {
children: ReactNode;
funnelId: string;
googleAnalyticsId?: string;
}
/**
* Создает сессию и инициализирует Unleash с sessionId
*/
export function UnleashSessionProvider({
children,
funnelId,
googleAnalyticsId,
}: UnleashSessionProviderProps) {
const [sessionId, setSessionId] = useState<string | null>(null);
const [isReady, setIsReady] = useState(false);
const { createSession } = useSession({ funnelId, googleAnalyticsId });
const initCalledRef = useRef(false);
useEffect(() => {
// Prevent multiple initializations
if (initCalledRef.current) return;
initCalledRef.current = true;
const initSession = async () => {
console.log(`[UnleashSessionProvider] Initializing session...`);
const result = await createSession();
if (result.sessionId) {
console.log(`[UnleashSessionProvider] Using sessionId: ${result.sessionId}`);
setSessionId(result.sessionId);
}
setIsReady(true);
};
initSession();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [funnelId]);
// Показываем loading пока создается сессия
if (!isReady || !sessionId) {
return null;
}
return (
<UnleashProvider sessionId={sessionId}>
{children}
</UnleashProvider>
);
}