Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/frontend/packages/swarm-ui/src/shell/AlertsMenu.tsx
iris 6f73bf0c55 frontend: one header menu for the links, user, settings and alerts popovers
Part of the unification order in the UI sweep issue (step c).

The four header menus each kept their own open state, dismiss hook and a copy
of the popover CSS, and the copies had drifted (offset, padding, fade-in). A
shared HeaderMenu now owns the state, the dismiss rules and the popover look;
each menu supplies its button and its rows. The settings popover moves 0.15em
lower and the settings and alerts popovers gain the fade-in.
2026-11-25 22:25:40 +01:00

76 lines
2.5 KiB
TypeScript

// <AlertsMenu> — 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 { BellIcon } from "@hive/shared/icons.js";
import { HeaderMenu } from "@hive/shared/header-menu.js";
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<Alert[]>("/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 : <AlertsButton firing={firing} />;
}
function AlertsButton({ firing }: { firing: Alert[] }) {
const label = alertsLabel(firing.length);
return (
<HeaderMenu
class="alerts-menu"
label={label}
popoverClass="header-menu-wide"
trigger={
<>
<BellIcon />
<span class="alerts-menu-count" aria-hidden="true">
{firing.length}
</span>
</>
}
>
<ul class="alerts-menu-list">
{firing.map((a) => {
const since = activeSinceMs(a);
return (
<li key={alertKey(a)} class="alerts-menu-item">
<span class="alerts-menu-name">{a.name}</span>
{since !== null ? (
<span class="alerts-menu-since">
<RelativeTime epochMs={since} />
</span>
) : null}
{a.summary ? (
<span class="alerts-menu-summary">{a.summary}</span>
) : null}
</li>
);
})}
</ul>
<ExternalLink href={GRAFANA_ALERTS_URL} class="header-menu-item">
open Grafana's alert list
</ExternalLink>
</HeaderMenu>
);
}