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:
parent
16a5bff2b1
commit
3be4ec069c
4 changed files with 84 additions and 96 deletions
|
|
@ -25,6 +25,7 @@
|
|||
"./forms.js": "./src/forms.js",
|
||||
"./prefs.js": "./src/prefs.ts",
|
||||
"./use-dismiss.js": "./src/use-dismiss.ts",
|
||||
"./time.js": "./src/time.ts",
|
||||
"./dom.js": "./src/dom.js",
|
||||
"./modal.js": "./src/modal.js",
|
||||
"./shadow-css.js": "./src/shadow-css.js",
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
// Tiny DOM-builder helper shared by the dashboard and the per-agent UI —
|
||||
// both packages built their own copy independently; this is the merged
|
||||
// canonical version (dashboard's, which had the extra `data-` branch;
|
||||
// functionally identical to the `class`/`html` handling either package
|
||||
// used).
|
||||
// Tiny DOM-builder helper shared by the dashboard and the per-agent UI. There
|
||||
// is no `html` attribute on purpose: content is built from nodes and text, so a
|
||||
// string from the API can never become markup.
|
||||
//
|
||||
// `el(tag, attrs, ...children)` creates an element, applying `attrs` as
|
||||
// either the `class`/`html` special cases or plain attributes, and
|
||||
// either the `class` special case or plain attributes, and
|
||||
// appending `children` (strings become text nodes, `null`/`undefined`
|
||||
// entries are skipped so callers can inline conditional children).
|
||||
// `tag` can be any custom element's tag name too (e.g. `el('hive-btn',
|
||||
|
|
@ -15,7 +13,6 @@ export const el = (tag, attrs = {}, ...children) => {
|
|||
const e = document.createElement(tag);
|
||||
for (const [k, v] of Object.entries(attrs)) {
|
||||
if (k === "class") e.className = v;
|
||||
else if (k === "html") e.innerHTML = v;
|
||||
else e.setAttribute(k, v);
|
||||
}
|
||||
for (const c of children) {
|
||||
|
|
|
|||
|
|
@ -8,11 +8,10 @@
|
|||
// `asyncBtn(btn, fn)` — the single reusable component:
|
||||
// 1. Guards double-click: returns immediately if `btn` is already
|
||||
// disabled (prevents a second identical request from firing).
|
||||
// 2. Saves `btn.innerHTML` and replaces it with a spinner during the
|
||||
// async operation.
|
||||
// 3. Re-enables the button and restores the original content when `fn`
|
||||
// resolves or rejects (via `finally`), so callers don't need
|
||||
// save/restore boilerplate.
|
||||
// 2. Puts a spinner before the label (`markBusy`) during the async
|
||||
// operation; the label stays, so the button keeps its width.
|
||||
// 3. Re-enables the button and takes the spinner away when `fn`
|
||||
// resolves or rejects (via `finally`).
|
||||
//
|
||||
// Usage:
|
||||
// btn.addEventListener('click', () => asyncBtn(btn, async () => {
|
||||
|
|
@ -30,15 +29,32 @@
|
|||
// own catch — an unhandled rejection from `fn` will propagate normally.
|
||||
export function asyncBtn(btn, fn) {
|
||||
if (btn.disabled) return; // double-click guard
|
||||
const orig = btn.innerHTML;
|
||||
btn.disabled = true;
|
||||
btn.innerHTML = '<span class="spinner">◐</span>';
|
||||
const done = markBusy(btn);
|
||||
return fn().finally(() => {
|
||||
btn.disabled = false;
|
||||
btn.innerHTML = orig;
|
||||
done();
|
||||
});
|
||||
}
|
||||
|
||||
// A spinner before the button's label, and `aria-busy`; the label stays, so the
|
||||
// button does not change width and a screen reader still has its name. Built
|
||||
// from nodes, not an HTML string. Returns the function that takes it away.
|
||||
export function markBusy(btn) {
|
||||
const spinner = document.createElement("span");
|
||||
spinner.className = "spinner";
|
||||
spinner.setAttribute("aria-hidden", "true");
|
||||
spinner.textContent = "◐";
|
||||
const gap = document.createTextNode(" ");
|
||||
btn.prepend(spinner, gap);
|
||||
btn.setAttribute("aria-busy", "true");
|
||||
return () => {
|
||||
spinner.remove();
|
||||
gap.remove();
|
||||
btn.removeAttribute("aria-busy");
|
||||
};
|
||||
}
|
||||
|
||||
// Page-level submit interceptor for `data-async` forms — the pattern every
|
||||
// dashboard/agent action button uses (spawn, cancel, purge,
|
||||
// rebuild, …). Without this a `data-async` form POSTs natively and the
|
||||
|
|
|
|||
40
frontend/packages/shared/src/time.ts
Normal file
40
frontend/packages/shared/src/time.ts
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
// Relative time and duration wording, for the swarm UI and the hive dashboard:
|
||||
// one phrasing ("5m ago", "in 5m", "3m 12s") rather than a copy per package.
|
||||
|
||||
// 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 "5s/5m/5h/5d ago" shape.
|
||||
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`;
|
||||
}
|
||||
Loading…
Reference in a new issue