fix chat session leak

This commit is contained in:
2026-04-16 08:40:38 -06:00
parent f03df0c4c6
commit d4c7b3469e

View File

@@ -2,11 +2,13 @@
import { createContext, useContext, useState, useRef, useCallback, useEffect } from "react";
import { createStompClient } from "@/lib/chatSocket";
import { useAuth } from "@/context/AuthContext";
const ChatWidgetContext = createContext(null);
const API_BASE = "";
export function ChatWidgetProvider({ children }) {
const { user } = useAuth();
const [isOpen, setIsOpen] = useState(false);
const [view, setView] = useState("ai"); // "ai" | "history" | "live"
@@ -72,6 +74,24 @@ export function ChatWidgetProvider({ children }) {
}
}, []);
const prevUserIdRef = useRef(user?.id);
useEffect(() => {
const currentId = user?.id ?? null;
const prevId = prevUserIdRef.current;
prevUserIdRef.current = currentId;
if (prevId !== null && prevId !== currentId) {
setAiMessages([]);
localStorage.removeItem("fc_aiMessages");
setConversations([]);
setActiveConvId(null);
setActiveConv(null);
setLiveMessages([]);
disconnectStomp();
setView("ai");
setIsOpen(false);
}
}, [user?.id, disconnectStomp]);
const subscribeToConversation = useCallback((client, convId) => {
client.subscribe(`/topic/chat/conversations/${convId}`, (frame) => {
try {