Public: anyone can read this; the crew can change it.
/next archives or snoozes many decisions at once and undoes the batch in one tap #15868
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15835-next-archives-or-snoozes-many-decisions → main 0 commits fork 4 threads · 4 unresolved
9 files
changed +458
−7
docs/features/next-batch.md added +11
| @@ -0,0 +1,11 @@ | ||
| 1 | --- | |
| 2 | topic: Getting around: keys, the palette and search | |
| 3 | row: Snooze many decisions on /next at once | |
| 4 | --- | |
| 5 | # Snooze many decisions on /next | |
| 6 | ||
| 7 | Web: `src/web/pages/next_batch.ts`. Tick the box on rows of `/next`, press **Snooze a day** in the filter bar; **Undo (n)** brings the batch back for 60 seconds. | |
| 8 | ||
| 9 | Terminal: `src/tui/screens/next.ts`. `x` ticks the row under the cursor, `s` snoozes the ticked rows for a day, `z` takes the last batch back. | |
| 10 | ||
| 11 | Both doors call `snoozeMany` and `undoMany` in `src/actions/next.ts`: each row is its own move with the single move's permission and refusals, a row that cannot move is skipped and named, and a batch is at most 50 rows. Readers and agents see no tick boxes and no keys. |
src/actions/next.ts +106
| @@ -8,6 +8,8 @@ import type { App } from "../app.ts"; | ||
| 8 | 8 | import { addDependency, repoGraph } from "../core/dependencies.ts"; |
| 9 | 9 | import { voidOpen } from "../core/estimates.ts"; |
| 10 | 10 | import { getIssue, type Issue } from "../core/issues.ts"; |
| 11 | import { setSnooze, snoozeOf } from "../core/gate_snooze.ts"; | |
| 12 | import { liftHold } from "./gate.ts"; | |
| 11 | 13 | import { isStructural, pillarsOf } from "../core/pillars.ts"; |
| 12 | 14 | import { clearRank, lift, MAX_RANKED, ranksInRepo, writeOrder } from "../core/rank.ts"; |
| 13 | 15 | import { nextUp } from "../core/next_up.ts"; |
| @@ -116,3 +118,107 @@ export function stepTo(numbers: readonly number[], at: number, step: -1 | 1): st | ||
| 116 | 118 | if (at < 1) return undefined; |
| 117 | 119 | return at === 1 ? "top" : `after:${numbers[at - 2]}`; |
| 118 | 120 | } |
| 121 | ||
| 122 | /* Batch snooze (#15835): snooze many decisions at once, and take the batch back. | |
| 123 | * Each row is its own `rankIssue`; undo lifts what this person snoozed in the last minute. */ | |
| 124 | ||
| 125 | /** The most rows one batch takes. */ | |
| 126 | export const BATCH_MAX = 50; | |
| 127 | /** How long the undo of a batch lasts. */ | |
| 128 | export const UNDO_MS = 60_000; | |
| 129 | const DAY_MS = 86_400_000; | |
| 130 | ||
| 131 | export const BATCH_NONE = "Tick at least one row."; | |
| 132 | export const BATCH_TOO_MANY = `One batch takes at most ${BATCH_MAX} rows; tick fewer.`; | |
| 133 | export const UNDO_LATE = "The minute to undo has passed, or someone else changed it."; | |
| 134 | ||
| 135 | export interface Skipped { | |
| 136 | readonly number: number; | |
| 137 | readonly reason: string; | |
| 138 | } | |
| 139 | export interface BatchDone { | |
| 140 | readonly done: number[]; | |
| 141 | readonly skipped: Skipped[]; | |
| 142 | } | |
| 143 | ||
| 144 | /** Issue numbers from form values: whole positive numbers, each once, in order given. */ | |
| 145 | export function numbersOf(values: readonly string[]): number[] { | |
| 146 | const seen = new Set<number>(); | |
| 147 | for (const v of values) | |
| 148 | if (/^[1-9][0-9]{0,8}$/.test(v)) seen.add(Number(v)); | |
| 149 | return [...seen]; | |
| 150 | } | |
| 151 | ||
| 152 | function gateway(app: App, actor: User, repo: Repo, numbers: readonly number[]): Result<void, string> { | |
| 153 | if (numbers.length === 0) return err(BATCH_NONE); | |
| 154 | if (numbers.length > BATCH_MAX) return err(BATCH_TOO_MANY); | |
| 155 | if (actor.kind === "agent" || !canWrite(app.db, repo, actor)) return err(NEXT_FORBIDDEN); | |
| 156 | return ok(undefined); | |
| 157 | } | |
| 158 | ||
| 159 | /** Snooze every number for a day. Each row is checked on its own. */ | |
| 160 | export function snoozeMany( | |
| 161 | app: App, | |
| 162 | actor: User, | |
| 163 | repo: Repo, | |
| 164 | numbers: readonly number[], | |
| 165 | now: Date = new Date(), | |
| 166 | ): Result<BatchDone, string> { | |
| 167 | const gate = gateway(app, actor, repo, numbers); | |
| 168 | if (!gate.ok) return gate; | |
| 169 | const until = new Date(now.getTime() + DAY_MS).toISOString(); | |
| 170 | const done: number[] = []; | |
| 171 | const skipped: Skipped[] = []; | |
| 172 | for (const number of numbers) { | |
| 173 | const moved = rankIssue(app, actor, repo, number, { to: "not_now" }); | |
| 174 | if (!moved.ok) { | |
| 175 | skipped.push({ number, reason: moved.error.message }); | |
| 176 | continue; | |
| 177 | } | |
| 178 | setSnooze(app.db, moved.value.id, until, actor.id, now); | |
| 179 | app.bus.append({ kind: "issue.snoozed", repoId: repo.id, actorId: actor.id, payload: { number, until } }); | |
| 180 | done.push(number); | |
| 181 | } | |
| 182 | return ok({ done, skipped }); | |
| 183 | } | |
| 184 | ||
| 185 | /** Whether this number is still a snooze `actor` set within the undo minute. */ | |
| 186 | export function undoable(app: App, actor: User, repo: Repo, number: number, now: Date): boolean { | |
| 187 | const issue = getIssue(app.db, repo.id, number); | |
| 188 | const s = issue === undefined ? undefined : snoozeOf(app.db, issue.id); | |
| 189 | return s !== undefined && s.setBy === actor.id && now.getTime() - Date.parse(s.createdAt) <= UNDO_MS; | |
| 190 | } | |
| 191 | ||
| 192 | /** Bring back the rows of a batch still inside its minute. */ | |
| 193 | export function undoMany( | |
| 194 | app: App, | |
| 195 | actor: User, | |
| 196 | repo: Repo, | |
| 197 | numbers: readonly number[], | |
| 198 | now: Date = new Date(), | |
| 199 | ): Result<BatchDone, string> { | |
| 200 | const gate = gateway(app, actor, repo, numbers); | |
| 201 | if (!gate.ok) return gate; | |
| 202 | const done: number[] = []; | |
| 203 | const skipped: Skipped[] = []; | |
| 204 | for (const number of numbers) { | |
| 205 | if (!undoable(app, actor, repo, number, now)) { | |
| 206 | skipped.push({ number, reason: UNDO_LATE }); | |
| 207 | continue; | |
| 208 | } | |
| 209 | const lifted = liftHold(app, actor, repo, getIssue(app.db, repo.id, number)!); | |
| 210 | if (lifted.ok) done.push(number); | |
| 211 | else skipped.push({ number, reason: `Could not bring it back (${String(lifted.error)}).` }); | |
| 212 | } | |
| 213 | return ok({ done, skipped }); | |
| 214 | } | |
| 215 | ||
| 216 | const list = (ns: readonly number[]) => ns.map((n) => `#${n}`).join(", "); | |
| 217 | ||
| 218 | /** The one sentence a page or a script gets after a batch. */ | |
| 219 | export function batchWords(verb: "Snoozed" | "Brought back", r: BatchDone): string { | |
| 220 | const head = r.done.length === 0 ? `${verb} nothing.` : `${verb} ${list(r.done)}.`; | |
| 221 | return r.skipped.length === 0 | |
| 222 | ? head | |
| 223 | : `${head} Skipped: ${r.skipped.map((s) => `#${s.number} (${s.reason})`).join("; ")}`; | |
| 224 | } |
src/core/gate_snooze.ts +10
| @@ -56,3 +56,13 @@ export function dueSnoozes(db: Database, now: Date, limit = 200): Snooze[] { | ||
| 56 | 56 | .all(now.toISOString(), limit) |
| 57 | 57 | .map((r) => ({ issueId: r.issue_id, until: r.until, setBy: r.set_by })); |
| 58 | 58 | } |
| 59 | ||
| 60 | /** The snooze on one issue, or undefined when it has none. */ | |
| 61 | export function snoozeOf(db: Database, issueId: string): (Snooze & { readonly createdAt: string }) | undefined { | |
| 62 | const r = db | |
| 63 | .query<{ until: string; set_by: string; created_at: string }, [string]>( | |
| 64 | `SELECT until, set_by, created_at FROM gate_snooze WHERE issue_id = ?1`, | |
| 65 | ) | |
| 66 | .get(issueId); | |
| 67 | return r === null ? undefined : { issueId, until: r.until, setBy: r.set_by, createdAt: r.created_at }; | |
| 68 | } |
src/http/server.ts +2
| @@ -283,6 +283,7 @@ import { mapPage } from "../web/pages/map.ts"; | ||
| 283 | 283 | import { spaceJson, spacePage } from "../web/pages/space.ts"; |
| 284 | 284 | import { firstSubmit } from "../web/pages/first.ts"; |
| 285 | 285 | import { nextPage, nextSubmit } from "../web/pages/next.ts"; |
| 286 | import { nextBatch } from "../web/pages/next_batch.ts"; | |
| 286 | 287 | import { weekPage, weekSubmit } from "../web/pages/week.ts"; |
| 287 | 288 | import { costCsvDownload, costPage } from "../web/pages/cost.ts"; |
| 288 | 289 | import { |
| @@ -1487,6 +1488,7 @@ export function buildRouter(): Router<Handler> { | ||
| 1487 | 1488 | router.add("GET", "/:owner/:name/space.json", spaceJson); |
| 1488 | 1489 | router.add("GET", "/:owner/:name/next", nextPage); |
| 1489 | 1490 | router.add("POST", "/:owner/:name/next", nextSubmit); |
| 1491 | router.add("POST", "/:owner/:name/next/batch", nextBatch); | |
| 1490 | 1492 | router.add("GET", "/:owner/:name/week", weekPage); |
| 1491 | 1493 | router.add("GET", "/:owner/:name/cost", costPage); |
| 1492 | 1494 | router.add("GET", "/:owner/:name/cost.csv", costCsvDownload); |
src/tui/screens/next.ts +41 −5
| @@ -5,15 +5,17 @@ | ||
| 5 | 5 | * reader sees the order and no keys to change it, an agent likewise, and a |
| 6 | 6 | * guest sees what `canRead` lets the web show, sealed issues left to the same |
| 7 | 7 | * scope the page uses. `t` puts the issue under the cursor on top, `u` and |
| 8 | * `d` move it a place, `n` puts it behind the Later gate; each saves at once. | |
| 9 | * Subscribed to the bus while on the stack, so a move made on the web, over | |
| 10 | * MCP or in another terminal reorders the list here, and one made here | |
| 11 | * (`issue.ranked`) reaches the web the same way. | |
| 8 | * `d` move it a place, `x` ticks a row, `s` snoozes the ticked rows a day and `z` takes | |
| 9 | * that batch back (the page's tick boxes and Undo, #15835), `n` puts it behind the Later gate; each | |
| 10 | * saves at once. Subscribed to the bus while on the stack, so a move made on the web, over MCP or | |
| 11 | * in another terminal reorders the list here, and one made here (`issue.ranked`) reaches the web | |
| 12 | * the same way. | |
| 12 | 13 | */ |
| 13 | 14 | |
| 14 | 15 | import { rankedWords, rankIssue, stepTo, type Place, placeOf } from "../../actions/next.ts"; |
| 15 | 16 | import { listChapters, itemsOf, chapterLineWords } from "../../core/chapters.ts"; |
| 16 | 17 | import { nextUp, type NextRow } from "../../core/next_up.ts"; |
| 18 | import { batchWords, snoozeMany, undoMany } from "../../actions/next.ts"; | |
| 17 | 19 | import { setPlannerSwitch } from "../../actions/planner_switch.ts"; |
| 18 | 20 | import { pauseAt, weekLine, type WeekLine } from "../../core/growth.ts"; |
| 19 | 21 | import { pauseSentence } from "../../core/planner_pause.ts"; |
| @@ -157,6 +159,34 @@ export function nextScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 157 | 159 | rank(to === undefined ? undefined : placeOf(to)); |
| 158 | 160 | }; |
| 159 | 161 | |
| 162 | // Ticked rows (by issue number) and the last batch snoozed here: the page's tick boxes and | |
| 163 | // Undo, through the same snoozeMany/undoMany. | |
| 164 | const ticked = new Set<number>(); | |
| 165 | let lastBatch: number[] = []; | |
| 166 | const tickRow = (): void => { | |
| 167 | const row = current(); | |
| 168 | if (row === undefined || !may()) return; | |
| 169 | const n = row.issue.number; | |
| 170 | if (!ticked.delete(n)) ticked.add(n); | |
| 171 | ctx.invalidate(); | |
| 172 | }; | |
| 173 | const batch = (undoing: boolean): void => { | |
| 174 | if (!may()) return; | |
| 175 | const numbers = undoing ? lastBatch : [...ticked]; | |
| 176 | const done = undoing ? undoMany(f.app, f.user!, repo, numbers) : snoozeMany(f.app, f.user!, repo, numbers); | |
| 177 | if (!done.ok) { | |
| 178 | ctx.toast(done.error, "danger"); | |
| 179 | return; | |
| 180 | } | |
| 181 | if (undoing) lastBatch = lastBatch.filter((n) => !done.value.done.includes(n)); | |
| 182 | else { | |
| 183 | lastBatch = done.value.done; | |
| 184 | for (const n of done.value.done) ticked.delete(n); | |
| 185 | } | |
| 186 | reload(); | |
| 187 | ctx.toast(batchWords(undoing ? "Brought back" : "Snoozed", done.value), done.value.done.length > 0 ? "success" : "danger"); | |
| 188 | }; | |
| 189 | ||
| 160 | 190 | const open = (): void => { |
| 161 | 191 | const row = current(); |
| 162 | 192 | if (row !== undefined) ctx.push(issueScreen(f, ctx, local(f, repo), row.issue.number)); |
| @@ -170,7 +200,7 @@ export function nextScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 170 | 200 | const line = week === undefined ? null : lineAfter(week); |
| 171 | 201 | const ends = line !== null && line > 0 && at < line && (shown[i + 1] ?? line) >= line; |
| 172 | 202 | return { |
| 173 | title: `${at + 1}. #${r.issue.number} ${r.issue.title}`, | |
| 203 | title: `${ticked.has(r.issue.number) ? "[x] " : ""}${at + 1}. #${r.issue.number} ${r.issue.title}`, | |
| 174 | 204 | secondary: `${r.byOwner ? "Owner's pick · " : ""}${r.role === "blocker" ? "Blocker" : "Enabler"}`, |
| 175 | 205 | meta: [stateWord(r, { stage: stages.get(r.issue.number) ?? "issue", waits: r.waits, held: r.state === "held", onIt: r.onIt !== null }), r.onIt ?? "next", estimate === undefined ? "" : rangeWords(estimate), r.why ?? ""] |
| 176 | 206 | .filter((x) => x !== "") |
| @@ -190,6 +220,9 @@ export function nextScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 190 | 220 | binding(["u"], "up", { disabled: !on || rows.indexOf(current()!) === 0 }), |
| 191 | 221 | binding(["d"], "down", { disabled: !on || rows.indexOf(current()!) >= rows.length - 1 }), |
| 192 | 222 | binding(["l"], "not now", { disabled: !on }), |
| 223 | binding(["x"], "tick", { disabled: !on }), | |
| 224 | binding(["s"], "snooze", { disabled: !may() || ticked.size === 0 }), | |
| 225 | binding(["z"], "undo", { disabled: !may() || lastBatch.length === 0 }), | |
| 193 | 226 | binding(["/"], "filter"), |
| 194 | 227 | ...(owns() && growing !== undefined ? [binding(["p"], growing.until === null ? "pause planner" : "planner on")] : []), |
| 195 | 228 | ]; |
| @@ -224,6 +257,9 @@ export function nextScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 224 | 257 | else if (e.key === "t") rank({ to: "top" }); |
| 225 | 258 | else if (e.key === "u") step(-1); |
| 226 | 259 | else if (e.key === "d") step(1); |
| 260 | else if (e.key === "x") tickRow(); | |
| 261 | else if (e.key === "s") batch(false); | |
| 262 | else if (e.key === "z") batch(true); | |
| 227 | 263 | else if (e.key === "/") ask(); |
| 228 | 264 | else if (e.key === "p") pause(); |
| 229 | 265 | else if (e.key === "l") rank({ to: "not_now" }); |
src/web/pages/next.ts +6 −2
| @@ -30,6 +30,7 @@ import { btn, kbd, listCard, row } from "../ui.ts"; | ||
| 30 | 30 | import { GROWTH_DAYS, growthLine } from "./next_growth.ts"; |
| 31 | 31 | import { listChapters } from "../../core/chapters.ts"; |
| 32 | 32 | import { chapterLine, chaptersSection } from "./next_chapters.ts"; |
| 33 | import { batchButtons, undoButton, batchForms, tick, undoLive } from "./next_batch.ts"; | |
| 33 | 34 | import { countWords, filterPath, markBefore, filtered, matches, readFilter } from "./next_filter.ts"; |
| 34 | 35 | |
| 35 | 36 | /** |
| @@ -177,7 +178,7 @@ function line( | ||
| 177 | 178 | const up = stepTo(numbers, at, -1); |
| 178 | 179 | const down = stepTo(numbers, at, 1); |
| 179 | 180 | const controls = writer |
| 180 | ? html`${at > 0 || away ? move(base, n, "top", "Top", "t", back) : null}${away | |
| 181 | ? html`${away ? null : tick(n)}${at > 0 || away ? move(base, n, "top", "Top", "t", back) : null}${away | |
| 181 | 182 | ? null |
| 182 | 183 | : html`${up === undefined ? null : move(base, n, up, "Up", "u", back)}${down === undefined |
| 183 | 184 | ? null |
| @@ -263,11 +264,13 @@ export function nextPage(ctx: Ctx): Response { | ||
| 263 | 264 | : filter.pillar !== null && filter.pillar !== "Later" && !pillars!.pillars.some((p) => p.short === filter.pillar) |
| 264 | 265 | ? `This repository has no pillar ${filter.pillar}.` |
| 265 | 266 | : "Nothing here matches."; |
| 267 | const live = writer ? undoLive(ctx, repo) : []; | |
| 266 | 268 | const bar = html`<form method="get" action="${base}/next" class="form-actions" role="search"> |
| 267 | 269 | <input class="input" name="pillar" type="text" maxlength="8" value="${filter.pillar ?? ""}" placeholder="Pillar: P7 or Later" aria-label="Pillar" autocomplete="off" /> |
| 268 | 270 | <input class="input" name="q" type="search" maxlength="80" value="${filter.q}" placeholder="A word in the title" aria-label="Filter by word" autocomplete="off" /> |
| 269 | 271 | ${btn("Filter", { type: "submit", size: "sm" })} |
| 270 | 272 | ${isFiltered ? html`<a href="${base}/next">Clear</a>` : null} |
| 273 | ${writer ? batchButtons(live) : null} | |
| 271 | 274 | </form>`; |
| 272 | 275 | const chapters = listChapters(ctx.app.db, repo.id); |
| 273 | 276 | const body = html`${repoHeader(ctx, repo, "issues")} |
| @@ -276,8 +279,9 @@ export function nextPage(ctx: Ctx): Response { | ||
| 276 | 279 | ${chapterLine(ctx, repo, chapters)} |
| 277 | 280 | ${growthLine(week7, pauseAt(week7, new Date()), writer && canAdmin(ctx.app.db, repo, ctx.auth.user!), base, back)} |
| 278 | 281 | <p class="error" id="next-flash" role="alert" hidden></p> |
| 282 | ${writer ? batchForms(base, back, live) : null} | |
| 279 | 283 | ${rows.length === 0 |
| 280 | ? html`<p class="muted">Nothing is ready: everything open waits for something. <a href="${base}/map">See the map</a>.</p>` | |
| 284 | ? html`<p class="muted">Nothing is ready: everything open waits for something. <a href="${base}/map">See the map</a>. ${undoButton(live)}</p>` | |
| 281 | 285 | : html`${bar}${isFiltered ? html`<p class="muted" id="next-count">${countWords(ats.length, rows.length)} shown${ats.length + parked.length === 0 ? html`. ${nothingWords}` : null}. Numbers and the cut line are the whole list's; the week's capacity bar is left out while filtered.</p>` : null} |
| 282 | 286 | ${first.length > 0 ? section(isFiltered ? "Matching" : "Up next", first, true, "next-list") : null}${rest.length > 0 ? section("After", rest, false) : null}`} |
| 283 | 287 | ${chaptersSection(ctx, repo, back, chapters)} |
src/web/pages/next_batch.ts added +72
| @@ -0,0 +1,72 @@ | ||
| 1 | /** | |
| 2 | * Snooze many rows of `/next` at once and undo the batch (#15835). The ticked | |
| 3 | * rows are checkboxes tied to one form by `form="next-batch"`, so no script is | |
| 4 | * needed; the POST lands back on the page with `?undo=` naming the rows, and | |
| 5 | * the page offers Undo only for rows still inside their minute. | |
| 6 | */ | |
| 7 | import { batchWords, BATCH_MAX, numbersOf, snoozeMany, undoable, undoMany, UNDO_MS } from "../../actions/next.ts"; | |
| 8 | import type { Ctx } from "../../http/context.ts"; | |
| 9 | import { loadRepo, notFoundPage, repoBase, signInFirst } from "../chrome.ts"; | |
| 10 | import { backTo, readForm } from "../form.ts"; | |
| 11 | import { html, type Html } from "../html.ts"; | |
| 12 | import { flashOrJson } from "../layout.ts"; | |
| 13 | import { btn } from "../ui.ts"; | |
| 14 | import type { Repo } from "../../core/repos.ts"; | |
| 15 | ||
| 16 | export const BATCH_FORM = "next-batch"; | |
| 17 | ||
| 18 | /** The checkbox on a row, tied to the batch form: bare, named by its aria-label. */ | |
| 19 | export function tick(number: number): Html { | |
| 20 | return html`<input type="checkbox" name="n" value="${String(number)}" form="${BATCH_FORM}" aria-label="Snooze #${String(number)}" />`; | |
| 21 | } | |
| 22 | ||
| 23 | /** Numbers named in `?undo=1,2,3` that this person can still undo. */ | |
| 24 | export function undoLive(ctx: Ctx, repo: Repo): number[] { | |
| 25 | const user = ctx.auth.user; | |
| 26 | const asked = ctx.url.searchParams.get("undo"); | |
| 27 | if (!user || asked === null) return []; | |
| 28 | const now = new Date(); | |
| 29 | return numbersOf(asked.split(",").slice(0, BATCH_MAX)).filter((n) => undoable(ctx.app, user, repo, n, now)); | |
outdated
Principle 1: "Tick rows below, at most 50 at a time" explains the control twice. The server already says it when the cap is hit; drop the span. |
||
| 30 | } | |
| 31 | ||
| 32 | /** The two forms the buttons and boxes point at; they draw nothing themselves. */ | |
| 33 | export function batchForms(base: string, back: string, live: readonly number[]): Html { | |
| 34 | return html`<form method="post" action="${base}/next/batch" id="${BATCH_FORM}" hidden> | |
| 35 | <input type="hidden" name="back" value="${back}" /> | |
| 36 | <input type="hidden" name="do" value="snooze" /> | |
| 37 | </form>${live.length === 0 ? null : html`<form method="post" action="${base}/next/batch" id="next-undo" hidden> | |
| 38 | <input type="hidden" name="back" value="${back}" /> | |
| 39 | <input type="hidden" name="do" value="undo" /> | |
| 40 | ${live.map((n) => html`<input type="hidden" name="n" value="${String(n)}" />`)} | |
| 41 | </form>`}`; | |
| 42 | } | |
| 43 | ||
| 44 | /** Undo for the last minute's batch, tied to its form. */ | |
| 45 | export function undoButton(live: readonly number[]): Html | null { | |
outdated
Principle 1: the button already says Undo (n); "Brings n back; open for 60 seconds" repeats it. Drop or reduce to the 60 seconds only. |
||
| 46 | return live.length === 0 ? null : btn(`Undo (${String(live.length)})`, { type: "submit", size: "sm", variant: "outline", form: "next-undo", title: `Open for ${String(UNDO_MS / 1000)} seconds` }); | |
| 47 | } | |
| 48 | ||
| 49 | /** The buttons for the filter bar: snooze what is ticked, and undo. */ | |
| 50 | export function batchButtons(live: readonly number[]): Html { | |
| 51 | return html`${btn("Snooze a day", { type: "submit", size: "sm", variant: "outline", form: BATCH_FORM })}${undoButton(live)}`; | |
| 52 | } | |
| 53 | ||
| 54 | /** POST /:owner/:name/next/batch — `do` is `snooze` or `undo`; `n` is each ticked issue number. */ | |
| 55 | export async function nextBatch(ctx: Ctx): Promise<Response> { | |
| 56 | const repo = loadRepo(ctx); | |
| 57 | if (!repo) return notFoundPage(ctx); | |
| 58 | if (!ctx.auth.user) return signInFirst(ctx); | |
| 59 | const form = await readForm(ctx, "rw"); | |
| 60 | if (form instanceof Response) return form; | |
| 61 | const dest = backTo(form, `${repoBase(repo)}/next`); | |
| 62 | const values = form.getAll("n").filter((v): v is string => typeof v === "string"); | |
| 63 | // Bounded before any parsing: a body of thousands of fields is refused, not walked. | |
| 64 | if (values.length > BATCH_MAX * 4) return flashOrJson(ctx, dest, { tone: "danger", message: `One batch takes at most ${BATCH_MAX} rows; tick fewer.` }); | |
| 65 | const numbers = numbersOf(values); | |
| 66 | const undoing = form.get("do") === "undo"; | |
| 67 | const result = undoing ? undoMany(ctx.app, ctx.auth.user, repo, numbers) : snoozeMany(ctx.app, ctx.auth.user, repo, numbers); | |
| 68 | if (!result.ok) return flashOrJson(ctx, dest, { tone: "danger", message: result.error }); | |
| 69 | const message = batchWords(undoing ? "Brought back" : "Snoozed", result.value); | |
| 70 | const to = undoing || result.value.done.length === 0 ? dest : dest + (dest.includes("?") ? "&" : "?") + "undo=" + result.value.done.join(","); | |
| 71 | return flashOrJson(ctx, to, { message }, { done: result.value.done, skipped: result.value.skipped }); | |
| 72 | } |
test/next-batch-snooze.test.ts added +118
| @@ -0,0 +1,118 @@ | ||
| 1 | /** Tick rows on /next, snooze them a day, undo the batch in one tap (#15835). */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { openIssue, setIssueState } from "../src/actions/issues.ts"; | |
| 8 | import { createRepo } from "../src/actions/repos.ts"; | |
| 9 | import { gateHolding } from "../src/core/gate_hold.ts"; | |
| 10 | import { snoozeOf } from "../src/core/gate_snooze.ts"; | |
| 11 | import { getIssue, type Issue } from "../src/core/issues.ts"; | |
| 12 | import type { Repo } from "../src/core/repos.ts"; | |
| 13 | import { createSession } from "../src/core/sessions.ts"; | |
| 14 | import { createUser, type User } from "../src/core/users.ts"; | |
| 15 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 16 | import { snoozeMany, undoMany, UNDO_MS } from "../src/actions/next.ts"; | |
| 17 | ||
| 18 | let dir: string; | |
| 19 | let app: App; | |
| 20 | let servers: Servers; | |
| 21 | let alice: User; | |
| 22 | let bob: User; | |
| 23 | let cookie: string; | |
| 24 | let bobCookie: string; | |
| 25 | let repo: Repo; | |
| 26 | const made: Issue[] = []; | |
| 27 | const openOne = (title: string): Issue => { | |
| 28 | const opened = openIssue(app, alice, repo, { title }); | |
| 29 | if (!opened.ok) throw new Error(`could not open "${title}": ${String(opened.error)}`); | |
| 30 | return opened.value; | |
| 31 | }; | |
| 32 | const held = (n: number) => gateHolding(app.db, repo, getIssue(app.db, repo.id, n)!) !== undefined; | |
| 33 | const post = (path: string, body: URLSearchParams, c = cookie) => | |
| 34 | fetch(`${servers.url}${path}`, { method: "POST", body, redirect: "manual", headers: { cookie: c, origin: servers.url, accept: "application/json" } }); | |
| 35 | ||
| 36 | beforeAll(async () => { | |
| 37 | dir = await mkdtemp(join(tmpdir(), "forge-next-batch-")); | |
| 38 | app = createApp({ dataDir: dir, port: 0 }); | |
| 39 | servers = await serve(app); | |
| 40 | const a = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 41 | const b = await createUser(app.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-good-passphrase" }); | |
| 42 | if (!a.ok || !b.ok) throw new Error("users"); | |
| 43 | alice = a.value; | |
| 44 | bob = b.value; | |
| 45 | cookie = `forge_session=${createSession(app.db, alice.id).token}`; | |
| 46 | bobCookie = `forge_session=${createSession(app.db, bob.id).token}`; | |
| 47 | const r = await createRepo(app, alice, { name: "batch", visibility: "public" }); | |
| 48 | if (!r.ok) throw new Error(JSON.stringify(r.error)); | |
| 49 | repo = r.value; | |
| 50 | openOne("P0 Notes: keep notes"); | |
| 51 | openOne("Later gate: nothing behind this starts until the owner opens it"); | |
| 52 | for (let i = 1; i <= 4; i++) made.push(openOne(`Decision ${i}`)); | |
| 53 | }); | |
| 54 | afterAll(async () => { | |
| 55 | await servers.stop(); | |
| 56 | app.close(); | |
| 57 | await rm(dir, { recursive: true, force: true }); | |
| 58 | }); | |
| 59 | ||
| 60 | test("every row carries a checkbox and the bar offers Snooze a day", async () => { | |
| 61 | const page = await (await fetch(`${servers.url}/alice/batch/next`, { headers: { cookie } })).text(); | |
| 62 | expect(page).toContain('name="n" value="' + made[0]!.number + '"'); | |
| 63 | expect(page).toContain("Snooze a day"); | |
| 64 | const reader = await (await fetch(`${servers.url}/alice/batch/next`, { headers: { cookie: bobCookie } })).text(); | |
| 65 | expect(reader).not.toContain("Snooze a day"); | |
| 66 | }); | |
| 67 | ||
| 68 | test("tick three, snooze, undo: the three are back; a closed one is skipped and named", async () => { | |
| 69 | const [a, b, c, d] = made; | |
| 70 | expect(setIssueState(app, alice, repo, d!, "closed").ok).toBe(true); | |
| 71 | const res = await post("/alice/batch/next/batch", new URLSearchParams([["do", "snooze"], ["n", String(a!.number)], ["n", String(b!.number)], ["n", String(c!.number)], ["n", String(d!.number)], ["n", "9999"]])); | |
| 72 | const said = (await res.json()) as { ok: boolean; flash: string; done: number[]; skipped: { number: number }[] }; | |
| 73 | expect(said.done).toEqual([a!.number, b!.number, c!.number]); | |
| 74 | expect(said.skipped.map((s) => s.number)).toEqual([d!.number, 9999]); | |
| 75 | expect(said.flash).toContain(`#${d!.number} (`); | |
| 76 | for (const i of [a, b, c]) { | |
| 77 | expect(held(i!.number)).toBe(true); | |
| 78 | expect(snoozeOf(app.db, i!.id)).toBeDefined(); | |
| 79 | } | |
| 80 | const offer = await (await fetch(`${servers.url}/alice/batch/next?undo=${said.done.join(",")}`, { headers: { cookie } })).text(); | |
| 81 | expect(offer).toContain("Undo (3)"); | |
| 82 | const back = await post("/alice/batch/next/batch", new URLSearchParams([["do", "undo"], ...said.done.map((n) => ["n", String(n)] as [string, string])])); | |
| 83 | expect(((await back.json()) as { done: number[] }).done).toEqual(said.done); | |
| 84 | for (const i of [a, b, c]) { | |
| 85 | expect(held(i!.number)).toBe(false); | |
| 86 | expect(snoozeOf(app.db, i!.id)).toBeUndefined(); | |
| 87 | } | |
| 88 | }); | |
| 89 | ||
| 90 | test("undo ends after a minute, and is only for the person who snoozed", () => { | |
| 91 | const n = made[0]!.number; | |
| 92 | const now = new Date(); | |
| 93 | expect(snoozeMany(app, alice, repo, [n], now).ok).toBe(true); | |
| 94 | const late = undoMany(app, alice, repo, [n], new Date(now.getTime() + UNDO_MS + 1000)); | |
| 95 | expect(late.ok && late.value.done).toEqual([]); | |
| 96 | expect(held(n)).toBe(true); | |
| 97 | }); | |
| 98 | ||
| 99 | test("a batch is at most 50 rows, empty is refused, a reader and a stranger's origin are refused", async () => { | |
| 100 | const many = Array.from({ length: 51 }, (_, i) => i + 1); | |
| 101 | const big = snoozeMany(app, alice, repo, many); | |
| 102 | expect(big.ok).toBe(false); | |
| 103 | expect(snoozeMany(app, alice, repo, []).ok).toBe(false); | |
| 104 | const reader = await post("/alice/batch/next/batch", new URLSearchParams([["do", "snooze"], ["n", String(made[1]!.number)]]), bobCookie); | |
| 105 | expect(reader.status).toBe(422); | |
| 106 | expect(held(made[1]!.number)).toBe(false); | |
| 107 | const cross = await fetch(`${servers.url}/alice/batch/next/batch`, { method: "POST", body: new URLSearchParams([["n", String(made[1]!.number)]]), redirect: "manual", headers: { cookie, origin: "https://evil.example" } }); | |
| 108 | expect(cross.status).toBe(403); | |
| 109 | expect(held(made[1]!.number)).toBe(false); | |
| 110 | }); | |
| 111 | ||
| 112 | test("the page's tick is a bare checkbox with its label, and the batch controls sit in the one filter bar", async () => { | |
| 113 | const page = await (await fetch(`${servers.url}/alice/batch/next`, { headers: { cookie } })).text(); | |
| 114 | expect(page).toMatch(/<input type="checkbox" name="n" value="\d+" form="next-batch" aria-label="Snooze #\d+" \/>/); | |
| 115 | expect(page).not.toContain("Tick rows below"); | |
| 116 | const bar = page.slice(page.indexOf(`class="form-actions" role="search"`), page.indexOf("</form>", page.indexOf(`class="form-actions" role="search"`))); | |
| 117 | expect(bar).toContain('form="next-batch"'); | |
| 118 | }); |
test/tui-next-batch.test.ts added +92
| @@ -0,0 +1,92 @@ | ||
| 1 | /** | |
| 2 | * The terminal ticks rows on Next up, snoozes them a day and takes the batch back (#15835), through | |
| 3 | * the same snoozeMany/undoMany as the page; a reader's keys do nothing. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { openIssue } from "../src/actions/issues.ts"; | |
| 10 | import { createRepo } from "../src/actions/repos.ts"; | |
| 11 | import { createApp, type App as Forge } from "../src/app.ts"; | |
| 12 | import { snoozeOf } from "../src/core/gate_snooze.ts"; | |
| 13 | import { getIssue } from "../src/core/issues.ts"; | |
| 14 | import type { Repo } from "../src/core/repos.ts"; | |
| 15 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; | |
| 16 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 17 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 18 | import { quietLogs } from "./helpers/globals.ts"; | |
| 19 | import { settleApp } from "./helpers/tui.ts"; | |
| 20 | ||
| 21 | quietLogs(); | |
| 22 | ||
| 23 | let dir: string; | |
| 24 | let forge: Forge; | |
| 25 | let alice: User; | |
| 26 | let bob: User; | |
| 27 | let repo: Repo; | |
| 28 | ||
| 29 | const keyEvent = (name: string) => { | |
| 30 | const ctrl = name.startsWith("ctrl+"); | |
| 31 | return { type: "key" as const, key: ctrl ? name.slice(5) : name, ctrl, alt: false, shift: false }; | |
| 32 | }; | |
| 33 | ||
| 34 | function boot(user: User) { | |
| 35 | const terminal = headlessTerminal(140, 30); | |
| 36 | const { app } = createForgeApp(forge, { sshUsername: "t", remoteAddress: "127.0.0.1", user }, { terminal, exitHooks: false }); | |
| 37 | app.start(); | |
| 38 | const press = async (...names: string[]) => { | |
| 39 | names.forEach((n) => app.dispatch(keyEvent(n))); | |
| 40 | await settleApp(app); | |
| 41 | }; | |
| 42 | const toNext = async () => { | |
| 43 | await settleApp(app); | |
| 44 | await press("ctrl+k", ..."Next up in a repository".split(""), "enter", ..."alice/plan".split(""), "enter"); | |
| 45 | }; | |
| 46 | return { text: () => app.screen.toText(), press, toNext, stop: () => app.stop() }; | |
| 47 | } | |
| 48 | ||
| 49 | beforeAll(async () => { | |
| 50 | dir = await mkdtemp(join(tmpdir(), "forge-tui-next-batch-")); | |
| 51 | forge = createApp({ dataDir: dir, port: 0 }); | |
| 52 | for (const [username, first] of [["alice", true], ["bob", false]] as const) { | |
| 53 | const made = await createUser(forge.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst: first }); | |
| 54 | if (!made.ok) throw new Error(made.error); | |
| 55 | } | |
| 56 | alice = getUserByName(forge.db, "alice")!; | |
| 57 | bob = getUserByName(forge.db, "bob")!; | |
| 58 | const r = await createRepo(forge, alice, { name: "plan", visibility: "public" }); | |
| 59 | if (!r.ok) throw new Error("repo"); | |
| 60 | repo = r.value; | |
| 61 | for (const t of ["Later gate: nothing behind this starts until the owner opens it", "One", "Two", "Three"]) if (!openIssue(forge, alice, repo, { title: t }).ok) throw new Error("issue"); | |
| 62 | }); | |
| 63 | ||
| 64 | afterAll(async () => { | |
| 65 | forge.close(); | |
| 66 | await rm(dir, { recursive: true, force: true }); | |
| 67 | }); | |
| 68 | ||
| 69 | const snoozed = (n: number) => snoozeOf(forge.db, getIssue(forge.db, repo.id, n)!.id) !== undefined; | |
| 70 | ||
| 71 | test("x ticks two rows, s snoozes them a day, z takes the batch back", async () => { | |
| 72 | const h = boot(alice); | |
| 73 | await h.toNext(); | |
| 74 | await h.press("x", "j", "x"); | |
| 75 | expect(h.text()).toContain("[x] 1. #2"); | |
| 76 | expect(h.text()).toContain("[x] 2. #3"); | |
| 77 | await h.press("s"); | |
| 78 | expect(snoozed(2) && snoozed(3)).toBe(true); | |
| 79 | expect(snoozed(4)).toBe(false); | |
| 80 | await h.press("z"); | |
| 81 | expect(snoozed(2) || snoozed(3)).toBe(false); | |
| 82 | h.stop(); | |
| 83 | }); | |
| 84 | ||
| 85 | test("a reader's x and s change nothing", async () => { | |
| 86 | const h = boot(bob); | |
| 87 | await h.toNext(); | |
| 88 | await h.press("x", "s"); | |
| 89 | expect(h.text()).not.toContain("[x]"); | |
| 90 | expect(snoozed(2)).toBe(false); | |
| 91 | h.stop(); | |
| 92 | }); |
new above
offline
Principles 1 and 8: a visible "Tick" word on every row is noise; the checkbox with its aria-label says it. Use a bare checkbox, not hand-rolled label markup.