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" ;
37import * as DateTime from "effect/DateTime" ;
48
59interface 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
236242function 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-
316318function 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+ */
330335export 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
338343export 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