import { useEffect, useRef } from 'react'; import { AlertWebSocket } from '@/services/alerts'; import { useAlertStore } from '@/stores/alertStore'; import { useConfigStore } from '@/stores/configStore'; export function useAlerts() { const wsRef = useRef(null); const config = useConfigStore((s) => s.config); const { showAlert, isSuppressed, activeAlert } = useAlertStore(); useEffect(() => { if (!config?.alerts.enabled) return; const ws = new AlertWebSocket(); wsRef.current = ws; const unsub = ws.subscribe((event) => { // Don't show if there's already an active alert if (useAlertStore.getState().activeAlert) return; // Check suppression if (isSuppressed(event.camera, config.alerts.suppression_seconds)) return; showAlert(event); }); ws.connect(); return () => { unsub(); ws.disconnect(); wsRef.current = null; }; }, [config?.alerts.enabled]); }