Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Sidebar.snooze.ts

127 lines4.4 KB

1/**
2 * Snooze preset resolution for the sidebar snooze menu. Pure functions so
3 * the preset math (evening/tomorrow/next-week boundaries) is unit-testable
4 * without a DOM.
5 *
6 * Presets deliberately skew short: agent-thread rhythms are hours (a CI
7 * run, a teammate review, the next work session), not days.
8 */
9import { parseTimestampDate } from "../timestampFormat";
10​
11type SnoozePresetId = "hour" | "evening" | "tomorrow" | "next-week";
12​
13export interface SnoozePreset {
14 readonly id: SnoozePresetId;
15 readonly label: string;
16 /** Menu-row time column. Complements the label instead of repeating it:
17 "Tomorrow" pairs with "9:00 AM", not "tomorrow 9:00 AM". */
18 readonly whenLabel: string;
19 /** ISO wake time. */
20 readonly snoozedUntil: string;
22​
23function timeOfDayLabel(date: Date): string {
24 return date.toLocaleTimeString(undefined, { hour: "numeric", minute: "2-digit" });
26​
27const EVENING_HOUR = 18;
28const MORNING_HOUR = 9;
29const HOUR_MS = 60 * 60 * 1_000;
30const DAY_MS = 24 * HOUR_MS;
31​
32function atHour(base: Date, hour: number): Date {
33 const next = new Date(base);
34 next.setHours(hour, 0, 0, 0);
35 return next;
37​
38// Calendar-day advance instead of adding DAY_MS: fixed millisecond offsets
39// land on the wrong local day across DST transitions (a spring-forward day
40// is 23 hours, so 23:30 + 24h skips the whole next day).
41function addDays(base: Date, days: number): Date {
42 const next = new Date(base);
43 next.setDate(next.getDate() + days);
44 return next;
46​
47/**
48 * Presets for "snooze until", computed against local time. "This evening"
49 * only appears while it is still meaningfully before evening; after that
50 * the list starts at "Tomorrow".
51 */
52export function resolveSnoozePresets(now: Date): ReadonlyArray<SnoozePreset> {
53 const inAnHour = new Date(now.getTime() + HOUR_MS);
54 const presets: SnoozePreset[] = [
55 {
56 id: "hour",
57 label: "In 1 hour",
58 whenLabel: timeOfDayLabel(inAnHour),
59 snoozedUntil: inAnHour.toISOString(),
60 },
61 ];
62​
63 const evening = atHour(now, EVENING_HOUR);
64 // Suppress the evening preset once it is within an hour (or past): it
65 // would duplicate "In 1 hour" or point at the past.
66 if (evening.getTime() - now.getTime() > HOUR_MS) {
67 presets.push({
68 id: "evening",
69 label: "This evening",
70 whenLabel: timeOfDayLabel(evening),
71 snoozedUntil: evening.toISOString(),
72 });
73 }
74​
75 const tomorrow = atHour(addDays(now, 1), MORNING_HOUR);
76 presets.push({
77 id: "tomorrow",
78 label: "Tomorrow",
79 whenLabel: timeOfDayLabel(tomorrow),
80 snoozedUntil: tomorrow.toISOString(),
81 });
82​
83 // Next Monday 9:00 (a week out when today is Monday).
84 const daysUntilMonday = (1 - now.getDay() + 7) % 7 || 7;
85 const nextWeek = atHour(addDays(now, daysUntilMonday), MORNING_HOUR);
86 presets.push({
87 id: "next-week",
88 label: "Next week",
89 whenLabel: `${nextWeek.toLocaleDateString(undefined, { weekday: "short" })} ${timeOfDayLabel(nextWeek)}`,
90 snoozedUntil: nextWeek.toISOString(),
91 });
92​
93 return presets;
95​
96/**
97 * Compact "wakes in" label for snoozed rows: "2h", "18h", "3d". Minutes
98 * round up so a snooze never reads "0m" while still hidden.
99 */
100export function snoozeWakeLabel(snoozedUntil: string, now: Date): string {
101 const wake = parseTimestampDate(snoozedUntil);
102 if (wake === null) return "now";
103 const remainingMs = wake.getTime() - now.getTime();
104 if (remainingMs <= 0) return "now";
105 if (remainingMs < HOUR_MS) return `${Math.max(1, Math.ceil(remainingMs / 60_000))}m`;
106 if (remainingMs < DAY_MS) return `${Math.ceil(remainingMs / HOUR_MS)}h`;
107 return `${Math.ceil(remainingMs / DAY_MS)}d`;
109​
110/**
111 * Human wake time for menus and toasts: "tomorrow 9:00", "Mon 9:00",
112 * "17:30" (today).
113 */
114export function snoozeWakeDescription(snoozedUntil: string, now: Date): string {
115 const wake = parseTimestampDate(snoozedUntil);
116 if (wake === null) return "";
117 const time = wake.toLocaleTimeString(undefined, { hour: "numeric", minute: "2-digit" });
118 const startOfToday = new Date(now);
119 startOfToday.setHours(0, 0, 0, 0);
120 const dayDelta = Math.floor((wake.getTime() - startOfToday.getTime()) / DAY_MS);
121 if (dayDelta === 0) return time;
122 if (dayDelta === 1) return `tomorrow ${time}`;
123 const weekday = wake.toLocaleDateString(undefined, { weekday: "short" });
124 if (dayDelta < 7) return `${weekday} ${time}`;
125 const date = wake.toLocaleDateString(undefined, { month: "short", day: "numeric" });
126 return `${date}, ${time}`;