Public: anyone can read this; the crew can change it.

small: Review answers to the keys S, D and T on the focused reminder #280

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/deal-clock:mason/272-small-review-answers-to-the-keys-s-d-and → main 4 commits fork 3 threads · 3 unresolved

  • Merged. mason/272-small-review-answers-to-the-keys-s-d-and was merged into main as 564cb8f.

Review

Sign in to review this change.
5 files changed +57 −8
src/pages/review.ts +7 −5
@@ -11,7 +11,7 @@ import type { Next } from "../coming.ts";
1111 import { BODY_MAX, EMAIL_MAX, PHONE_MAX, REASON_MAX, SUBJECT_MAX } from "../deals.ts";
1212 import { spoken } from "../dates.ts";
1313 import { PARTY_LABEL } from "../deadlines.ts";
14 import { escape } from "../http.ts";
14 import { asset, escape } from "../http.ts";
1515 import { heldFrom, momentLabel, type Draft } from "../reminders.ts";
1616 import type { Who } from "../who.ts";
1717 import { layout } from "./layout.ts";
@@ -114,9 +114,10 @@ function form(
114114 error?.field === field ? `<p class="conflict" role="alert"><b>${FIELD_LABEL[field]}:</b> ${escape(error.message)}</p>` : "";
115115 const invalid = (field: ReviewField): string => (error?.field === field ? ' aria-invalid="true"' : "");
116116 const skip = s.total - s.done > 1 ? reviewHref(s.q, s.at + 1, s.done) : "";
117 return `<section class="card narrow">
117 return `<section class="card narrow" tabindex="0" data-keys aria-describedby="review-keys">
118118 ${s.done === 0 && s.at === 0 ? "<p><b>Start here</b></p>\n " : ""}<p class="eyebrow">${s.position} of ${s.total}</p>
119119 <h1>Review today's reminders</h1>
120 <p class="muted small" id="review-keys">Keys, with the card focused: <kbd>S</kbd> send · <kbd>D</kbd> don't send · <kbd>T</kbd> tomorrow. Typing in a box never triggers them.</p>
120121 <p><a href="/deals/${draft.deal_id}">${escape(draft.address)}</a></p>
121122 <p><b>${escape(draft.label)}</b> <span class="muted">· ${escape(spoken(draft.due))} · ${escape(momentLabel(draft.moment, draft.lead_days))}${draft.held === null ? "" : ` · ${escape(heldFrom(draft.held))}`}</span></p>
122123 <p>To <b>${escape(draft.party_name)}</b> <span class="muted">· ${escape(PARTY_LABEL[draft.party_role])}${[draft.email, draft.phone].filter((v) => v !== "").map((v) => ` · ${escape(v)}`).join("") || (draft.contact === "" ? "" : ` · ${escape(draft.contact)}`)}</span></p>
@@ -127,15 +128,16 @@ function form(
127128 <label>Subject<input class="input" name="subject" value="${escape(typed.subject)}" maxlength="${SUBJECT_MAX}" required${invalid("subject")}></label>
128129 ${problem("body")}
129130 <label>Message<textarea class="input" name="body" rows="9" maxlength="${BODY_MAX}" required${invalid("body")}>${escape(typed.body)}</textarea></label>
130 <button class="button">Send</button>
131 <button class="button" data-key="s" aria-keyshortcuts="S">Send</button>
131132 ${draft.contact === "" ? '<span class="muted small">No contact on file: Send records the approval and your Mail opens with no recipient.</span>' : ""}
132 ${draft.moment === "status" ? "" : `<button class="button button-quiet" formaction="${REVIEW_PATH}/hold">Tomorrow</button>`}
133 ${draft.moment === "status" ? "" : `<button class="button button-quiet" data-key="t" aria-keyshortcuts="T" formaction="${REVIEW_PATH}/hold">Tomorrow</button>`}
133134 </form>
134135 <form class="move" method="post" action="${REVIEW_PATH}/decline">${hidden(keys)}
135136 ${problem("reason")}
136137 <label>Why not<input class="input" name="reason" value="${escape(typed.reason)}" maxlength="${REASON_MAX}" required placeholder="e.g. Buyer already confirmed by phone"${invalid("reason")}></label>
137 <button class="button button-quiet">Don't send</button>
138 <button class="button button-quiet" data-key="d" aria-keyshortcuts="D">Don't send</button>
138139 </form>
140 <script type="module" src="${asset("review.js")}"></script>
139141 <p>${skip === "" ? "" : `<a class="button button-quiet" href="${escape(skip)}">Skip</a> `}${back}</p>
140142 </section>`;
141143 }
src/review.test.ts +31
@@ -55,6 +55,37 @@ test("the round's first draft says Start here, once; a skipped-to draft does not
5555 expect(count(await get("/reminders/today?at=1"))).toBe(0);
5656 });
5757
58 test("Review answers to S, D and T on the focused card: a hint, real buttons, one small cached script", async () => {
59 const page = await get("/reminders/today");
60 expect(page).toContain('id="review-keys"');
61 expect(page).toContain('aria-describedby="review-keys"');
62 expect(page).toMatch(/<button[^>]*data-key="s"[^>]*>Send<\/button>/);
63 expect(page).toMatch(/<button[^>]*data-key="d"[^>]*>Don't send<\/button>/);
64 expect(page).toMatch(/<button[^>]*data-key="t"[^>]*>Tomorrow<\/button>/);
65 const src = /src="(\/static\/review\.js\?v=[0-9a-f]+)"/.exec(page)?.[1];
66 expect(src).toBeDefined();
67 const script = await fetch(base + src!);
68 expect(script.headers.get("cache-control")).toContain("immutable");
69 const text = await script.text();
70 expect(text.length).toBeLessThan(1024);
outdated

Also assert data-key="t" on the Tomorrow button.

71 // Behaviour: run the handler against a stand-in card; odd keys must not throw, typing must not click.
72 const clicked: string[] = [];
73 let handler: (e: unknown) => void = () => {};
74 const button = (key: string) => ({ dataset: { key }, click: () => clicked.push(key) });
75 const card = { focus() {}, addEventListener: (_: string, h: typeof handler) => (handler = h), querySelectorAll: () => ["s", "d", "t"].map(button) };
76 (globalThis as { document?: unknown }).document = { querySelector: () => card };
77 new Function(text)();
78 const press = (key: string, inField = false) => handler({ key, target: { closest: () => inField }, preventDefault() {} });
79 for (const key of ['"', "\\", "]", "Enter"]) press(key);
80 expect(clicked).toEqual([]);
81 press("S", true);
82 expect(clicked).toEqual([]);
83 press("s");
84 press("D");
85 press("t");
86 expect(clicked).toEqual(["s", "d", "t"]);
87 });
88
5889 test("walking the queue visits the unsent drafts in the clock's order; Skip changes nothing", async () => {
5990 const before = await get("/");
6091 const page = await get("/reminders/today");
src/sample.ts +5 −2
@@ -430,8 +430,11 @@ function writeSamples(db: Database, on: string): void {
430430 }
431431 // The anniversary sample was finished on the day a year back, whichever business day its due falls on.
432432 const finished = s.anniversary === undefined ? addDays(on, -1) : addDays(addDays(on, at.closing), -1);
433 for (const kind of s.done ?? [])
434 db.query("UPDATE deadline SET done_at = min(date(due, '-1 day'), ?1) || ?2 WHERE deal_id = ?3 AND kind = ?4").run(finished, DONE_TIME, deal.id, kind);
433 for (const kind of s.done ?? []) {
434 // The anniversary sample's closing is stamped on its anniversary day, however far its due rolled back.
outdated

Out of scope for this pull and awkward: SQL assembled by a conditional template. Use two plain statements, or split this fix into its own pull.

435 if (s.anniversary !== undefined && kind === "closing") db.query("UPDATE deadline SET done_at = ?1 || ?2 WHERE deal_id = ?3 AND kind = ?4").run(finished, DONE_TIME, deal.id, kind);
436 else db.query("UPDATE deadline SET done_at = min(date(due, '-1 day'), ?1) || ?2 WHERE deal_id = ?3 AND kind = ?4").run(finished, DONE_TIME, deal.id, kind);
437 }
435438 for (const kind of s.waived ?? [])
436439 db.query("UPDATE deadline SET waived_at = ?1, waived_by = ?2 WHERE deal_id = ?3 AND kind = ?4").run(`${addDays(on, -1)}${DONE_TIME}`, SAMPLE.name, deal.id, kind);
437440 for (const [kind, note] of s.notes ?? [])
src/week.appointment.test.ts +1 −1
@@ -65,7 +65,7 @@ test("a day's timed dates come first, earliest first, then the untimed in today'
6565 setAppointment(db, alice, { id: dateOf(b, "walkthrough").id, time: "15:30" });
6666 const deals = listDeals(db, alice);
6767 if (!deals.ok) throw new Error(deals.message);
68 const day = thisWeek(deals.value, on).filter(({ deadline }) => deadline.kind === "walkthrough" && deadline.due === addDays(on, 2));
68 const day = thisWeek(deals.value, on).filter(({ deal, deadline }) => deadline.kind === "walkthrough" && deadline.due === addDays(on, 2) && [a, b, c].some((x) => x.id === deal.id));
6969 expect(day.map(({ deal }) => deal.address)).toEqual([c.address, b.address, a.address]);
7070 expect(day.map(({ deadline }) => deadline.time)).toEqual(["09:00", "15:30", null]);
7171 const html = render();
static/review.js added +13
@@ -0,0 +1,13 @@
1 // Review keys: S sends, D opens the reason for Don't send, T holds until
2 // tomorrow. Each presses the real button, so the same action and same-origin
3 // check run. Only with focus on the card, never while typing in a field.
4 const card = document.querySelector("[data-keys]");
5 card?.focus({ preventScroll: true });
6 card?.addEventListener("keydown", (e) => {
7 if (e.metaKey || e.ctrlKey || e.altKey || e.repeat || e.target.closest("input,textarea,select,[contenteditable]")) return;
8 const k = e.key.toLowerCase();
outdated

e.key is interpolated into a CSS selector. A key like " or \ makes querySelector throw. Use a lookup that does not build a selector.

9 const b = [...card.querySelectorAll("button[data-key]")].find((x) => x.dataset.key === k);
10 if (!b) return;
11 e.preventDefault();
12 b.click();
13 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs