From 3c4ec9cac9a5afe850054225e57cd4148ecbb055 Mon Sep 17 00:00:00 2001 From: Harkamal Randhawa Date: Thu, 16 Apr 2026 08:40:38 -0600 Subject: [PATCH] fix chat session leak --- web/context/ChatWidgetContext.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/web/context/ChatWidgetContext.js b/web/context/ChatWidgetContext.js index 5eb72f45..02b7135a 100644 --- a/web/context/ChatWidgetContext.js +++ b/web/context/ChatWidgetContext.js @@ -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 {