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

Sidebar.snooze.ts

65 lines2.2 KB

1import type { EnvironmentId } from "@t3tools/contracts";
2import type { TimestampFormat } from "@t3tools/contracts/settings";
3import { msg } from "gt-react";
4import {
5 pickSnoozePreset as pickSharedSnoozePreset,
6 resolveSnoozePresets as resolveSharedSnoozePresets,
7 snoozeWakeLabel,
8 type SnoozePreset,
9} from "@t3tools/client-runtime/state/thread-settled";
10​
11import { m, type MessageResolver } from "../i18n/messages";
12import { getFormattingLocale } from "../i18n/runtime";
13import { appAtomRegistry } from "../rpc/atomRegistry";
14import { serverEnvironment } from "../state/server";
15import { formatShortTimestamp } from "../timestampFormat";
16import { toastManager } from "./ui/toast";
17​
18export { snoozeWakeLabel, type SnoozePreset };
19​
20/** The snooze menu for a thread, from its environment's presets, read when the menu opens. */
21export function resolveSnoozePresets(
22 now: Date,
23 timestampFormat: TimestampFormat,
24 environmentId: EnvironmentId,
25 resolve: MessageResolver = m,
26): ReadonlyArray<SnoozePreset> {
27 return resolveSharedSnoozePresets(now, snoozeOptions(timestampFormat, environmentId), resolve);
29​
30/** A shown menu choice as picked; see the shared `pickSnoozePreset`. */
31export function pickSnoozePreset(
32 shown: ReadonlyArray<SnoozePreset>,
33 id: string,
34 timestampFormat: TimestampFormat,
35 environmentId: EnvironmentId,
36 resolve: MessageResolver = m,
37): SnoozePreset | undefined {
38 const picked = pickSharedSnoozePreset({
39 shown,
40 id,
41 now: new Date(),
42 ...snoozeOptions(timestampFormat, environmentId),
43 resolve,
44 });
45 if (!picked) {
46 toastManager.add({
47 type: "warning",
48 title: resolve(msg("Could not snooze thread")),
49 description: resolve(msg("That snooze time has passed. Choose another time.")),
50 });
51 }
52 return picked;
54​
55function snoozeOptions(timestampFormat: TimestampFormat, environmentId: EnvironmentId) {
56 const locale = getFormattingLocale();
57 const presets = appAtomRegistry.get(
58 serverEnvironment.settingsValueAtom(environmentId),
59 )?.snoozePresets;
60 return {
61 ...(presets ? { presets } : {}),
62 formatTime: (date: Date) => formatShortTimestamp(date.toISOString(), timestampFormat, locale),
63 locale,
64 };