// 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, modal } = rt; const render = (...args) => rt.render(...args); const selectRun = (...args) => rt.selectRun(...args); /** * The monochrome outlined bell for the Runs heading: an inline SVG stroked with * `currentColor` so it follows the button's text colour instead of the colour * emoji's fixed yellow. Decorative — the button's `aria-label` carries the name. */ function attentionBellIcon() { const NS = "http://www.w3.org/2000/svg"; const svg = document.createElementNS(NS, "svg"); svg.setAttribute("class", "attention-bell-icon"); svg.setAttribute("viewBox", "0 0 24 24"); svg.setAttribute("aria-hidden", "true"); svg.setAttribute("focusable", "false"); for (const d of ["M6 8a6 6 0 0 1 12 0c0 7 3 9 3 9H3s3-2 3-9", "M10.3 21a1.94 1.94 0 0 0 3.4 0"]) { const path = document.createElementNS(NS, "path"); path.setAttribute("d", d); path.setAttribute("fill", "none"); path.setAttribute("stroke", "currentColor"); path.setAttribute("stroke-width", "1.7"); path.setAttribute("stroke-linecap", "round"); path.setAttribute("stroke-linejoin", "round"); svg.append(path); } return svg; } /** * The attention bell action, shown in the Runs list heading (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(attentionBellIcon()); 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 count = attentionCount(); const label = attentionBadgeLabel(count); const button = document.querySelector("#attentionBell"); if (button) { 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(); } } // The Runs destination mirrors the same count, so an operator on any surface // can see that something needs attention without first opening Runs. for (const destinationBadge of document.querySelectorAll("[data-attention-badge]")) { destinationBadge.hidden = count <= 0; destinationBadge.textContent = count > 0 ? String(count) : ""; } } // --- Attention / "Needs you" surface (R15) --------------------------------- /** * The attention sheet: the ordered list from the pure `attentionEntries` model, * opened from the bell in the Runs heading. 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(); modal.close("attention-sheet"); 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" }); // One count for the whole open sheet: only repeated labels need the run id // appended, so dismissing stays unambiguous without noisy names elsewhere. const labelCounts = new Map(); for (const entry of entries) { labelCounts.set(entry.label, (labelCounts.get(entry.label) ?? 0) + 1); } 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, labelCounts)); } const sheet = h("div", { class: "attention-sheet-panel", tabindex: "-1" }, head, body); overlay.append(sheet); attentionLayer.replaceChildren(overlay); modal.open("attention-sheet", overlay, { // The sheet is rebuilt by renderAttentionSheet, not by renderContent's // staged set, so it is managed imperatively rather than reconciled away. renderManaged: false, onClose: () => closeAttentionSheet(), triggerSelector: "#attentionBell", fallbackSelectors: ["#listTitle", "#pane-main"], }); } /** One attention row: status icon, the two text lines, and a trailing Dismiss. */ function renderAttentionRow(entry, labelCounts = null) { const glyphState = entry.kind === AttentionKind.Decision ? "waiting" : "failed"; const repeated = (labelCounts?.get(entry.label) ?? 0) > 1; const dismissLabel = repeated ? `Dismiss ${entry.label} (run ${entry.runId})` : `Dismiss ${entry.label}`; 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": dismissLabel, title: dismissLabel, 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(); } /** Close the sheet, dropping the snapshot; the controller returns focus to the bell. */ function closeAttentionSheet({ restoreFocus = true } = {}) { if (!state.attentionSheetOpen) return; state.attentionSheetOpen = false; state.attentionSnapshot = null; // The sheet is managed imperatively (renderManaged: false), so the controller // must be told it closed; otherwise it stays on the stack and the app root is // left inert, which silently swallows every real click. Focus moves after the // repaint, since the close re-renders the surface the trigger lives in. modal.close("attention-sheet", { restoreFocus: false }); render(); if (restoreFocus) document.getElementById("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, }; }