Skip to content

Commit 56f5b5e

Browse files
Merge pull request #225 from aibtcdev/staging
Staging
2 parents db9f9ff + aec2b47 commit 56f5b5e

File tree

1 file changed

+12
-31
lines changed

1 file changed

+12
-31
lines changed

src/components/chat/chat-window.tsx

+12-31
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export function ChatWindow() {
2323
selectedAgentId,
2424
setSelectedAgent,
2525
connect,
26-
disconnect,
2726
activeThreadId,
2827
} = useChatStore();
2928

@@ -32,52 +31,34 @@ export function ChatWindow() {
3231

3332
const memoizedConnect = useCallback(
3433
(token: string) => {
35-
console.log("Attempting to connect...");
36-
connect(token);
34+
if (!isConnected && token) {
35+
console.log("Attempting to connect...");
36+
connect(token);
37+
}
3738
},
38-
[connect]
39+
[connect, isConnected]
3940
);
4041

41-
const memoizedDisconnect = useCallback(() => {
42-
console.log("Disconnecting...");
43-
disconnect();
44-
}, [disconnect]);
45-
4642
useEffect(() => {
4743
if (!accessToken) return;
4844

49-
const connectWithDelay = () => {
50-
if (process.env.NODE_ENV === "development") {
51-
setTimeout(() => {
52-
memoizedConnect(accessToken);
53-
}, 100);
54-
} else {
45+
const handleVisibilityChange = () => {
46+
if (document.visibilityState === "visible" && !isConnected) {
5547
memoizedConnect(accessToken);
5648
}
5749
};
5850

59-
const handleVisibilityChange = () => {
60-
if (document.visibilityState === "visible") {
61-
// console.log("Page is now visible. Checking connection...");
62-
if (!isConnected) {
63-
// console.log("Not connected. Attempting to reconnect...");
64-
connectWithDelay();
65-
} else {
66-
// console.log("Already connected. No action needed.");
67-
}
68-
}
69-
};
51+
// Initial connection
52+
memoizedConnect(accessToken);
7053

71-
connectWithDelay();
54+
// Add visibility change listener
7255
document.addEventListener("visibilitychange", handleVisibilityChange);
7356

57+
// Cleanup
7458
return () => {
7559
document.removeEventListener("visibilitychange", handleVisibilityChange);
76-
if (process.env.NODE_ENV !== "development") {
77-
memoizedDisconnect();
78-
}
7960
};
80-
}, [accessToken, memoizedConnect, memoizedDisconnect, isConnected]);
61+
}, [accessToken, memoizedConnect, isConnected]);
8162

8263
if (!accessToken) {
8364
return (

0 commit comments

Comments
 (0)