Open History to see the last commit
1import type { TimestampFormat } from "@t3tools/contracts/settings";
2import {
3 resolveSnoozePresets as resolveSharedSnoozePresets,
4 snoozeWakeLabel,
5 type SnoozePreset,
6} from "@t3tools/client-runtime/state/thread-settled";
7
8import { formatShortTimestamp, parseTimestampDate } from "../timestampFormat";
9
10export { snoozeWakeLabel, type SnoozePreset };
11
12const DAY_MS = 24 * 60 * 60 * 1_000;
13
14function timeOfDayLabel(date: Date, timestampFormat: TimestampFormat): string {
15 return formatShortTimestamp(date.toISOString(), timestampFormat);
16}
17
18export function resolveSnoozePresets(
19 now: Date,
20 timestampFormat: TimestampFormat,
21): ReadonlyArray<SnoozePreset> {
22 return resolveSharedSnoozePresets(now).map((preset) => {
23 const wake = parseTimestampDate(preset.snoozedUntil);
24 if (wake === null) return preset;
25 const time = timeOfDayLabel(wake, timestampFormat);
26 return {
27 ...preset,
28 whenLabel:
29 preset.id === "next-week"
30 ? `${wake.toLocaleDateString(undefined, { weekday: "short" })} ${time}`
31 : time,
32 };
33 });
34}
35
36export function snoozeWakeDescription(
37 snoozedUntil: string,
38 now: Date,
39 timestampFormat: TimestampFormat,
40): string {
41 const wake = parseTimestampDate(snoozedUntil);
42 if (wake === null) return "";
43 const time = timeOfDayLabel(wake, timestampFormat);
44 const startOfToday = new Date(now);
45 startOfToday.setHours(0, 0, 0, 0);
46 const dayDelta = Math.floor((wake.getTime() - startOfToday.getTime()) / DAY_MS);
47 if (dayDelta === 0) return time;
48 if (dayDelta === 1) return `tomorrow ${time}`;
49 const weekday = wake.toLocaleDateString(undefined, { weekday: "short" });
50 if (dayDelta < 7) return `${weekday} ${time}`;
51 const date = wake.toLocaleDateString(undefined, { month: "short", day: "numeric" });
52 return `${date}, ${time}`;
53}