SPB Git

Repository Public

SnoozePicker.tsx 6.7 KB 121 lines tsx

1// SP Mail Next — sélecteur d'échéance : mise en veille (et replanification), rappel de suivi (et relance). Échéances2// proposées calculées dans le fuseau de l'appareil, date et heure libres ; feuille du bas au téléphone.3import { useId, useMemo, useState } from "react"4import { t } from "../../i18n"5import type { MsgKey } from "../../i18n"6import { NOTE_MAX, deviceZone, isFuture, parseLocal, reminderPresets, snoozePresets, toLocalFields, type Preset } from "../../next/snooze"7import { SnoozeUi, closeWhen, fmtWhen, rearmReminder, remindTargets, reschedule, snoozeTargets, targetCount, type WhenDialog } from "../../next/snoozeUi"8import { S } from "../../state/app"9import { useStore } from "../../state/store"10import { Icon, type IconName } from "../kit"11import { Sheet } from "./RowMenuSheet"12import "./snooze.css"1314const PRESET_ICON: Record<string, IconName> = { laterToday: "clock", tonight: "moon", tomorrow: "sun", weekend: "calendar", nextWeekend: "calendar", nextWeek: "calendar", in3days: "calendar", in1week: "calendar", in2weeks: "calendar" }1516function WhenSheet({ dialog }: { dialog: WhenDialog }) {17  const online = useStore(S, (s) => s.online)18  const zone = deviceZone()19  const now = useMemo(() => new Date(), [])20  const remind = dialog.kind === "remind" || dialog.kind === "rearm"21  const presets: Preset[] = useMemo(() => (remind ? reminderPresets(now, zone) : snoozePresets(now, zone)), [remind, now, zone])22  const initial = useMemo(() => {23    const cur = dialog.kind === "reschedule" ? new Date(dialog.snooze.until) : dialog.kind === "rearm" ? new Date(dialog.reminder.due_at) : null24    return toLocalFields(isFuture(cur, now) ? cur : (presets.find((p) => p.id === "tomorrow") ?? presets[0]).at, zone)25  }, [dialog, presets, now, zone])26  const [date, setDate] = useState(initial.date)27  const [time, setTime] = useState(initial.time)28  const [past, setPast] = useState(false)29  const [onlyIfNoReply, setOnlyIfNoReply] = useState(false)30  const [note, setNote] = useState("")31  const ids = { date: useId(), time: useId(), note: useId(), only: useId(), hint: useId(), err: useId() }3233  // le dialogue se ferme tout de suite : la mise en veille est optimiste, le résultat s'annonce par une notification34  const submit = (at: Date) => {35    if (!online) return36    closeWhen()37    if (dialog.kind === "snooze") void snoozeTargets(dialog.targets, at)38    else if (dialog.kind === "reschedule") void reschedule(dialog.snooze, at)39    else if (dialog.kind === "remind") void remindTargets(dialog.targets, { dueAt: at, onlyIfNoReply, note })40    else void rearmReminder(dialog.reminder, at)41  }42  const custom = () => {43    const at = parseLocal(date, time, zone)44    if (!isFuture(at)) return setPast(true)45    submit(at)46  }47  const title = dialog.kind === "snooze" ? t("nx.snooze.pickerTitle") : dialog.kind === "reschedule" ? t("nx.snooze.rescheduleTitle") : dialog.kind === "remind" ? t("nx.remind.title") : t("nx.remind.rearmTitle")48  const sub = dialog.kind === "snooze" || dialog.kind === "remind" ? t("nx.snooze.count", { n: targetCount(dialog.targets) }) : (dialog.kind === "reschedule" ? dialog.snooze.subject : dialog.reminder.subject) || t("nx.snooze.noSubject")49  const submitLabel = dialog.kind === "snooze" ? t("nx.snooze.customSubmit") : dialog.kind === "reschedule" ? t("nx.snooze.customReschedule") : dialog.kind === "remind" ? t("nx.remind.customSubmit") : t("nx.remind.customRearm")50  return (51    <Sheet title={title} sub={sub} onClose={closeWhen} testid={remind ? "remind-picker" : "snooze-picker"}>52      <div className="stack nx-when">53        {!online ? (54          <p className="banner" role="status">55            <Icon name="offline" small />56            <span>{t("nx.snooze.offline")}</span>57          </p>58        ) : null}59        {dialog.kind === "remind" ? (60          <div className="stack nx-when-opts">61            <label className="check" htmlFor={ids.only}>62              <input id={ids.only} type="checkbox" checked={onlyIfNoReply} aria-describedby={ids.hint} onChange={(e) => setOnlyIfNoReply(e.target.checked)} />63              <span>{t("nx.remind.onlyIfNoReply")}</span>64            </label>65            <p id={ids.hint} className="nx-note nx-when-hint">66              {t("nx.remind.onlyIfNoReplyHint")}67            </p>68            <div className="field">69              <label htmlFor={ids.note}>{t("nx.remind.note")}</label>70              <input id={ids.note} className="input" value={note} maxLength={NOTE_MAX} placeholder={t("nx.remind.notePlaceholder")} autoComplete="off" onChange={(e) => setNote(e.target.value)} />71            </div>72          </div>73        ) : null}74        <ul className="nx-when-presets" aria-label={remind ? t("nx.remind.when") : t("nx.snooze.presets")}>75          {presets.map((p, i) => (76            <li key={p.id}>77              <button type="button" className="nx-when-preset" data-preset={p.id} data-autofocus={i === 0 ? "" : undefined} disabled={!online} onClick={() => submit(p.at)}>78                <Icon name={PRESET_ICON[p.id] ?? "clock"} />79                <span className="nx-when-preset-name">{t(`${remind ? "nx.remind.preset" : "nx.snooze.preset"}.${p.id}` as MsgKey)}</span>80                <span className="nx-meta">{fmtWhen(p.at, true)}</span>81              </button>82            </li>83          ))}84        </ul>85        <form86          className="nx-when-custom"87          aria-label={t("nx.snooze.custom")}88          onSubmit={(e) => {89            e.preventDefault()90            custom()91          }}92        >93          <div className="field">94            <label htmlFor={ids.date}>{t("nx.snooze.customDate")}</label>95            <input id={ids.date} className="input" type="date" required value={date} aria-invalid={past || undefined} aria-describedby={past ? ids.err : undefined} onChange={(e) => (setDate(e.target.value), setPast(false))} />96          </div>97          <div className="field">98            <label htmlFor={ids.time}>{t("nx.snooze.customTime")}</label>99            <input id={ids.time} className="input" type="time" required value={time} aria-invalid={past || undefined} aria-describedby={past ? ids.err : undefined} onChange={(e) => (setTime(e.target.value), setPast(false))} />100          </div>101          <button type="submit" className="btn primary" disabled={!online}>102            {submitLabel}103          </button>104          {past ? (105            <p id={ids.err} className="nx-when-error" role="alert">106              {t("nx.snooze.err.past")}107            </p>108          ) : null}109        </form>110        <p className="nx-note nx-when-zone">{t("nx.snooze.zone", { zone: zone.replace(/_/g, " ") })}</p>111      </div>112    </Sheet>113  )114}115116export default function SnoozePicker() {117  const dialog = useStore(SnoozeUi, (s) => s.dialog)118  if (!dialog) return null119  return <WhenSheet key={dialog.kind} dialog={dialog} />120}121
Files main
Menu

Theme