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
+687
View File
@@ -0,0 +1,687 @@
// Full-screen and modal dialogs owned by the run detail and chat surfaces:
// the rendered/raw diff viewer, the artifact viewer, the human-decision sheet,
// the shared cancel/retry confirm and the shared Action-failed report.
import { h, canCopyLink, canShare, copyArtifactLink, copyText, shareArtifact } from "../dom.js";
import { changeKindTone, describeError } from "../format.js";
import {
BINARY_DIFF_MESSAGE,
NO_LINE_CHANGES_MESSAGE,
TRUNCATED_PATCH_NOTE,
changeKindTitle,
changePositionLabel,
changeStat,
diffLineClass,
diffLineSymbol,
entryHasPatch,
gutterText,
gutterWidth,
parseUnifiedPatch,
patchUnavailableReason,
} from "../changes.js";
import {
ArtifactKind,
artifactKind,
artifactViewerPositionLabel,
pagedIndex,
resolvedArtifactUrl,
} from "../artifacts.js";
import {
ACTION_FAILED_OK,
ACTION_FAILED_TITLE,
CANCEL_CONFIRM,
CANCEL_DIALOG_MESSAGE,
DECISION_CANCEL,
DECISION_CONFIRM,
DECISION_SENDING,
DISMISS_KEEP,
DecisionDraft,
FEEDBACK_LABEL,
RESTART_CONFIRM,
RESTART_DIALOG_MESSAGE,
RETRY_CONFIRM,
RETRY_DIALOG_MESSAGE,
cancelDialogTitle,
decisionSheetMeta,
decisionSheetTitle,
optionTargetText,
restartDialogTitle,
retryDialogTitle,
retryRequestKey,
} from "../actions.js";
export function createDialogsView(rt) {
const { state, repo } = rt;
const renderContent = (...args) => rt.renderContent(...args);
const selectRun = (...args) => rt.selectRun(...args);
let changeViewerOverlay = null;
let changeViewerKeydown = null;
let changeViewerIndex = null;
/** Close the diff viewer, returning focus to the row that opened it. */
function closeChangeViewer() {
if (changeViewerKeydown) {
document.removeEventListener("keydown", changeViewerKeydown);
changeViewerKeydown = null;
}
const index = changeViewerIndex;
changeViewerOverlay?.remove();
changeViewerOverlay = null;
changeViewerIndex = null;
if (index != null) {
document.querySelector(`.change-row[data-change-index="${index}"]`)?.focus();
}
}
/**
* The full-screen diff viewer for one changed file. It pages through the run's
* changed files exactly as the artifact viewer pages through a gallery's media
* (one shared index, previous/next affordances, Escape/the close button/backdrop
* dismissing), and shows both forms the brief requires, switchable from one
* control row: the **Rendered** reading (each hunk's `@@` header, the shared
* diff line language and the old/new gutter numbers) and the **Raw patch** (the
* server's unified patch verbatim in monospace). A file the server could not
* give a patch for (binary, truncated, or a capture that recorded none) shows the
* honest reason in place of a diff rather than a fabricated one.
*/
function openChangeViewer(files, startIndex) {
closeChangeViewer();
if (!files?.length) return;
let index = Math.min(Math.max(Number(startIndex) || 0, 0), files.length - 1);
let form = "rendered";
const overlay = h("div", {
class: "change-viewer",
role: "dialog",
"aria-modal": "true",
"aria-label": "Diff viewer",
});
overlay.addEventListener("click", (event) => {
if (event.target === overlay) closeChangeViewer();
});
const renderViewer = () => {
const entry = files[index];
changeViewerIndex = index;
const stat = changeStat(entry.added, entry.removed);
const hasPatch = entryHasPatch(entry);
const titles = h("div", { class: "change-viewer-titles" });
const titleRow = h("div", { class: "change-viewer-title-row" });
titleRow.append(
h("span", { class: `change-kind ${changeKindTone(entry.kind)}`, text: changeKindTitle(entry.kind) }),
);
if (stat !== "") titleRow.append(h("span", { class: "change-viewer-stat monospace", text: stat }));
titles.append(titleRow);
titles.append(h("div", { class: "change-viewer-path monospace", text: entry.path }));
titles.append(
h("div", {
class: "change-viewer-position muted",
text: `${changePositionLabel(index, files.length)} · changed by ${(entry.steps ?? []).join(" → ")}`,
}),
);
const head = h(
"div",
{ class: "change-viewer-head" },
titles,
h("button", {
class: "change-viewer-copy",
type: "button",
text: "Copy raw patch",
disabled: !hasPatch,
onclick: () => copyText(entry.patch ?? ""),
}),
h("button", {
class: "change-viewer-close",
type: "button",
text: "Close diff",
onclick: closeChangeViewer,
}),
);
const controls = h(
"div",
{ class: "change-viewer-controls" },
h("button", {
class: `change-form${form === "rendered" ? " active" : ""}`,
type: "button",
"data-form": "rendered",
text: "Rendered",
disabled: form === "rendered",
onclick: () => {
form = "rendered";
renderViewer();
},
}),
h("button", {
class: `change-form${form === "raw" ? " active" : ""}`,
type: "button",
"data-form": "raw",
text: "Raw patch",
disabled: !hasPatch,
onclick: () => {
form = "raw";
renderViewer();
},
}),
h("div", { class: "change-viewer-spacer" }),
h("button", {
class: "change-page",
type: "button",
"data-page": "prev",
text: "Previous file",
disabled: index === 0,
onclick: () => {
index = Math.max(index - 1, 0);
form = "rendered";
renderViewer();
},
}),
h("button", {
class: "change-page",
type: "button",
"data-page": "next",
text: "Next file",
disabled: index === files.length - 1,
onclick: () => {
index = Math.min(index + 1, files.length - 1);
form = "rendered";
renderViewer();
},
}),
);
const body = h("div", { class: "change-viewer-body" });
if (entry.binary || !hasPatch) {
body.append(
h("div", {
class: "change-honest muted",
text: entry.binary ? BINARY_DIFF_MESSAGE : patchUnavailableReason(entry),
}),
);
} else if (form === "raw") {
body.append(h("pre", { class: "change-raw monospace", text: entry.patch ?? "" }));
} else {
body.append(renderRenderedDiff(entry));
}
overlay.replaceChildren(head, controls, body);
};
renderViewer();
document.body.append(overlay);
changeViewerOverlay = overlay;
changeViewerKeydown = (event) => {
if (event.key === "Escape") closeChangeViewer();
};
document.addEventListener("keydown", changeViewerKeydown);
}
/** The rendered reading: the stat line, an optional truncation note, each hunk. */
function renderRenderedDiff(entry) {
const wrap = h("div", { class: "change-diff" });
const parsed = parseUnifiedPatch(entry.patch);
const statLine = h("div", { class: "change-diff-stat" });
if (parsed.added > 0) statLine.append(h("span", { class: "stat-add", text: `+${parsed.added}` }));
if (parsed.removed > 0) statLine.append(h("span", { class: "stat-del", text: `-${parsed.removed}` }));
if (parsed.added === 0 && parsed.removed === 0) {
statLine.append(h("span", { class: "muted", text: NO_LINE_CHANGES_MESSAGE }));
}
wrap.append(statLine);
if (entry.truncated) wrap.append(h("div", { class: "change-diff-note muted", text: TRUNCATED_PATCH_NOTE }));
for (const hunk of parsed.hunks) wrap.append(renderHunk(hunk));
return wrap;
}
/**
* One `@@` hunk: its header on a raised surface, then its lines in a
* horizontally scrollable container, each with its old/new gutter numbers (blank
* on the absent side and on a `\ No newline` marker) and the shared
* `{symbol} {text}` coloured by kind.
*/
function renderHunk(hunk) {
const width = gutterWidth(hunk);
const wrap = h("div", { class: "change-hunk" });
wrap.append(h("div", { class: "change-hunk-head", text: hunk.header }));
const lines = h("div", { class: "change-hunk-lines" });
for (const line of hunk.lines) {
const gutter = line.noNewlineMarker
? " ".repeat(width * 2 + 1)
: `${gutterText(line.oldLine, width)} ${gutterText(line.newLine, width)}`;
lines.append(
h(
"div",
{ class: "change-line" },
h("span", { class: "change-gutter", text: gutter }),
h("span", { class: `change-text ${diffLineClass(line.kind)}`, text: `${diffLineSymbol(line.kind)} ${line.text}` }),
),
);
}
wrap.append(lines);
return wrap;
}
function openDecisionSheet(decision) {
state.runDecision = decision;
state.decisionOpen = true;
state.decisionDraft = new DecisionDraft();
state.decisionError = "";
renderContent();
document.querySelector(".decision-sheet .decision-option")?.focus();
}
/** Dismiss the decision sheet (never while a decision is in flight). */
function closeDecisionSheet() {
if (state.decisionSending) return;
const wasOpen = state.decisionOpen;
state.decisionOpen = false;
state.runDecision = null;
state.decisionError = "";
state.decisionDraft = new DecisionDraft();
if (!wasOpen) return;
renderContent();
document.querySelector(".decision-card .decision-open")?.focus();
}
/**
* The decision sheet (R11): the waiting stage, the author's recorded
* instructions, one selectable row per option (with its resolved target when
* declared), the optional feedback field and the deliberate confirm. Selecting a
* row never sends; a refusal keeps the sheet open with the server's exact reason.
*/
function renderDecisionSheet(decision) {
const overlay = h("div", {
class: "decision-sheet",
role: "dialog",
"aria-modal": "true",
"aria-label": decisionSheetTitle(decision.stageKey),
});
overlay.addEventListener("click", (event) => {
if (event.target === overlay) closeDecisionSheet();
});
const sheet = h("div", { class: "decision-sheet-panel" });
sheet.append(h("h2", { class: "decision-sheet-title", text: decisionSheetTitle(decision.stageKey) }));
sheet.append(h("div", { class: "decision-sheet-meta muted", text: decisionSheetMeta(decision) }));
if (String(decision.instructions ?? "").trim() !== "") {
sheet.append(h("p", { class: "decision-instructions", text: decision.instructions }));
}
const options = h("div", { class: "decision-options", role: "radiogroup", "aria-label": "Options" });
for (const option of decision.options) {
const selected = state.decisionDraft.selected === option.key;
const row = h("button", {
class: `decision-option${selected ? " selected" : ""}`,
type: "button",
role: "radio",
"aria-checked": selected ? "true" : "false",
"data-option": option.key,
onclick: () => {
state.decisionDraft = state.decisionDraft.select(option.key);
renderContent();
},
});
row.append(h("span", { class: "decision-radio", "aria-hidden": "true" }));
const body = h("span", { class: "decision-option-body" });
body.append(h("span", { class: "decision-option-key", text: option.key }));
const target = optionTargetText(option.target);
if (target !== "") body.append(h("span", { class: "decision-option-target muted", text: target }));
row.append(body);
options.append(row);
}
sheet.append(options);
const field = h("label", { class: "decision-field" });
field.append(h("span", { class: "decision-field-label", text: FEEDBACK_LABEL }));
field.append(
h("input", {
class: "decision-feedback",
type: "text",
value: state.decisionDraft.feedback,
spellcheck: "false",
oninput: (event) => {
state.decisionDraft = state.decisionDraft.withFeedback(event.target.value);
},
}),
);
sheet.append(field);
if (state.decisionError) sheet.append(h("div", { class: "decision-error bad", text: state.decisionError }));
const actions = h("div", { class: "decision-sheet-actions" });
actions.append(
h("button", {
class: "ghost decision-cancel",
type: "button",
text: DECISION_CANCEL,
disabled: state.decisionSending,
onclick: closeDecisionSheet,
}),
);
actions.append(
h("button", {
class: "decision-confirm",
type: "button",
text: state.decisionSending ? DECISION_SENDING : DECISION_CONFIRM,
disabled: !state.decisionDraft.canSubmit || state.decisionSending,
onclick: () => confirmDecision(decision),
}),
);
sheet.append(actions);
overlay.append(sheet);
return overlay;
}
/** Send the chosen decision; on refusal show the server's reason and re-read. */
async function confirmDecision(decision) {
if (state.decisionSending || !state.decisionDraft.canSubmit) return;
const option = state.decisionDraft.selected;
state.decisionSending = true;
state.decisionError = "";
renderContent();
try {
await repo.decideRun(decision.runId, {
stage: decision.stageKey,
option,
feedback: state.decisionDraft.feedback,
});
state.decisionSending = false;
state.decisionOpen = false;
state.runDecision = null;
state.decisionDraft = new DecisionDraft();
await selectRun(decision.runId, { silent: true });
} catch (error) {
state.decisionSending = false;
state.decisionError = describeError(error);
await selectRun(decision.runId, { silent: true });
}
renderContent();
}
/** Open one of the shared confirm dialogs (cancel / retry / restart). */
function openConfirm(kind) {
state.confirmKind = kind;
renderContent();
}
function dismissConfirm() {
state.confirmKind = null;
renderContent();
}
/**
* The shared confirm dialog: the exact Android title/message for cancel and
* retry (with Keep as the dismiss), plus the browser-only "Restart from start"
* secondary confirm that plainly states the whole-pipeline replay. The dismiss
* button is "Keep".
*/
function renderConfirmDialog(d) {
const kind = state.confirmKind;
if (!kind) return null;
const runId = kind.runId ?? d.id;
let title;
let message;
let confirmLabel;
let secondary = null;
if (kind.kind === "cancel") {
title = cancelDialogTitle(runId);
message = CANCEL_DIALOG_MESSAGE;
confirmLabel = CANCEL_CONFIRM;
} else if (kind.kind === "retry") {
title = retryDialogTitle(runId);
message = RETRY_DIALOG_MESSAGE;
confirmLabel = RETRY_CONFIRM;
secondary = { label: RESTART_CONFIRM, next: { kind: "restart", runId } };
} else {
title = restartDialogTitle(runId);
message = RESTART_DIALOG_MESSAGE;
confirmLabel = RESTART_CONFIRM;
}
const overlay = h("div", {
class: `confirm-dialog confirm-${kind.kind}`,
role: "dialog",
"aria-modal": "true",
"aria-label": title,
});
overlay.addEventListener("click", (event) => {
if (event.target === overlay) dismissConfirm();
});
const panel = h("div", { class: "confirm-panel" });
panel.append(h("h2", { class: "confirm-title", text: title }));
panel.append(h("p", { class: "confirm-message", text: message }));
const actions = h("div", { class: "confirm-actions" });
if (secondary) {
actions.append(
h("button", {
class: "ghost confirm-secondary",
type: "button",
text: secondary.label,
onclick: () => openConfirm(secondary.next),
}),
);
}
actions.append(
h("button", {
class: "ghost confirm-dismiss",
type: "button",
text: DISMISS_KEEP,
onclick: dismissConfirm,
}),
);
actions.append(
h("button", {
class: "confirm-accept",
type: "button",
text: confirmLabel,
onclick: () => acceptConfirm(kind),
}),
);
panel.append(actions);
overlay.append(panel);
return overlay;
}
/**
* Run the confirmed action, then a silent get_run refresh — exactly as the
* Android client does. A server refusal is reported through the one shared
* Action-failed dialog.
*/
async function acceptConfirm(kind) {
const runId = kind.runId ?? state.runDetail?.id;
state.confirmKind = null;
renderContent();
try {
if (kind.kind === "cancel") {
await repo.cancelRun(runId);
} else {
const mode = kind.kind === "restart" ? "restart" : "resume";
await repo.retryRun(runId, { mode, requestKey: retryRequestKey(runId) });
}
} catch (error) {
state.actionError = describeError(error);
}
await selectRun(runId, { silent: true });
renderContent();
}
/** The one shared "Action failed" dialog (title, the server's message, OK). */
function renderActionFailedDialog() {
const overlay = h("div", {
class: "action-failed",
role: "dialog",
"aria-modal": "true",
"aria-label": ACTION_FAILED_TITLE,
});
overlay.addEventListener("click", (event) => {
if (event.target === overlay) dismissActionError();
});
const panel = h("div", { class: "action-failed-panel" });
panel.append(h("h2", { class: "action-failed-title", text: ACTION_FAILED_TITLE }));
panel.append(h("p", { class: "action-failed-message", text: state.actionError }));
panel.append(
h("button", { class: "action-failed-ok", type: "button", text: ACTION_FAILED_OK, onclick: dismissActionError }),
);
overlay.append(panel);
return overlay;
}
function dismissActionError() {
state.actionError = "";
renderContent();
}
let viewerOverlay = null;
let viewerKeydown = null;
/** Close the full-screen viewer and remove its Escape listener. */
function closeArtifactViewer() {
if (viewerKeydown) {
document.removeEventListener("keydown", viewerKeydown);
viewerKeydown = null;
}
viewerOverlay?.remove();
viewerOverlay = null;
}
/**
* Open the full-screen viewer at `startIndex` into `viewable`, the same ordered
* media list the grid drew. Previous/Next are disabled at the ends and absent for
* a single artifact; Escape and a backdrop click close it. Nothing autoplays: a
* non-image artifact shows a still poster placeholder instead.
*/
function openArtifactViewer(viewable, startIndex) {
closeArtifactViewer();
if (!viewable?.length) return;
let index = pagedIndex(startIndex, 0, viewable.length);
const overlay = h("div", {
class: "artifact-viewer",
role: "dialog",
"aria-modal": "true",
"aria-label": "Artifact viewer",
});
overlay.addEventListener("click", (event) => {
if (event.target === overlay) closeArtifactViewer();
});
const renderViewer = () => {
const artifact = viewable[index] ?? {};
const resolved = resolvedArtifactUrl(artifact, state.url);
const kind = artifactKind(artifact.content_type, artifact.name);
const name = artifact.name || "(unnamed artifact)";
const board = h("div", { class: "viewer-board" });
if (kind === ArtifactKind.Image && resolved) {
board.append(h("img", { class: "viewer-image", src: resolved, alt: name }));
} else {
board.append(
h(
"div",
{ class: "viewer-placeholder" },
h("span", { class: "artifact-play", text: "▶" }),
h("span", { class: "viewer-placeholder-name", text: name }),
),
);
}
const head = h(
"div",
{ class: "viewer-head" },
h("div", { class: "viewer-title", text: name }),
h("div", {
class: "viewer-position muted",
text: artifactViewerPositionLabel(index, viewable.length, artifact.content_type),
}),
);
const actions = h("div", { class: "viewer-actions" });
if (resolved) {
actions.append(
h("button", {
class: "viewer-action",
type: "button",
text: "Open",
onclick: () => window.open(resolved, "_blank", "noopener,noreferrer"),
}),
h("button", {
class: "viewer-action",
type: "button",
text: "Copy link",
disabled: !canCopyLink(),
onclick: () => copyArtifactLink(resolved),
}),
h("button", {
class: "viewer-action",
type: "button",
text: "Share",
disabled: !canShare(),
onclick: () => shareArtifact(resolved),
}),
);
}
actions.append(
h("button", {
class: "viewer-action viewer-close",
type: "button",
text: "Close viewer",
onclick: closeArtifactViewer,
}),
);
const nodes = [head, board];
if (viewable.length > 1) {
nodes.push(
h(
"div",
{ class: "viewer-nav" },
h("button", {
class: "viewer-page",
type: "button",
text: "Previous artifact",
disabled: pagedIndex(index, -1, viewable.length) === index,
onclick: () => {
index = pagedIndex(index, -1, viewable.length);
renderViewer();
},
}),
h("button", {
class: "viewer-page",
type: "button",
text: "Next artifact",
disabled: pagedIndex(index, 1, viewable.length) === index,
onclick: () => {
index = pagedIndex(index, 1, viewable.length);
renderViewer();
},
}),
),
);
}
nodes.push(actions);
overlay.replaceChildren(...nodes);
};
renderViewer();
document.body.append(overlay);
viewerOverlay = overlay;
viewerKeydown = (event) => {
if (event.key === "Escape") closeArtifactViewer();
};
document.addEventListener("keydown", viewerKeydown);
}
return {
closeChangeViewer,
openChangeViewer,
openDecisionSheet,
closeDecisionSheet,
renderDecisionSheet,
confirmDecision,
openConfirm,
dismissConfirm,
renderConfirmDialog,
acceptConfirm,
renderActionFailedDialog,
dismissActionError,
closeArtifactViewer,
openArtifactViewer,
};
}