mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-11-16 04:45:33 -08:00
Show working feedback and a way out on the Lessons review flow
AI Assisted
This commit is contained in:
1 parent
f61ef6e0a7
commit
2e2b3e1139
7 files changed
+422
-46
No files matched your search
@@ -445,17 +445,17 @@ test("lessons card: Review on the banner leads to the card over the dashboard, w
|
||||
const dlg = s.page.locator("#lessons-modal");
|
||||
await dlg.locator(".lessons-row").waitFor();
|
||||
assert.equal(await main.getByRole("heading", { name: "What are we doing today?" }).count(), 1, "the menu stays under it");
|
||||
assert.equal(await dlg.getByRole("button", { name: "Send to Plan2Code" }).isDisabled(), true, "nothing chosen yet");
|
||||
assert.equal(await dlg.getByRole("button", { name: "Save lessons" }).isDisabled(), true, "nothing chosen yet");
|
||||
// An emptied Reword field holds the dialog's Send off, as the footer's Send is held elsewhere.
|
||||
await dlg.getByRole("button", { name: "Reword" }).click();
|
||||
const text = dlg.getByLabel("The lesson");
|
||||
await text.waitFor();
|
||||
assert.equal(await dlg.getByRole("button", { name: "Send to Plan2Code" }).isDisabled(), false, "a reword ready to go");
|
||||
assert.equal(await dlg.getByRole("button", { name: "Save lessons" }).isDisabled(), false, "a reword ready to go");
|
||||
await text.fill("");
|
||||
await until(async () => await dlg.getByRole("button", { name: "Send to Plan2Code" }).isDisabled(), 3000);
|
||||
await until(async () => await dlg.getByRole("button", { name: "Save lessons" }).isDisabled(), 3000);
|
||||
await dlg.getByRole("button", { name: "Keep" }).click();
|
||||
await dlg.getByRole("button", { name: "Keep", pressed: true }).waitFor();
|
||||
await dlg.getByRole("button", { name: "Send to Plan2Code" }).click();
|
||||
await dlg.getByRole("button", { name: "Save lessons" }).click();
|
||||
const sent = s.wait(10);
|
||||
assert.equal(sent.code, 0);
|
||||
assert.equal(sent.actions.some((a) => a.i === "__launch"), false, "no skill launched");
|
||||
@@ -467,6 +467,139 @@ test("lessons card: Review on the banner leads to the card over the dashboard, w
|
||||
assert.equal(await dlg.isVisible(), false, "the answered card does not open again");
|
||||
});
|
||||
|
||||
// The dashboard with one lesson waiting, Review pressed and the agent's card up.
|
||||
const LESSON_ID = "20261113-120000-eeeeee";
|
||||
async function reviewOpen(t) {
|
||||
const { s, main } = await dashboard(t, PROJECT("on"), (p) =>
|
||||
writeWaiting(p, { id: LESSON_ID, type: "lesson", text: "Run a page test alone first.", noted: new Date().toISOString() })
|
||||
);
|
||||
await main.locator(".learn-banner").getByRole("button", { name: "Review" }).click();
|
||||
return { s, main, dlg: s.page.locator("#lessons-modal") };
|
||||
}
|
||||
const postCard = (s) => s.post({ items: [CARD([{ ...ROWS[0], id: LESSON_ID }])], agent: { status: "waiting" } });
|
||||
|
||||
test("lessons card: Review shows working at once, and ends when the card opens", async (t) => {
|
||||
const { s, main, dlg } = await reviewOpen(t);
|
||||
const banner = main.locator(".learn-banner");
|
||||
await banner.getByText("Opening lessons…").waitFor();
|
||||
assert.equal(await banner.getAttribute("aria-busy"), "true");
|
||||
assert.equal(await banner.getByRole("button", { name: "Review" }).isDisabled(), true, "no second press while it opens");
|
||||
s.wait(10);
|
||||
postCard(s);
|
||||
await dlg.locator(".lessons-row").waitFor();
|
||||
assert.equal(await banner.getByText("Opening lessons…").count(), 0, "the spinner is gone once the card is up");
|
||||
});
|
||||
|
||||
test("lessons card: Review with nothing to show says so instead of spinning", async (t) => {
|
||||
const { s, main } = await reviewOpen(t);
|
||||
await main.locator(".learn-banner").getByText("Opening lessons…").waitFor();
|
||||
s.wait(10);
|
||||
s.post({ menu: { note: "Nothing is waiting." }, agent: { status: "working" } });
|
||||
s.post({ agent: { status: "waiting" } });
|
||||
await s.page.getByText("No lessons are waiting any more.").waitFor();
|
||||
assert.equal(await main.locator(".learn-banner").getByText("Opening lessons…").count(), 0);
|
||||
});
|
||||
|
||||
test("lessons card: Not now and the corner close leave every lesson waiting and tell the agent", async (t) => {
|
||||
for (const how of ["Not now", "x", "Escape"]) {
|
||||
const { s, dlg } = await reviewOpen(t);
|
||||
s.wait(10);
|
||||
postCard(s);
|
||||
await dlg.locator(".lessons-row").waitFor();
|
||||
assert.equal(await dlg.getByRole("button", { name: "Close without saving" }).count(), 1, "a corner close is there");
|
||||
if (how === "Not now") await dlg.getByRole("button", { name: "Not now" }).click();
|
||||
else if (how === "x") await dlg.getByRole("button", { name: "Close without saving" }).click();
|
||||
else await s.page.keyboard.press("Escape");
|
||||
const sent = s.wait(10);
|
||||
assert.equal(sent.code, 0, how);
|
||||
assert.deepEqual(sent.actions.find((a) => a.i === "lessons").decisions, [], `${how}: nothing decided`);
|
||||
await dlg.waitFor({ state: "hidden" });
|
||||
}
|
||||
});
|
||||
|
||||
test("lessons card: closing with choices made asks first, and Keep editing goes back", async (t) => {
|
||||
const { s, dlg } = await reviewOpen(t);
|
||||
s.wait(10);
|
||||
postCard(s);
|
||||
await dlg.getByRole("button", { name: "Keep" }).click();
|
||||
await dlg.getByRole("button", { name: "Keep", pressed: true }).waitFor();
|
||||
await s.page.keyboard.press("Escape");
|
||||
const ask = dlg.locator(".lessons-unsaved");
|
||||
await ask.waitFor();
|
||||
assert.match(await textOf(ask), /You have choices that are not saved\./);
|
||||
await ask.getByRole("button", { name: "Keep editing" }).click();
|
||||
await ask.waitFor({ state: "hidden" });
|
||||
assert.equal(await dlg.isVisible(), true, "still open");
|
||||
assert.equal(await dlg.getByRole("button", { name: "Keep", pressed: true }).count(), 1, "the choice is kept");
|
||||
// Discard and close leaves everything waiting.
|
||||
await dlg.getByRole("button", { name: "Close without saving" }).click();
|
||||
await ask.getByRole("button", { name: "Discard and close" }).click();
|
||||
const sent = s.wait(10);
|
||||
assert.deepEqual(sent.actions.find((a) => a.i === "lessons").decisions, []);
|
||||
await dlg.waitFor({ state: "hidden" });
|
||||
});
|
||||
|
||||
test("lessons card: Save lessons shows saving, locks the card, then the banner takes over until the agent is done", async (t) => {
|
||||
const { s, main, dlg } = await reviewOpen(t);
|
||||
s.wait(10);
|
||||
postCard(s);
|
||||
await dlg.getByRole("button", { name: "Keep" }).click();
|
||||
await dlg.getByRole("button", { name: "Keep", pressed: true }).waitFor();
|
||||
// Hold the send so the saving state can be seen.
|
||||
let release;
|
||||
const held = new Promise((r) => (release = r));
|
||||
await s.page.route("**/submit", async (route) => {
|
||||
await held;
|
||||
await route.continue();
|
||||
});
|
||||
await dlg.getByRole("button", { name: "Save lessons" }).click();
|
||||
await dlg.getByText("Saving…").waitFor();
|
||||
assert.equal(await dlg.getAttribute("aria-busy"), "true");
|
||||
assert.equal(await dlg.getByRole("button", { name: "Not now" }).isDisabled(), true);
|
||||
assert.equal(await dlg.getByRole("button", { name: "Close without saving" }).isDisabled(), true);
|
||||
release();
|
||||
await dlg.waitFor({ state: "hidden" });
|
||||
const sent = s.wait(10);
|
||||
assert.deepEqual(sent.actions.find((a) => a.i === "lessons").decisions, [{ id: LESSON_ID, choice: "Keep" }]);
|
||||
await main.locator(".learn-banner").getByText("Saving your lessons…").waitFor();
|
||||
s.post({ menu: { note: "Kept 1 lesson." }, agent: { status: "working" } });
|
||||
s.post({ agent: { status: "waiting" } });
|
||||
await main.getByText("Kept 1 lesson.").waitFor();
|
||||
await until(async () => (await main.getByText("Saving your lessons…").count()) === 0, 5000);
|
||||
});
|
||||
|
||||
test("lessons card: a refused send keeps the dialog open with the choice still made", async (t) => {
|
||||
const { s, dlg } = await reviewOpen(t);
|
||||
s.wait(10);
|
||||
postCard(s);
|
||||
await dlg.getByRole("button", { name: "Keep" }).click();
|
||||
await dlg.getByRole("button", { name: "Keep", pressed: true }).waitFor();
|
||||
await s.page.route("**/submit", (route) => route.fulfill({ status: 409, contentType: "application/json", body: "{}" }));
|
||||
await dlg.getByRole("button", { name: "Save lessons" }).click();
|
||||
await until(async () => (await dlg.getByText("Saving…").count()) === 0, 5000);
|
||||
assert.equal(await dlg.isVisible(), true, "still open");
|
||||
assert.equal(await dlg.getByRole("button", { name: "Keep", pressed: true }).count(), 1, "the choice is kept");
|
||||
});
|
||||
|
||||
test("lessons card: Not now carries a typed note, and a click outside closes only while nothing is chosen", async (t) => {
|
||||
const { s, dlg } = await reviewOpen(t);
|
||||
s.wait(10);
|
||||
postCard(s);
|
||||
await dlg.locator(".lessons-row").waitFor();
|
||||
// Outside click with a choice made: stays open.
|
||||
await dlg.getByRole("button", { name: "Keep" }).click();
|
||||
await dlg.getByRole("button", { name: "Keep", pressed: true }).waitFor();
|
||||
await s.page.mouse.click(5, 5);
|
||||
assert.equal(await dlg.isVisible(), true, "a stray click keeps the dialog while a choice is made");
|
||||
await dlg.getByRole("button", { name: "Keep", pressed: true }).click();
|
||||
await dlg.getByRole("button", { name: "Keep" }).first().waitFor();
|
||||
await dlg.getByLabel("Anything to add?").fill("Ask me again next week.");
|
||||
await dlg.getByRole("button", { name: "Not now" }).click();
|
||||
const sent = s.wait(10);
|
||||
assert.match(sent.reply, /Ask me again next week\./);
|
||||
assert.deepEqual(sent.actions.find((a) => a.i === "lessons").decisions, []);
|
||||
});
|
||||
|
||||
test("the loop stop card says when lessons are waiting", async (t) => {
|
||||
const project = tempProject(PROJECT("on"));
|
||||
writeWaiting(project, { id: "20261113-120000-999999", type: "lesson", text: "Waits.", noted: new Date().toISOString() });
|
||||
|
||||
@@ -22,7 +22,7 @@ import {
|
||||
adopt, askSuggestions, banner, connect, fillSky, fitGrowBox, loadState, markSent, maybeWelcome, render, renderFooter,
|
||||
renderSidebar, renderWhere, save, startClock, startLooks, stagedPayload, syncWorkspaceScope,
|
||||
wakeHoldsChime, wireAsk, wireCards, wireChrome, wireCleanup, wireDashboard, wireDocs, wireEnding,
|
||||
wireActivities, wireLoopSetup, wireHelp, launchSkill, sendLearning, renderLearningRow, learningBlocked, openHelpOn, openLoopSetup, wireLooks, wireNotepad, wirePrefs, wireWorkspace,
|
||||
wireActivities, wireLoopSetup, wireHelp, launchSkill, sendLearning, beginLearningSave, renderLearningRow, learningBlocked, openHelpOn, openLoopSetup, wireLooks, wireNotepad, wirePrefs, wireWorkspace,
|
||||
openPrefs, markSaved, confirmUnsaved, loadAgentsView, wireAgentsView, hasUnsaved, unsavedName, saveOpen, discardOpen, focusOpen,
|
||||
renderCard, send, refreshLessonsSend,
|
||||
} from "./screens/core.js";
|
||||
@@ -37,7 +37,7 @@ import { sound } from "./sound.js";
|
||||
// and the few calls a screen makes into another are handed to state.js first.
|
||||
onSave(save);
|
||||
useHooks({
|
||||
launchSkill, sendLearning, renderLearningRow, learningBlocked, openHelpOn, openLoopSetup, askSuggestions, banner, fitGrowBox, markSent, maybeWelcome, renderFooter, renderSidebar, renderWhere, stagedPayload, syncWorkspaceScope,
|
||||
launchSkill, sendLearning, beginLearningSave, renderLearningRow, learningBlocked, openHelpOn, openLoopSetup, askSuggestions, banner, fitGrowBox, markSent, maybeWelcome, renderFooter, renderSidebar, renderWhere, stagedPayload, syncWorkspaceScope,
|
||||
openPrefs, markSaved, confirmUnsaved, loadAgentsView, wireAgentsView, hasUnsaved, unsavedName, saveOpen, discardOpen, focusOpen,
|
||||
renderCard, send, refreshLessonsSend,
|
||||
});
|
||||
|
||||
@@ -85,7 +85,7 @@ import { mountAgentsView } from "./agents-view.js";
|
||||
|
||||
// What the entry needs from the screens at start-up, handed on from here: only
|
||||
// the core imports a screen.
|
||||
export { launchSkill, sendLearning, wakeHoldsChime, wireDashboard } from "./dashboard.js";
|
||||
export { launchSkill, sendLearning, beginLearningSave, wakeHoldsChime, wireDashboard } from "./dashboard.js";
|
||||
export { renderLearningRow, learningBlocked, refreshLessonsSend } from "./learning-row.js";
|
||||
import { renderLessonsDialog } from "./learning-row.js";
|
||||
export { fillSky } from "./starting.js";
|
||||
|
||||
@@ -10,6 +10,8 @@ import {
|
||||
loadOverview,
|
||||
finish,
|
||||
loopDriving,
|
||||
learningBusyNow,
|
||||
LEARNING_BUSY_MS,
|
||||
} from "../selectors.js";
|
||||
import { hooks, page, redraw, saveNow } from "../state.js";
|
||||
import { el, approvalTip, mdInto, releasesLink, copyButton, lightDismiss, SVG_NS } from "../render.js";
|
||||
@@ -1393,10 +1395,29 @@ async function sendDelete(spec) {
|
||||
// The Learning banner above the title, or null: only while lessons wait and
|
||||
// Learning is on or not set up. Review sends the __learning action.
|
||||
function learningBanner(sc) {
|
||||
const state = sc && sc.learning && sc.learning.state;
|
||||
const busy = learningBusyNow(state);
|
||||
// Review pressed, or the choices just saved: the banner says what the agent
|
||||
// is doing, in the same place, so the wait is never silent.
|
||||
const working = busy && (busy.what === "review" || busy.what === "decide") ? busy : null;
|
||||
const text = learningBannerText(sc && sc.learning);
|
||||
if (!text) return null;
|
||||
if (!text && !working) return null;
|
||||
const banner = el("div", "dash-nudge learn-banner");
|
||||
banner.setAttribute("role", "status");
|
||||
if (working) {
|
||||
banner.classList.add("is-busy");
|
||||
banner.setAttribute("aria-busy", "true");
|
||||
const line = el("span", "dash-nudge-text learn-busy");
|
||||
line.append(el("span", "spinner tiny"), document.createTextNode(working.what === "review" ? "Opening lessons…" : "Saving your lessons…"));
|
||||
banner.appendChild(line);
|
||||
if (working.what === "review") {
|
||||
const review = el("button", "btn small primary", "Review");
|
||||
review.type = "button";
|
||||
review.disabled = true;
|
||||
banner.appendChild(review);
|
||||
}
|
||||
return banner;
|
||||
}
|
||||
banner.appendChild(el("span", "dash-nudge-text", text));
|
||||
const review = el("button", "btn small primary", "Review");
|
||||
review.type = "button";
|
||||
@@ -1408,19 +1429,66 @@ function learningBanner(sc) {
|
||||
return banner;
|
||||
}
|
||||
|
||||
// The agent finishing with nothing to show ends the wait early: a Review whose
|
||||
// card never came (the banner was stale) says so at once instead of spinning
|
||||
// to the limit, and a saved choice stops when the agent is back to waiting.
|
||||
// `seen` waits for the send to be picked up first (or three seconds), so the
|
||||
// gap before the agent collects it is not read as "finished".
|
||||
function watchLearningBusy(busy) {
|
||||
const timer = setInterval(() => {
|
||||
if (page.learningBusy !== busy) return clearInterval(timer);
|
||||
if (Date.now() - busy.at > LEARNING_BUSY_MS) {
|
||||
clearInterval(timer);
|
||||
page.learningBusy = null;
|
||||
redraw();
|
||||
return;
|
||||
}
|
||||
if (page.pendingResult || agentWorking()) {
|
||||
busy.seen = true;
|
||||
return;
|
||||
}
|
||||
// Idle and never seen busy: the agent was quicker than the page's poll
|
||||
// (or the send is a moment from being picked up), so give it three seconds.
|
||||
if (!busy.seen && Date.now() - busy.at < 3000) return;
|
||||
clearInterval(timer);
|
||||
page.learningBusy = null;
|
||||
if (busy.what === "review") {
|
||||
hooks.banner("No lessons are waiting any more.", "info");
|
||||
setTimeout(() => hooks.banner(null), 4000);
|
||||
}
|
||||
redraw();
|
||||
}, 400);
|
||||
}
|
||||
|
||||
/**
|
||||
* The lessons dialog's Save lessons went through: the banner's place says
|
||||
* "Saving your lessons…" until the agent is done with the choices.
|
||||
*/
|
||||
export function beginLearningSave() {
|
||||
const sc = scan();
|
||||
const busy = { what: "decide", from: sc && sc.learning && sc.learning.state, at: Date.now(), seen: false };
|
||||
page.learningBusy = busy;
|
||||
watchLearningBusy(busy);
|
||||
setTimeout(redraw, LEARNING_BUSY_MS + 500);
|
||||
redraw();
|
||||
}
|
||||
|
||||
// A Learning control that changes the project: a __learning action to the
|
||||
// dashboard agent, posted the way sendDelete() posts. No hop: the agent runs
|
||||
// lessons.mjs, rescans and posts the menu again.
|
||||
export async function sendLearning(what) {
|
||||
if (hooks.learningBlocked()) return;
|
||||
// Switching on or off shows as working at once, before the agent answers.
|
||||
// Switching on or off, or opening Review, shows as working at once, before
|
||||
// the agent answers.
|
||||
const sc = scan();
|
||||
const from = sc && sc.learning && sc.learning.state;
|
||||
if ((what === "switch-on" || what === "switch-off") && from) {
|
||||
page.learningBusy = { what, from, at: Date.now() };
|
||||
if ((what === "switch-on" || what === "switch-off" || what === "review") && from) {
|
||||
const busy = { what, from, at: Date.now(), seen: false };
|
||||
page.learningBusy = busy;
|
||||
redraw();
|
||||
// One redraw at the limit, so a switch the agent never answers stops spinning.
|
||||
setTimeout(redraw, 30500);
|
||||
// One redraw at the limit, so an action the agent never answers stops spinning.
|
||||
setTimeout(redraw, LEARNING_BUSY_MS + 500);
|
||||
if (what === "review") watchLearningBusy(busy);
|
||||
}
|
||||
try {
|
||||
const res = await fetch("/submit", {
|
||||
|
||||
@@ -9,11 +9,11 @@
|
||||
// in dashboard.js, reached through hooks: a screen never imports another),
|
||||
// and is disabled whenever a card would be.
|
||||
|
||||
import { page, redraw, hooks } from "../state.js";
|
||||
import { page, redraw, hooks, stage } from "../state.js";
|
||||
import { el, mdInto, lightDismiss } from "../render.js";
|
||||
import { postJson } from "../net.js";
|
||||
import { sendState } from "../send-state.js";
|
||||
import { loopDriving, isOpen, stagedCount } from "../selectors.js";
|
||||
import { loopDriving, isOpen, stagedCount, learningBusyNow } from "../selectors.js";
|
||||
|
||||
const TOP_STATES = new Set(["not-set-up", "off", "paused"]);
|
||||
|
||||
@@ -48,23 +48,14 @@ export function learningBlocked() {
|
||||
return "";
|
||||
}
|
||||
|
||||
// How long a switch on or off may show as working before the row gives up
|
||||
// waiting for the new state (the agent normally answers in a second or two).
|
||||
const BUSY_MS = 30000;
|
||||
|
||||
/**
|
||||
* The switch the person just pressed while the agent works on it, or null.
|
||||
* sendLearning() sets page.learningBusy the instant a switch is pressed;
|
||||
* it ends when the scan shows a different state, the send fails, or BUSY_MS pass.
|
||||
* The switch the person just pressed while the agent works on it, or null (the
|
||||
* rule is learningBusyNow() in selectors.js; Review and a saved choice show in
|
||||
* the banner instead).
|
||||
*/
|
||||
function busySwitch(state) {
|
||||
const busy = page.learningBusy;
|
||||
if (!busy) return null;
|
||||
if (busy.from !== state || Date.now() - busy.at > BUSY_MS) {
|
||||
page.learningBusy = null;
|
||||
return null;
|
||||
}
|
||||
return busy;
|
||||
const busy = learningBusyNow(state);
|
||||
return busy && (busy.what === "switch-on" || busy.what === "switch-off") ? busy : null;
|
||||
}
|
||||
|
||||
function agentButton(text, cls, what) {
|
||||
@@ -203,6 +194,80 @@ function wireOutside() {
|
||||
|
||||
/* ------------------------------------------------------------- the dialogs */
|
||||
|
||||
// The dialog's own state, kept across the dashboard's redraws: a send from it
|
||||
// in flight (the card locks), and the "choices not saved" ask after a close
|
||||
// was asked for while something is chosen.
|
||||
let saving = false;
|
||||
let askingClose = false;
|
||||
// The card the dialog shows now, for the handlers made once with the dialog.
|
||||
let shownItem = null;
|
||||
|
||||
/** How many lessons the person has chosen on the card so far (its draft). */
|
||||
function chosenCount(item) {
|
||||
try {
|
||||
const d = JSON.parse(page.local.drafts["lessons-" + item.id] || "null");
|
||||
return d && d.picks && typeof d.picks === "object" ? Object.values(d.picks).filter(Boolean).length : 0;
|
||||
} catch {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the staged choices from the dialog. While it goes the card locks and
|
||||
* the button says Saving…; the dialog closes by itself once the send is in,
|
||||
* and the dashboard's banner takes over with "Saving your lessons…". `undo`
|
||||
* puts staged choices back the way they were when the send did not go.
|
||||
*/
|
||||
async function saveLessons(item, undo) {
|
||||
if (saving) return;
|
||||
saving = true;
|
||||
askingClose = false;
|
||||
redraw();
|
||||
try {
|
||||
await hooks.send();
|
||||
} finally {
|
||||
saving = false;
|
||||
}
|
||||
if (page.local.sent && page.local.sent[item.id]?.value) {
|
||||
hooks.beginLearningSave();
|
||||
return;
|
||||
}
|
||||
if (undo) undo();
|
||||
redraw();
|
||||
}
|
||||
|
||||
/** Not now: leave every lesson waiting, and tell the agent so it is not left holding a card no one can answer. */
|
||||
function leaveAllWaiting(item) {
|
||||
// A send that cannot go says so, rather than a close that does nothing.
|
||||
const state = sendState(true);
|
||||
if (state.disabled) {
|
||||
hooks.banner(state.why || "Plan2Code cannot take that right now. Try again in a moment.", "warn");
|
||||
return;
|
||||
}
|
||||
const before = page.local.staged[item.id];
|
||||
// A note typed in "Anything to add?" goes with it.
|
||||
stage(item.id, { type: "answer", kind: "lessons", decisions: [], ...(before && before.text ? { text: before.text } : {}) });
|
||||
return saveLessons(item, () => stage(item.id, before === undefined ? null : before));
|
||||
}
|
||||
|
||||
/** The close, the Not now button and Escape: straight out when nothing is chosen, an ask first when something is. */
|
||||
function requestClose(item) {
|
||||
if (saving) return;
|
||||
if (chosenCount(item) > 0) {
|
||||
askingClose = true;
|
||||
redraw();
|
||||
queueMicrotask(() => document.querySelector("#lessons-modal .lessons-unsaved .btn")?.focus());
|
||||
return;
|
||||
}
|
||||
leaveAllWaiting(item);
|
||||
}
|
||||
|
||||
function spinnerLabel(text) {
|
||||
const wrap = document.createDocumentFragment();
|
||||
wrap.append(el("span", "spinner tiny"), document.createTextNode(text));
|
||||
return wrap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Review from the dashboard (FR-18): the dashboard agent posts the lessons
|
||||
* card while the workflow is still `dashboard`, and its page draws no question
|
||||
@@ -220,45 +285,97 @@ export function renderLessonsDialog(on) {
|
||||
const sent = item && ((page.local.sent && page.local.sent[item.id]?.value) || item.submitted);
|
||||
let dlg = document.getElementById("lessons-modal");
|
||||
if (!item || sent) {
|
||||
if (dlg && dlg.open) dlg.close();
|
||||
if (dlg && dlg.open) {
|
||||
dlg.close();
|
||||
// The Review button that opened it is the place to land again.
|
||||
document.querySelector(".learn-banner button:not(:disabled)")?.focus();
|
||||
}
|
||||
askingClose = false;
|
||||
return;
|
||||
}
|
||||
shownItem = item;
|
||||
if (!dlg) {
|
||||
dlg = el("dialog", "modal lessons-dialog");
|
||||
dlg.id = "lessons-modal";
|
||||
dlg.setAttribute("aria-label", "Lessons to review");
|
||||
// Not light-dismissed, and Escape does not close it: the agent is waiting
|
||||
// on this card, and the dialog is the only place it is drawn. Leave them
|
||||
// all waiting, then Send, is the way out.
|
||||
dlg.addEventListener("cancel", (e) => e.preventDefault());
|
||||
// The agent is waiting on this card, and the dialog is the only place it
|
||||
// is drawn, so a close is never only a hide: Escape, the corner ×, Not now
|
||||
// and a click outside all leave every lesson waiting and say so (a click
|
||||
// outside only while nothing is chosen, so a stray one never costs picks).
|
||||
dlg.addEventListener("cancel", (e) => {
|
||||
e.preventDefault();
|
||||
if (shownItem) requestClose(shownItem);
|
||||
});
|
||||
lightDismiss(dlg, () => {
|
||||
if (shownItem && !saving && !chosenCount(shownItem)) leaveAllWaiting(shownItem);
|
||||
return false;
|
||||
});
|
||||
document.body.appendChild(dlg);
|
||||
}
|
||||
const scroll = dlg.scrollTop;
|
||||
dlg.classList.toggle("is-saving", saving);
|
||||
dlg.setAttribute("aria-busy", saving ? "true" : "false");
|
||||
const card = hooks.renderCard(item);
|
||||
if (saving) card.inert = true;
|
||||
const x = el("button", "modal-x", "×");
|
||||
x.type = "button";
|
||||
x.setAttribute("aria-label", "Close without saving");
|
||||
x.disabled = saving;
|
||||
x.addEventListener("click", () => requestClose(item));
|
||||
const actions = el("div", "modal-actions");
|
||||
const go = el("button", "btn primary", "Send to Plan2Code");
|
||||
go.type = "button";
|
||||
go.id = "lessons-send";
|
||||
go.addEventListener("click", () => hooks.send());
|
||||
actions.appendChild(go);
|
||||
if (stagedCount() > 1) actions.appendChild(el("p", "help", "Anything else ready to send goes with it."));
|
||||
dlg.replaceChildren(card, actions);
|
||||
if (askingClose && !saving) {
|
||||
const ask = el("div", "lessons-unsaved");
|
||||
ask.setAttribute("role", "alert");
|
||||
ask.appendChild(el("p", "lessons-unsaved-text", "You have choices that are not saved."));
|
||||
const row = el("div", "modal-actions");
|
||||
const keep = el("button", "btn ghost", "Keep editing");
|
||||
keep.type = "button";
|
||||
keep.addEventListener("click", () => {
|
||||
askingClose = false;
|
||||
redraw();
|
||||
});
|
||||
const drop = el("button", "btn danger", "Discard and close");
|
||||
drop.type = "button";
|
||||
drop.addEventListener("click", () => leaveAllWaiting(item));
|
||||
const save = el("button", "btn primary", "Save lessons");
|
||||
save.type = "button";
|
||||
save.disabled = !page.local.staged[item.id] || sendState().disabled;
|
||||
save.addEventListener("click", () => saveLessons(item));
|
||||
row.append(keep, drop, save);
|
||||
ask.appendChild(row);
|
||||
actions.appendChild(ask);
|
||||
} else {
|
||||
const later = el("button", "btn ghost", "Not now");
|
||||
later.type = "button";
|
||||
later.id = "lessons-later";
|
||||
later.disabled = saving;
|
||||
later.addEventListener("click", () => requestClose(item));
|
||||
const go = el("button", "btn primary");
|
||||
go.type = "button";
|
||||
go.id = "lessons-send";
|
||||
if (saving) go.appendChild(spinnerLabel("Saving…"));
|
||||
else go.textContent = "Save lessons";
|
||||
go.addEventListener("click", () => saveLessons(item));
|
||||
actions.append(later, go);
|
||||
if (stagedCount() > 1) actions.appendChild(el("p", "help", "Anything else ready to send goes with it."));
|
||||
}
|
||||
dlg.replaceChildren(card, actions, x);
|
||||
refreshLessonsSend();
|
||||
if (!dlg.open) dlg.showModal();
|
||||
dlg.scrollTop = scroll;
|
||||
}
|
||||
|
||||
/**
|
||||
* The dialog's Send, set again whenever what is staged changes, including the
|
||||
* quiet staging of typing (a Reword field emptied, a note): on only while the
|
||||
* lessons card holds an answer and a send can go.
|
||||
* The dialog's Save lessons, set again whenever what is staged changes,
|
||||
* including the quiet staging of typing (a Reword field emptied, a note): on
|
||||
* only while the lessons card holds an answer and a send can go.
|
||||
*/
|
||||
export function refreshLessonsSend() {
|
||||
const go = document.getElementById("lessons-send");
|
||||
if (!go) return;
|
||||
const items = (page.S && Array.isArray(page.S.items) && page.S.items) || [];
|
||||
const item = items.find((i) => i && i.kind === "lessons" && isOpen(i));
|
||||
go.disabled = !item || !page.local.staged[item.id] || sendState().disabled;
|
||||
go.disabled = saving || !item || !page.local.staged[item.id] || sendState().disabled;
|
||||
}
|
||||
|
||||
function dialog(id, cls, title) {
|
||||
|
||||
@@ -12,6 +12,31 @@ import { ASK_BUSY_MS, chatAnswering } from "./chat.js";
|
||||
|
||||
export const WORKING_GRACE_MS = 5 * 60 * 1000;
|
||||
|
||||
// How long a Learning action may show as working before the page gives up
|
||||
// waiting for the agent (it normally answers in a second or two).
|
||||
export const LEARNING_BUSY_MS = 30000;
|
||||
|
||||
/**
|
||||
* The Learning action the person just pressed while the agent works on it, or
|
||||
* null. sendLearning() (dashboard.js) sets page.learningBusy the instant a
|
||||
* button is pressed: `switch-on` and `switch-off` end when the scan shows a
|
||||
* different state than `from`, `review` ends when its lessons card is open,
|
||||
* and any of them ends after LEARNING_BUSY_MS. The agent finishing with
|
||||
* nothing to show is ended by sendLearning()'s watcher, not here.
|
||||
*/
|
||||
export function learningBusyNow(state) {
|
||||
const busy = page.learningBusy;
|
||||
if (!busy) return null;
|
||||
const end = () => {
|
||||
page.learningBusy = null;
|
||||
return null;
|
||||
};
|
||||
if (Date.now() - busy.at > LEARNING_BUSY_MS) return end();
|
||||
if ((busy.what === "switch-on" || busy.what === "switch-off") && busy.from !== state) return end();
|
||||
if (busy.what === "review" && ((page.S && page.S.items) || []).some((i) => i && i.kind === "lessons" && isOpen(i))) return end();
|
||||
return busy;
|
||||
}
|
||||
|
||||
// Two minutes with the agent mid-turn, three when it is merely meant to be
|
||||
// listening. The difference is the cost of being wrong: a working agent that
|
||||
// goes quiet is worth mentioning early, whereas the gap between `wait`
|
||||
|
||||
@@ -197,6 +197,39 @@
|
||||
.modal.lessons-dialog { width: min(720px, calc(100vw - 32px)); }
|
||||
.lessons-dialog .card { margin: 0; box-shadow: none; border: 0; padding: 0; }
|
||||
|
||||
/* Switching Learning on or off: shown the moment the button is pressed. */
|
||||
/* The lessons dialog: closable (the corner ×, Not now, Escape), and locked
|
||||
while Save lessons goes. */
|
||||
.lessons-dialog .modal-actions { align-items: center; justify-content: flex-end; }
|
||||
.lessons-dialog .modal-actions .btn { margin: 0; }
|
||||
.lessons-dialog .modal-actions .btn .spinner { margin-right: 8px; }
|
||||
.modal-x {
|
||||
position: absolute;
|
||||
top: 10px;
|
||||
right: 12px;
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
padding: 0;
|
||||
font-size: 22px;
|
||||
line-height: 1;
|
||||
color: var(--ink-3);
|
||||
background: none;
|
||||
border: 0;
|
||||
border-radius: calc(var(--radius) * 0.5);
|
||||
cursor: pointer;
|
||||
}
|
||||
.modal-x:hover:not(:disabled),
|
||||
.modal-x:focus-visible { color: var(--ink); background: var(--panel-2); }
|
||||
.modal-x:disabled { opacity: 0.4; cursor: default; }
|
||||
.lessons-dialog.is-saving .card { opacity: 0.6; }
|
||||
.lessons-unsaved { flex: 1 1 100%; padding: 10px 12px; border-left: 4px solid var(--warn); background: var(--warn-soft); border-radius: calc(var(--radius) * 0.5); }
|
||||
.lessons-unsaved .modal-actions { flex-wrap: wrap; }
|
||||
.lessons-unsaved .btn { white-space: nowrap; }
|
||||
.lessons-unsaved-text { margin: 0 0 8px; font-size: 13.5px; color: var(--ink); }
|
||||
|
||||
/* Switching Learning on or off, opening Review, saving: shown the moment the
|
||||
button is pressed. */
|
||||
.learn-busy { display: inline-flex; align-items: center; gap: 8px; color: var(--ink-3); }
|
||||
.learn-banner.is-busy .dash-nudge-text { display: inline-flex; align-items: center; gap: 8px; }
|
||||
.learn-row.is-busy .learn-pill { opacity: 0.6; }
|
||||
Reference in new issue
Block a user