fix chat session leak
This commit is contained in:
@@ -2,11 +2,13 @@
|
|||||||
|
|
||||||
import { createContext, useContext, useState, useRef, useCallback, useEffect } from "react";
|
import { createContext, useContext, useState, useRef, useCallback, useEffect } from "react";
|
||||||
import { createStompClient } from "@/lib/chatSocket";
|
import { createStompClient } from "@/lib/chatSocket";
|
||||||
|
import { useAuth } from "@/context/AuthContext";
|
||||||
|
|
||||||
const ChatWidgetContext = createContext(null);
|
const ChatWidgetContext = createContext(null);
|
||||||
const API_BASE = "";
|
const API_BASE = "";
|
||||||
|
|
||||||
export function ChatWidgetProvider({ children }) {
|
export function ChatWidgetProvider({ children }) {
|
||||||
|
const { user } = useAuth();
|
||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
const [view, setView] = useState("ai"); // "ai" | "history" | "live"
|
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) => {
|
const subscribeToConversation = useCallback((client, convId) => {
|
||||||
client.subscribe(`/topic/chat/conversations/${convId}`, (frame) => {
|
client.subscribe(`/topic/chat/conversations/${convId}`, (frame) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user