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

Merged opened this

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

Merged

mason/speed-to-lead:mason/99-a-lead-s-page-prints-as-a-one-page-brief → main 1 commit fork

  • Merged. mason/99-a-lead-s-page-prints-as-a-one-page-brief was merged into main as 0747c35.

Review

Sign in to review this change.
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
159159 lead's history and goes no further. To run the tour again, restart the
160160 samples (step 1).
161161
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
162166 ## How it works
163167
164168 **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 &lt;$650k&gt;", "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: {
6363 <link rel="stylesheet" href="${asset("app.css")}">
6464 ${input.bare === true ? "" : `<script type="module" src="${asset("live.js")}"></script>
6565 <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>`}
6768 <body>
6869 <div class="page">
6970 <header class="head">
src/pages/lead.ts +47 −3
@@ -5,7 +5,7 @@
55 */
66
77 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";
99 import { fill, joiner, SAMPLE_REPLIES } from "../replies.ts";
1010 import { escape } from "../http.ts";
1111 import { referrerAddress } from "../thanks.ts";
@@ -50,6 +50,8 @@ export function leadPage(input: {
5050 notice?: string | undefined;
5151 /** What the draft box holds when it is not the draft: a reply was added, or an edit was refused. Opens the edit. */
5252 editing?: string | undefined;
53 /** `?print=1`: show the one-page brief in place of the desk, as it prints. */
54 print?: boolean;
5355 }): string {
5456 const { who, agent, lead, mayUpdate, next, now, notice, editing } = input;
5557 const undo = mayUpdate ? (input.undo ?? null) : null;
@@ -319,8 +321,9 @@ export function leadPage(input: {
319321 path,
320322 notice,
321323 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>
324327 ${onward}
325328 <header class="lead-head">
326329 <div>
@@ -358,6 +361,47 @@ export function leadPage(input: {
358361 });
359362 }
360363
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 * `@media 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
361405 /**
362406 * The thank-you to whoever referred the lead: labelled as going to the
363407 * 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> =>
694694 const leadView = (
695695 c: Context,
696696 lead: LeadDetail,
697 extra: { notice?: string | undefined; editing?: string | undefined },
697 extra: { notice?: string | undefined; editing?: string | undefined; print?: boolean },
698698 ): string =>
699699 leadPage({
700700 who: c.who,
@@ -1125,7 +1125,7 @@ export const routes: readonly Route[] = [
11251125 STATUS[found.error],
11261126 );
11271127 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);
11291129 },
11301130 api: {
11311131 summary:
static/app.css +34
@@ -826,3 +826,37 @@ label.check input {
826826 page-break-after: avoid;
827827 }
828828 }
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(--space-4);
846 border-top: 1px solid currentcolor;
847 }
848 .brief dl {
849 display: grid;
850 grid-template-columns: max-content 1fr;
851 gap: var(--space-1) var(--space-3);
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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs