import { alertInfo } from "@/lib/alerts" import { $alerts, $allSystemsById } from "@/lib/stores" import type { AlertRecord } from "@/types" import { Plural, Trans } from "@lingui/react/macro" import { useStore } from "@nanostores/react" import { getPagePath } from "@nanostores/router" import { useMemo } from "react" import { $router, Link } from "./router" import { Alert, AlertTitle, AlertDescription } from "./ui/alert" import { Card, CardHeader, CardTitle, CardContent } from "./ui/card" export const ActiveAlerts = () => { const alerts = useStore($alerts) const systems = useStore($allSystemsById) const { activeAlerts, alertsKey } = useMemo(() => { const activeAlerts: AlertRecord[] = [] // key to prevent re-rendering if alerts change but active alerts didn't const alertsKey: string[] = [] for (const systemId of Object.keys(alerts)) { for (const alert of alerts[systemId].values()) { if (alert.triggered && alert.name in alertInfo) { activeAlerts.push(alert) alertsKey.push(`${alert.system}${alert.value}${alert.min}`) } } } return { activeAlerts, alertsKey } }, [alerts]) // biome-ignore lint/correctness/useExhaustiveDependencies: alertsKey is inclusive return useMemo(() => { if (activeAlerts.length === 0) { return null } return (
Active Alerts
{activeAlerts.length > 0 && (
{activeAlerts.map((alert) => { const info = alertInfo[alert.name as keyof typeof alertInfo] return ( {systems[alert.system]?.name} {info.name().toLowerCase().replace("cpu", "CPU")} {alert.name === "Status" ? ( Connection is down ) : ( Exceeds {alert.value} {info.unit} in last )} ) })}
)}
) }, [alertsKey.join("")]) }