// — a header bell with the count of firing alerts, opening a // popover that lists them and links to Grafana. It draws nothing while no // alert fires, and while the alerts can't be read (the swarm may have no // Grafana): no button beats a dead one. It polls `GET /api/alerts`, the same // list the reports tab shows. import { useRef, useState } from "preact/hooks"; import { BellIcon } from "@hive/shared/icons.js"; import { IconButton } from "@hive/shared/icon-button.js"; import { useDismiss } from "@hive/shared/use-dismiss.js"; import "@hive/shared/icon-button.css"; import { ExternalLink } from "../ui/external-link/ExternalLink.js"; import { fetchJson, usePolled } from "../ui/polled/usePolled.js"; import { RelativeTime } from "../ui/relative-time/RelativeTime.js"; import { GRAFANA_ALERTS_URL, activeSinceMs, alertKey, alertsLabel, firingAlerts, type Alert, } from "../pages/alerts.js"; import "./AlertsMenu.css"; const POLL_MS = 30_000; export function AlertsMenu() { const { data, error } = usePolled( POLL_MS, (signal) => fetchJson("/api/alerts", signal), { cacheKey: "alerts" }, ); // A failed poll keeps the last answer in `data`, so the bell checks `error` // too rather than show a list that may have gone stale. The controller // answers 503 both for a swarm with no Grafana and for a Grafana that // didn't answer, so a swarm without one costs an error response per poll. const firing = data && !error ? firingAlerts(data) : []; return firing.length === 0 ? null : ; } function AlertsButton({ firing }: { firing: Alert[] }) { const [open, setOpen] = useState(false); const rootRef = useRef(null); useDismiss({ open, onDismiss: () => setOpen(false), inside: [rootRef], focusOnEscape: () => rootRef.current?.querySelector("button") ?? null, }); const label = alertsLabel(firing.length); return (
setOpen((v) => !v)} expanded={open} > {open ? (
    {firing.map((a) => { const since = activeSinceMs(a); return (
  • {a.name} {since !== null ? ( ) : null} {a.summary ? ( {a.summary} ) : null}
  • ); })}
open Grafana's alert list
) : null}
); }