mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-21 15:55:50 +00:00
26 lines
601 B
TypeScript
26 lines
601 B
TypeScript
import { useEffect, useRef, useCallback } from 'react'
|
|
import { connectSSE, disconnectSSE } from '../api/sse'
|
|
import type { SessionEvent } from '../types'
|
|
|
|
export function useSSE(
|
|
sessionId: string | null,
|
|
onEvent: (event: SessionEvent) => void,
|
|
) {
|
|
const onEventRef = useRef(onEvent)
|
|
onEventRef.current = onEvent
|
|
|
|
const stableCallback = useCallback((event: SessionEvent) => {
|
|
onEventRef.current(event)
|
|
}, [])
|
|
|
|
useEffect(() => {
|
|
if (!sessionId) return
|
|
|
|
connectSSE(sessionId, stableCallback)
|
|
|
|
return () => {
|
|
disconnectSSE()
|
|
}
|
|
}, [sessionId, stableCallback])
|
|
}
|