Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/frontend/packages/swarm-ui/src/pages/AlertsPage.tsx
iris 25965341e0 frontend: one tone vocabulary for badges, hints, banners and toasts
Part of the unification order in the UI sweep issue (step b).

A badge named its tones positive/warning/negative, a hint muted/warn/error/ok,
a banner info/warning/error and a toast info/ok/error. They now all take the
shared Tone: neutral, info, ok, warn, error (a badge adds accent for a selected
choice). Warn is amber everywhere (a hint used yellow), info is cyan everywhere
(a toast used purple). A test pins the list against each component's CSS.
2026-11-25 21:47:01 +01:00

155 lines
4.3 KiB
TypeScript

// <AlertsCard> — a card of the reports tab: the alerts the swarm's Grafana holds
// now, from swarm-controller's `GET /api/alerts`. Only the firing ones show at
// first; a button in the header adds the pending, no-data and errored ones.
// Same shape as `HivesCard`: one fetch on a `RefreshIntervalPicker` cadence,
// rendered as a `Table`.
import { useState } from "preact/hooks";
import { ApiErrorPanel } from "@hive/shared/api-error-panel.js";
import { Badge, type BadgeTone } from "@hive/shared/badge.js";
import { Button } from "../ui/button/Button.js";
import { ExternalLink } from "../ui/external-link/ExternalLink.js";
import { LazyDisclosure } from "../ui/disclosure/LazyDisclosure.js";
import { RelativeTime } from "../ui/relative-time/RelativeTime.js";
import {
RefreshIntervalPicker,
type RefreshIntervalMs,
} from "../ui/refresh-interval/RefreshInterval.js";
import { fetchJson, usePolled } from "../ui/polled/usePolled.js";
import { StaleNote } from "../ui/polled/StaleNote.js";
import { Table, type TableColumn } from "../ui/table/Table.js";
import { TableSkeleton } from "../ui/table/TableSkeleton.js";
import { Toolbar } from "../ui/toolbar/Toolbar.js";
import {
GRAFANA_ALERTS_URL,
activeSinceMs,
alertKey,
listedAlerts,
type Alert,
} from "./alerts.js";
const STATE_TONE: Record<string, BadgeTone> = {
Alerting: "error",
Error: "error",
NoData: "warn",
Pending: "warn",
};
const COLUMNS: TableColumn<Alert>[] = [
{
key: "name",
header: "alert",
render: (a) => a.name,
sortBy: (a) => a.name,
filterValue: (a) => a.name,
},
{
key: "state",
header: "state",
render: (a) => {
const since = activeSinceMs(a);
return (
<Badge
tone={STATE_TONE[a.state] ?? "neutral"}
value={
since !== null ? (
<>
{a.state} (<RelativeTime epochMs={since} />)
</>
) : (
a.state
)
}
/>
);
},
sortBy: (a) => a.state,
filterValues: (a) => [a.state],
filterMode: "multiselect",
},
{
key: "summary",
header: "summary",
render: (a) => a.summary ?? "—",
sortBy: (a) => a.summary ?? "",
filterValue: (a) => a.summary ?? "",
},
];
const DEFAULT_INTERVAL_MS: RefreshIntervalMs = 30_000;
function AlertsBody() {
const [showAll, setShowAll] = useState(false);
const [intervalMs, setIntervalMs] =
useState<RefreshIntervalMs>(DEFAULT_INTERVAL_MS);
const {
data: alerts,
error,
updatedAt,
refreshing,
} = usePolled(
intervalMs,
(signal) => fetchJson<Alert[]>("/api/alerts", signal),
{ cacheKey: "alerts" },
);
const listed = alerts ? listedAlerts(alerts, showAll) : null;
const hidden = alerts && listed ? alerts.length - listed.length : 0;
return (
<>
<Toolbar>
<ExternalLink href={GRAFANA_ALERTS_URL}>
Grafana's alert list
</ExternalLink>
{showAll || hidden > 0 ? (
<Button onClick={() => setShowAll(!showAll)}>
{showAll ? "only firing" : `show ${hidden} more`}
</Button>
) : null}
<RefreshIntervalPicker
id="alerts-refresh"
value={intervalMs}
onChange={setIntervalMs}
refreshing={refreshing}
/>
</Toolbar>
{error ? (
<ApiErrorPanel
context="failed to load Grafana's alerts"
problem={error}
/>
) : null}
<StaleNote
updatedAt={updatedAt}
failed={error !== null}
hasData={alerts !== null}
/>
{!error && alerts === null ? (
<TableSkeleton columns={COLUMNS} label="the alerts" />
) : null}
{listed && listed.length === 0 ? (
<p>{showAll ? "no alerts." : "nothing is alerting."}</p>
) : null}
{listed && listed.length > 0 ? (
<Table
columns={COLUMNS}
rows={listed}
rowKey={alertKey}
storageKey="swarm-ui:alerts:table-filters"
/>
) : null}
</>
);
}
// A folded card on the reports tab: read and polled only once it is opened.
export function AlertsCard() {
return (
<LazyDisclosure
title="alerts"
icon="🚨"
hint="what the swarm's Grafana holds now"
>
<AlertsBody />
</LazyDisclosure>
);
}