fix: WS reconnect leak, refetch on reconnect, error handling

- intentionalClose ref prevents onclose from reconnecting after cleanup
- refetch full history on WS reconnect (catches missed messages)
- onerror handler, try/catch on JSON.parse
- fixes codex review: orphaned sockets, stale closures, missing messages

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-29 20:48:23 +02:00
parent 407ac504b8
commit 9e375fd953
2 changed files with 32 additions and 10 deletions

View File

@@ -42,13 +42,17 @@ export default function App() {
// WebSocket: append new messages in real-time
const handleWsMessage = useCallback((msg: Message) => {
setMessages((prev) => {
// Deduplicate — the sender also fetches after posting
if (prev.some((m) => m.id === msg.id)) return prev;
return [...prev, msg];
});
}, []);
useChannelSocket(activeChannelId, handleWsMessage);
// On WS reconnect, refetch full history to catch missed messages
const handleWsReconnect = useCallback(() => {
loadMessages();
}, [loadMessages]);
useChannelSocket(activeChannelId, handleWsMessage, handleWsReconnect);
useEffect(() => { loadChannels(); }, [loadChannels]);