diff --git a/frontend/src/hooks/useSSE.ts b/frontend/src/hooks/useSSE.ts index e98cfb19..6a08e023 100644 --- a/frontend/src/hooks/useSSE.ts +++ b/frontend/src/hooks/useSSE.ts @@ -8,6 +8,7 @@ export const useSSE = (opcodeUrl: string | null | undefined, directory?: string) const queryClient = useQueryClient() const eventSourceRef = useRef(null) const urlRef = useRef(null) + const reconnectTimeoutRef = useRef | null>(null) const [isConnected, setIsConnected] = useState(false) const [error, setError] = useState(null) @@ -35,6 +36,16 @@ export const useSSE = (opcodeUrl: string | null | undefined, directory?: string) urlRef.current = eventSourceUrl const connectSSE = () => { + if (reconnectTimeoutRef.current) { + clearTimeout(reconnectTimeoutRef.current) + reconnectTimeoutRef.current = null + } + + if (eventSourceRef.current) { + eventSourceRef.current.close() + eventSourceRef.current = null + } + try { const eventSource = new EventSource(eventSourceUrl) eventSourceRef.current = eventSource @@ -42,6 +53,8 @@ export const useSSE = (opcodeUrl: string | null | undefined, directory?: string) eventSource.onopen = () => { setIsConnected(true) setError(null) + queryClient.invalidateQueries({ queryKey: ['opencode', 'sessions', opcodeUrl, directory] }) + queryClient.invalidateQueries({ queryKey: ['opencode', 'messages', opcodeUrl] }) } eventSource.onerror = (e) => { @@ -69,6 +82,12 @@ export const useSSE = (opcodeUrl: string | null | undefined, directory?: string) } } + const handleReconnect = () => { + if (!eventSourceRef.current || eventSourceRef.current.readyState === EventSource.CLOSED) { + connectSSE() + } + } + const handleSSEEvent = (event: SSEEvent) => { switch (event.type) { case 'session.updated': @@ -230,7 +249,16 @@ case 'message.updated': connectSSE() + window.addEventListener('focus', handleReconnect) + window.addEventListener('online', handleReconnect) + return () => { + window.removeEventListener('focus', handleReconnect) + window.removeEventListener('online', handleReconnect) + if (reconnectTimeoutRef.current) { + clearTimeout(reconnectTimeoutRef.current) + reconnectTimeoutRef.current = null + } if (eventSourceRef.current) { eventSourceRef.current.close() eventSourceRef.current = null