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

Merged opened this

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

Merged

mason/speed-to-lead:mason/65-times-on-the-lead-page-and-the-activity → main 2 commits fork 1 thread · 1 unresolved

  • Merged. mason/65-times-on-the-lead-page-and-the-activity was merged into main as 8003750.

Review

Sign in to review this change.
7 files changed +86 −29
src/pages/activity.ts +4 −4
@@ -22,13 +22,13 @@ const leadOf = (row: AuditRow, names: ReadonlyMap<number, string>): string => {
2222 : `<a href="/leads/${Number(row.target)}">${escape(name)}</a>`;
2323 };
2424
25 function sentence(row: AuditRow, names: ReadonlyMap<number, string>): string {
25 function sentence(row: AuditRow, names: ReadonlyMap<number, string>, zone: string): string {
2626 const text = said(row, leadOf(row, names), escape);
2727 const refused =
2828 row.allowed === 1
2929 ? ""
3030 : ` <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>`;
3232 }
3333
3434 /** Leads lost in the last 30 days, counted by the reason the agent gave. */
@@ -60,7 +60,7 @@ export function activityPage(input: {
6060 const list =
6161 rows.length === 0
6262 ? `<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>`;
6464 return layout({
6565 who,
6666 agent,
@@ -68,7 +68,7 @@ export function activityPage(input: {
6868 heading: "Activity",
6969 body: `<p><a class="backlink" href="/">← All leads</a></p>
7070 <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>

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.

7272 ${lostBlock(lost)}
7373 ${links}
7474 ${list}
src/pages/desk.ts +2 −2
@@ -168,7 +168,7 @@ function status(lead: DeskLead, now: number, zone: string): string {
168168 if (lead.ready !== null)
169169 return `<span class="status" data-ready>Day ${lead.ready.day} follow-up ready</span>`;
170170 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>`;
172172 return `<span class="status">Follow-ups done</span>`;
173173 }
174174
@@ -177,7 +177,7 @@ const row = (lead: DeskLead, now: number, zone: string): string => `<li>
177177 <span class="lead-main">
178178 <span class="lead-name">${escape(lead.name)}</span>${noteLine(lead.note) === "" ? "" : `
179179 <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>
181181 <span class="ask">${escape(lead.message)}</span>
182182 ${status(lead, now, zone)}
183183 </span>
src/pages/lead.ts +11 −11
@@ -87,7 +87,7 @@ export function leadPage(input: {
8787 ? `<div class="pane ours"><h2>Nothing to send right now</h2><p class="muted">${
8888 closed
8989 ? 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.`
9191 : `Marked ${STATE_LABEL[lead.state].toLowerCase()}: out of the follow-up plan.`
9292 : lead.messages.some((m) => m.status === "scheduled")
9393 ? "The next follow-up is drafted below and comes due on its day."
@@ -147,16 +147,16 @@ export function leadPage(input: {
147147 </li>`
148148 : c !== undefined
149149 ? `<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>
151151 ${c.note === "" ? "" : `<p class="body">${escape(c.note)}</p>`}
152152 </li>`
153153 : r !== undefined
154154 ? `<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>
156156 <p class="body">${escape(r.body)}</p>
157157 </li>`
158158 : `<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>
160160 ${words(m!)}${link(m!)}
161161 </li>`,
162162 )
@@ -170,13 +170,13 @@ export function leadPage(input: {
170170 ? `<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>`
171171 : `<section class="card"><h2>Follow-up plan</h2>${
172172 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>`
174174 : ""
175175 }<ol class="plan">${cadence
176176 .map(
177177 (m) => `<li data-status="${m.status}">
178178 <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>
180180 <span class="plan-status">${waits(m) ? "Waits for the showing" : PLAN[m.status]}</span>
181181 ${m.status === "scheduled" ? `<details class="peek"><summary>Preview</summary>${words(m)}</details>` : ""}
182182 ${
@@ -289,7 +289,7 @@ export function leadPage(input: {
289289 <header class="lead-head">
290290 <div>
291291 <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>
293293 <p class="contact">${contact}${
294294 mayUpdate && lead.phone !== ""
295295 ? ` · <a href="#log-call">Log a call</a>`
@@ -298,10 +298,10 @@ export function leadPage(input: {
298298 </div>
299299 ${clock(lead, now, owed)}
300300 </header>
301 ${noteCard(lead, path, mayUpdate)}
301 ${noteCard(lead, path, mayUpdate, agent.timeZone)}
302302 <div class="split">
303303 <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>
305305 <blockquote>${escape(lead.message)}</blockquote>
306306 </div>
307307 ${draft}
@@ -319,13 +319,13 @@ export function leadPage(input: {
319319 }
320320
321321 /** "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 {
323323 if (lead.note === "" && !may) return "";
324324 return `<section class="card note" id="note">
325325 <h2>Your note <span class="muted">· private, never sent to the lead${
326326 lead.noteUpdatedAt === null
327327 ? ""
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>`
329329 }</span></h2>${lead.note === "" ? "" : `
330330 <p class="body">${escape(lead.note)}</p>`}${
331331 may
src/pages/parts.ts +6 −4
@@ -28,11 +28,11 @@ export function clock(lead: Lead, now = Date.now(), owedSince: string | null = n
2828 export const source = (lead: Lead): string =>
2929 `<span class="chip">${escape(SOURCE_LABEL[lead.source])}</span>`;
3030
31 export const consent = (lead: Lead): string =>
31 export const consent = (lead: Lead, zone: string): string =>
3232 channelFor(lead) === "text"
3333 ? `<span class="badge" data-consent="text" title="They gave written consent to be texted">Text OK</span>`
3434 : 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>`
3636 : `<span class="badge" data-consent="email" title="No written consent to text: email only">Email only</span>`;
3737
3838 export const STATE_LABEL: Readonly<Record<Lead["state"], string>> = {
@@ -43,17 +43,19 @@ export const STATE_LABEL: Readonly<Record<Lead["state"], string>> = {
4343 };
4444
4545 /** "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 =>
4747 new Date(iso).toLocaleDateString("en-US", {
4848 weekday: "short",
4949 month: "short",
5050 day: "numeric",
51 timeZone: zone,
5152 });
5253
53 export const when = (iso: string): string =>
54 export const when = (iso: string, zone: string): string =>
5455 new Date(iso).toLocaleString("en-US", {
5556 month: "short",
5657 day: "numeric",
5758 hour: "numeric",
5859 minute: "2-digit",
60 timeZone: zone,
5961 });
src/terminal.test.ts +12 −1
@@ -6,7 +6,7 @@ import { expect, test } from "bun:test";
66 import { mkdtempSync } from "node:fs";
77 import { tmpdir } from "node:os";
88 import { join } from "node:path";
9 import { getLead, listLeads } from "./actions.ts";
9 import { getLead, listLeads, saveProfile } from "./actions.ts";
1010 import { sourceLine } from "./clock.ts";
1111 import { openDb } from "./db.ts";
1212 import { agent } from "./env.ts";
@@ -359,6 +359,17 @@ test("a opens Activity: an admin reads sentences and filters to messages sent; a
359359 }
360360 });
361361
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
362373 test("o puts a closed lead back in follow-ups", () => {
363374 const db = fresh();
364375 const t = terminal(db, alice);
src/terminal.ts +4 −7
@@ -61,6 +61,7 @@ import { SOURCE_LABEL, SOURCES, channelFor, isAnswer, type Channel, type Source
6161 import { ssh, title, where } from "./env.ts";
6262 import { LOST_REASONS, REASON_LABEL, lostSaid } from "./lost.ts";
6363 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";
6465 import { currentAgent } from "./profile.ts";
6566 import type { Who } from "./who.ts";
6667
@@ -466,7 +467,8 @@ export function terminal(
466467 });
467468 if (!found.ok) return [found.message, "", dim("esc back")];
468469 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)}.`), ""];
470472 if (rows.length === 0) lines.push(dim("Nothing here yet."));
471473 for (const row of rows) {
472474 const name = names.get(Number(row.target));
@@ -475,12 +477,7 @@ export function terminal(
475477 name === undefined ? "a removed lead" : clean(name),
476478 clean,
477479 );
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);
484481 lines.push(`${text}${row.allowed === 1 ? "" : " (refused)"} ${dim(`· ${at}`)}`);
485482 }
486483 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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs