"use client"; import { useEffect, useLayoutEffect, useRef } from "react"; import type { TicketEvent } from "@/lib/events/bus"; /** Subscribes to a ticket-events SSE endpoint for the lifetime of the component. */ export function useTicketEvents(url: string, onEvent: (event: TicketEvent) => void) { const handlerRef = useRef(onEvent); useLayoutEffect(() => { handlerRef.current = onEvent; }); useEffect(() => { const source = new EventSource(url); source.onmessage = (e) => { try { handlerRef.current(JSON.parse(e.data) as TicketEvent); } catch { // malformed payload — ignore rather than crash the subscription } }; return () => source.close(); }, [url]); }