web: split app.js into view modules

Pure move, behaviour unchanged: app.js is now the shell (boot, state, client wiring, connection/banner, navigation and routes, render dispatch, header). Views live under src/views with shared DOM and format helpers. client.js stays DOM-free; no circular imports; every server-derived string still uses textContent.
This commit is contained in:
2026-10-19 16:37:31 +01:00
parent 02afed6a01
commit 007d22eb83
14 changed files with 7326 additions and 6947 deletions
+326
View File
@@ -0,0 +1,326 @@
// The header attention bell and the "Needs you" sheet, plus the notification
// ledger's frame observer. Pure ordering/labels live in attention.js; this module
// owns only the DOM and the page's dismissal state.
import { h } from "../dom.js";
import {
ATTENTION_STORAGE_KEY,
AttentionKind,
attentionBadgeLabel,
attentionDismiss,
attentionDismissAll,
attentionDismissalSnapshot,
attentionEntries,
attentionRowDescription,
decodeAttentionDismissals,
encodeAttentionDismissals,
} from "../attention.js";
import { NOTIFY_STORAGE_KEY, decodeNotificationSettings, encodeNotificationSettings } from "../notify.js";
import { stateGlyph } from "../runDetail.js";
/** The dismissed identities from storage at boot; anything unreadable is empty. */
export function loadAttentionDismissed() {
try {
return decodeAttentionDismissals(localStorage.getItem(ATTENTION_STORAGE_KEY));
} catch {
return new Set();
}
}
/** The asked flag from storage at boot; anything unreadable means never asked. */
export function loadNotifyAsked() {
try {
return decodeNotificationSettings(localStorage.getItem(NOTIFY_STORAGE_KEY)).asked;
} catch {
return false;
}
}
export function createAttentionView(rt) {
const { state, attentionLayer, runAlerts } = rt;
const render = (...args) => rt.render(...args);
const selectRun = (...args) => rt.selectRun(...args);
/**
* The attention bell action, shown in the top bar on every view (R15). Its
* accessible name comes from `attentionBadgeLabel`; the visible badge is drawn
* only when something genuinely needs the operator, and the count comes from the
* same pure model the sheet reads.
*/
function renderAttentionBell() {
const count = attentionCount();
const label = attentionBadgeLabel(count);
const button = h("button", {
id: "attentionBell",
class: "attention-bell",
type: "button",
"aria-label": label,
title: label,
onclick: openAttentionSheet,
});
button.append(h("span", { class: "attention-bell-glyph", "aria-hidden": "true", text: "\uD83D\uDD14" }));
if (count > 0) {
button.append(
h("span", {
class: "attention-badge",
"aria-hidden": "true",
"data-count": String(count),
text: String(count),
}),
);
}
return button;
}
/** The number of entries the attention surface currently holds. */
function attentionCount() {
return attentionEntries(state.runs, state.attentionDismissed).length;
}
/**
* Update the bell in place after the run list (which the surface derives from)
* loads, without rebuilding the header and disturbing the URL field.
*/
function refreshAttentionBell() {
const button = document.querySelector("#attentionBell");
if (!button) return;
const count = attentionCount();
const label = attentionBadgeLabel(count);
button.setAttribute("aria-label", label);
button.title = label;
let badge = button.querySelector(".attention-badge");
if (count > 0) {
if (!badge) {
badge = h("span", { class: "attention-badge", "aria-hidden": "true" });
button.append(badge);
}
badge.dataset.count = String(count);
badge.textContent = String(count);
} else if (badge) {
badge.remove();
}
}
// --- Attention / "Needs you" surface (R15) ---------------------------------
/**
* The attention sheet: the ordered list from the pure `attentionEntries` model,
* opened from the bell in the top bar. It is a single managed dialog — Escape,
* a backdrop click and the Close button all close it — with the exact app
* wording: title `Needs you`, rows of status icon + the run label + the
* `{pipeline} · {detail}` line + a trailing `Dismiss`, and the empty state
* `Nothing needs you right now.` with no Clear all.
*
* `state.attentionSnapshot` is captured only when the sheet opens and kept until
* it closes, so Clear all acts on what the operator actually saw and an entry
* that arrives while it is open is shown but not cleared.
*/
function renderAttentionSheet() {
if (!state.attentionSheetOpen) {
attentionLayer.replaceChildren();
return;
}
const entries = attentionEntries(state.runs, state.attentionDismissed);
const overlay = h("div", {
class: "attention-sheet",
role: "dialog",
"aria-modal": "true",
"aria-label": "Needs you",
});
overlay.addEventListener("click", (event) => {
if (event.target === overlay) closeAttentionSheet();
});
const head = h(
"div",
{ class: "attention-sheet-head" },
h("h2", { class: "attention-sheet-title", text: "Needs you" }),
h("button", {
class: "attention-sheet-close",
type: "button",
text: "Close",
onclick: () => closeAttentionSheet(),
}),
);
const body = h("div", { class: "attention-sheet-body" });
if (entries.length === 0) {
body.append(h("p", { class: "attention-empty", text: "Nothing needs you right now." }));
} else {
body.append(
h("button", {
class: "attention-clear",
type: "button",
text: "Clear all",
onclick: clearAttentionSheet,
}),
);
for (const entry of entries) body.append(renderAttentionRow(entry));
}
const sheet = h("div", { class: "attention-sheet-panel", tabindex: "-1" }, head, body);
overlay.append(sheet);
attentionLayer.replaceChildren(overlay);
}
/** One attention row: status icon, the two text lines, and a trailing Dismiss. */
function renderAttentionRow(entry) {
const glyphState = entry.kind === AttentionKind.Decision ? "waiting" : "failed";
const open = h(
"button",
{
class: "attention-row-open",
type: "button",
"data-run-id": String(entry.runId),
"data-kind": entry.kind.name.toLowerCase(),
onclick: () => activateAttentionEntry(entry),
},
h("span", {
class: `attention-icon chip-${glyphState}`,
"aria-hidden": "true",
text: stateGlyph(glyphState),
}),
h(
"span",
{ class: "attention-text" },
h("span", { class: "attention-label", text: entry.label }),
h("span", { class: "attention-desc", text: attentionRowDescription(entry) }),
),
);
const dismiss = h("button", {
class: "attention-dismiss",
type: "button",
"aria-label": "Dismiss",
title: "Dismiss",
onclick: (event) => {
// The trailing control must never activate the row it sits in.
event.stopPropagation();
dismissAttentionEntries([entry]);
},
});
dismiss.append(h("span", { "aria-hidden": "true", text: "\u2715" }));
return h("div", { class: "attention-row", "data-key": entry.key }, open, dismiss);
}
/** Open the sheet, capturing the dismissal snapshot it will act on. */
function openAttentionSheet() {
state.attentionSheetOpen = true;
state.attentionSnapshot = attentionDismissalSnapshot(
attentionEntries(state.runs, state.attentionDismissed),
);
render();
document.querySelector(".attention-sheet-close")?.focus();
}
/** Close the sheet, dropping the snapshot; focus returns to the bell. */
function closeAttentionSheet({ restoreFocus = true } = {}) {
if (!state.attentionSheetOpen) return;
state.attentionSheetOpen = false;
state.attentionSnapshot = null;
render();
if (restoreFocus) document.querySelector("#attentionBell")?.focus();
}
/** Dismiss one or more entries for good, persisting the identity set. */
function dismissAttentionEntries(entries) {
if (!entries?.length) return;
let next = new Set(state.attentionDismissed);
for (const entry of entries) next = attentionDismiss(next, entry.key);
persistAttentionDismissed(next);
render();
}
/** Clear all applies only the identities captured when the sheet opened. */
function clearAttentionSheet() {
const snapshot = state.attentionSnapshot ?? [];
const next = attentionDismissAll(state.attentionDismissed, snapshot);
persistAttentionDismissed(next);
render();
}
/**
* Activating a row closes the sheet and opens the run. A Failed row is dismissed
* first (opening it is the operator dealing with it); a Decision row is not —
* only answering the decision clears it.
*/
function activateAttentionEntry(entry) {
if (entry.kind === AttentionKind.Failed) {
persistAttentionDismissed(attentionDismiss(state.attentionDismissed, entry.key));
}
state.attentionSheetOpen = false;
state.attentionSnapshot = null;
state.view = "runs";
render();
selectRun(entry.runId);
}
/** Store the dismissed identities as one JSON array under one localStorage key. */
function persistAttentionDismissed(next) {
state.attentionDismissed = next;
try {
localStorage.setItem(ATTENTION_STORAGE_KEY, encodeAttentionDismissals(next));
} catch {
// Storage disabled or full: the in-memory set still applies for this session.
}
}
/**
* Fold one pushed `run_event` frame into the notification ledger (L5). It does
* not touch the runs list (R10's fold is separate); it only decides whether this
* transition raises exactly one notification. A run already failed/waiting when
* seeded, a duplicate frame, a non-qualifying status and a key the operator
* dismissed all produce nothing.
*/
function onRunAlertFrame(frame) {
const alert = runAlerts.observe(frame, { dismissed: state.attentionDismissed });
if (!alert || !notificationsAllowed()) return;
try {
const notification = new Notification(alert.title, { body: alert.body, tag: alert.key });
notification.onclick = () => {
window.focus();
state.view = "runs";
render();
selectRun(alert.runId);
};
} catch {
// Some browsers can throw even with permission granted; the in-page Needs
// you surface remains the only honest channel and nothing is faked.
}
}
/** True only when a real browser notification can be constructed right now. */
function notificationsAllowed() {
return (
typeof globalThis.Notification === "function" &&
globalThis.isSecureContext === true &&
globalThis.Notification.permission === "granted"
);
}
/** Persist only the asked flag; never a permission claim, endpoint or credential. */
function persistNotifyAsked(asked) {
state.notifyAsked = Boolean(asked);
try {
localStorage.setItem(NOTIFY_STORAGE_KEY, encodeNotificationSettings({ asked }));
} catch {
// Storage disabled or full: the in-memory flag still applies for this session.
}
}
return {
renderAttentionBell,
attentionCount,
refreshAttentionBell,
renderAttentionSheet,
openAttentionSheet,
closeAttentionSheet,
dismissAttentionEntries,
clearAttentionSheet,
activateAttentionEntry,
persistAttentionDismissed,
persistNotifyAsked,
onRunAlertFrame,
notificationsAllowed,
};
}