Files
plan2code/src/web-console/public/review-loop-end.js
T
2026-12-05 06:41:04 +00:00

213 lines
9.2 KiB
JavaScript

// Plan2Code Web Console - how a Review Loop run ends: the stop screens, the sign-off and the finished screen; defines only.
import { el } from "./render.js";
import { field, toggle } from "./loop-parts.js";
import { blockBody } from "./doc-files.js";
import { docs } from "./selectors.js";
import { tellApp } from "./app-bridge.js";
import { page, redraw } from "./state.js";
// The app's `loopEnd` with `kind: "review"` (patches.mjs reviewLoopEndProblems):
// reason why it ended: clean, cap, person or failed
// report a doc on the page holding the Markdown report
// partial after Stop now: the partial diff doc and its stats, until Keep or Discard
// suggestions, canSquash, canContinue, open, signedOff, note
// Before sign-off the card offers Continue (when allowed), the Suggestions pick,
// the squash switch and Sign off; after it, the finished screen's three ways on.
const HEADINGS = {
clean: "Clean",
cap: "Stopped at the round cap",
person: "Stopped by you",
failed: "Failed",
};
// What the card remembers across redraws, keyed by the ending it belongs to:
// the ticked Suggestions, the squash switch, and which press is waiting on the app.
let ticks = { key: null, picks: new Set(), squash: false };
let pressed = { key: null, what: "" };
let lastKey = null;
// The ending a press belongs to. Keep or Discard reposts the same ending without
// its partial changes, so a press waiting on the app ends with that change.
function endKey(end) {
return `review|${end.line}|${end.reason}|${end.signedOff ? "signed" : "open"}|${end.partial ? "partial" : "whole"}`;
}
// The ending the ticks and the squash switch belong to: the same through Keep
// or Discard, so what the person ticked is not cleared by settling the partial changes.
function tickKey(end) {
return `review|${end.line}|${end.reason}|${end.signedOff ? "signed" : "open"}`;
}
// The press the page is waiting on (set each draw), so its button can show work.
let busyWhat = "";
function button(label, id, cls, disabled, onClick, what = "") {
const working = Boolean(what) && busyWhat === what;
const b = el("button", "btn" + (cls ? " " + cls : "") + (working ? " is-busy" : ""), working ? "" : label);
if (working) {
const spin = el("span", "spinner tiny");
spin.setAttribute("aria-hidden", "true");
b.append(spin, document.createTextNode("Working…"));
b.setAttribute("aria-busy", "true");
}
b.type = "button";
b.id = id;
b.disabled = disabled;
b.addEventListener("click", onClick);
return b;
}
function stats(partial) {
const row = el("p", "diff-totals review-end-stats");
row.appendChild(el("span", null, `${partial.files} ${partial.files === 1 ? "file" : "files"} changed, `));
row.appendChild(el("span", "diff-plus", `+${partial.added}`));
row.appendChild(document.createTextNode(" "));
row.appendChild(el("span", "diff-minus", `-${partial.removed}`));
return row;
}
function where(item) {
if (!item.file) return "";
return item.line ? `${item.file}:${item.line}` : item.file;
}
/** How a Review Loop run ended, drawn from the app's `loopEnd`. */
export function renderReviewLoopEnd(end) {
const key = endKey(end);
if (lastKey !== key) {
lastKey = key;
page.reviewAction = null;
}
const ticked = tickKey(end);
if (ticks.key !== ticked) ticks = { key: ticked, picks: new Set(), squash: false };
const failed =
page.reviewAction && !page.reviewAction.ok ? page.reviewAction.error : page.loopAction && !page.loopAction.ok ? page.loopAction.error : "";
const busy = pressed.key === key && !failed;
busyWhat = busy ? pressed.what : "";
const press = (what, kind, extra) => {
if (page.gone || busy) return;
pressed = { key, what };
page.reviewAction = null;
page.loopAction = null;
tellApp(kind, extra);
redraw();
};
const card = el("article", "card handoff loop-end review-end");
card.appendChild(el("p", "card-kicker", end.signedOff ? "Review Loop finished" : "Review Loop ended"));
const heading = end.signedOff ? "Signed off" : HEADINGS[end.reason] || end.line;
card.appendChild(el("h2", "loop-end-heading review-end-heading", heading));
if (end.line) card.appendChild(el("p", "loop-line review-end-line", end.line));
if (end.note) card.appendChild(el("p", "callout is-info review-end-note", end.note));
const report = docs().find((d) => d.id === end.report);
if (report) {
const read = el("button", "btn sm review-end-report", "Read the full report");
read.type = "button";
read.addEventListener("click", () => {
page.view = "doc:" + report.id;
redraw();
});
card.appendChild(read);
}
if (end.signedOff) {
drawFinished(card, end, busy, press);
} else {
drawStop(card, end, busy, press);
}
if (failed) card.appendChild(el("p", "loop-error review-end-error", failed));
return card;
}
function drawPartial(card, end, busy, press) {
const partial = end.partial;
const box = el("section", "review-end-partial");
box.appendChild(el("h3", null, "What Stop now left"));
box.appendChild(stats(partial));
const doc = docs().find((d) => d.id === partial.diff);
const block = doc && (doc.blocks || []).find((b) => typeof b.diff === "string" || typeof b.text === "string");
if (block) {
const view = el("div", "review-end-diff");
view.appendChild(blockBody(block, doc));
box.appendChild(view);
}
box.appendChild(el("p", "loop-end-hint", "Keep leaves these changes in your files. Discard takes them out and cannot be undone."));
const row = el("div", "btn-row");
row.appendChild(button("Keep these changes", "review-end-keep", "primary", busy, () => press("keep", "review-loop-partial", { keep: true }), "keep"));
row.appendChild(button("Discard them", "review-end-discard", "", busy, () => press("discard", "review-loop-partial", { keep: false }), "discard"));
box.appendChild(row);
card.appendChild(box);
}
function drawStop(card, end, busy, press) {
if (end.partial) drawPartial(card, end, busy, press);
// While the partial work is undecided the app refuses Continue, so it is not offered.
if (end.canContinue && !end.partial) {
const choice = el("div", "loop-end-choice");
const label = end.reason === "cap" ? "Run one more round" : "Continue";
choice.appendChild(button(label, "review-end-continue", "", busy, () => press("continue", "review-loop-continue"), "continue"));
choice.appendChild(el("p", "loop-end-hint", "Pick the review up again with the same setup."));
card.appendChild(choice);
}
const suggestions = Array.isArray(end.suggestions) ? end.suggestions : [];
if (suggestions.length) {
const pick = field("Suggestions", "Nothing is ticked. Ticked ones are applied in one last pass, with their own check and commit.");
pick.classList.add("review-end-suggestions");
for (const s of suggestions) {
const label = toggle(`review-end-pick-${s.id}`, "", ticks.picks.has(s.id), (on) => {
if (on) ticks.picks.add(s.id);
else ticks.picks.delete(s.id);
});
label.classList.add("review-end-pick");
const text = label.querySelector("span");
text.appendChild(el("strong", null, s.title));
const at = where(s);
if (at) text.appendChild(el("code", "review-end-where", at));
pick.appendChild(label);
}
card.appendChild(pick);
}
if (end.canSquash) {
const sq = field("Commits");
sq.appendChild(toggle("review-end-squash", "Squash the round commits into one", ticks.squash, (on) => (ticks.squash = on)));
card.appendChild(sq);
}
const row = el("div", "btn-row");
row.appendChild(
button("Sign off", "review-end-signoff", "primary", busy || Boolean(end.partial), () => press("sign-off", "review-loop-sign-off", { picks: [...ticks.picks], squash: ticks.squash }), "sign-off")
);
card.appendChild(row);
card.appendChild(
el("p", "loop-end-hint", end.partial ? "Keep or discard the partial changes first, then sign off." : "Sign off finishes the run, with or without picks.")
);
}
function drawFinished(card, end, busy, press) {
const actions = el("div", "loop-end-actions review-end-next");
const add = (label, id, hint, primary, onClick, what) => {
const choice = el("div", "loop-end-choice" + (primary ? " is-recommended" : ""));
choice.appendChild(button(label, id, primary ? "primary" : "", busy, onClick, what));
choice.appendChild(el("p", "loop-end-hint", hint));
actions.appendChild(choice);
};
add("PR Prep", "review-end-pr-prep", "Get the branch ready for a pull request.", true, () => press("pr-prep", "review-loop-next", { next: "pr-prep" }), "pr-prep");
if (Array.isArray(end.open) && end.open.length) {
add("Fix what's left by hand", "review-end-quick-task", "Start a Quick task with the Critical and Warning findings still open.", false, () =>
press("quick-task", "review-loop-next", { next: "quick-task" }), "quick-task"
);
}
add("Back to the dashboard", "review-end-dashboard", "Leave this screen and pick another skill. Your work stays as it is.", false, () => press("dashboard", "loop-dashboard"), "dashboard");
card.appendChild(actions);
}
/** The end card is no longer drawn (Continue started another round): a press and
* the ending it belonged to are over, so the next ending starts with live buttons. */
export function forgetReviewEnd() {
pressed = { key: null, what: "" };
lastKey = null;
}