Watch
0
0
Fork
You've already forked hyperhive
0

frontend: one time wording for the swarm UI and the dashboard, and no HTML strings in the dashboard's busy buttons

fmtAgo, fmtUntil and durationLabel move to @hive/shared/time.js. The swarm UI's util.ts is deleted and its importers (RelativeTime, ReviewRuns, BuildsPage) read the shared module directly. The dashboard's own copies are gone: its fmtAgeSecs (a third phrasing, used for two status lines) becomes the shared fmtAgo, and the dashboard util.js lost fmtAgo, fmtElapsed, epochSec and truncate, which nothing called. asyncBtn and the bulk action buttons mark themselves busy with markBusy: a spinner node before the label and aria-busy, in place of replacing the button's innerHTML with a string (and the label, so the button changed width and lost its name). The el() helper loses its html attribute.
This commit is contained in:
iris 2026-11-19 19:21:15 +01:00
commit 3be4ec069c
6 changed files with 84 additions and 96 deletions

View file

@ -25,7 +25,7 @@ import {
import { fetchJson, usePolled } from "../../ui/polled/usePolled.js";
import { StaleNote } from "../../ui/polled/StaleNote.js";
import { RelativeTime } from "../../ui/relative-time/RelativeTime.js";
import { durationLabel } from "../../util.js";
import { durationLabel } from "@hive/shared/time.js";
import {
OUTCOME_LABEL,
OUTCOME_TONE,

View file

@ -23,7 +23,7 @@ import { fetchJson, usePolled } from "../../ui/polled/usePolled.js";
import { StaleNote } from "../../ui/polled/StaleNote.js";
import { RelativeTime } from "../../ui/relative-time/RelativeTime.js";
import { TryOutResult } from "../review-common/TryOutResult.js";
import { durationLabel } from "../../util.js";
import { durationLabel } from "@hive/shared/time.js";
import {
OUTCOME_LABEL,
OUTCOME_TONE,

View file

@ -9,7 +9,7 @@
// package's matrix-rain effect — and resyncs immediately on becoming
// visible again instead of waiting out the rest of a stale interval.
import { useEffect, useState } from "preact/hooks";
import { fmtAgo, fmtUntil } from "../../util.js";
import { fmtAgo, fmtUntil } from "@hive/shared/time.js";
// Cheap at the handful of rows/chips this renders for today, and coarse
// enough to be correct without visibly ticking every frame — the same

View file

@ -1,43 +0,0 @@
// Small pure render helpers shared across swarm-ui pages. Not folded
// into `@hive/shared` — swarm-ui's formatting choices aren't a
// cross-package contract, and there's nothing here worth sharing.
// Relative age, coarsened to one unit ("5m ago"). Built on the native
// `Intl.RelativeTimeFormat` (no dependency needed — mara asked whether
// a lightweight package was warranted; the runtime already ships this)
// with `style: 'narrow'`, which produces the same "5s/5m/5h/5d ago"
// shape the dashboard package's hand-rolled equivalent does, verified
// against a real `Intl` call rather than assumed from the spec.
const RTF = new Intl.RelativeTimeFormat("en", {
numeric: "always",
style: "narrow",
});
// The same coarsening for an instant still ahead ("in 5m"). A separate
// function rather than a negative `fmtAgo`: that one clamps to zero so a
// clock a little ahead of the server never reads as "-3s ago".
export function fmtUntil(secondsAhead: number): string {
const ahead = Math.max(0, Math.floor(secondsAhead));
if (ahead < 60) return RTF.format(ahead, "second");
if (ahead < 3600) return RTF.format(Math.floor(ahead / 60), "minute");
if (ahead < 86400) return RTF.format(Math.floor(ahead / 3600), "hour");
return RTF.format(Math.floor(ahead / 86400), "day");
}
export function fmtAgo(ageSeconds: number): string {
const age = Math.max(0, Math.floor(ageSeconds));
if (age < 60) return RTF.format(-age, "second");
if (age < 3600) return RTF.format(-Math.floor(age / 60), "minute");
if (age < 86400) return RTF.format(-Math.floor(age / 3600), "hour");
return RTF.format(-Math.floor(age / 86400), "day");
}
/** How long a run took, as `45s`, `3m 12s` or `1h 4m`. A clock that ran
* backwards reads as `0s`. */
export function durationLabel(startedUnix: number, finishedUnix: number) {
const total = Math.max(0, Math.round(finishedUnix - startedUnix));
if (total < 60) return `${total}s`;
const minutes = Math.floor(total / 60);
if (minutes < 60) return `${minutes}m ${total % 60}s`;
return `${Math.floor(minutes / 60)}h ${minutes % 60}m`;
}

View file

@ -2,7 +2,7 @@
// paths one terminal is reached at.
import assert from "node:assert/strict";
import { test } from "node:test";
import { durationLabel } from "../src/util.ts";
import { durationLabel } from "../../shared/src/time.ts";
import { termPaths } from "../src/agent/terminal/term-paths.ts";
test("durationLabel names seconds, minutes and hours", () => {

View file

@ -0,0 +1,32 @@
// The relative-time wording both UIs share: one unit, clamped at zero, and the
// forward form. Run by `npm test`.
import assert from "node:assert/strict";
import { test } from "node:test";
import { fmtAgo, fmtUntil } from "../../shared/src/time.ts";
test("an age reads in its largest whole unit", () => {
assert.equal(fmtAgo(5), "5s ago");
assert.equal(fmtAgo(59), "59s ago");
assert.equal(fmtAgo(60), "1m ago");
assert.equal(fmtAgo(3599), "59m ago");
assert.equal(fmtAgo(3600), "1h ago");
assert.equal(fmtAgo(86399), "23h ago");
assert.equal(fmtAgo(86400), "1d ago");
assert.equal(fmtAgo(3 * 86400 + 5), "3d ago");
});
test("a clock a little ahead never reads as a negative age", () => {
assert.equal(fmtAgo(-3), "0s ago");
});
test("a fractional age is floored", () => {
assert.equal(fmtAgo(61.9), "1m ago");
});
test("an instant ahead reads in the same units, forward", () => {
assert.equal(fmtUntil(5), "in 5s");
assert.equal(fmtUntil(300), "in 5m");
assert.equal(fmtUntil(7200), "in 2h");
assert.equal(fmtUntil(2 * 86400), "in 2d");
assert.equal(fmtUntil(-4), "in 0s");
});