Public: anyone can read this; the crew can change it.
A lead's page prints as a one-page brief to take to a showing: who they are, what they asked, the private note, the next showing and the last few messages #105
Check · Review · Land · Live done — Landed and live in production.
mason/speed-to-lead:mason/99-a-lead-s-page-prints-as-a-one-page-brief → main 1 commit fork
8 files
changed +167
−6
README.md +4
| @@ -159,6 +159,10 @@ Nothing is really sent in this prototype: "send" records the message in the | ||
| 159 | 159 | lead's history and goes no further. To run the tour again, restart the |
| 160 | 160 | samples (step 1). |
| 161 | 161 | |
| 162 | To take a lead to a showing on paper, press "Print brief" on their page: one | |
| 163 | sheet with their details, what they asked, your private note, the next showing | |
| 164 | and the last three messages (`?print=1` shows the same sheet on screen). | |
| 165 | ||
| 162 | 166 | ## How it works |
| 163 | 167 | |
| 164 | 168 | **When follow-ups run out.** A lead that is still new or contacted, whose |
docs/design/areas/lead.md added +14
| @@ -0,0 +1,14 @@ | ||
| 1 | # Lead: the printed brief | |
| 2 | ||
| 3 | A lead's page prints as one sheet to carry to a showing. No new script is | |
| 4 | needed to see it: "Print brief" is a plain link to `?print=1`, which shows the | |
| 5 | brief in place of the desk; `static/print.js` (same origin, no inline handler) | |
| 6 | turns the click into the browser's print dialog. | |
| 7 | ||
| 8 | New classes: `.brief` (the article: name, contact, texting consent, source, | |
| 9 | next showing, what they asked, the private note, the last three messages, and | |
| 10 | the agent's name, brokerage and license in `.brief-foot`) and `.printing` (on | |
| 11 | `#lead` at `?print=1`). `.brief` is `display: none` on screen, so nothing | |
| 12 | shifts. In `@media print` every other child of `#lead` is hidden, as are the | |
| 13 | header and footer, so the buttons, forms, drafts editor and follow-up controls | |
| 14 | never reach paper. |
src/brief.test.ts added +56
| @@ -0,0 +1,56 @@ | ||
| 1 | /** The printed brief: a link, a sheet in the page, print rules; the note is for a signed-in reader only. */ | |
| 2 | import { 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 { addLead, getLead, setNote, type Result } from "./actions.ts"; | |
| 7 | import { openDb } from "./db.ts"; | |
| 8 | import { currentAgent } from "./profile.ts"; | |
| 9 | import { leadPage } from "./pages/lead.ts"; | |
| 10 | import type { Who } from "./who.ts"; | |
| 11 | ||
| 12 | const alice: Who = { role: "user", id: "u1", name: "alice" }; | |
| 13 | const T0 = new Date("2026-03-01T10:00:00Z"); | |
| 14 | const must = <T>(r: Result<T>): T => { | |
| 15 | if (!r.ok) throw new Error(r.message); | |
| 16 | return r.value; | |
| 17 | }; | |
| 18 | ||
| 19 | function setup() { | |
| 20 | const db = openDb(mkdtempSync(join(tmpdir(), "app-brief-"))); | |
| 21 | const made = must( | |
| 22 | addLead(db, alice, { name: "Dana Reys", email: "[email protected]", phone: "(206) 555-0199", source: "website", message: "Can I see 12 Oak Street?", consent: true }, T0), | |
| 23 | ) as { id: number }; | |
| 24 | must(setNote(db, alice, { id: made.id, note: "Pre-approved <$650k>" }, T0)); | |
| 25 | return { db, id: made.id }; | |
| 26 | } | |
| 27 | ||
| 28 | test("the lead page carries the Print brief link and a brief with what the agent needs", () => { | |
| 29 | const { db, id } = setup(); | |
| 30 | const lead = must(getLead(db, alice, { id })); | |
| 31 | const page = leadPage({ who: alice, agent: currentAgent(db), lead, mayUpdate: true, next: null, now: T0.getTime() + 1000 }); | |
| 32 | expect(page).toContain(`<a href="/leads/${id}?print=1" data-print>Print brief</a>`); | |
| 33 | const brief = page.slice(page.indexOf('<article class="brief"'), page.indexOf("</article>")); | |
| 34 | for (const s of ["Dana Reys", "[email protected]", "(206) 555-0199", "Agreed to texts", "Can I see 12 Oak Street?", "Pre-approved <$650k>", "License", "none booked"]) | |
| 35 | expect(brief).toContain(s); | |
| 36 | expect(page).not.toContain('class="printing"'); | |
| 37 | expect(leadPage({ who: alice, agent: currentAgent(db), lead, mayUpdate: true, next: null, now: T0.getTime(), print: true })).toContain('class="printing"'); | |
| 38 | }); | |
| 39 | ||
| 40 | test("the stylesheet hides the desk and shows the brief in print, and no inline script is added", async () => { | |
| 41 | const css = await Bun.file(join(import.meta.dir, "..", "static", "app.css")).text(); | |
| 42 | const print = css.slice(css.lastIndexOf("@media print")); | |
| 43 | expect(print).toContain("#lead > :not(.brief)"); | |
| 44 | expect(print).toContain("display: none"); | |
| 45 | expect(css).toMatch(/\.brief \{\s*display: none;/); | |
| 46 | const { db, id } = setup(); | |
| 47 | const page = leadPage({ who: alice, agent: currentAgent(db), lead: must(getLead(db, alice, { id })), mayUpdate: true, next: null, now: T0.getTime() }); | |
| 48 | expect(page).not.toMatch(/onclick|<script>/); | |
| 49 | }); | |
| 50 | ||
| 51 | test("a signed-out visitor is refused the lead, so the private note never reaches them", () => { | |
| 52 | const { db, id } = setup(); | |
| 53 | const r = getLead(db, { role: "anon" }, { id }); | |
| 54 | expect(r.ok).toBe(false); | |
| 55 | expect(JSON.stringify(r)).not.toContain("Pre-approved"); | |
| 56 | }); |
src/pages/layout.ts +2 −1
| @@ -63,7 +63,8 @@ export function layout(input: { | ||
| 63 | 63 | <link rel="stylesheet" href="${asset("app.css")}"> |
| 64 | 64 | ${input.bare === true ? "" : `<script type="module" src="${asset("live.js")}"></script> |
| 65 | 65 | <script type="module" src="${asset("clock.js")}"></script> |
| 66 | <script type="module" src="${asset("replies.js")}"></script>`} | |
| 66 | <script type="module" src="${asset("replies.js")}"></script> | |
| 67 | <script type="module" src="${asset("print.js")}"></script>`} | |
| 67 | 68 | <body> |
| 68 | 69 | <div class="page"> |
| 69 | 70 | <header class="head"> |
src/pages/lead.ts +47 −3
| @@ -5,7 +5,7 @@ | ||
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | import { callLine, MAX_NOTE, OUTCOME_LABEL, OUTCOMES } from "../calls.ts"; |
| 8 | import { channelFor, dayLabel, isAnswer, planLabel, MAX_BODY } from "../drafts.ts"; | |
| 8 | import { channelFor, dayLabel, SOURCE_LABEL, isAnswer, planLabel, MAX_BODY } from "../drafts.ts"; | |
| 9 | 9 | import { fill, joiner, SAMPLE_REPLIES } from "../replies.ts"; |
| 10 | 10 | import { escape } from "../http.ts"; |
| 11 | 11 | import { referrerAddress } from "../thanks.ts"; |
| @@ -50,6 +50,8 @@ export function leadPage(input: { | ||
| 50 | 50 | notice?: string | undefined; |
| 51 | 51 | /** What the draft box holds when it is not the draft: a reply was added, or an edit was refused. Opens the edit. */ |
| 52 | 52 | editing?: string | undefined; |
| 53 | /** `?print=1`: show the one-page brief in place of the desk, as it prints. */ | |
| 54 | print?: boolean; | |
| 53 | 55 | }): string { |
| 54 | 56 | const { who, agent, lead, mayUpdate, next, now, notice, editing } = input; |
| 55 | 57 | const undo = mayUpdate ? (input.undo ?? null) : null; |
| @@ -319,8 +321,9 @@ export function leadPage(input: { | ||
| 319 | 321 | path, |
| 320 | 322 | notice, |
| 321 | 323 | heading: lead.name, |
| 322 | body: `<div id="lead" data-live="lead.changed"> | |
| 323 | <p><a class="backlink" href="/">← All leads</a></p> | |
| 324 | body: `<div id="lead" data-live="lead.changed"${input.print === true ? ' class="printing"' : ""}> | |
| 325 | ${briefCard(lead, agent, now)} | |
| 326 | <p><a class="backlink" href="/">← All leads</a> · <a href="${path}?print=1" data-print>Print brief</a></p> | |
| 324 | 327 | ${onward} |
| 325 | 328 | <header class="lead-head"> |
| 326 | 329 | <div> |
| @@ -358,6 +361,47 @@ export function leadPage(input: { | ||
| 358 | 361 | }); |
| 359 | 362 | } |
| 360 | 363 | |
| 364 | /** The most of a message the brief carries: it has one page. */ | |
| 365 | const BRIEF_WORDS = 280; | |
| 366 | const BRIEF_MESSAGES = 3; | |
| 367 | ||
| 368 | /** | |
| 369 | * The lead on one sheet for the agent to carry to a showing. Hidden on screen; | |
| 370 | * ` print` (and `?print=1`) show it and hide the rest of the page. The | |
| 371 | * private note is in it only because the page itself is for a signed-in reader. | |
| 372 | */ | |
| 373 | function briefCard(lead: LeadDetail, agent: Agent, now: number): string { | |
| 374 | const zone = agent.timeZone; | |
| 375 | const said = [ | |
| 376 | ...lead.messages | |
| 377 | .filter((m) => m.status === "sent") | |
| 378 | .map((m) => ({ at: m.sent_at ?? m.due_at, who: agent.name, body: m.body })), | |
| 379 | ...lead.replies.map((r) => ({ at: r.at, who: lead.name, body: r.body })), | |
| 380 | ] | |
| 381 | .sort((a, b) => Date.parse(b.at) - Date.parse(a.at)) | |
| 382 | .slice(0, BRIEF_MESSAGES); | |
| 383 | const cut = (t: string): string => (t.length > BRIEF_WORDS ? `${t.slice(0, BRIEF_WORDS).trimEnd()}…` : t); | |
| 384 | const texts = channelFor(lead) === "text" ? "Agreed to texts" : "Email only (no written consent to text)"; | |
| 385 | const item = (k: string, v: string): string => `<dt>${k}</dt><dd>${v}</dd>`; | |
| 386 | return `<article class="brief" aria-label="Brief for ${escape(lead.name)}"> | |
| 387 | <h1>${escape(lead.name)}</h1> | |
| 388 | <dl> | |
| 389 | ${item("Email", lead.email === "" ? "none" : escape(lead.email))} | |
| 390 | ${item("Phone", lead.phone === "" ? "none" : escape(lead.phone))} | |
| 391 | ${item("Texts", texts)} | |
| 392 | ${item("Source", escape(SOURCE_LABEL[lead.source]))} | |
| 393 | ${item("Showing", lead.showing === null ? "none booked" : escape(showingLine(lead.showing, now, zone)))} | |
| 394 | </dl> | |
| 395 | <h2>They asked <span class="muted">· ${escape(when(lead.created_at, zone))}</span></h2> | |
| 396 | <p class="body">${escape(lead.message)}</p> | |
| 397 | ${lead.note === "" ? "" : `<h2>Your note <span class="muted">· private</span></h2> | |
| 398 | <p class="body">${escape(lead.note)}</p>`} | |
| 399 | ${said.length === 0 ? "" : `<h2>Last messages</h2> | |
| 400 | <ol>${said.map((m) => `<li><span class="muted">${escape(when(m.at, zone))} · ${escape(m.who)}</span> ${escape(cut(m.body))}</li>`).join("")}</ol>`} | |
| 401 | <p class="brief-foot">${escape(agent.name)} · ${escape(agent.brokerage)} · License ${escape(agent.license)}</p> | |
| 402 | </article>`; | |
| 403 | } | |
| 404 | ||
| 361 | 405 | /** |
| 362 | 406 | * The thank-you to whoever referred the lead: labelled as going to the |
| 363 | 407 | * referrer, not the lead, and approved on its own button like any message. |
src/routes.ts +2 −2
| @@ -694,7 +694,7 @@ const withReplies = (input: Record<string, unknown>): Record<string, unknown> => | ||
| 694 | 694 | const leadView = ( |
| 695 | 695 | c: Context, |
| 696 | 696 | lead: LeadDetail, |
| 697 | extra: { notice?: string | undefined; editing?: string | undefined }, | |
| 697 | extra: { notice?: string | undefined; editing?: string | undefined; print?: boolean }, | |
| 698 | 698 | ): string => |
| 699 | 699 | leadPage({ |
| 700 | 700 | who: c.who, |
| @@ -1125,7 +1125,7 @@ export const routes: readonly Route[] = [ | ||
| 1125 | 1125 | STATUS[found.error], |
| 1126 | 1126 | ); |
| 1127 | 1127 | const { notice, clear } = readNotice(c.request); |
| 1128 | return html(leadView(c, found.value, { notice }), 200, clear); | |
| 1128 | return html(leadView(c, found.value, { notice, print: c.url.searchParams.get("print") === "1" }), 200, clear); | |
| 1129 | 1129 | }, |
| 1130 | 1130 | api: { |
| 1131 | 1131 | summary: |
static/app.css +34
| @@ -826,3 +826,37 @@ label.check input { | ||
| 826 | 826 | page-break-after: avoid; |
| 827 | 827 | } |
| 828 | 828 | } |
| 829 | ||
| 830 | /* The lead's brief: hidden on screen, the only thing on the sheet in print | |
| 831 | (or on screen at ?print=1, where print.js is not needed). */ | |
| 832 | .brief { | |
| 833 | display: none; | |
| 834 | } | |
| 835 | @media print { | |
| 836 | #lead > :not(.brief) { | |
| 837 | display: none; | |
| 838 | } | |
| 839 | .brief { | |
| 840 | display: block; | |
| 841 | break-inside: avoid; | |
| 842 | font-size: 11pt; | |
| 843 | } | |
| 844 | .brief-foot { | |
| 845 | margin-top: var(); | |
| 846 | border-top: 1px solid currentcolor; | |
| 847 | } | |
| 848 | .brief dl { | |
| 849 | display: grid; | |
| 850 | grid-template-columns: max-content 1fr; | |
| 851 | gap: var() var(); | |
| 852 | } | |
| 853 | .brief dd { | |
| 854 | margin: 0; | |
| 855 | } | |
| 856 | } | |
| 857 | #lead.printing > :not(.brief) { | |
| 858 | display: none; | |
| 859 | } | |
| 860 | #lead.printing .brief { | |
| 861 | display: block; | |
| 862 | } |
static/print.js added +8
| @@ -0,0 +1,8 @@ | ||
| 1 | // "Print brief" is a link to ?print=1 that works with no script; with this | |
| 2 | // loaded it opens the browser's print dialog on the page already there. | |
| 3 | document.addEventListener("click", (e) => { | |
| 4 | const link = e.target instanceof Element ? e.target.closest("a[data-print]") : null; | |
| 5 | if (link === null) return; | |
| 6 | e.preventDefault(); | |
| 7 | window.print(); | |
| 8 | }); |
new above
offline