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

Every page's header says how many messages wait for the agent's approval, and one tap opens the review #92

Merged opened this

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

Merged

mason/deal-clock:mason/89-every-page-s-header-says-how-many-messag → main 2 commits fork 1 thread · 1 unresolved

  • Merged. mason/89-every-page-s-header-says-how-many-messag was merged into main as 6a5634c.

Review

Sign in to review this change.
15 files changed +172 −52
README.md +1 −1
@@ -71,7 +71,7 @@ Signed out, `/` shows what deal-clock does, three short points, a still picture
7171 there opens the sheet of the date it is for, where the reminder is edited,
7272 sent or declined. Many to get through? **Review all**, beside that heading,
7373 walks you through them one page at a time: Send or Don't send, and the next
74 one appears; Skip leaves one for later.
74 one appears; Skip leaves one for later. Wherever you are, the header's **Review** link carries the same count ("Review 3") and opens that walk with one tap.
7575 Decided against a message? Beside Send, **Don't send** asks why (up to 200
7676 characters). The reminder stops counting as "to send" and reads "Not sent:
7777 <reason> · <who>"; the reason is kept, and **Undo** makes it a draft again.
src/pages/activity.ts +2 −1
@@ -16,6 +16,7 @@ import { layout } from "./layout.ts";
1616 export function activityPage(input: {
1717 who: Who;
1818 agent?: Agent | undefined;
19 waiting?: number | undefined;
1920 db: Database;
2021 /** Up to one more than a page: the extra says there are older rows. */
2122 rows: readonly AuditRow[];
@@ -46,5 +47,5 @@ export function activityPage(input: {
4647 <p class="summary">Who approved, declined or changed what, newest first. ${filter}</p>
4748 ${rows.length === 0 ? `<p class="muted">Nothing yet.</p>` : `<ul class="stack">${rows.map(line).join("")}</ul>`}
4849 ${next}`;
49 return layout({ body, who, agent, path: "/activity", heading: "Activity", live: false });
50 return layout({ body, who, agent, waiting: input.waiting, path: "/activity", heading: "Activity", live: false });
5051 }
src/pages/clock.ts +6 −2
@@ -141,6 +141,8 @@ const pages = memoizer<string>(3);
141141 /** The clock, rendered once while nothing it shows has changed. */
142142 export function clock(input: {
143143 who: Who;
144 /** How many of today's messages wait for approval, for the header. */
145 waiting?: number | undefined;
144146 agent?: Agent | undefined;
145147 deals: readonly DealView[];
146148 on: string;
@@ -149,7 +151,7 @@ export function clock(input: {
149151 q?: string | undefined;
150152 }): string {
151153 const key = Bun.hash(
152 JSON.stringify([input.who, input.on, input.notice ?? null, input.open ?? null, input.q ?? "", input.agent]) +
154 JSON.stringify([input.who, input.on, input.notice ?? null, input.open ?? null, input.q ?? "", input.agent, input.waiting]) +
153155 input.deals.map(print).join(),
154156 );
155157 return pages(`${input.deals.length}:${key}`, () => render(input));
@@ -157,6 +159,8 @@ export function clock(input: {
157159
158160 function render(input: {
159161 who: Who;
162 /** How many of today's messages wait for approval, for the header. */
163 waiting?: number | undefined;
160164 agent?: Agent | undefined;
161165 deals: readonly DealView[];
162166 on: string;
@@ -250,5 +254,5 @@ function render(input: {
250254 }
251255 <p class="muted small">Each date reminds its parties a few days before (three unless you set another, per date) and the morning of, and the one who owes a date is asked for an update the morning after it slips. Send records your approval and opens your own Mail or Messages with the words.</p>
252256 </section>`}`;
253 return layout({ body, who, agent, path: "/", notice, open: opened?.href });
257 return layout({ body, who, agent, waiting: input.waiting, path: "/", notice, open: opened?.href });
254258 }
src/pages/deal.ts +6 −2
@@ -79,6 +79,8 @@ function messages(deal: DealView, may: boolean): string {
7979
8080 export function dealPage(input: {
8181 who: Who;
82 /** How many of today's messages wait for approval, for the header. */
83 waiting?: number | undefined;
8284 agent?: Agent | undefined;
8385 deal: DealView;
8486 on: string;
@@ -189,7 +191,7 @@ export function dealPage(input: {
189191 })
190192 : ""
191193 }`;
192 return layout({ body, who, agent, path, heading: deal.address, notice, open: opened?.href });
194 return layout({ body, who, agent, waiting: input.waiting, path, heading: deal.address, notice, open: opened?.href });
193195 }
194196
195197 /** What the form was last sent with, what was wrong with it, and the dates it would make. */
@@ -215,6 +217,8 @@ const previewList = ({ plan, dates }: NonNullable<NewDealState["preview"]>): str
215217
216218 export function newDealPage(input: {
217219 who: Who;
220 /** How many of today's messages wait for approval, for the header. */
221 waiting?: number | undefined;
218222 agent?: Agent | undefined;
219223 on: string;
220224 notice?: string | undefined;
@@ -275,5 +279,5 @@ export function newDealPage(input: {
275279 <p class="muted small">Reading these from the contract PDF is phase two.</p>
276280 </section>
277281 ${state?.preview === undefined ? "" : previewList(state.preview)}`;
278 return layout({ body, who, agent, path: "/deals/new", heading: "New deal", notice });
282 return layout({ body, who, agent, waiting: input.waiting, path: "/deals/new", heading: "New deal", notice });
279283 }
src/pages/layout.ts +10 −2
@@ -25,6 +25,12 @@ import type { Who } from "../who.ts";
2525
2626 const iconHref = asset("icon.svg");
2727
28 /** The header's way into the review: the count of waiting messages, in a slot that keeps its width. */
29 function reviewLink(waiting: number): string {

waiting defaults to 0 in layout, so any signed-in page that does not pass it (the 404 page) claims "none waiting" falsely. Render the link only when a count was given, or pass the real count everywhere.

30 const name = waiting === 0 ? "Review, none waiting" : `Review, ${waiting} waiting`;
31 return `<a id="review-link" class="button button-quiet review-link" data-live="deal.changed" href="/reminders/today" aria-label="${name}">Review <span class="review-count">${waiting === 0 ? "" : waiting}</span></a>`;
32 }
33
2834 export function layout(input: {
2935 body: string;
3036 who: Who;
@@ -35,6 +41,8 @@ export function layout(input: {
3541 notice?: string | undefined;
3642 /** The link a notice about a sent reminder offers: the href `outbox` made for it. */
3743 open?: string | null | undefined;
44 /** How many of today's messages wait for approval; the header's Review link says so, and is left out when the count is not known. */
45 waiting?: number | undefined;
3846 /** Whether the page keeps its regions live; false for a page with none. */
3947 live?: boolean;
4048 /** A page handed to someone outside the app: the brand only, no sign-in, footer or script. */
@@ -42,7 +50,7 @@ export function layout(input: {
4250 /** A class on <body>, for styles that belong to one page only. */
4351 bodyClass?: string;
4452 }): string {
45 const { body, who, agent = defaults, path, notice, open, plain = false, bodyClass } = input;
53 const { body, who, agent = defaults, path, notice, open, plain = false, bodyClass, waiting } = input;
4654 const pageTitle = input.heading === undefined ? title : `${input.heading} · ${title}`;
4755 const to = encodeURIComponent(path);
4856 const door =
@@ -86,7 +94,7 @@ ${plain || input.live === false ? "" : `<script type="module" src="${asset("live
8694 ? ""
8795 : `<nav class="row">
8896 ${env === "production" ? "" : `<span class="pill" data-env="${escape(env)}">${escape(where)}</span>`}
89 ${who.role === "anon" ? "" : `<a class="button button-quiet" href="/">Clock</a> <a class="button button-quiet" href="/people">People</a> <a class="button button-quiet" href="/profile">Profile</a>${permit(who, "audit", "list") === "yes" ? ` <a class="button button-quiet" href="/activity">Activity</a>` : ""}`}
97 ${who.role === "anon" ? "" : `<a class="button button-quiet" href="/">Clock</a> <a class="button button-quiet" href="/people">People</a> ${waiting === undefined ? "" : `${reviewLink(waiting)} `}<a class="button button-quiet" href="/profile">Profile</a>${permit(who, "audit", "list") === "yes" ? ` <a class="button button-quiet" href="/activity">Activity</a>` : ""}`}
9098 ${door}
9199 </nav>`
92100 }
src/pages/missing.ts +2 −2
@@ -8,7 +8,7 @@
88 import type { Who } from "../who.ts";
99 import { layout } from "./layout.ts";
1010
11 export function missingPage(input: { who: Who; path: string; deal?: boolean }): string {
11 export function missingPage(input: { who: Who; path: string; deal?: boolean; waiting?: number | undefined }): string {
1212 const { who, path, deal = false } = input;
1313 const sentence = deal
1414 ? "This deal is not here; it may have been removed."
@@ -18,5 +18,5 @@ export function missingPage(input: { who: Who; path: string; deal?: boolean }):
1818 ? `<a class="button" href="/__forge/signin?to=${encodeURIComponent("/")}">Sign in</a>`
1919 : `<a class="button" href="/">Back to the clock</a>`;
2020 const body = `<section class="card narrow"><h1>Nothing here</h1><p>${sentence}</p><p>${way}</p></section>`;
21 return layout({ body, who, path, heading: "Nothing here", live: false });
21 return layout({ body, who, path, heading: "Nothing here", live: false, waiting: input.waiting });
2222 }
src/pages/people.ts +2 −2
@@ -22,10 +22,10 @@ const person = (p: Person, on: string): string => `<section class="card person">
2222 ${p.owes.length === 0 ? `<p class="muted">Nothing open</p>` : `<ul class="week-list">${p.owes.map((o) => owedRow(o, on)).join("")}</ul>`}
2323 </section>`;
2424
25 export function peoplePage(input: { who: Who; agent?: Agent | undefined; people: readonly Person[]; on: string }): string {
25 export function peoplePage(input: { who: Who; agent?: Agent | undefined; waiting?: number | undefined; people: readonly Person[]; on: string }): string {
2626 const { who, people, on } = input;
2727 const body = `<h1>People</h1>
2828 <p class="summary">Everyone on your deals, with every open date they owe, soonest first.</p>
2929 ${people.length === 0 ? `<p class="muted">No one yet: add a deal and its parties.</p>` : people.map((p) => person(p, on)).join("")}`;
30 return layout({ body, who, agent: input.agent, path: "/people", heading: "People", live: false });
30 return layout({ body, who, agent: input.agent, waiting: input.waiting, path: "/people", heading: "People", live: false });
3131 }
src/pages/profile.ts +3 −1
@@ -17,6 +17,8 @@ export interface ProfileState {
1717
1818 export function profilePage(input: {
1919 who: Who;
20 /** How many of today's messages wait for approval, for the header. */
21 waiting?: number | undefined;
2022 profile: Profile;
2123 /** Whether the table lets `who` save it. */
2224 mayChange: boolean;
@@ -62,5 +64,5 @@ export function profilePage(input: {
6264 <p class="muted">${profile.saved ? "Saved by you." : "These are the defaults the app was started with."} The header, the client timeline and the sign-off of every unsent reminder carry them. Reminders already sent keep the words that went out.</p>
6365 ${mayChange ? form : readOnly}
6466 </section>`;
65 return layout({ body, who, agent, path: "/profile", heading: "Profile", notice, live: false });
67 return layout({ body, who, agent, path: "/profile", heading: "Profile", notice, waiting: input.waiting, live: false });
6668 }
src/pages/refused.ts +2 −1
@@ -13,6 +13,7 @@ import { layout } from "./layout.ts";
1313 export function refusedPage(input: {
1414 who: Who;
1515 agent?: Agent | undefined;
16 waiting?: number | undefined;
1617 path: string;
1718 heading: string;
1819 error: Failure;
@@ -23,5 +24,5 @@ export function refusedPage(input: {
2324 error === "signin_required"
2425 ? `<section class="card narrow"><h1>${escape(heading)}</h1><p>${escape(message)}</p><p><a class="button" href="/__forge/signin?to=${encodeURIComponent(path)}">Sign in</a></p></section>`
2526 : `<section class="card narrow"><h1>${escape(heading)}</h1><p>${escape(message)} <a href="/">Back to the clock</a>.</p></section>`;
26 return layout({ body, who, agent, path, heading });
27 return layout({ body, who, agent, path, heading, waiting: input.waiting });
2728 }
src/pages/review.ts +3 −1
@@ -47,6 +47,8 @@ const FIELD_LABEL: Readonly<Record<ReviewField, string>> = { subject: "Subject",
4747
4848 export function reviewPage(input: {
4949 who: Who;
50 /** How many of today's messages wait for approval, for the header. */
51 waiting?: number | undefined;
5052 agent?: Agent | undefined;
5153 /** The drafts still to send, in the clock's order. */
5254 queue: readonly Draft[];
@@ -68,7 +70,7 @@ export function reviewPage(input: {
6870 draft === undefined
6971 ? `<section class="card narrow"><h1>Review today's reminders</h1><p>All of today's reminders are sent or declined.</p><p>${back}</p></section>`
7072 : form(draft, { q, at, done, position: done + at + 1, total: done + queue.length, error, typed }, back);
71 return layout({ body, who, agent, path: REVIEW_PATH, heading: "Review today's reminders", notice: input.notice, open: input.open, live: false });
73 return layout({ body, who, agent, waiting: input.waiting, path: REVIEW_PATH, heading: "Review today's reminders", notice: input.notice, open: input.open, live: false });
7274 }
7375
7476 function form(
src/reminders.ts +12
@@ -334,3 +334,15 @@ export const pending = (d: Draft, day: string): boolean =>
334334 */
335335 export const todaysDrafts = (deals: readonly DealView[], day: string, agent: Agent): Draft[] =>
336336 deals.flatMap((deal) => drafts(deal, agent).filter((d) => pending(d, day)));
337
338 const waitingIn = memoizer<number>(400);
339
340 /**
341 * How many of `todaysDrafts` still wait for approval: the number the review
342 * page walks through, counted once per deal while the deal stands.
343 */
344 export const waitingCount = (deals: readonly DealView[], day: string, agent: Agent): number =>
345 deals.reduce(
346 (n, deal) => n + waitingIn(`${day}|${JSON.stringify(agent)}|${print(deal)}`, () => todaysDrafts([deal], day, agent).filter(unsent).length),
347 0,
348 );
src/routes.ts +55 −36
@@ -86,7 +86,7 @@ import { activityPage } from "./pages/activity.ts";
8686 import { ACTIVITY_PAGE } from "./activity.ts";
8787 import { timelinePage } from "./pages/timeline.ts";
8888 import { welcomePage } from "./pages/welcome.ts";
89 import { dueOn, remindsLine, todaysDrafts, unsent, type Draft } from "./reminders.ts";
89 import { dueOn, remindsLine, todaysDrafts, unsent, waitingCount, type Agent, type Draft } from "./reminders.ts";
9090 import { token, tokensCss } from "./tokens.ts";
9191 import type { Who } from "./who.ts";
9292
@@ -350,7 +350,7 @@ const STATUS: Record<Failure, number> = {
350350
351351 /** A deal that is not there, for a reader who may read deals: the product's own 404. */
352352 const missingDeal = (c: Context) =>
353 html(missingPage({ who: c.who, path: c.url.pathname, deal: true }), 404);
353 html(missingPage({ who: c.who, waiting: waitingFor(c), path: c.url.pathname, deal: true }), 404);
354354
355355 /** Whether a client wants JSON: anything under the API, or an `accept` that asks for it. */
356356 const wantsJson = (request: Request, url: URL): boolean =>
@@ -359,9 +359,9 @@ const wantsJson = (request: Request, url: URL): boolean =>
359359 (request.headers.get("accept") ?? "").includes("application/json");
360360
361361 /** No route matched: JSON for an API client, the product's page for a person. */
362 export function notFound(request: Request, url: URL, who: Who): Response {
362 export function notFound(request: Request, url: URL, who: Who, db: Database): Response {
363363 if (wantsJson(request, url)) return json({ error: "not_found" }, 404);
364 return html(missingPage({ who, path: url.pathname }), 404);
364 return html(missingPage({ who, waiting: waitingIn(db, who, agentOf(db)), path: url.pathname }), 404);
365365 }
366366
367367 /** A result as JSON: the value, or `{ error, message }` with the matching status. */
@@ -407,9 +407,28 @@ const unconfirmed = (c: Context, path: string): Response =>
407407 function reviewQueue(c: Context, q: string): Result<Draft[]> {
408408 const deals = listDeals(c.db, c.who);
409409 if (!deals.ok) return deals;
410 return { ok: true, value: todaysDrafts(find(deals.value, q), today(), agentOf(c.db)).filter(unsent) };
410 return { ok: true, value: todaysDrafts(find(deals.value, q), today(), agentFor(c)).filter(unsent) };
411411 }
412412
413 /** Whose deals these are, read once per request however many times a page and its header ask. */
414 const agents = new WeakMap<Context, Agent>();
415 function agentFor(c: Context): Agent {
416 let agent = agents.get(c);
417 if (agent === undefined) agents.set(c, (agent = agentOf(c.db)));
418 return agent;
419 }
420
421 /**
422 * What the header's Review link says: today's drafts waiting for approval,
423 * among the deals this person may list, counted as the review page counts them.
424 */
425 function waitingIn(db: Database, who: Who, agent: Agent): number {
426 if (who.role === "anon") return 0;
427 const deals = listDeals(db, who);
428 return deals.ok ? waitingCount(deals.value, today(), agent) : 0;
429 }
430 const waitingFor = (c: Context): number => waitingIn(c.db, c.who, agentFor(c));
431
413432 /** A small count from the query or a form, or 0. */
414433 const count = (value: unknown): number => (typeof value === "string" && /^\d{1,4}$/.test(value) ? Number(value) : 0);
415434
@@ -430,7 +449,7 @@ async function reviewed(c: Context, act: (f: Record<string, string>) => Result<{
430449 const result = act(f);
431450 if (result.ok) return back(said, reviewHref(q, at, settled + 1), said === SENT ? result.value.id : undefined);
432451 if (result.error === "signin_required" || result.error === "forbidden")
433 return html(refusedPage({ who: c.who, agent: agentOf(c.db), path: reviewHref(q, at, settled), heading: "Review today's reminders", error: result.error, message: result.message }), STATUS[result.error]);
452 return html(refusedPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), path: reviewHref(q, at, settled), heading: "Review today's reminders", error: result.error, message: result.message }), STATUS[result.error]);
434453 const queue = reviewQueue(c, q);
435454 const same = queue.ok
436455 ? queue.value.findIndex((d) => d.deadline_id === Number(f.deadline) && d.party_id === Number(f.party) && d.moment === f.moment)
@@ -438,7 +457,7 @@ async function reviewed(c: Context, act: (f: Record<string, string>) => Result<{
438457 if (result.error !== "invalid" || !queue.ok || same === -1) return back(result.message, reviewHref(q, at, settled));
439458 const typed = { subject: f.subject ?? "", body: f.body ?? "", reason: f.reason ?? "" };
440459 return html(
441 reviewPage({ who: c.who, agent: agentOf(c.db), queue: queue.value, q, at: same, done: settled, error: { field: fieldOf(result.message), message: result.message }, typed }),
460 reviewPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), queue: queue.value, q, at: same, done: settled, error: { field: fieldOf(result.message), message: result.message }, typed }),
442461 STATUS.invalid,
443462 );
444463 }
@@ -483,11 +502,11 @@ export const routes: readonly Route[] = [
483502 path: "/",
484503 handle: (c) => {
485504 const { notice, open, clear } = readNotice(c.request);
486 if (c.who.role === "anon") return html(welcomePage({ who: c.who, agent: agentOf(c.db), on: today() }), 200, clear);
505 if (c.who.role === "anon") return html(welcomePage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), on: today() }), 200, clear);
487506 const deals = listDeals(c.db, c.who);
488507 if (!deals.ok)
489508 return html(
490 refusedPage({ who: c.who, agent: agentOf(c.db), path: "/", heading: "The clock", error: deals.error, message: deals.message }),
509 refusedPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), path: "/", heading: "The clock", error: deals.error, message: deals.message }),
491510 200,
492511 clear,
493512 );
@@ -496,7 +515,7 @@ export const routes: readonly Route[] = [
496515 return html(
497516 refusedPage({
498517 who: c.who,
499 agent: agentOf(c.db),
518 agent: agentFor(c),
500519 path: "/",
501520 heading: "The clock",
502521 error: "invalid",
@@ -505,7 +524,7 @@ export const routes: readonly Route[] = [
505524 400,
506525 clear,
507526 );
508 return html(clock({ who: c.who, agent: agentOf(c.db), deals: deals.value, on: today(), notice, open, q }), 200, clear);
527 return html(clock({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), deals: deals.value, on: today(), notice, open, q }), 200, clear);
509528 },
510529 },
511530 {
@@ -515,10 +534,10 @@ export const routes: readonly Route[] = [
515534 const deals = listDeals(c.db, c.who);
516535 if (!deals.ok)
517536 return html(
518 refusedPage({ who: c.who, agent: agentOf(c.db), path: "/people", heading: "People", error: deals.error, message: deals.message }),
537 refusedPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), path: "/people", heading: "People", error: deals.error, message: deals.message }),
519538 STATUS[deals.error],
520539 );
521 return html(peoplePage({ who: c.who, agent: agentOf(c.db), people: people(deals.value, today()), on: today() }));
540 return html(peoplePage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), people: people(deals.value, today()), on: today() }));
522541 },
523542 },
524543 {
@@ -535,10 +554,10 @@ export const routes: readonly Route[] = [
535554 });
536555 const path = "/activity";
537556 if (!got.ok)
538 return html(refusedPage({ who: c.who, agent: agentOf(c.db), path, heading: "Activity", error: got.error, message: got.message }), STATUS[got.error]);
557 return html(refusedPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), path, heading: "Activity", error: got.error, message: got.message }), STATUS[got.error]);
539558 if (bad)
540 return html(refusedPage({ who: c.who, agent: agentOf(c.db), path, heading: "Activity", error: "invalid", message: "Older is a whole number, and the only filter is messages." }), 400);
541 return html(activityPage({ who: c.who, agent: agentOf(c.db), db: c.db, rows: got.value, only: only === "messages" }));
559 return html(refusedPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), path, heading: "Activity", error: "invalid", message: "Older is a whole number, and the only filter is messages." }), 400);
560 return html(activityPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), db: c.db, rows: got.value, only: only === "messages" }));
542561 },
543562 },
544563 {
@@ -547,7 +566,7 @@ export const routes: readonly Route[] = [
547566 handle: (c) => {
548567 const deals = listDeals(c.db, c.who);
549568 if (!deals.ok) return text(deals.message, STATUS[deals.error]);
550 return new Response(book(deals.value, new URL(publicOrigin).hostname, new Date(), agentOf(c.db)), {
569 return new Response(book(deals.value, new URL(publicOrigin).hostname, new Date(), agentFor(c)), {
551570 headers: {
552571 "content-type": "text/calendar; charset=utf-8",
553572 "content-disposition": 'attachment; filename="deal-clock.ics"',
@@ -562,7 +581,7 @@ export const routes: readonly Route[] = [
562581 path: "/deals/new",
563582 handle: (c) => {
564583 const { notice, clear } = readNotice(c.request);
565 return html(newDealPage({ who: c.who, agent: agentOf(c.db), on: today(), notice }), 200, clear);
584 return html(newDealPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), on: today(), notice }), 200, clear);
566585 },
567586 },
568587 {
@@ -576,7 +595,7 @@ export const routes: readonly Route[] = [
576595 return html(
577596 refusedPage({
578597 who: c.who,
579 agent: agentOf(c.db),
598 agent: agentFor(c),
580599 path: c.url.pathname,
581600 heading: "A deal",
582601 error: found.error,
@@ -585,7 +604,7 @@ export const routes: readonly Route[] = [
585604 STATUS[found.error],
586605 );
587606 const { notice, open, clear } = readNotice(c.request);
588 return html(dealPage({ who: c.who, agent: agentOf(c.db), deal: found.value, on: today(), notice, open }), 200, clear);
607 return html(dealPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), deal: found.value, on: today(), notice, open }), 200, clear);
589608 },
590609 },
591610 {
@@ -596,7 +615,7 @@ export const routes: readonly Route[] = [
596615 const found = getDeal(c.db, c.who, { id: Number(c.params.id) });
597616 if (!found.ok && (found.error === "not_found" || found.error === "invalid")) return missingDeal(c);
598617 if (!found.ok) return text(found.message, STATUS[found.error]);
599 return html(dealSheets(found.value, c.who, today(), "/", agentOf(c.db)));
618 return html(dealSheets(found.value, c.who, today(), "/", agentFor(c)));
600619 },
601620 },
602621 {
@@ -610,7 +629,7 @@ export const routes: readonly Route[] = [
610629 return html(
611630 refusedPage({
612631 who: c.who,
613 agent: agentOf(c.db),
632 agent: agentFor(c),
614633 path: c.url.pathname,
615634 heading: "A deal",
616635 error: found.error,
@@ -618,7 +637,7 @@ export const routes: readonly Route[] = [
618637 }),
619638 STATUS[found.error],
620639 );
621 return html(timelinePage({ who: c.who, agent: agentOf(c.db), deal: found.value, on: today() }));
640 return html(timelinePage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), deal: found.value, on: today() }));
622641 },
623642 },
624643 {
@@ -628,7 +647,7 @@ export const routes: readonly Route[] = [
628647 const found = getDeal(c.db, c.who, { id: Number(c.params.id) });
629648 if (!found.ok) return text(found.message, STATUS[found.error]);
630649 const name = found.value.address.replace(/[^A-Za-z0-9]+/g, "-").replace(/^-|-$/g, "").toLowerCase() || "deal";
631 return new Response(calendar(found.value, new URL(publicOrigin).hostname, new Date(), agentOf(c.db)), {
650 return new Response(calendar(found.value, new URL(publicOrigin).hostname, new Date(), agentFor(c)), {
632651 headers: {
633652 "content-type": "text/calendar; charset=utf-8",
634653 "content-disposition": `attachment; filename="${name}.ics"`,
@@ -647,7 +666,7 @@ export const routes: readonly Route[] = [
647666 const made = createDeal(c.db, c.who, newDeal(fromForm(f)));
648667 if (made.ok) return back("Deal opened. Every date is on the clock.", `/deals/${made.value.id}`);
649668 if (made.error !== "invalid") return back(made.message, "/deals/new");
650 return html(newDealPage({ who: c.who, agent: agentOf(c.db), on: today(), state: { values: f, problem: made } }), 422);
669 return html(newDealPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), on: today(), state: { values: f, problem: made } }), 422);
651670 },
652671 },
653672 {
@@ -661,9 +680,9 @@ export const routes: readonly Route[] = [
661680 if (!may.ok) return back(may.message, "/deals/new");
662681 const planned = plan(newDeal(fromForm(f)));
663682 if (!planned.ok)
664 return html(newDealPage({ who: c.who, agent: agentOf(c.db), on: today(), state: { values: f, problem: planned } }), 422);
683 return html(newDealPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), on: today(), state: { values: f, problem: planned } }), 422);
665684 const preview = { plan: planned.value, dates: plannedDates(planned.value) };
666 return html(newDealPage({ who: c.who, agent: agentOf(c.db), on: today(), state: { values: f, preview } }));
685 return html(newDealPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), on: today(), state: { values: f, preview } }));
667686 },
668687 },
669688 {
@@ -700,11 +719,11 @@ export const routes: readonly Route[] = [
700719 const got = getProfile(c.db, c.who);
701720 if (!got.ok)
702721 return html(
703 refusedPage({ who: c.who, agent: agentOf(c.db), path: "/profile", heading: "Profile", error: got.error, message: got.message }),
722 refusedPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), path: "/profile", heading: "Profile", error: got.error, message: got.message }),
704723 STATUS[got.error],
705724 clear,
706725 );
707 return html(profilePage({ who: c.who, profile: got.value, mayChange: permit(c.who, "profile", "update") === "yes", notice }), 200, clear);
726 return html(profilePage({ who: c.who, waiting: waitingFor(c), profile: got.value, mayChange: permit(c.who, "profile", "update") === "yes", notice }), 200, clear);
708727 },
709728 },
710729 {
@@ -718,7 +737,7 @@ export const routes: readonly Route[] = [
718737 if (saved.error !== "invalid") return back(saved.message, "/profile");
719738 const profile = getProfile(c.db, c.who);
720739 if (!profile.ok) return back(saved.message, "/profile");
721 return html(profilePage({ who: c.who, profile: profile.value, mayChange: true, state: { values: f, problem: saved } }), 422);
740 return html(profilePage({ who: c.who, waiting: waitingFor(c), profile: profile.value, mayChange: true, state: { values: f, problem: saved } }), 422);
722741 },
723742 },
724743 {
@@ -773,7 +792,7 @@ export const routes: readonly Route[] = [
773792 if (saved.error !== "invalid" || f.address === undefined) return back(saved.message, `/deals/${id}`);
774793 const found = getDeal(c.db, c.who, { id });
775794 if (!found.ok) return back(found.message, `/deals/${id}`);
776 return html(dealPage({ who: c.who, agent: agentOf(c.db), deal: found.value, on: today(), problem: { value: f.address, message: saved.message } }), STATUS.invalid);
795 return html(dealPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), deal: found.value, on: today(), problem: { value: f.address, message: saved.message } }), STATUS.invalid);
777796 },
778797 },
779798 {
@@ -902,18 +921,18 @@ export const routes: readonly Route[] = [
902921 const { notice, open, clear } = readNotice(c.request);
903922 const q = query(c.url.searchParams.get("q"));
904923 if (q === undefined)
905 return html(refusedPage({ who: c.who, agent: agentOf(c.db), path: "/reminders/today", heading: "Review today's reminders", error: "invalid", message: FIND_REFUSAL }), 400, clear);
924 return html(refusedPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), path: "/reminders/today", heading: "Review today's reminders", error: "invalid", message: FIND_REFUSAL }), 400, clear);
906925 const queue = reviewQueue(c, q);
907926 if (!queue.ok)
908927 return html(
909 refusedPage({ who: c.who, agent: agentOf(c.db), path: "/reminders/today", heading: "Review today's reminders", error: queue.error, message: queue.message }),
928 refusedPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), path: "/reminders/today", heading: "Review today's reminders", error: queue.error, message: queue.message }),
910929 STATUS[queue.error],
911930 clear,
912931 );
913932 const sent = open === undefined ? undefined : listDeals(c.db, c.who);
914933 const link = open === undefined || !sent?.ok ? undefined : sent.value.flatMap((d) => d.sent).find((m) => m.id === open)?.href ?? null;
915934 return html(
916 reviewPage({ who: c.who, agent: agentOf(c.db), queue: queue.value, q, at: count(c.url.searchParams.get("at")), done: count(c.url.searchParams.get("done")), notice, open: link }),
935 reviewPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), queue: queue.value, q, at: count(c.url.searchParams.get("at")), done: count(c.url.searchParams.get("done")), notice, open: link }),
917936 200,
918937 clear,
919938 );
@@ -951,7 +970,7 @@ export const routes: readonly Route[] = [
951970 const deals = listDeals(c.db, c.who);
952971 if (!deals.ok) return answer(deals);
953972 const on = today();
954 return json({ on, reminders: dueOn(deals.value, on, agentOf(c.db)) });
973 return json({ on, reminders: dueOn(deals.value, on, agentFor(c)) });
955974 },
956975 api: {
957976 summary:
@@ -1066,7 +1085,7 @@ export const routes: readonly Route[] = [
10661085 {
10671086 method: "GET",
10681087 path: "/api/deals/:id/status",
1069 handle: (c) => answer(statusUpdates(c.db, c.who, { dealId: Number(c.params.id), agent: agentOf(c.db) })),
1088 handle: (c) => answer(statusUpdates(c.db, c.who, { dealId: Number(c.params.id), agent: agentFor(c) })),
10701089 api: {
10711090 summary:
10721091 "The status update each buyer and seller with a contact is owed: what is done, what is next, when it closes, signed with the agent's profile. A sent or declined one is not offered again until a date of the deal is done, moved, added or removed; a closed deal has none. Nothing is sent: send one with POST …/status/send, or decline it with POST …/reminders/decline (moment status, the closing date's deadline_id).",
src/server.ts +1 −1
@@ -51,7 +51,7 @@ export function serve(options: ServeOptions) {
5151 request.method === "HEAD" ? "GET" : request.method,
5252 url.pathname,
5353 );
54 if (found === undefined) return head(request, notFound(request, url, me));
54 if (found === undefined) return head(request, notFound(request, url, me, db));
5555 const response = await found.route.handle({
5656 request,
5757 url,
src/waiting.test.ts added +59
@@ -0,0 +1,59 @@
1 /** The header on every page says how many messages wait for approval, and the number is the review page's own. */
2 import { afterAll, beforeEach, expect, test } from "bun:test";
3 import { mkdtempSync } from "node:fs";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { forgetRationing } from "./security.ts";
7 import { serve } from "./server.ts";
8 import { sign } from "./who.ts";
9
10 const KEY = "a-key-only-the-forge-holds";
11 const server = serve({ port: 0, dataDir: mkdtempSync(join(tmpdir(), "app-waiting-")), identityKey: KEY });
12 const base = `http://${server.hostname}:${server.port}`;
13 afterAll(() => server.stop(true));
14 beforeEach(forgetRationing);
15
16 const identity = `u1|alice|user|${Date.now() + 600_000}`;
17 const alice = { "x-forge-identity": identity, "x-forge-signature": sign(identity, KEY) };
18 const get = async (path: string, headers: Record<string, string> = alice): Promise<string> => (await fetch(base + path, { headers })).text();
19
20 /** What the header's Review link says: its number, or null when it reads "Review" alone. */
21 const headerCount = (page: string): number | null => {
22 const link = /<a id="review-link"[^>]*href="\/reminders\/today" aria-label="([^"]*)">Review <span class="review-count">(\d*)<\/span><\/a>/.exec(page);
23 expect(link).not.toBeNull();
24 expect(link![1]).toBe(link![2] === "" ? "Review, none waiting" : `Review, ${link![2]} waiting`);
25 return link![2] === "" ? null : Number(link![2]);
26 };
27
28 /** The review page's own total: how many it walks through. */
29 const reviewTotal = (page: string): number => Number(/(\d+) of (\d+)/.exec(page)?.[2] ?? 0);
30
31 const post = (path: string, fields: Record<string, string>) =>
32 fetch(base + path, { method: "POST", body: new URLSearchParams(fields), headers: { ...alice, "content-type": "application/x-www-form-urlencoded" }, redirect: "manual" });
33
34 test("a page other than the clock carries the count the review page walks through", async () => {
35 const total = reviewTotal(await get("/reminders/today"));
36 expect(total).toBeGreaterThan(0);
37 for (const path of ["/people", "/profile", "/deals/new", "/reminders/today"]) expect(headerCount(await get(path))).toBe(total);
38 expect(headerCount(await get("/"))).toBe(total);
39 // An unknown address is a page too: its header must not claim none.
40 expect(headerCount(await get("/no-such-page"))).toBe(total);
41 });
42
43 test("approving or declining every message leaves the next page with none", async () => {
44 const before = headerCount(await get("/people"))!;
45 for (let left = before; left > 0; left--) {
46 const page = await get("/reminders/today");
47 const hidden = (name: string) => new RegExp(`name="${name}" value="([^"]*)"`).exec(page)![1]!;
48 const keys = { deadline: hidden("deadline"), party: hidden("party"), moment: hidden("moment"), at: "0", done: "0", q: "" };
49 const res = left % 2 === 0 ? await post("/reminders/today/send", { ...keys, subject: "Hello", body: "Words." }) : await post("/reminders/today/decline", { ...keys, reason: "Handled by phone" });
50 expect(res.status).toBe(303);
51 expect(headerCount(await get("/people"))).toBe(left === 1 ? null : left - 1);
52 }
53 expect(headerCount(await get("/profile"))).toBeNull();
54 expect(await get("/reminders/today")).toContain("All of today's reminders are sent or declined");
55 });
56
57 test("a signed-out visitor's header has no Review link", async () => {
58 expect(await get("/", {})).not.toContain("review-link");
59 });
static/app.css +8
@@ -962,6 +962,14 @@ a.marker {
962962 max-width: 24rem;
963963 }
964964
965 /* The header's Review link: the count has a slot of its own, so it appearing moves nothing. */
966 .review-count {
967 display: inline-block;
968 min-width: 2ch;
969 text-align: start;
970 font-variant-numeric: tabular-nums;
971 }
972
965973 @media print {
966974 .timeline-page {
967975 background: white;

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs