Skip to content

Commit ca84a38

Browse files
saphidclaude
andcommitted
fix(web): keep built-in snooze presets in place and normalize saved favorites
Saved snooze durations no longer reshuffle the menus. The built-in presets keep their usual order and saved favorites follow them, soonest first, so a user with no favorites sees exactly the menu they had. - Settings decoding now keeps one favorite per length ("60 minutes" and "1 hour" are the same), the newest five, and drops ones too long to ever wake. Stored, imported and patched settings follow the same cap as saving one, and adding a favorite uses the same rule. - A favorite that wakes when a built-in does is offered once, on the built-in row, which carries its remove control so it stays removable. - Cover a favorite re-added while its removal is still being written. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 285ad7c commit ca84a38

8 files changed

Lines changed: 191 additions & 78 deletions

File tree

‎apps/web/src/components/Sidebar.snooze.test.ts‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ function localDate(year: number, month: number, day: number, hour: number, minut
88
}
99

1010
describe("resolveSnoozePresets", () => {
11-
it("orders favorites chronologically among the built-in presets", () => {
11+
it("keeps the built-in presets in order and adds favorites after them, soonest first", () => {
1212
// Wednesday 2026-04-08 10:00 local.
1313
const presets = resolveSnoozePresets(localDate(2026, 4, 8, 10), "locale", [
14-
{ amount: 2, unit: "hours" },
1514
{ amount: 2, unit: "days" },
15+
{ amount: 2, unit: "hours" },
1616
]);
1717
expect(presets.map((preset) => preset.id)).toEqual([
1818
"hour",
19-
"favorite:2-hours",
2019
"three-hours",
2120
"evening",
2221
"tomorrow",
23-
"favorite:2-days",
2422
"next-week",
23+
"favorite:2-hours",
24+
"favorite:2-days",
2525
]);
2626
const twoHours = presets.find((preset) => preset.id === "favorite:2-hours");
2727
expect(twoHours).toMatchObject({ label: "In 2 hours" });
@@ -35,14 +35,6 @@ describe("resolveSnoozePresets", () => {
3535
expect(twoDays!.whenLabel.startsWith(weekday(twoDays!))).toBe(true);
3636
});
3737

38-
it("keeps wake times ascending when evening falls before three hours", () => {
39-
// 15:30: in 1 hour 16:30, this evening 18:00, in 3 hours 18:30.
40-
const ids = resolveSnoozePresets(localDate(2026, 4, 8, 15, 30), "locale").map(
41-
(preset) => preset.id,
42-
);
43-
expect(ids.slice(0, 3)).toEqual(["hour", "evening", "three-hours"]);
44-
});
45-
4638
it("offers one hour, three hours, evening, tomorrow, and next week in the morning", () => {
4739
// Wednesday 2026-04-08 10:00 local.
4840
const presets = resolveSnoozePresets(localDate(2026, 4, 8, 10), "locale");

‎apps/web/src/components/Sidebar.snooze.ts‎

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { SnoozeFavorite, TimestampFormat } from "@t3tools/contracts/settings";
22
import {
3-
resolveSnoozeFavoritePresets,
4-
resolveSnoozePresets as resolveSharedSnoozePresets,
3+
resolveSnoozePresetsWithFavorites,
54
snoozeWakeLabel,
65
type SnoozePreset,
76
} from "@t3tools/client-runtime/state/thread-settled";
@@ -16,29 +15,27 @@ function timeOfDayLabel(date: Date, timestampFormat: TimestampFormat): string {
1615
return formatShortTimestamp(date.toISOString(), timestampFormat);
1716
}
1817

19-
/** The built-in presets and the user's saved custom durations, soonest wake first. */
18+
/** The built-in presets, then the user's saved custom durations. */
2019
export function resolveSnoozePresets(
2120
now: Date,
2221
timestampFormat: TimestampFormat,
2322
favorites: ReadonlyArray<SnoozeFavorite> = [],
2423
): ReadonlyArray<SnoozePreset> {
25-
return [...resolveSharedSnoozePresets(now), ...resolveSnoozeFavoritePresets(favorites, now)]
26-
.sort((a, b) => Date.parse(a.snoozedUntil) - Date.parse(b.snoozedUntil))
27-
.map((preset) => {
28-
const wake = parseTimestampDate(preset.snoozedUntil);
29-
if (wake === null) return preset;
30-
const time = timeOfDayLabel(wake, timestampFormat);
31-
return {
32-
...preset,
33-
// A wake on another day names its weekday, so a saved duration such
34-
// as "26 hours" does not read as earlier than "Tomorrow".
35-
whenLabel:
36-
preset.id === "next-week" ||
37-
(preset.id.startsWith("favorite:") && wake.toDateString() !== now.toDateString())
38-
? `${wake.toLocaleDateString(undefined, { weekday: "short" })} ${time}`
39-
: time,
40-
};
41-
});
24+
return resolveSnoozePresetsWithFavorites(favorites, now).map((preset) => {
25+
const wake = parseTimestampDate(preset.snoozedUntil);
26+
if (wake === null) return preset;
27+
const time = timeOfDayLabel(wake, timestampFormat);
28+
return {
29+
...preset,
30+
// A wake on another day names its weekday, so a saved duration such
31+
// as "26 hours" does not read as earlier than "Tomorrow".
32+
whenLabel:
33+
preset.id === "next-week" ||
34+
(preset.id.startsWith("favorite:") && wake.toDateString() !== now.toDateString())
35+
? `${wake.toLocaleDateString(undefined, { weekday: "short" })} ${time}`
36+
: time,
37+
};
38+
});
4239
}
4340

4441
/**

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
canSnooze,
3535
effectiveSnoozed,
3636
removeSnoozeFavorite,
37-
snoozeFavoriteId,
37+
snoozeFavoriteLabel,
3838
threadWokeAt,
3939
} from "@t3tools/client-runtime/state/thread-settled";
4040
import { createInboxReturnTracker } from "@t3tools/client-runtime/state/thread-inbox";
@@ -618,7 +618,7 @@ function SnoozeMenuButton(props: {
618618
</Tooltip>
619619
<MenuPopup side="bottom" align="end">
620620
{presets.map((preset) => {
621-
const favorite = snoozeFavorites.find((saved) => snoozeFavoriteId(saved) === preset.id);
621+
const favorite = preset.favorite;
622622
const item = (
623623
<MenuItem
624624
key={preset.id}
@@ -648,8 +648,8 @@ function SnoozeMenuButton(props: {
648648
<MenuItem
649649
data-favorite-remove=""
650650
closeOnClick={false}
651-
aria-label={`Remove ${preset.label} from favorites`}
652-
label={`Remove ${preset.label} from favorites`}
651+
aria-label={`Remove ${snoozeFavoriteLabel(favorite)} from favorites`}
652+
label={`Remove ${snoozeFavoriteLabel(favorite)} from favorites`}
653653
variant="ghost"
654654
className="h-7 min-h-7 justify-center"
655655
onClick={(event) => {

‎apps/web/src/hooks/useSettings.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ import {
2727
resolveEnvironmentIdentificationMode,
2828
} from "./useSettings";
2929
import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings";
30+
import {
31+
addSnoozeFavorite,
32+
removeSnoozeFavorite,
33+
} from "@t3tools/client-runtime/state/thread-settled";
3034

3135
beforeEach(() => {
3236
persistenceMocks.getClientSettings.mockReset().mockResolvedValue(null);
@@ -354,6 +358,40 @@ describe("persistClientSettingsUpdate", () => {
354358
expect(durableSettings.browserProfiles).toContainEqual(importedProfile);
355359
});
356360

361+
it("applies a favorite re-added while its removal is still being written", async () => {
362+
const favorite = { amount: 2, unit: "hours" } as const;
363+
let finishRemoval!: () => void;
364+
const removal = new Promise<void>((resolve) => {
365+
finishRemoval = resolve;
366+
});
367+
const persist = vi
368+
.fn<(settings: typeof DEFAULT_CLIENT_SETTINGS) => Promise<void>>()
369+
.mockImplementationOnce(() => removal)
370+
.mockResolvedValue(undefined);
371+
__setClientSettingsForTests({ ...DEFAULT_CLIENT_SETTINGS, snoozeFavorites: [favorite] });
372+
373+
const removed = persistClientSettingsUpdate(
374+
(current) => ({
375+
...current,
376+
snoozeFavorites: removeSnoozeFavorite(current.snoozeFavorites, favorite),
377+
}),
378+
persist,
379+
);
380+
// Queued while the removal is in flight: it must see the removal, not skip as a duplicate.
381+
const added = persistClientSettingsUpdate(
382+
(current) => ({
383+
...current,
384+
snoozeFavorites: addSnoozeFavorite(current.snoozeFavorites, favorite),
385+
}),
386+
persist,
387+
);
388+
finishRemoval();
389+
await Promise.all([removed, added]);
390+
391+
expect(persist).toHaveBeenCalledTimes(2);
392+
expect(getClientSettings().snoozeFavorites).toEqual([favorite]);
393+
});
394+
357395
it("continues the queue after a rejected write", async () => {
358396
const failure = new Error("disk full");
359397
const persist = vi

‎packages/client-runtime/src/state/customSnooze.test.ts‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// @effect-diagnostics globalDate:off -- Tests exercise local calendar and elapsed-time snooze input.
22
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
3+
import { MAX_SNOOZE_FAVORITES } from "@t3tools/contracts/settings";
34
import {
45
addSnoozeFavorite,
56
localSnoozeDate,
67
localSnoozeTime,
7-
MAX_SNOOZE_FAVORITES,
88
removeSnoozeFavorite,
99
resolveCustomSnooze,
10-
resolveSnoozeFavoritePresets,
10+
resolveSnoozePresetsWithFavorites,
1111
snoozeFavoriteLabel,
1212
} from "./threadSettled.ts";
1313

@@ -81,34 +81,43 @@ describe("snooze favorites", () => {
8181
expect(snoozeFavoriteLabel({ amount: 45, unit: "minutes" })).toBe("45 minutes");
8282
});
8383

84+
const favoriteRows = (favorites: Parameters<typeof resolveSnoozePresetsWithFavorites>[0]) =>
85+
resolveSnoozePresetsWithFavorites(favorites, now).filter((preset) => preset.favorite);
86+
8487
it("resolves favorites relative to the menu opening time", () => {
85-
const [preset] = resolveSnoozeFavoritePresets([{ amount: 2, unit: "hours" }], now);
88+
const [preset] = favoriteRows([{ amount: 2, unit: "hours" }]);
8689
expect(preset).toMatchObject({
8790
id: "favorite:2-hours",
8891
label: "In 2 hours",
8992
snoozedUntil: new Date(now.getTime() + 2 * 3_600_000).toISOString(),
93+
favorite: { amount: 2, unit: "hours" },
94+
});
95+
});
96+
97+
it("offers a favorite that wakes with a built-in preset once, on the built-in row", () => {
98+
const presets = resolveSnoozePresetsWithFavorites([{ amount: 60, unit: "minutes" }], now);
99+
expect(presets.map((preset) => preset.id)).not.toContain("favorite:60-minutes");
100+
expect(presets.find((preset) => preset.id === "hour")).toMatchObject({
101+
label: "In 1 hour",
102+
favorite: { amount: 60, unit: "minutes" },
90103
});
91104
});
92105

93106
it("skips favorites whose wake time overflows instead of throwing", () => {
94107
expect(
95-
resolveSnoozeFavoritePresets(
96-
[
97-
{ amount: 1e300, unit: "hours" },
98-
{ amount: 2, unit: "hours" },
99-
],
100-
now,
101-
).map((preset) => preset.id),
108+
favoriteRows([
109+
{ amount: 1e300, unit: "hours" },
110+
{ amount: 2, unit: "hours" },
111+
]).map((preset) => preset.id),
102112
).toEqual(["favorite:2-hours"]);
103-
const maxDate = new Date(8.64e15);
104-
const justBefore = new Date(maxDate.getTime() - 60_000);
105-
expect(resolveSnoozeFavoritePresets([{ amount: 2, unit: "minutes" }], justBefore)).toEqual([]);
106113
});
107114

108115
it("ignores duplicates, drops the oldest past the cap, and removes by value", () => {
109116
const two = { amount: 2, unit: "hours" } as const;
110117
const list = addSnoozeFavorite([], two);
111118
expect(addSnoozeFavorite(list, { ...two })).toBe(list);
119+
// The same length in other units is the same favorite.
120+
expect(addSnoozeFavorite(list, { amount: 120, unit: "minutes" })).toBe(list);
112121
let full = list;
113122
for (let minutes = 1; minutes <= MAX_SNOOZE_FAVORITES; minutes += 1) {
114123
full = addSnoozeFavorite(full, { amount: minutes, unit: "minutes" });
@@ -118,5 +127,6 @@ describe("snooze favorites", () => {
118127
expect(removeSnoozeFavorite(full, { amount: 1, unit: "minutes" })).toHaveLength(
119128
MAX_SNOOZE_FAVORITES - 1,
120129
);
130+
expect(removeSnoozeFavorite(list, { amount: 120, unit: "minutes" })).toEqual([]);
121131
});
122132
});

‎packages/client-runtime/src/state/threadSettled.ts‎

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// @effect-diagnostics globalDate:off -- UI snooze presets use local calendar boundaries and Intl labels.
2-
import type { SnoozeFavorite } from "@t3tools/contracts/settings";
2+
import {
3+
normalizeSnoozeFavorites,
4+
snoozeFavoriteDurationMs,
5+
type SnoozeFavorite,
6+
} from "@t3tools/contracts/settings";
37
import * as DateTime from "effect/DateTime";
48

59
interface SettlementRunLike {
@@ -231,6 +235,8 @@ export interface SnoozePreset {
231235
readonly whenLabel: string;
232236
/** ISO wake time. */
233237
readonly snoozedUntil: string;
238+
/** The saved favorite this row offers, which its remove control removes. */
239+
readonly favorite?: SnoozeFavorite;
234240
}
235241

236242
function snoozeTimeOfDayLabel(date: Date): string {
@@ -309,15 +315,11 @@ export function resolveSnoozePresets(now: Date): ReadonlyArray<SnoozePreset> {
309315
return presets;
310316
}
311317

312-
export const MAX_SNOOZE_FAVORITES = 5;
313-
314-
const SNOOZE_UNIT_MS = { minutes: 60_000, hours: HOUR_MS, days: 24 * HOUR_MS } as const;
315-
316318
function sameSnoozeFavorite(a: SnoozeFavorite, b: SnoozeFavorite): boolean {
317-
return a.amount === b.amount && a.unit === b.unit;
319+
return snoozeFavoriteDurationMs(a) === snoozeFavoriteDurationMs(b);
318320
}
319321

320-
export function snoozeFavoriteId(favorite: SnoozeFavorite): `favorite:${string}` {
322+
function snoozeFavoriteId(favorite: SnoozeFavorite): `favorite:${string}` {
321323
return `favorite:${favorite.amount}-${favorite.unit}`;
322324
}
323325

@@ -326,13 +328,16 @@ export function snoozeFavoriteLabel(favorite: SnoozeFavorite): string {
326328
return `${favorite.amount} ${favorite.amount === 1 ? favorite.unit.slice(0, -1) : favorite.unit}`;
327329
}
328330

329-
/** Appends a favorite. A duplicate is a no-op; past the cap the oldest is dropped. */
331+
/**
332+
* Appends a favorite. One of the same length is a no-op; past the cap the
333+
* oldest is dropped (see `normalizeSnoozeFavorites`).
334+
*/
330335
export function addSnoozeFavorite(
331336
favorites: ReadonlyArray<SnoozeFavorite>,
332337
favorite: SnoozeFavorite,
333338
): ReadonlyArray<SnoozeFavorite> {
334339
if (favorites.some((existing) => sameSnoozeFavorite(existing, favorite))) return favorites;
335-
return [...favorites, favorite].slice(-MAX_SNOOZE_FAVORITES);
340+
return normalizeSnoozeFavorites([...favorites, favorite]);
336341
}
337342

338343
export function removeSnoozeFavorite(
@@ -343,31 +348,41 @@ export function removeSnoozeFavorite(
343348
}
344349

345350
/**
346-
* Saved custom durations as snooze presets, resolved against `now` so "In 2
347-
* hours" is relative to the menu opening, like the built-in presets.
348-
* `whenLabel` is the clock time; clients localize it like the other presets.
351+
* The built-in presets in their usual order, then the saved favorites soonest
352+
* first, all resolved against `now` so "In 2 hours" is relative to the menu
353+
* opening. A favorite that wakes when a built-in does ("60 minutes" and "In 1
354+
* hour") is not offered twice: it rides on that built-in's row as `favorite`,
355+
* which is what the row's remove control removes.
349356
*/
350-
export function resolveSnoozeFavoritePresets(
357+
export function resolveSnoozePresetsWithFavorites(
351358
favorites: ReadonlyArray<SnoozeFavorite>,
352359
now: Date,
353360
): ReadonlyArray<SnoozePreset> {
354-
return favorites.flatMap((favorite) => {
361+
const presets: SnoozePreset[] = [...resolveSnoozePresets(now)];
362+
const favoritePresets: SnoozePreset[] = [];
363+
for (const favorite of favorites) {
355364
// A saved duration that no longer resolves (overflowing date) is skipped
356365
// so it cannot break the menus the user would remove it from.
357366
const snoozedUntil = resolveCustomSnooze(
358367
{ mode: "duration", amount: String(favorite.amount), unit: favorite.unit },
359368
now,
360369
);
361-
if (snoozedUntil === null) return [];
362-
return [
363-
{
364-
id: snoozeFavoriteId(favorite),
365-
label: `In ${snoozeFavoriteLabel(favorite)}`,
366-
whenLabel: snoozeTimeOfDayLabel(new Date(snoozedUntil)),
367-
snoozedUntil,
368-
},
369-
];
370-
});
370+
if (snoozedUntil === null) continue;
371+
const builtIn = presets.findIndex((preset) => preset.snoozedUntil === snoozedUntil);
372+
if (builtIn !== -1) {
373+
presets[builtIn] = { ...presets[builtIn]!, favorite };
374+
continue;
375+
}
376+
favoritePresets.push({
377+
id: snoozeFavoriteId(favorite),
378+
label: `In ${snoozeFavoriteLabel(favorite)}`,
379+
whenLabel: snoozeTimeOfDayLabel(new Date(snoozedUntil)),
380+
snoozedUntil,
381+
favorite,
382+
});
383+
}
384+
favoritePresets.sort((a, b) => Date.parse(a.snoozedUntil) - Date.parse(b.snoozedUntil));
385+
return [...presets, ...favoritePresets];
371386
}
372387

373388
/**
@@ -400,7 +415,7 @@ export function resolveCustomSnooze(input: CustomSnoozeInput, now: Date): string
400415
if (input.mode === "duration") {
401416
const amount = Number(input.amount);
402417
if (!Number.isFinite(amount) || amount <= 0) return null;
403-
const unitMs = SNOOZE_UNIT_MS[input.unit];
418+
const unitMs = { minutes: 60_000, hours: HOUR_MS, days: 24 * HOUR_MS }[input.unit];
404419
wake = new Date(now.getTime() + amount * unitMs);
405420
} else {
406421
if (!/^\d{4}-\d{2}-\d{2}$/.test(input.date) || !/^\d{2}:\d{2}$/.test(input.time)) return null;

0 commit comments

Comments
 (0)