"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({ active: 0, critical: 0 }); export function AlarmProvider({ children }: { children: React.ReactNode }) { const [counts, setCounts] = useState({ 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 {children}; } export function useAlarmCount() { return useContext(AlarmContext); }