feat: restyle the web UI as a spaceship cockpit
The map is a canopy with a bezel, an inset window and a sill carrying a Window | Radar view switch. Consoles open as centered screens; Orbits, Outposts, Activity and the snapshot sheet follow one vocabulary of recessed fields, bevelled keys and lamps that stay distinct by shape. - Activity replaces the Restore tab with one log of backups and restores. Rows lead to a snapshot or an orbit; restores and exports start from the snapshot. The model is a pure function with tests. - Restore opens from a snapshot as a two column screen: choose and confirm on the left, job progress or countdown on the right. - Add Segmented and a themed Select (portal list, keyboard and form support). - Add a spotlight-style search panel. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
173
web/src/lib/activity.ts
Normal file
173
web/src/lib/activity.ts
Normal file
@@ -0,0 +1,173 @@
|
||||
import type { CaptureJob, Orbit, RestoreJob, Snapshot } from "../api";
|
||||
import { formatBytes, formatWhen } from "./format.ts";
|
||||
|
||||
/** Where an entry leads. Restores and exports start from a snapshot, so that is where most entries go. */
|
||||
export type ActivityTarget = { kind: "snapshot" | "orbit"; id: string };
|
||||
|
||||
export type ActivityEntry = {
|
||||
id: string;
|
||||
kind: "backup" | "restore";
|
||||
/** Milliseconds since the epoch, or null when the hub does not say when it happened. */
|
||||
at: number | null;
|
||||
tone: "red" | "amber" | "green" | "grey";
|
||||
state: string;
|
||||
title: string;
|
||||
meta: string;
|
||||
group: "needs" | "running" | "history";
|
||||
opens: ActivityTarget | null;
|
||||
/** Position along the five phases of a job in flight, 0 to 4. */
|
||||
progress?: number;
|
||||
};
|
||||
|
||||
const captureLabels: Record<string, string> = {
|
||||
queued: "Waiting for outpost",
|
||||
claimed: "Assigned to agent",
|
||||
dumping: "Preparing source",
|
||||
uploading: "Saving backup",
|
||||
dump_failed: "Source preparation failed or timed out",
|
||||
upload_failed: "Backup transfer failed",
|
||||
completed: "Backup completed",
|
||||
};
|
||||
const captureOrder = ["queued", "claimed", "dumping", "uploading", "completed"];
|
||||
|
||||
const restoreLabels: Record<string, string> = {
|
||||
queued: "Waiting for outpost",
|
||||
claimed: "Assigned to agent",
|
||||
checking: "Verifying archive and target",
|
||||
safety: "Saving a copy of the target",
|
||||
ready: "Safety copy verified",
|
||||
applying: "Applying restore",
|
||||
completed: "Restore complete",
|
||||
wrong_target: "Target identity check failed",
|
||||
invalid_payload: "Archive check failed",
|
||||
safety_failed: "Safety copy could not be verified",
|
||||
apply_failed: "Apply failed",
|
||||
lease_lost: "Agent disconnected during apply",
|
||||
reconciled: "Recovery verified",
|
||||
cancelled: "Cancelled",
|
||||
};
|
||||
const restoreOrder: string[][] = [["queued", "claimed"], ["checking"], ["safety"], ["ready", "applying"], ["completed", "reconciled"]];
|
||||
|
||||
const shortId = (id: string) => id.slice(0, 8);
|
||||
const inFlight = (status: string) => status === "queued" || status === "running" || status === "applying";
|
||||
|
||||
function restoreEntry(job: RestoreJob, snapshots: Map<string, Snapshot>): ActivityEntry {
|
||||
const source = snapshots.get(job.snapshot_id);
|
||||
const from = source ? `${source.orbit_name} · ${formatWhen(source.taken_at)}` : `Snapshot ${shortId(job.snapshot_id)}`;
|
||||
const phase = restoreLabels[job.phase] ?? job.status;
|
||||
const base = {
|
||||
id: `restore-${job.id}`,
|
||||
kind: "restore" as const,
|
||||
at: job.created_at * 1000,
|
||||
title: `${from} → ${job.target.name} / ${job.target.identity.database}`,
|
||||
opens: source ? { kind: "snapshot" as const, id: source.id } : null,
|
||||
};
|
||||
if (job.status === "recovery_required") {
|
||||
return { ...base, group: "needs", tone: "red", state: "Recovery required",
|
||||
meta: `Restore · ${phase.toLowerCase()} · target locked until verified · open the snapshot` };
|
||||
}
|
||||
if (inFlight(job.status)) {
|
||||
const progress = Math.max(0, restoreOrder.findIndex((phases) => phases.includes(job.phase)));
|
||||
return { ...base, group: "running", tone: "amber", state: restoreLabels[job.phase] ?? "Running", progress,
|
||||
meta: `Restore · step ${progress + 1} of 5 · by ${job.actor}` };
|
||||
}
|
||||
if (job.status === "failed") {
|
||||
return { ...base, group: "history", tone: "red", state: "Failed", meta: `Restore · ${phase.toLowerCase()} · by ${job.actor}` };
|
||||
}
|
||||
if (job.status === "cancelled") {
|
||||
return { ...base, group: "history", tone: "grey", state: "Cancelled", meta: `Restore · by ${job.actor}` };
|
||||
}
|
||||
const recovered = job.status === "reconciled";
|
||||
return { ...base, group: "history", tone: "green", state: recovered ? "Recovered" : "Complete",
|
||||
meta: `Restore · ${job.safety_snapshot_id ? "safety copy kept · " : ""}by ${job.actor}` };
|
||||
}
|
||||
|
||||
function captureEntry(job: CaptureJob, orbits: Map<string, Orbit>): ActivityEntry | undefined {
|
||||
const orbit = orbits.get(job.profile.orbit_id);
|
||||
const name = orbit?.name ?? job.profile.application_id;
|
||||
const base = {
|
||||
id: `backup-${job.id}`,
|
||||
kind: "backup" as const,
|
||||
at: job.created_at * 1000,
|
||||
title: name,
|
||||
opens: orbit ? { kind: "orbit" as const, id: orbit.id } : null,
|
||||
};
|
||||
const phase = captureLabels[job.phase] ?? job.status;
|
||||
if (job.status === "queued" || job.status === "running") {
|
||||
const progress = Math.max(0, captureOrder.indexOf(job.phase));
|
||||
return { ...base, group: "running", tone: "amber", state: phase, progress,
|
||||
meta: `Backup via Outpost · step ${progress + 1} of 5 · by ${job.actor}` };
|
||||
}
|
||||
if (job.status === "failed") {
|
||||
return { ...base, group: "history", tone: "red", state: "Failed", meta: `Backup via Outpost · ${phase.toLowerCase()} · nothing was stored` };
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function buildActivity({ snapshots, orbits, captureJobs, restoreJobs, now }: {
|
||||
snapshots: readonly Snapshot[];
|
||||
orbits: readonly Orbit[];
|
||||
captureJobs: readonly CaptureJob[];
|
||||
restoreJobs: readonly RestoreJob[];
|
||||
now: number;
|
||||
}): ActivityEntry[] {
|
||||
const snapshotById = new Map(snapshots.map((snapshot) => [snapshot.id, snapshot]));
|
||||
const orbitById = new Map(orbits.map((orbit) => [orbit.id, orbit]));
|
||||
const jobForSnapshot = new Map(captureJobs.filter((job) => job.snapshot_id).map((job) => [job.snapshot_id!, job]));
|
||||
const entries: ActivityEntry[] = [];
|
||||
|
||||
for (const snapshot of snapshots) {
|
||||
const job = jobForSnapshot.get(snapshot.id);
|
||||
entries.push({
|
||||
id: `snapshot-${snapshot.id}`,
|
||||
kind: "backup",
|
||||
at: Date.parse(snapshot.taken_at) || null,
|
||||
tone: "green",
|
||||
state: "Stored",
|
||||
title: `${snapshot.orbit_name} → ${formatBytes(snapshot.size_bytes)} snapshot`,
|
||||
meta: `Backup${job ? ` via Outpost · by ${job.actor}` : ""} · ${shortId(snapshot.id)}`,
|
||||
group: "history",
|
||||
opens: { kind: "snapshot", id: snapshot.id },
|
||||
});
|
||||
}
|
||||
for (const job of captureJobs) {
|
||||
const entry = captureEntry(job, orbitById);
|
||||
if (entry) entries.push(entry);
|
||||
}
|
||||
// Orbits the hub runs itself have no job list, only their last status; an outpost orbit is covered by its jobs above.
|
||||
for (const orbit of orbits) {
|
||||
if (orbit.application_id || orbit.access === "outpost") continue;
|
||||
if (orbit.last_status === "running" || orbit.last_status === "queued" || orbit.last_status === "rehearsing") {
|
||||
entries.push({ id: `orbit-${orbit.id}-running`, kind: "backup", at: now, tone: "amber", state: "Running",
|
||||
title: orbit.name, meta: "Backup via Hub", group: "running", opens: { kind: "orbit", id: orbit.id } });
|
||||
} else if (orbit.last_status === "failed") {
|
||||
entries.push({ id: `orbit-${orbit.id}-failed`, kind: "backup", at: null, tone: "red", state: "Last backup failed",
|
||||
title: orbit.name, meta: "Backup via Hub · the hub does not record when it failed", group: "needs",
|
||||
opens: { kind: "orbit", id: orbit.id } });
|
||||
}
|
||||
}
|
||||
for (const job of restoreJobs) entries.push(restoreEntry(job, snapshotById));
|
||||
|
||||
const rank = { needs: 0, running: 1, history: 2 } as const;
|
||||
return entries.sort((a, b) => rank[a.group] - rank[b.group] || (b.at ?? -Infinity) - (a.at ?? -Infinity) || a.id.localeCompare(b.id));
|
||||
}
|
||||
|
||||
export function activityCounts(entries: readonly ActivityEntry[]) {
|
||||
return {
|
||||
running: entries.filter(({ group }) => group === "running").length,
|
||||
needs: entries.filter(({ group }) => group === "needs").length,
|
||||
};
|
||||
}
|
||||
|
||||
/** "Today", "Yesterday" or a short date, in the viewer's time zone. */
|
||||
export function dayLabel(at: number, now: number): string {
|
||||
const day = (time: number) => new Date(time).setHours(0, 0, 0, 0);
|
||||
const gap = Math.round((day(now) - day(at)) / 86_400_000);
|
||||
if (gap <= 0) return "Today";
|
||||
if (gap === 1) return "Yesterday";
|
||||
return new Date(at).toLocaleDateString(undefined, { month: "short", day: "numeric", ...(gap > 300 ? { year: "numeric" } : {}) });
|
||||
}
|
||||
|
||||
export function clock(at: number): string {
|
||||
return new Date(at).toLocaleTimeString(undefined, { hour: "2-digit", minute: "2-digit", hour12: false });
|
||||
}
|
||||
Reference in New Issue
Block a user