Skip to content

Commit c3858bf

Browse files
t3dotggclaude
andauthored
fix(web): snooze options sort soonest first, fit 2 digits, and link from custom snooze (#26427)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent d2429f7 commit c3858bf

4 files changed

Lines changed: 117 additions & 15 deletions

File tree

‎apps/web/src/components/CustomSnoozeDialog.tsx‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useEffect, useId, useState } from "react";
2+
import { useNavigate } from "@tanstack/react-router";
23
import { create } from "zustand";
34
import {
45
localSnoozeDate,
@@ -7,7 +8,7 @@ import {
78
type CustomSnoozeInput,
89
} from "@t3tools/client-runtime/state/thread-settled";
910
import { Button } from "./ui/button";
10-
import { CalendarIcon } from "lucide-react";
11+
import { CalendarIcon, Settings2Icon } from "lucide-react";
1112
import { Calendar } from "./ui/calendar";
1213
import { weekStartsOn } from "../timestampFormat";
1314
import { useNowMinute } from "../hooks/useNowMinute";
@@ -63,6 +64,7 @@ export function CustomSnoozeDialogHost() {
6364

6465
function CustomSnoozeDialog() {
6566
const id = useId();
67+
const navigate = useNavigate();
6668
const [initial] = useState(() => new Date(Date.now() + 3_600_000));
6769
const nowMinute = useNowMinute();
6870
// Follows the clock so a dialog left open past midnight stops offering yesterday.
@@ -225,6 +227,21 @@ function CustomSnoozeDialog() {
225227
)}
226228
</DialogPanel>
227229
<DialogFooter>
230+
<Button
231+
type="button"
232+
variant="ghost-muted"
233+
className="sm:mr-auto"
234+
onClick={() => {
235+
finish(null);
236+
void navigate({
237+
to: "/settings/threads",
238+
hash: "snooze-options",
239+
});
240+
}}
241+
>
242+
<Settings2Icon />
243+
Edit snooze options
244+
</Button>
228245
<Button type="button" variant="outline" onClick={() => finish(null)}>
229246
Cancel
230247
</Button>

‎apps/web/src/components/settings/SnoozePresetsSettings.logic.test.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { reconcileSnoozeDraft } from "./SnoozePresetsSettings.logic";
3+
import { reconcileSnoozeDraft, sortSnoozeRows } from "./SnoozePresetsSettings.logic";
44

55
describe("reconcileSnoozeDraft", () => {
66
it("keeps the draft while our own writes echo back in order", () => {
@@ -18,3 +18,21 @@ describe("reconcileSnoozeDraft", () => {
1818
expect(reconcileSnoozeDraft(["a", "b", "a"], "b")).toEqual({ adopt: false, pending: ["a"] });
1919
});
2020
});
21+
22+
describe("sortSnoozeRows", () => {
23+
it("orders rows by wake time and puts a passed today option last", () => {
24+
// 16:00 local, so 20 hours wakes tomorrow and today 09:00 has passed.
25+
const now = new Date(2026, 9, 28, 16, 0);
26+
const sorted = sortSnoozeRows(
27+
[
28+
{ kind: "after", minutes: 20 * 60 },
29+
{ kind: "at", day: "today", time: "09:00" },
30+
{ kind: "at", day: "today", time: "18:00" },
31+
{ kind: "after", minutes: 60 },
32+
],
33+
["20h", "passed", "evening", "1h"],
34+
now,
35+
);
36+
expect(sorted.keys).toEqual(["1h", "evening", "20h", "passed"]);
37+
});
38+
});

‎apps/web/src/components/settings/SnoozePresetsSettings.logic.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { SnoozePresetSetting } from "@t3tools/contracts/settings";
2+
import { resolveSnoozePreset } from "@t3tools/client-runtime/state/thread-settled";
13
import * as Equal from "effect/Equal";
24

35
/**
@@ -13,3 +15,26 @@ export function reconcileSnoozeDraft<T>(
1315
? { adopt: true }
1416
: { adopt: false, pending: pending.slice(acknowledged + 1) };
1517
}
18+
19+
/**
20+
* Orders editor rows the way the menu lists them: soonest wake first, with options that
21+
* have no wake right now (a passed "Today") last. Ties keep their current order.
22+
*/
23+
export function sortSnoozeRows<K>(
24+
presets: ReadonlyArray<SnoozePresetSetting>,
25+
keys: ReadonlyArray<K>,
26+
now: Date,
27+
): { readonly presets: ReadonlyArray<SnoozePresetSetting>; readonly keys: ReadonlyArray<K> } {
28+
const order = presets
29+
.map((preset, index) => ({
30+
index,
31+
wake: resolveSnoozePreset(preset, now)?.snoozedUntil ?? null,
32+
}))
33+
.toSorted((a, b) =>
34+
a.wake === b.wake ? 0 : a.wake === null ? 1 : b.wake === null ? -1 : a.wake < b.wake ? -1 : 1,
35+
);
36+
return {
37+
presets: order.map(({ index }) => presets[index]!),
38+
keys: order.map(({ index }) => keys[index]!),
39+
};
40+
}

‎apps/web/src/components/settings/SnoozePresetsSettings.tsx‎

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import * as Schema from "effect/Schema";
1616
import { PlusIcon, XIcon } from "lucide-react";
1717

1818
import { useNowMinute } from "../../hooks/useNowMinute";
19-
import { reconcileSnoozeDraft } from "./SnoozePresetsSettings.logic";
19+
import { reconcileSnoozeDraft, sortSnoozeRows } from "./SnoozePresetsSettings.logic";
2020
import { formatShortTimestamp } from "../../timestampFormat";
2121
import { Button } from "../ui/button";
2222
import { Input } from "../ui/input";
@@ -90,6 +90,16 @@ function nextCandidate(presets: ReadonlyArray<SnoozePresetSetting>): SnoozePrese
9090
);
9191
}
9292

93+
// Row order is only display order, so it does not make a list differ from the defaults.
94+
function sameSnoozePresets(
95+
left: ReadonlyArray<SnoozePresetSetting>,
96+
right: ReadonlyArray<SnoozePresetSetting>,
97+
) {
98+
const ids = (presets: ReadonlyArray<SnoozePresetSetting>) =>
99+
presets.map(snoozePresetId).toSorted();
100+
return Equal.equals(ids(left), ids(right));
101+
}
102+
93103
const isSnoozePresetDay = Schema.is(SnoozePresetDay);
94104

95105
interface KindMemory {
@@ -109,16 +119,28 @@ function withWhen(
109119
return { kind: "at", day: when, time: preset.kind === "at" ? preset.time : memory.time };
110120
}
111121

112-
/** Edits the snooze menu's options. Menus sort them by wake time, so rows keep the order they were added in. */
122+
/**
123+
* Edits the snooze menu's options. Rows sort by wake time like the menu, but only when focus
124+
* leaves the list, so a row never moves under the pointer mid-edit.
125+
*/
113126
export function SnoozePresetsRow() {
114127
const settings = useScopedSettings();
115128
const updateSettings = useUpdateScopedSettings();
116129
const saved = settings.snoozePresets;
130+
const nowMinute = useNowMinute();
131+
const now = new Date(`${nowMinute}Z`);
117132
// Edits build on a local draft, so a second edit before the first save echoes back
118133
// does not restore what the first one changed. Echoes of our own writes keep the draft.
119-
const [presets, setPresets] = useState(saved);
120-
// Row keys survive removals, so a row's in-progress field state stays with that row.
121-
const [keys, setKeys] = useState<ReadonlyArray<number>>(() => saved.map((_, index) => index));
134+
// Row keys survive removals and sorts, so a row's in-progress field state stays with it.
135+
// One state, so a sort queued in the same event as an edit applies on top of it.
136+
const [rows, setRows] = useState(() =>
137+
sortSnoozeRows(
138+
saved,
139+
saved.map((_, index) => index),
140+
now,
141+
),
142+
);
143+
const { presets, keys } = rows;
122144
const [nextKey, setNextKey] = useState(saved.length);
123145
// Null forces the next render to adopt the saved value, as after a failed write.
124146
const [savedSource, setSavedSource] = useState<ReadonlyArray<SnoozePresetSetting> | null>(saved);
@@ -134,8 +156,7 @@ export function SnoozePresetsRow() {
134156
const reconciled = reconcileSnoozeDraft(written, saved);
135157
if (reconciled.adopt) {
136158
setWritten([]);
137-
setPresets(saved);
138-
setKeys(freshKeys(saved.length));
159+
setRows(sortSnoozeRows(saved, freshKeys(saved.length), now));
139160
} else {
140161
setWritten(reconciled.pending);
141162
}
@@ -145,8 +166,7 @@ export function SnoozePresetsRow() {
145166
nextKeys: ReadonlyArray<number> = keys,
146167
) => {
147168
if (!mixed && Equal.equals(snoozePresets, presets)) return;
148-
setPresets(snoozePresets);
149-
setKeys(nextKeys);
169+
setRows({ presets: snoozePresets, keys: nextKeys });
150170
setWritten((previous) => [...previous, snoozePresets]);
151171
updateSettings(
152172
{ snoozePresets },
@@ -168,10 +188,17 @@ export function SnoozePresetsRow() {
168188
{...searchableSetting("snooze-options")}
169189
description="Choices in every snooze menu, listed soonest first. Weekdays mean the next one after today."
170190
resetAction={
171-
!mixed && Equal.equals(presets, DEFAULT_SNOOZE_PRESETS) ? null : (
191+
!mixed && sameSnoozePresets(presets, DEFAULT_SNOOZE_PRESETS) ? null : (
172192
<SettingResetButton
173193
label="snooze options"
174-
onClick={() => save(DEFAULT_SNOOZE_PRESETS, freshKeys(DEFAULT_SNOOZE_PRESETS.length))}
194+
onClick={() => {
195+
const sorted = sortSnoozeRows(
196+
DEFAULT_SNOOZE_PRESETS,
197+
freshKeys(DEFAULT_SNOOZE_PRESETS.length),
198+
now,
199+
);
200+
save(sorted.presets, sorted.keys);
201+
}}
175202
/>
176203
)
177204
}
@@ -188,6 +215,7 @@ export function SnoozePresetsRow() {
188215
)
189216
}
190217
onAdd={() => save([...presets, nextCandidate(presets)], [...keys, ...freshKeys(1)])}
218+
onSettle={() => setRows((current) => sortSnoozeRows(current.presets, current.keys, now))}
191219
/>
192220
</SettingsRow>
193221
);
@@ -200,6 +228,7 @@ function SnoozePresetList(props: {
200228
onChange: (index: number, preset: SnoozePresetSetting) => void;
201229
onRemove: (index: number) => void;
202230
onAdd: () => void;
231+
onSettle: () => void;
203232
}) {
204233
const nowMinute = useNowMinute();
205234
const now = new Date(`${nowMinute}Z`);
@@ -232,7 +261,20 @@ function SnoozePresetList(props: {
232261
);
233262

234263
return (
235-
<div className="flex flex-col gap-1.5 pt-3 pb-2">
264+
<div
265+
className="flex flex-col gap-1.5 pt-3 pb-2"
266+
onBlur={(event) => {
267+
const list = event.currentTarget;
268+
const next = event.relatedTarget;
269+
if (list.contains(next)) return;
270+
// Select popups portal out of the list, so focus moving into one is not leaving it.
271+
if (next instanceof Element && next.closest('[data-slot="select-popup"]')) return;
272+
// Waits for the amount field's blur commit, and skips its Enter blur-and-refocus.
273+
queueMicrotask(() => {
274+
if (!list.contains(document.activeElement)) props.onSettle();
275+
});
276+
}}
277+
>
236278
{props.presets.map((preset, index) => {
237279
const preview = previews[index]!;
238280
return (
@@ -371,7 +413,7 @@ function DurationFields(props: { minutes: number; onChange: (minutes: number) =>
371413
<>
372414
<NumberField
373415
size="sm"
374-
className="w-24"
416+
className="w-32"
375417
min={1}
376418
max={maxAmount}
377419
step={1}

0 commit comments

Comments
 (0)