Public: anyone can read this; the crew can change it.
Times on the lead page and the Activity page read in the agent's time zone, not the server's #67
Check · Review · Land · Live done — Landed and live in production.
mason/speed-to-lead:mason/65-times-on-the-lead-page-and-the-activity → main 2 commits fork 1 thread · 1 unresolved
7 files
changed +86
−29
src/pages/activity.ts +4 −4
| @@ -22,13 +22,13 @@ const leadOf = (row: AuditRow, names: ReadonlyMap<number, string>): string => { | ||
| 22 | 22 | : `<a href="/leads/${Number(row.target)}">${escape(name)}</a>`; |
| 23 | 23 | }; |
| 24 | 24 | |
| 25 | function sentence(row: AuditRow, names: ReadonlyMap<number, string>): string { | |
| 25 | function sentence(row: AuditRow, names: ReadonlyMap<number, string>, zone: string): string { | |
| 26 | 26 | const text = said(row, leadOf(row, names), escape); |
| 27 | 27 | const refused = |
| 28 | 28 | row.allowed === 1 |
| 29 | 29 | ? "" |
| 30 | 30 | : ` <span class="chip" data-state="lost">refused</span>`; |
| 31 | return `<li class="activity-row">${text}${refused} <span class="muted">· ${escape(when(row.at))}</span></li>`; | |
| 31 | return `<li class="activity-row">${text}${refused} <span class="muted">· <time datetime="${escape(row.at)}">${escape(when(row.at, zone))}</time></span></li>`; | |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | /** Leads lost in the last 30 days, counted by the reason the agent gave. */ |
| @@ -60,7 +60,7 @@ export function activityPage(input: { | ||
| 60 | 60 | const list = |
| 61 | 61 | rows.length === 0 |
| 62 | 62 | ? `<p class="empty">Nothing here yet.</p>` |
| 63 | : `<ul class="card stack activity">${rows.map((r) => sentence(r, names)).join("\n")}</ul>`; | |
| 63 | : `<ul class="card stack activity">${rows.map((r) => sentence(r, names, agent.timeZone)).join("\n")}</ul>`; | |
| 64 | 64 | return layout({ |
| 65 | 65 | who, |
| 66 | 66 | agent, |
| @@ -68,7 +68,7 @@ export function activityPage(input: { | ||
| 68 | 68 | heading: "Activity", |
| 69 | 69 | body: `<p><a class="backlink" href="/">← All leads</a></p> |
| 70 | 70 | <h1>Activity</h1> |
| 71 | <p class="muted">Every change, newest first, and who made it. Refused attempts are shown too.</p> | |
| 71 | <p class="muted">Every change, newest first, and who made it. Refused attempts are shown too. Times are in ${escape(agent.timeZone)}.</p> | |
| 72 | 72 | ${lostBlock(lost)} |
| 73 | 73 | ${links} |
| 74 | 74 | ${list} |
src/pages/desk.ts +2 −2
| @@ -168,7 +168,7 @@ function status(lead: DeskLead, now: number, zone: string): string { | ||
| 168 | 168 | if (lead.ready !== null) |
| 169 | 169 | return `<span class="status" data-ready>Day ${lead.ready.day} follow-up ready</span>`; |
| 170 | 170 | if (lead.next_at !== null) |
| 171 | return `<span class="status">Next follow-up ${escape(day(held(lead, lead.next_at)))}</span>`; | |
| 171 | return `<span class="status">Next follow-up ${escape(day(held(lead, lead.next_at), zone))}</span>`; | |
| 172 | 172 | return `<span class="status">Follow-ups done</span>`; |
| 173 | 173 | } |
| 174 | 174 | |
| @@ -177,7 +177,7 @@ const row = (lead: DeskLead, now: number, zone: string): string => `<li> | ||
| 177 | 177 | <span class="lead-main"> |
| 178 | 178 | <span class="lead-name">${escape(lead.name)}</span>${noteLine(lead.note) === "" ? "" : ` |
| 179 | 179 | <span class="note-line">${escape(noteLine(lead.note))}</span>`} |
| 180 | <span class="tags">${source(lead)}${consent(lead)}${showingBadge(lead, now, zone)}</span> | |
| 180 | <span class="tags">${source(lead)}${consent(lead, zone)}${showingBadge(lead, now, zone)}</span> | |
| 181 | 181 | <span class="ask">${escape(lead.message)}</span> |
| 182 | 182 | ${status(lead, now, zone)} |
| 183 | 183 | </span> |
src/pages/lead.ts +11 −11
| @@ -87,7 +87,7 @@ export function leadPage(input: { | ||
| 87 | 87 | ? `<div class="pane ours"><h2>Nothing to send right now</h2><p class="muted">${ |
| 88 | 88 | closed |
| 89 | 89 | ? lead.state === "lost" |
| 90 | ? `Lost — ${escape(lostSaid(lead))}${lead.lostAt === null ? "" : `, ${escape(day(lead.lostAt))}`}: out of the follow-up plan.` | |
| 90 | ? `Lost — ${escape(lostSaid(lead))}${lead.lostAt === null ? "" : `, ${escape(day(lead.lostAt, agent.timeZone))}`}: out of the follow-up plan.` | |
| 91 | 91 | : `Marked ${STATE_LABEL[lead.state].toLowerCase()}: out of the follow-up plan.` |
| 92 | 92 | : lead.messages.some((m) => m.status === "scheduled") |
| 93 | 93 | ? "The next follow-up is drafted below and comes due on its day." |
| @@ -147,16 +147,16 @@ export function leadPage(input: { | ||
| 147 | 147 | </li>` |
| 148 | 148 | : c !== undefined |
| 149 | 149 | ? `<li data-from="call"> |
| 150 | <p class="meta">${callLine(c.outcome)} · <time datetime="${escape(c.at)}">${escape(when(c.at))}</time></p> | |
| 150 | <p class="meta">${callLine(c.outcome)} · <time datetime="${escape(c.at)}">${escape(when(c.at, agent.timeZone))}</time></p> | |
| 151 | 151 | ${c.note === "" ? "" : `<p class="body">${escape(c.note)}</p>`} |
| 152 | 152 | </li>` |
| 153 | 153 | : r !== undefined |
| 154 | 154 | ? `<li data-from="lead"> |
| 155 | <p class="meta">${escape(lead.name)} wrote back · ${CHANNEL[r.channel]} · <time datetime="${escape(r.at)}">${escape(when(r.at))}</time></p> | |
| 155 | <p class="meta">${escape(lead.name)} wrote back · ${CHANNEL[r.channel]} · <time datetime="${escape(r.at)}">${escape(when(r.at, agent.timeZone))}</time></p> | |
| 156 | 156 | <p class="body">${escape(r.body)}</p> |
| 157 | 157 | </li>` |
| 158 | 158 | : `<li> |
| 159 | <p class="meta">${titleOf(m!)} · ${CHANNEL[m!.channel]} · <time datetime="${escape(m!.sent_at ?? "")}">${escape(when(m!.sent_at ?? m!.due_at))}</time>${m!.sent_by ? ` · approved by ${escape(m!.sent_by)}` : ""}</p> | |
| 159 | <p class="meta">${titleOf(m!)} · ${CHANNEL[m!.channel]} · <time datetime="${escape(m!.sent_at ?? "")}">${escape(when(m!.sent_at ?? m!.due_at, agent.timeZone))}</time>${m!.sent_by ? ` · approved by ${escape(m!.sent_by)}` : ""}</p> | |
| 160 | 160 | ${words(m!)}${link(m!)} |
| 161 | 161 | </li>`, |
| 162 | 162 | ) |
| @@ -170,13 +170,13 @@ export function leadPage(input: { | ||
| 170 | 170 | ? `<section class="card"><h2>Follow-up plan</h2><p class="muted">Day 1, 3, 7, 14 and 30 follow-ups are drafted the moment the first reply goes out.</p></section>` |
| 171 | 171 | : `<section class="card"><h2>Follow-up plan</h2>${ |
| 172 | 172 | cadence.some(waits) |
| 173 | ? `<p class="hint">Follow-ups due before the showing (${escape(showingWhen(lead.showing!.at, now, agent.timeZone))}) wait until the day after it, ${escape(day(lead.showing!.hold_until))}.</p>` | |
| 173 | ? `<p class="hint">Follow-ups due before the showing (${escape(showingWhen(lead.showing!.at, now, agent.timeZone))}) wait until the day after it, ${escape(day(lead.showing!.hold_until, agent.timeZone))}.</p>` | |
| 174 | 174 | : "" |
| 175 | 175 | }<ol class="plan">${cadence |
| 176 | 176 | .map( |
| 177 | 177 | (m) => `<li data-status="${m.status}"> |
| 178 | 178 | <span class="plan-day">Day ${m.day}</span> |
| 179 | <span class="plan-when">${escape(day(m.sent_at ?? m.due_at))}</span> | |
| 179 | <span class="plan-when">${escape(day(m.sent_at ?? m.due_at, agent.timeZone))}</span> | |
| 180 | 180 | <span class="plan-status">${waits(m) ? "Waits for the showing" : PLAN[m.status]}</span> |
| 181 | 181 | ${m.status === "scheduled" ? `<details class="peek"><summary>Preview</summary>${words(m)}</details>` : ""} |
| 182 | 182 | ${ |
| @@ -289,7 +289,7 @@ export function leadPage(input: { | ||
| 289 | 289 | <header class="lead-head"> |
| 290 | 290 | <div> |
| 291 | 291 | <h1>${escape(lead.name)}</h1> |
| 292 | <p class="tags">${source(lead)}${consent(lead)}<span class="chip" data-state="${lead.state}">${STATE_LABEL[lead.state]}</span></p> | |
| 292 | <p class="tags">${source(lead)}${consent(lead, agent.timeZone)}<span class="chip" data-state="${lead.state}">${STATE_LABEL[lead.state]}</span></p> | |
| 293 | 293 | <p class="contact">${contact}${ |
| 294 | 294 | mayUpdate && lead.phone !== "" |
| 295 | 295 | ? ` · <a href="#log-call">Log a call</a>` |
| @@ -298,10 +298,10 @@ export function leadPage(input: { | ||
| 298 | 298 | </div> |
| 299 | 299 | ${clock(lead, now, owed)} |
| 300 | 300 | </header> |
| 301 | ${noteCard(lead, path, mayUpdate)} | |
| 301 | ${noteCard(lead, path, mayUpdate, agent.timeZone)} | |
| 302 | 302 | <div class="split"> |
| 303 | 303 | <div class="pane theirs"> |
| 304 | <h2>They asked <span class="muted">· <time datetime="${escape(lead.created_at)}">${escape(when(lead.created_at))}</time></span></h2> | |
| 304 | <h2>They asked <span class="muted">· <time datetime="${escape(lead.created_at)}">${escape(when(lead.created_at, agent.timeZone))}</time></span></h2> | |
| 305 | 305 | <blockquote>${escape(lead.message)}</blockquote> |
| 306 | 306 | </div> |
| 307 | 307 | ${draft} |
| @@ -319,13 +319,13 @@ export function leadPage(input: { | ||
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | /** "Your note": the agent's own, private; the form only for whoever may update the lead. */ |
| 322 | function noteCard(lead: LeadDetail, path: string, may: boolean): string { | |
| 322 | function noteCard(lead: LeadDetail, path: string, may: boolean, zone: string): string { | |
| 323 | 323 | if (lead.note === "" && !may) return ""; |
| 324 | 324 | return `<section class="card note" id="note"> |
| 325 | 325 | <h2>Your note <span class="muted">· private, never sent to the lead${ |
| 326 | 326 | lead.noteUpdatedAt === null |
| 327 | 327 | ? "" |
| 328 | : ` · edited <time datetime="${escape(lead.noteUpdatedAt)}">${escape(when(lead.noteUpdatedAt))}</time>` | |
| 328 | : ` · edited <time datetime="${escape(lead.noteUpdatedAt)}">${escape(when(lead.noteUpdatedAt, zone))}</time>` | |
| 329 | 329 | }</span></h2>${lead.note === "" ? "" : ` |
| 330 | 330 | <p class="body">${escape(lead.note)}</p>`}${ |
| 331 | 331 | may |
src/pages/parts.ts +6 −4
| @@ -28,11 +28,11 @@ export function clock(lead: Lead, now = Date.now(), owedSince: string | null = n | ||
| 28 | 28 | export const source = (lead: Lead): string => |
| 29 | 29 | `<span class="chip">${escape(SOURCE_LABEL[lead.source])}</span>`; |
| 30 | 30 | |
| 31 | export const consent = (lead: Lead): string => | |
| 31 | export const consent = (lead: Lead, zone: string): string => | |
| 32 | 32 | channelFor(lead) === "text" |
| 33 | 33 | ? `<span class="badge" data-consent="text" title="They gave written consent to be texted">Text OK</span>` |
| 34 | 34 | : lead.opted_out_at !== null |
| 35 | ? `<span class="badge" data-consent="email" title="They asked to stop texts">Email only (opted out ${escape(day(lead.opted_out_at))})</span>` | |
| 35 | ? `<span class="badge" data-consent="email" title="They asked to stop texts">Email only (opted out ${escape(day(lead.opted_out_at, zone))})</span>` | |
| 36 | 36 | : `<span class="badge" data-consent="email" title="No written consent to text: email only">Email only</span>`; |
| 37 | 37 | |
| 38 | 38 | export const STATE_LABEL: Readonly<Record<Lead["state"], string>> = { |
| @@ -43,17 +43,19 @@ export const STATE_LABEL: Readonly<Record<Lead["state"], string>> = { | ||
| 43 | 43 | }; |
| 44 | 44 | |
| 45 | 45 | /** "Tue, Oct 14", or with the time when it is today or soon. */ |
| 46 | export const day = (iso: string): string => | |
| 46 | export const day = (iso: string, zone: string): string => | |
| 47 | 47 | new Date(iso).toLocaleDateString("en-US", { |
| 48 | 48 | weekday: "short", |
| 49 | 49 | month: "short", |
| 50 | 50 | day: "numeric", |
| 51 | timeZone: zone, | |
| 51 | 52 | }); |
| 52 | 53 | |
| 53 | export const when = (iso: string): string => | |
| 54 | export const when = (iso: string, zone: string): string => | |
| 54 | 55 | new Date(iso).toLocaleString("en-US", { |
| 55 | 56 | month: "short", |
| 56 | 57 | day: "numeric", |
| 57 | 58 | hour: "numeric", |
| 58 | 59 | minute: "2-digit", |
| 60 | timeZone: zone, | |
| 59 | 61 | }); |
src/terminal.test.ts +12 −1
| @@ -6,7 +6,7 @@ import { expect, test } from "bun:test"; | ||
| 6 | 6 | import { mkdtempSync } from "node:fs"; |
| 7 | 7 | import { tmpdir } from "node:os"; |
| 8 | 8 | import { join } from "node:path"; |
| 9 | import { getLead, listLeads } from "./actions.ts"; | |
| 9 | import { getLead, listLeads, saveProfile } from "./actions.ts"; | |
| 10 | 10 | import { sourceLine } from "./clock.ts"; |
| 11 | 11 | import { openDb } from "./db.ts"; |
| 12 | 12 | import { agent } from "./env.ts"; |
| @@ -359,6 +359,17 @@ test("a opens Activity: an admin reads sentences and filters to messages sent; a | ||
| 359 | 359 | } |
| 360 | 360 | }); |
| 361 | 361 | |
| 362 | test("Activity in the terminal reads in the agent's zone and says which", () => { | |
| 363 | const db = fresh(); | |
| 364 | saveProfile(db, root, { ...agent, timeZone: "America/Los_Angeles" }); | |
| 365 | db.query("UPDATE audit SET at = '2026-11-02T01:30:00.000Z'").run(); | |
| 366 | const t = terminal(db, root); | |
| 367 | t.key("a"); | |
| 368 | expect(read(t)).toContain("Times are in America/Los_Angeles."); | |
| 369 | expect(read(t)).toContain("Nov 1, 5:30 PM"); | |
| 370 | expect(read(t)).not.toContain("Nov 2, 1:30 AM"); | |
| 371 | }); | |
| 372 | ||
| 362 | 373 | test("o puts a closed lead back in follow-ups", () => { |
| 363 | 374 | const db = fresh(); |
| 364 | 375 | const t = terminal(db, alice); |
src/terminal.ts +4 −7
| @@ -61,6 +61,7 @@ import { SOURCE_LABEL, SOURCES, channelFor, isAnswer, type Channel, type Source | ||
| 61 | 61 | import { ssh, title, where } from "./env.ts"; |
| 62 | 62 | import { LOST_REASONS, REASON_LABEL, lostSaid } from "./lost.ts"; |
| 63 | 63 | import { answerSince, CLOSED, MAX_QUERY, VIEWS, queryOf, type DeskLead, type LeadDetail, type Message, type View as DeskView } from "./leads.ts"; |
| 64 | import { when } from "./pages/parts.ts"; | |
| 64 | 65 | import { currentAgent } from "./profile.ts"; |
| 65 | 66 | import type { Who } from "./who.ts"; |
| 66 | 67 | |
| @@ -466,7 +467,8 @@ export function terminal( | ||
| 466 | 467 | }); |
| 467 | 468 | if (!found.ok) return [found.message, "", dim("esc back")]; |
| 468 | 469 | const { rows, names, older } = found.value; |
| 469 | const lines = [bold(v.sentOnly ? "Activity — messages sent" : "Activity"), ""]; | |
| 470 | const zone = currentAgent(db).timeZone; | |
| 471 | const lines = [bold(v.sentOnly ? "Activity — messages sent" : "Activity"), dim(`Times are in ${clean(zone)}.`), ""]; | |
| 470 | 472 | if (rows.length === 0) lines.push(dim("Nothing here yet.")); |
| 471 | 473 | for (const row of rows) { |
| 472 | 474 | const name = names.get(Number(row.target)); |
| @@ -475,12 +477,7 @@ export function terminal( | ||
| 475 | 477 | name === undefined ? "a removed lead" : clean(name), |
| 476 | 478 | clean, |
| 477 | 479 | ); |
| 478 | const at = new Date(row.at).toLocaleString("en-US", { | |
| 479 | month: "short", | |
| 480 | day: "numeric", | |
| 481 | hour: "numeric", | |
| 482 | minute: "2-digit", | |
| 483 | }); | |
| 480 | const at = when(row.at, zone); | |
| 484 | 481 | lines.push(`${text}${row.allowed === 1 ? "" : " (refused)"} ${dim(`· ${at}`)}`); |
| 485 | 482 | } |
| 486 | 483 | lines.push( |
src/timezone.test.ts added +47
| @@ -0,0 +1,47 @@ | ||
| 1 | /** Clock times on the lead page and Activity read in the agent's zone; the export stays ISO. */ | |
| 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 { getLead } from "./actions.ts"; | |
| 7 | import { openDb } from "./db.ts"; | |
| 8 | import { agent } from "./env.ts"; | |
| 9 | import { leadsCsv } from "./export.ts"; | |
| 10 | import { desk, insertLead } from "./leads.ts"; | |
| 11 | import { activityPage } from "./pages/activity.ts"; | |
| 12 | import { leadPage } from "./pages/lead.ts"; | |
| 13 | import { day, when } from "./pages/parts.ts"; | |
| 14 | import type { Who } from "./who.ts"; | |
| 15 | ||
| 16 | const alice: Who = { role: "user", id: "u1", name: "alice" }; | |
| 17 | const INSTANT = "2026-11-02T01:30:00.000Z"; | |
| 18 | const pacific = { ...agent, timeZone: "America/Los_Angeles" }; | |
| 19 | ||
| 20 | test("when and day read in the given zone", () => { | |
| 21 | expect(when(INSTANT, "America/Los_Angeles")).toBe("Nov 1, 5:30 PM"); | |
| 22 | expect(when(INSTANT, "UTC")).toBe("Nov 2, 1:30 AM"); | |
| 23 | expect(day(INSTANT, "America/Los_Angeles")).toBe("Sun, Nov 1"); | |
| 24 | }); | |
| 25 | ||
| 26 | test("the lead page shows the inquiry in Pacific time and keeps the ISO instant", () => { | |
| 27 | const db = openDb(mkdtempSync(join(tmpdir(), "app-tz-db-"))); | |
| 28 | const id = insertLead(db, { name: "Dana", email: "[email protected]", phone: "", source: "website", message: "Hi", consent: false }, INSTANT, agent); | |
| 29 | const lead = getLead(db, alice, { id }); | |
| 30 | if (!lead.ok) throw new Error("no lead"); | |
| 31 | const page = leadPage({ who: alice, agent: pacific, lead: lead.value, mayUpdate: true, next: null, now: Date.parse(INSTANT) }); | |
| 32 | expect(page).toContain(`<time datetime="${INSTANT}">Nov 1, 5:30 PM</time>`); | |
| 33 | expect(page).not.toContain("Nov 2, 1:30 AM"); | |
| 34 | }); | |
| 35 | ||
| 36 | test("Activity says which zone its times are in and shows each in it", () => { | |
| 37 | const row = { id: 1, at: INSTANT, who_id: "u1", who_name: "alice", role: "user", resource: "lead", verb: "update", target: "1", allowed: 1 as const, outcome: "ok", act: "reply", detail: null }; | |
| 38 | const page = activityPage({ who: { role: "admin", id: "a", name: "root" }, agent: pacific, rows: [row], names: new Map([[1, "Dana"]]), older: undefined, lost: [], sentOnly: false }); | |
| 39 | expect(page).toContain("Times are in America/Los_Angeles."); | |
| 40 | expect(page).toContain(`<time datetime="${INSTANT}">Nov 1, 5:30 PM</time>`); | |
| 41 | }); | |
| 42 | ||
| 43 | test("the CSV export keeps ISO instants whatever the profile zone", () => { | |
| 44 | const db = openDb(mkdtempSync(join(tmpdir(), "app-tz-db-"))); | |
| 45 | insertLead(db, { name: "Dana", email: "[email protected]", phone: "", source: "website", message: "Hi", consent: false }, INSTANT, pacific); | |
| 46 | expect(leadsCsv(desk(db, 1), new Map())).toContain(INSTANT); | |
| 47 | }); |
new above
offline
The terminal Activity screen (src/terminal.ts ~478) has no timeZone and still shows server time. Give it the agent zone and the same "Times are in …" note.