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
Check · Review · Land · Live done — Landed and live in production.
mason/deal-clock:mason/89-every-page-s-header-says-how-many-messag → main 2 commits fork 1 thread · 1 unresolved
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 | ||
| 71 | 71 | there opens the sheet of the date it is for, where the reminder is edited, |
| 72 | 72 | sent or declined. Many to get through? **Review all**, beside that heading, |
| 73 | 73 | 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. | |
| 75 | 75 | Decided against a message? Beside Send, **Don't send** asks why (up to 200 |
| 76 | 76 | characters). The reminder stops counting as "to send" and reads "Not sent: |
| 77 | 77 | <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"; | ||
| 16 | 16 | export function activityPage(input: { |
| 17 | 17 | who: Who; |
| 18 | 18 | agent?: Agent | undefined; |
| 19 | waiting?: number | undefined; | |
| 19 | 20 | db: Database; |
| 20 | 21 | /** Up to one more than a page: the extra says there are older rows. */ |
| 21 | 22 | rows: readonly AuditRow[]; |
| @@ -46,5 +47,5 @@ export function activityPage(input: { | ||
| 46 | 47 | <p class="summary">Who approved, declined or changed what, newest first. ${filter}</p> |
| 47 | 48 | ${rows.length === 0 ? `<p class="muted">Nothing yet.</p>` : `<ul class="stack">${rows.map(line).join("")}</ul>`} |
| 48 | 49 | ${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 }); | |
| 50 | 51 | } |
src/pages/clock.ts +6 −2
| @@ -141,6 +141,8 @@ const pages = memoizer<string>(3); | ||
| 141 | 141 | /** The clock, rendered once while nothing it shows has changed. */ |
| 142 | 142 | export function clock(input: { |
| 143 | 143 | who: Who; |
| 144 | /** How many of today's messages wait for approval, for the header. */ | |
| 145 | waiting?: number | undefined; | |
| 144 | 146 | agent?: Agent | undefined; |
| 145 | 147 | deals: readonly DealView[]; |
| 146 | 148 | on: string; |
| @@ -149,7 +151,7 @@ export function clock(input: { | ||
| 149 | 151 | q?: string | undefined; |
| 150 | 152 | }): string { |
| 151 | 153 | 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]) + | |
| 153 | 155 | input.deals.map(print).join(), |
| 154 | 156 | ); |
| 155 | 157 | return pages(`${input.deals.length}:${key}`, () => render(input)); |
| @@ -157,6 +159,8 @@ export function clock(input: { | ||
| 157 | 159 | |
| 158 | 160 | function render(input: { |
| 159 | 161 | who: Who; |
| 162 | /** How many of today's messages wait for approval, for the header. */ | |
| 163 | waiting?: number | undefined; | |
| 160 | 164 | agent?: Agent | undefined; |
| 161 | 165 | deals: readonly DealView[]; |
| 162 | 166 | on: string; |
| @@ -250,5 +254,5 @@ function render(input: { | ||
| 250 | 254 | } |
| 251 | 255 | <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> |
| 252 | 256 | </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 }); | |
| 254 | 258 | } |
src/pages/deal.ts +6 −2
| @@ -79,6 +79,8 @@ function messages(deal: DealView, may: boolean): string { | ||
| 79 | 79 | |
| 80 | 80 | export function dealPage(input: { |
| 81 | 81 | who: Who; |
| 82 | /** How many of today's messages wait for approval, for the header. */ | |
| 83 | waiting?: number | undefined; | |
| 82 | 84 | agent?: Agent | undefined; |
| 83 | 85 | deal: DealView; |
| 84 | 86 | on: string; |
| @@ -189,7 +191,7 @@ export function dealPage(input: { | ||
| 189 | 191 | }) |
| 190 | 192 | : "" |
| 191 | 193 | }`; |
| 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 }); | |
| 193 | 195 | } |
| 194 | 196 | |
| 195 | 197 | /** 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 | ||
| 215 | 217 | |
| 216 | 218 | export function newDealPage(input: { |
| 217 | 219 | who: Who; |
| 220 | /** How many of today's messages wait for approval, for the header. */ | |
| 221 | waiting?: number | undefined; | |
| 218 | 222 | agent?: Agent | undefined; |
| 219 | 223 | on: string; |
| 220 | 224 | notice?: string | undefined; |
| @@ -275,5 +279,5 @@ export function newDealPage(input: { | ||
| 275 | 279 | <p class="muted small">Reading these from the contract PDF is phase two.</p> |
| 276 | 280 | </section> |
| 277 | 281 | ${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 }); | |
| 279 | 283 | } |
src/pages/layout.ts +10 −2
| @@ -25,6 +25,12 @@ import type { Who } from "../who.ts"; | ||
| 25 | 25 | |
| 26 | 26 | const iconHref = asset("icon.svg"); |
| 27 | 27 | |
| 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 { | |
| 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 | ||
| 28 | 34 | export function layout(input: { |
| 29 | 35 | body: string; |
| 30 | 36 | who: Who; |
| @@ -35,6 +41,8 @@ export function layout(input: { | ||
| 35 | 41 | notice?: string | undefined; |
| 36 | 42 | /** The link a notice about a sent reminder offers: the href `outbox` made for it. */ |
| 37 | 43 | 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; | |
| 38 | 46 | /** Whether the page keeps its regions live; false for a page with none. */ |
| 39 | 47 | live?: boolean; |
| 40 | 48 | /** 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: { | ||
| 42 | 50 | /** A class on <body>, for styles that belong to one page only. */ |
| 43 | 51 | bodyClass?: string; |
| 44 | 52 | }): 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; | |
| 46 | 54 | const pageTitle = input.heading === undefined ? title : `${input.heading} · ${title}`; |
| 47 | 55 | const to = encodeURIComponent(path); |
| 48 | 56 | const door = |
| @@ -86,7 +94,7 @@ ${plain || input.live === false ? "" : `<script type="module" src="${asset("live | ||
| 86 | 94 | ? "" |
| 87 | 95 | : `<nav class="row"> |
| 88 | 96 | ${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>` : ""}`} | |
| 90 | 98 | ${door} |
| 91 | 99 | </nav>` |
| 92 | 100 | } |
src/pages/missing.ts +2 −2
| @@ -8,7 +8,7 @@ | ||
| 8 | 8 | import type { Who } from "../who.ts"; |
| 9 | 9 | import { layout } from "./layout.ts"; |
| 10 | 10 | |
| 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 { | |
| 12 | 12 | const { who, path, deal = false } = input; |
| 13 | 13 | const sentence = deal |
| 14 | 14 | ? "This deal is not here; it may have been removed." |
| @@ -18,5 +18,5 @@ export function missingPage(input: { who: Who; path: string; deal?: boolean }): | ||
| 18 | 18 | ? `<a class="button" href="/__forge/signin?to=${encodeURIComponent("/")}">Sign in</a>` |
| 19 | 19 | : `<a class="button" href="/">Back to the clock</a>`; |
| 20 | 20 | 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 }); | |
| 22 | 22 | } |
src/pages/people.ts +2 −2
| @@ -22,10 +22,10 @@ const person = (p: Person, on: string): string => `<section class="card person"> | ||
| 22 | 22 | ${p.owes.length === 0 ? `<p class="muted">Nothing open</p>` : `<ul class="week-list">${p.owes.map((o) => owedRow(o, on)).join("")}</ul>`} |
| 23 | 23 | </section>`; |
| 24 | 24 | |
| 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 { | |
| 26 | 26 | const { who, people, on } = input; |
| 27 | 27 | const body = `<h1>People</h1> |
| 28 | 28 | <p class="summary">Everyone on your deals, with every open date they owe, soonest first.</p> |
| 29 | 29 | ${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 }); | |
| 31 | 31 | } |
src/pages/profile.ts +3 −1
| @@ -17,6 +17,8 @@ export interface ProfileState { | ||
| 17 | 17 | |
| 18 | 18 | export function profilePage(input: { |
| 19 | 19 | who: Who; |
| 20 | /** How many of today's messages wait for approval, for the header. */ | |
| 21 | waiting?: number | undefined; | |
| 20 | 22 | profile: Profile; |
| 21 | 23 | /** Whether the table lets `who` save it. */ |
| 22 | 24 | mayChange: boolean; |
| @@ -62,5 +64,5 @@ export function profilePage(input: { | ||
| 62 | 64 | <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> |
| 63 | 65 | ${mayChange ? form : readOnly} |
| 64 | 66 | </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 }); | |
| 66 | 68 | } |
src/pages/refused.ts +2 −1
| @@ -13,6 +13,7 @@ import { layout } from "./layout.ts"; | ||
| 13 | 13 | export function refusedPage(input: { |
| 14 | 14 | who: Who; |
| 15 | 15 | agent?: Agent | undefined; |
| 16 | waiting?: number | undefined; | |
| 16 | 17 | path: string; |
| 17 | 18 | heading: string; |
| 18 | 19 | error: Failure; |
| @@ -23,5 +24,5 @@ export function refusedPage(input: { | ||
| 23 | 24 | error === "signin_required" |
| 24 | 25 | ? `<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>` |
| 25 | 26 | : `<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 }); | |
| 27 | 28 | } |
src/pages/review.ts +3 −1
| @@ -47,6 +47,8 @@ const FIELD_LABEL: Readonly<Record<ReviewField, string>> = { subject: "Subject", | ||
| 47 | 47 | |
| 48 | 48 | export function reviewPage(input: { |
| 49 | 49 | who: Who; |
| 50 | /** How many of today's messages wait for approval, for the header. */ | |
| 51 | waiting?: number | undefined; | |
| 50 | 52 | agent?: Agent | undefined; |
| 51 | 53 | /** The drafts still to send, in the clock's order. */ |
| 52 | 54 | queue: readonly Draft[]; |
| @@ -68,7 +70,7 @@ export function reviewPage(input: { | ||
| 68 | 70 | draft === undefined |
| 69 | 71 | ? `<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>` |
| 70 | 72 | : 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 }); | |
| 72 | 74 | } |
| 73 | 75 | |
| 74 | 76 | function form( |
src/reminders.ts +12
| @@ -334,3 +334,15 @@ export const pending = (d: Draft, day: string): boolean => | ||
| 334 | 334 | */ |
| 335 | 335 | export const todaysDrafts = (deals: readonly DealView[], day: string, agent: Agent): Draft[] => |
| 336 | 336 | 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"; | ||
| 86 | 86 | import { ACTIVITY_PAGE } from "./activity.ts"; |
| 87 | 87 | import { timelinePage } from "./pages/timeline.ts"; |
| 88 | 88 | 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"; | |
| 90 | 90 | import { token, tokensCss } from "./tokens.ts"; |
| 91 | 91 | import type { Who } from "./who.ts"; |
| 92 | 92 | |
| @@ -350,7 +350,7 @@ const STATUS: Record<Failure, number> = { | ||
| 350 | 350 | |
| 351 | 351 | /** A deal that is not there, for a reader who may read deals: the product's own 404. */ |
| 352 | 352 | 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); | |
| 354 | 354 | |
| 355 | 355 | /** Whether a client wants JSON: anything under the API, or an `accept` that asks for it. */ |
| 356 | 356 | const wantsJson = (request: Request, url: URL): boolean => |
| @@ -359,9 +359,9 @@ const wantsJson = (request: Request, url: URL): boolean => | ||
| 359 | 359 | (request.headers.get("accept") ?? "").includes("application/json"); |
| 360 | 360 | |
| 361 | 361 | /** 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 { | |
| 363 | 363 | 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); | |
| 365 | 365 | } |
| 366 | 366 | |
| 367 | 367 | /** A result as JSON: the value, or `{ error, message }` with the matching status. */ |
| @@ -407,9 +407,28 @@ const unconfirmed = (c: Context, path: string): Response => | ||
| 407 | 407 | function reviewQueue(c: Context, q: string): Result<Draft[]> { |
| 408 | 408 | const deals = listDeals(c.db, c.who); |
| 409 | 409 | 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) }; | |
| 411 | 411 | } |
| 412 | 412 | |
| 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 | ||
| 413 | 432 | /** A small count from the query or a form, or 0. */ |
| 414 | 433 | const count = (value: unknown): number => (typeof value === "string" && /^\d{1,4}$/.test(value) ? Number(value) : 0); |
| 415 | 434 | |
| @@ -430,7 +449,7 @@ async function reviewed(c: Context, act: (f: Record<string, string>) => Result<{ | ||
| 430 | 449 | const result = act(f); |
| 431 | 450 | if (result.ok) return back(said, reviewHref(q, at, settled + 1), said === SENT ? result.value.id : undefined); |
| 432 | 451 | 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]); | |
| 434 | 453 | const queue = reviewQueue(c, q); |
| 435 | 454 | const same = queue.ok |
| 436 | 455 | ? 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<{ | ||
| 438 | 457 | if (result.error !== "invalid" || !queue.ok || same === -1) return back(result.message, reviewHref(q, at, settled)); |
| 439 | 458 | const typed = { subject: f.subject ?? "", body: f.body ?? "", reason: f.reason ?? "" }; |
| 440 | 459 | 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 }), | |
| 442 | 461 | STATUS.invalid, |
| 443 | 462 | ); |
| 444 | 463 | } |
| @@ -483,11 +502,11 @@ export const routes: readonly Route[] = [ | ||
| 483 | 502 | path: "/", |
| 484 | 503 | handle: (c) => { |
| 485 | 504 | 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); | |
| 487 | 506 | const deals = listDeals(c.db, c.who); |
| 488 | 507 | if (!deals.ok) |
| 489 | 508 | 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 }), | |
| 491 | 510 | 200, |
| 492 | 511 | clear, |
| 493 | 512 | ); |
| @@ -496,7 +515,7 @@ export const routes: readonly Route[] = [ | ||
| 496 | 515 | return html( |
| 497 | 516 | refusedPage({ |
| 498 | 517 | who: c.who, |
| 499 | agent: agentOf(c.db), | |
| 518 | agent: agentFor(c), | |
| 500 | 519 | path: "/", |
| 501 | 520 | heading: "The clock", |
| 502 | 521 | error: "invalid", |
| @@ -505,7 +524,7 @@ export const routes: readonly Route[] = [ | ||
| 505 | 524 | 400, |
| 506 | 525 | clear, |
| 507 | 526 | ); |
| 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); | |
| 509 | 528 | }, |
| 510 | 529 | }, |
| 511 | 530 | { |
| @@ -515,10 +534,10 @@ export const routes: readonly Route[] = [ | ||
| 515 | 534 | const deals = listDeals(c.db, c.who); |
| 516 | 535 | if (!deals.ok) |
| 517 | 536 | 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 }), | |
| 519 | 538 | STATUS[deals.error], |
| 520 | 539 | ); |
| 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() })); | |
| 522 | 541 | }, |
| 523 | 542 | }, |
| 524 | 543 | { |
| @@ -535,10 +554,10 @@ export const routes: readonly Route[] = [ | ||
| 535 | 554 | }); |
| 536 | 555 | const path = "/activity"; |
| 537 | 556 | 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]); | |
| 539 | 558 | 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" })); | |
| 542 | 561 | }, |
| 543 | 562 | }, |
| 544 | 563 | { |
| @@ -547,7 +566,7 @@ export const routes: readonly Route[] = [ | ||
| 547 | 566 | handle: (c) => { |
| 548 | 567 | const deals = listDeals(c.db, c.who); |
| 549 | 568 | 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)), { | |
| 551 | 570 | headers: { |
| 552 | 571 | "content-type": "text/calendar; charset=utf-8", |
| 553 | 572 | "content-disposition": 'attachment; filename="deal-clock.ics"', |
| @@ -562,7 +581,7 @@ export const routes: readonly Route[] = [ | ||
| 562 | 581 | path: "/deals/new", |
| 563 | 582 | handle: (c) => { |
| 564 | 583 | 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); | |
| 566 | 585 | }, |
| 567 | 586 | }, |
| 568 | 587 | { |
| @@ -576,7 +595,7 @@ export const routes: readonly Route[] = [ | ||
| 576 | 595 | return html( |
| 577 | 596 | refusedPage({ |
| 578 | 597 | who: c.who, |
| 579 | agent: agentOf(c.db), | |
| 598 | agent: agentFor(c), | |
| 580 | 599 | path: c.url.pathname, |
| 581 | 600 | heading: "A deal", |
| 582 | 601 | error: found.error, |
| @@ -585,7 +604,7 @@ export const routes: readonly Route[] = [ | ||
| 585 | 604 | STATUS[found.error], |
| 586 | 605 | ); |
| 587 | 606 | 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); | |
| 589 | 608 | }, |
| 590 | 609 | }, |
| 591 | 610 | { |
| @@ -596,7 +615,7 @@ export const routes: readonly Route[] = [ | ||
| 596 | 615 | const found = getDeal(c.db, c.who, { id: Number(c.params.id) }); |
| 597 | 616 | if (!found.ok && (found.error === "not_found" || found.error === "invalid")) return missingDeal(c); |
| 598 | 617 | 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))); | |
| 600 | 619 | }, |
| 601 | 620 | }, |
| 602 | 621 | { |
| @@ -610,7 +629,7 @@ export const routes: readonly Route[] = [ | ||
| 610 | 629 | return html( |
| 611 | 630 | refusedPage({ |
| 612 | 631 | who: c.who, |
| 613 | agent: agentOf(c.db), | |
| 632 | agent: agentFor(c), | |
| 614 | 633 | path: c.url.pathname, |
| 615 | 634 | heading: "A deal", |
| 616 | 635 | error: found.error, |
| @@ -618,7 +637,7 @@ export const routes: readonly Route[] = [ | ||
| 618 | 637 | }), |
| 619 | 638 | STATUS[found.error], |
| 620 | 639 | ); |
| 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() })); | |
| 622 | 641 | }, |
| 623 | 642 | }, |
| 624 | 643 | { |
| @@ -628,7 +647,7 @@ export const routes: readonly Route[] = [ | ||
| 628 | 647 | const found = getDeal(c.db, c.who, { id: Number(c.params.id) }); |
| 629 | 648 | if (!found.ok) return text(found.message, STATUS[found.error]); |
| 630 | 649 | 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)), { | |
| 632 | 651 | headers: { |
| 633 | 652 | "content-type": "text/calendar; charset=utf-8", |
| 634 | 653 | "content-disposition": `attachment; filename="${name}.ics"`, |
| @@ -647,7 +666,7 @@ export const routes: readonly Route[] = [ | ||
| 647 | 666 | const made = createDeal(c.db, c.who, newDeal(fromForm(f))); |
| 648 | 667 | if (made.ok) return back("Deal opened. Every date is on the clock.", `/deals/${made.value.id}`); |
| 649 | 668 | 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); | |
| 651 | 670 | }, |
| 652 | 671 | }, |
| 653 | 672 | { |
| @@ -661,9 +680,9 @@ export const routes: readonly Route[] = [ | ||
| 661 | 680 | if (!may.ok) return back(may.message, "/deals/new"); |
| 662 | 681 | const planned = plan(newDeal(fromForm(f))); |
| 663 | 682 | 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); | |
| 665 | 684 | 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 } })); | |
| 667 | 686 | }, |
| 668 | 687 | }, |
| 669 | 688 | { |
| @@ -700,11 +719,11 @@ export const routes: readonly Route[] = [ | ||
| 700 | 719 | const got = getProfile(c.db, c.who); |
| 701 | 720 | if (!got.ok) |
| 702 | 721 | 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 }), | |
| 704 | 723 | STATUS[got.error], |
| 705 | 724 | clear, |
| 706 | 725 | ); |
| 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); | |
| 708 | 727 | }, |
| 709 | 728 | }, |
| 710 | 729 | { |
| @@ -718,7 +737,7 @@ export const routes: readonly Route[] = [ | ||
| 718 | 737 | if (saved.error !== "invalid") return back(saved.message, "/profile"); |
| 719 | 738 | const profile = getProfile(c.db, c.who); |
| 720 | 739 | 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); | |
| 722 | 741 | }, |
| 723 | 742 | }, |
| 724 | 743 | { |
| @@ -773,7 +792,7 @@ export const routes: readonly Route[] = [ | ||
| 773 | 792 | if (saved.error !== "invalid" || f.address === undefined) return back(saved.message, `/deals/${id}`); |
| 774 | 793 | const found = getDeal(c.db, c.who, { id }); |
| 775 | 794 | 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); | |
| 777 | 796 | }, |
| 778 | 797 | }, |
| 779 | 798 | { |
| @@ -902,18 +921,18 @@ export const routes: readonly Route[] = [ | ||
| 902 | 921 | const { notice, open, clear } = readNotice(c.request); |
| 903 | 922 | const q = query(c.url.searchParams.get("q")); |
| 904 | 923 | 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); | |
| 906 | 925 | const queue = reviewQueue(c, q); |
| 907 | 926 | if (!queue.ok) |
| 908 | 927 | 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 }), | |
| 910 | 929 | STATUS[queue.error], |
| 911 | 930 | clear, |
| 912 | 931 | ); |
| 913 | 932 | const sent = open === undefined ? undefined : listDeals(c.db, c.who); |
| 914 | 933 | const link = open === undefined || !sent?.ok ? undefined : sent.value.flatMap((d) => d.sent).find((m) => m.id === open)?.href ?? null; |
| 915 | 934 | 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 }), | |
| 917 | 936 | 200, |
| 918 | 937 | clear, |
| 919 | 938 | ); |
| @@ -951,7 +970,7 @@ export const routes: readonly Route[] = [ | ||
| 951 | 970 | const deals = listDeals(c.db, c.who); |
| 952 | 971 | if (!deals.ok) return answer(deals); |
| 953 | 972 | 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)) }); | |
| 955 | 974 | }, |
| 956 | 975 | api: { |
| 957 | 976 | summary: |
| @@ -1066,7 +1085,7 @@ export const routes: readonly Route[] = [ | ||
| 1066 | 1085 | { |
| 1067 | 1086 | method: "GET", |
| 1068 | 1087 | 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) })), | |
| 1070 | 1089 | api: { |
| 1071 | 1090 | summary: |
| 1072 | 1091 | "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) { | ||
| 51 | 51 | request.method === "HEAD" ? "GET" : request.method, |
| 52 | 52 | url.pathname, |
| 53 | 53 | ); |
| 54 | if (found === undefined) return head(request, notFound(request, url, me)); | |
| 54 | if (found === undefined) return head(request, notFound(request, url, me, db)); | |
| 55 | 55 | const response = await found.route.handle({ |
| 56 | 56 | request, |
| 57 | 57 | 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 { | ||
| 962 | 962 | max-width: 24rem; |
| 963 | 963 | } |
| 964 | 964 | |
| 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 | ||
| 965 | 973 | @media print { |
| 966 | 974 | .timeline-page { |
| 967 | 975 | background: white; |
new above
offline
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.