An agent card shows its freshness (stale with the last report's age, never reported, not in swarm identity) when it is not fresh, so the status filter has a visible reason. A failed state change shows its reason in a panel under the card, and a failed cancel shows its reason in the badge, instead of a hover title. Pages can set their title (usePageTitle) and the flow editor does, so several editor tabs differ; the 404 links home. The claude login dialog offers try again after a failure, announces its wait, and has a per-agent field id. The create-agent tabs go forward again as far as the operator has been, but not past a step that is invalid now (lastReachableStep, tested), and the disabled next and create buttons say why in a hint they are described by. usePolled reports when the last live answer arrived, and StaleNote says how old a list is under a failed refresh, or that it is the answer saved from the last visit, on the alerts, builds, review runs, hives, schedules and flows lists.
172 lines
5.7 KiB
TypeScript
172 lines
5.7 KiB
TypeScript
// <HivesCard> — the hive roster, a card on the swarm page that starts folded.
|
|
// Fetches
|
|
// swarm-controller's `GET /api/hives/status`, the swarm-wide status
|
|
// aggregate: one row per roster hive, freshness derived at read time
|
|
// from the status bucket. Supersedes the earlier `GET /api/hives` +
|
|
// static "configured" chip placeholder now that the real rollup
|
|
// exists — same component, richer data, no rebuild, exactly the plan
|
|
// that placeholder's comment laid out. Its own component/file rather
|
|
// than living in `App.tsx`, matching `JobsPage`'s shape: `App.tsx` is
|
|
// routing, a page owns its own fetch + render.
|
|
//
|
|
// Polls on a `RefreshIntervalPicker` cadence rather than fetching once
|
|
// at mount — no inputs on this page, so a re-fetch clobbering an
|
|
// in-progress edit (the hook's stated caller obligation) isn't a live
|
|
// concern here.
|
|
import { useState } from "preact/hooks";
|
|
import { ApiErrorPanel } from "@hive/shared/api-error-panel.js";
|
|
import { Badge, type BadgeTone } from "@hive/shared/badge.js";
|
|
import { ExternalLink } from "../../ui/external-link/ExternalLink.js";
|
|
import { DisclosureAction } from "../../ui/disclosure/DisclosureAction.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 { domainCell } from "../hive-domain.js";
|
|
|
|
type Freshness = "fresh" | "stale" | "never_reported" | "unknown";
|
|
|
|
interface HiveStatus {
|
|
name: string;
|
|
domain: string | null;
|
|
dashboard: string | null;
|
|
freshness: Freshness;
|
|
last_seen_unix: number | null;
|
|
// `age_seconds` (a once-computed-at-fetch age) is part of the wire
|
|
// shape but deliberately unused here — rendering it directly is the
|
|
// exact bug `RelativeTime` exists to fix, so this page recomputes the
|
|
// age client-side from `last_seen_unix` instead.
|
|
}
|
|
|
|
// One tone + label per freshness value. `unknown` (reporting but absent
|
|
// from the roster) reads `negative` — it's the one case this endpoint
|
|
// cannot vouch for at all, per `status.rs`'s doc comment.
|
|
const FRESHNESS: Record<Freshness, { tone: BadgeTone; label: string }> = {
|
|
fresh: { tone: "positive", label: "fresh" },
|
|
stale: { tone: "warning", label: "stale" },
|
|
never_reported: { tone: "neutral", label: "never reported" },
|
|
unknown: { tone: "negative", label: "unknown" },
|
|
};
|
|
|
|
const COLUMNS: TableColumn<HiveStatus>[] = [
|
|
{
|
|
key: "name",
|
|
header: "name",
|
|
render: (h) => h.name,
|
|
sortBy: (h) => h.name,
|
|
filterValue: (h) => h.name,
|
|
},
|
|
{
|
|
key: "domain",
|
|
header: "domain",
|
|
render: (h) => {
|
|
const { text, href } = domainCell(h);
|
|
if (!text) return "—";
|
|
return href ? <ExternalLink href={href}>{text}</ExternalLink> : text;
|
|
},
|
|
sortBy: (h) => domainCell(h).text,
|
|
filterValue: (h) => domainCell(h).text,
|
|
},
|
|
{
|
|
key: "status",
|
|
header: "status",
|
|
render: (h) => {
|
|
const { tone, label } = FRESHNESS[h.freshness];
|
|
return (
|
|
<Badge
|
|
tone={tone}
|
|
value={
|
|
h.last_seen_unix !== null ? (
|
|
<>
|
|
{label} (<RelativeTime epochMs={h.last_seen_unix * 1000} />)
|
|
</>
|
|
) : (
|
|
label
|
|
)
|
|
}
|
|
/>
|
|
);
|
|
},
|
|
sortBy: (h) => FRESHNESS[h.freshness].label,
|
|
// Multiselect (not the single-value `"select"` mode) so the operator
|
|
// can filter for e.g. "stale or offline" in one pass rather than one
|
|
// freshness state at a time.
|
|
filterValues: (h) => [FRESHNESS[h.freshness].label],
|
|
filterMode: "multiselect",
|
|
},
|
|
];
|
|
|
|
// 30s default: this card has no inputs to interrupt, and the whole
|
|
// point of a refresh-interval control is "no manual reload needed" — an
|
|
// operator who wants it off still can, but the out-of-the-box behaviour
|
|
// should actually solve the staleness problem rather than require an
|
|
// opt-in every visit.
|
|
const DEFAULT_INTERVAL_MS: RefreshIntervalMs = 30_000;
|
|
|
|
// The roster is read on the first open and followed after, so an agents page
|
|
// where the card is never opened does not poll it.
|
|
function HiveRoster() {
|
|
const [intervalMs, setIntervalMs] =
|
|
useState<RefreshIntervalMs>(DEFAULT_INTERVAL_MS);
|
|
const {
|
|
data: hives,
|
|
error,
|
|
updatedAt,
|
|
refreshing,
|
|
} = usePolled(
|
|
intervalMs,
|
|
(signal) => fetchJson<HiveStatus[]>("/api/hives/status", signal),
|
|
{ cacheKey: "hives" },
|
|
);
|
|
|
|
return (
|
|
<>
|
|
<DisclosureAction>
|
|
<RefreshIntervalPicker
|
|
id="hives-refresh"
|
|
value={intervalMs}
|
|
onChange={setIntervalMs}
|
|
refreshing={refreshing}
|
|
/>
|
|
</DisclosureAction>
|
|
{error ? (
|
|
<ApiErrorPanel context="failed to load the hives" problem={error} />
|
|
) : null}
|
|
<StaleNote
|
|
updatedAt={updatedAt}
|
|
failed={error !== null}
|
|
hasData={hives !== null}
|
|
/>
|
|
{!error && hives === null ? (
|
|
<TableSkeleton columns={COLUMNS} label="the hives" />
|
|
) : null}
|
|
{hives ? (
|
|
<Table
|
|
columns={COLUMNS}
|
|
rows={hives}
|
|
rowKey={(h) => h.name}
|
|
emptyMessage="no hives yet"
|
|
storageKey="swarm-ui:hives:table-filters"
|
|
/>
|
|
) : null}
|
|
</>
|
|
);
|
|
}
|
|
|
|
export function HivesCard() {
|
|
return (
|
|
<LazyDisclosure
|
|
title="hives"
|
|
icon="🐝"
|
|
hint="the swarm's hives and when each last reported"
|
|
>
|
|
<HiveRoster />
|
|
</LazyDisclosure>
|
|
);
|
|
}
|