2026-11-28 21:09:02 -08:00
// 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" } ` ;
}
2026-12-05 06:41:04 +00:00
// 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" );
}
2026-11-28 21:09:02 -08:00
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 ;
2026-12-05 06:41:04 +00:00
busyWhat = busy ? pressed . what : "" ;
2026-11-28 21:09:02 -08:00
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" );
2026-12-05 06:41:04 +00:00
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" ));
2026-11-28 21:09:02 -08:00
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" ;
2026-12-05 06:41:04 +00:00
choice . appendChild ( button ( label , "review-end-continue" , "" , busy , () => press ( "continue" , "review-loop-continue" ), "continue" ));
2026-11-28 21:09:02 -08:00
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 (
2026-12-05 06:41:04 +00:00
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" )
2026-11-28 21:09:02 -08:00
);
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" );
2026-12-05 06:41:04 +00:00
const add = ( label , id , hint , primary , onClick , what ) => {
2026-11-28 21:09:02 -08:00
const choice = el ( "div" , "loop-end-choice" + ( primary ? " is-recommended" : "" ));
2026-12-05 06:41:04 +00:00
choice . appendChild ( button ( label , id , primary ? "primary" : "" , busy , onClick , what ));
2026-11-28 21:09:02 -08:00
choice . appendChild ( el ( "p" , "loop-end-hint" , hint ));
actions . appendChild ( choice );
};
2026-12-05 06:41:04 +00:00
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" );
2026-11-28 21:09:02 -08:00
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 , () =>
2026-12-05 06:41:04 +00:00
press ( "quick-task" , "review-loop-next" , { next : "quick-task" }), "quick-task"
2026-11-28 21:09:02 -08:00
);
}
2026-12-05 06:41:04 +00:00
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" );
2026-11-28 21:09:02 -08:00
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 ;
}