Initial scaffold: React+TS+Vite frontend, FastAPI backend, config system
This commit is contained in:
35
frontend/src/hooks/useAlerts.ts
Normal file
35
frontend/src/hooks/useAlerts.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
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<AlertWebSocket | null>(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]);
|
||||
}
|
||||
Reference in New Issue
Block a user