first commit
This commit is contained in:
commit
4b98219bf7
144 changed files with 31561 additions and 0 deletions
34
frontend/lib/alarm-context.tsx
Normal file
34
frontend/lib/alarm-context.tsx
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
"use client";
|
||||
|
||||
import { createContext, useContext, useEffect, useState, useCallback } from "react";
|
||||
import { fetchAlarmStats, type AlarmStats } from "@/lib/api";
|
||||
|
||||
const SITE_ID = "sg-01";
|
||||
|
||||
type AlarmContextValue = { active: number; critical: number };
|
||||
const AlarmContext = createContext<AlarmContextValue>({ active: 0, critical: 0 });
|
||||
|
||||
export function AlarmProvider({ children }: { children: React.ReactNode }) {
|
||||
const [counts, setCounts] = useState<AlarmContextValue>({ active: 0, critical: 0 });
|
||||
|
||||
const poll = useCallback(async () => {
|
||||
try {
|
||||
const s: AlarmStats = await fetchAlarmStats(SITE_ID);
|
||||
setCounts({ active: s.active, critical: s.critical });
|
||||
} catch {
|
||||
// keep previous value
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
poll();
|
||||
const id = setInterval(poll, 15_000);
|
||||
return () => clearInterval(id);
|
||||
}, [poll]);
|
||||
|
||||
return <AlarmContext.Provider value={counts}>{children}</AlarmContext.Provider>;
|
||||
}
|
||||
|
||||
export function useAlarmCount() {
|
||||
return useContext(AlarmContext);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue