Repository Public
1// Bouton d'envoi scindé (envoyer · plus tard · avec rappel) et ses dialogues de date : choix rapides, date libre, fuseau affiché.2import { useEffect, useRef, useState } from "react"3import { createPortal } from "react-dom"4import { fmtDateTime, t, type MsgKey } from "../../i18n"5import { remindPresets, sendLaterPresets, type TimePreset } from "../../mail/compose"6import { SEND_AT_MAX_DAYS, parseLocalDateTime, toLocalInput } from "../../templates/send"7import { Dialog, Field, Icon } from "../kit"8import { CmpIcon } from "./icons"910const MAC = typeof navigator !== "undefined" && /Mac|iPhone|iPad/.test(navigator.platform)1112/** Fuseau de l'appareil, tel qu'il s'applique aux heures affichées : « America/Toronto (UTC−5) ». */13export function timeZoneLabel(at = new Date()): string {14 const zone = Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC"15 const off = -at.getTimezoneOffset()16 const h = Math.floor(Math.abs(off) / 60)17 const m = Math.abs(off) % 6018 return `${zone} (UTC${off < 0 ? "−" : "+"}${h}${m ? `:${String(m).padStart(2, "0")}` : ""})`19}2021/** `blocked` : raison pour laquelle l'envoi est impossible pour l'instant (hors ligne, expéditeur en cours de lecture) — le bouton la porte. */22export function SendButton({ sending, blocked, onSend, onLater, onRemind }: { sending: boolean; blocked?: string | null; onSend: () => void; onLater: () => void; onRemind: () => void }) {23 const [open, setOpen] = useState(false)24 const wrap = useRef<HTMLDivElement>(null)25 const menu = useRef<HTMLDivElement>(null)26 useEffect(() => {27 if (!open) return28 menu.current?.querySelector<HTMLElement>("button")?.focus()29 const away = (e: MouseEvent) => {30 if (!wrap.current?.contains(e.target as Node)) setOpen(false)31 }32 document.addEventListener("mousedown", away)33 return () => document.removeEventListener("mousedown", away)34 }, [open])35 const choose = (fn: () => void) => () => {36 setOpen(false)37 fn()38 }39 return (40 <div41 ref={wrap}42 className="cmp-send"43 onKeyDown={(e) => {44 if (!open) return45 if (e.key === "Escape") {46 e.stopPropagation()47 setOpen(false)48 wrap.current?.querySelector<HTMLElement>(".cmp-send-more")?.focus()49 return50 }51 if (e.key !== "ArrowDown" && e.key !== "ArrowUp" && e.key !== "Home" && e.key !== "End") return52 e.preventDefault()53 const items = Array.from(menu.current?.querySelectorAll<HTMLElement>("button") ?? [])54 const i = items.indexOf(document.activeElement as HTMLElement)55 items[e.key === "Home" ? 0 : e.key === "End" ? items.length - 1 : (i + (e.key === "ArrowDown" ? 1 : -1) + items.length) % items.length]?.focus()56 }}57 >58 <button type="button" className="btn primary cmp-send-main" onClick={onSend} disabled={sending || !!blocked} data-blocked={blocked ? "" : undefined} title={blocked ?? `${t("compose.send")} (${MAC ? "⌘" : "Ctrl+"}↵)`}>59 {sending ? <span className="spinner" aria-hidden="true" /> : <Icon name="send" />}60 <span>{sending ? t("compose.sending") : t("compose.send")}</span>61 </button>62 <button type="button" className="btn primary cmp-send-more" aria-haspopup="menu" aria-expanded={open} aria-label={t("cmp.send.more")} title={blocked ?? t("cmp.send.more")} disabled={sending || !!blocked} onClick={() => setOpen((o) => !o)}>63 <CmpIcon name="up" />64 </button>65 {open ? (66 <div ref={menu} className="menu cmp-send-menu" role="menu" aria-label={t("cmp.send.more")}>67 <button type="button" role="menuitem" onClick={choose(onLater)}>68 <Icon name="clock" small />69 <span>{t("cmp.send.later")}</span>70 </button>71 <button type="button" role="menuitem" onClick={choose(onRemind)}>72 <CmpIcon name="bell" small />73 <span>{t("cmp.send.remind")}</span>74 </button>75 </div>76 ) : null}77 </div>78 )79}8081function WhenDialog({ title, presets, presetKey, confirmLabel, hint, maxDays, extra, onConfirm, onClose }: { title: string; presets: TimePreset[]; presetKey: (id: TimePreset["id"]) => MsgKey; confirmLabel: string; hint: string; maxDays: number; extra?: React.ReactNode; onConfirm: (at: Date) => void; onClose: () => void }) {82 const [custom, setCustom] = useState("")83 const [error, setError] = useState<string | null>(null)84 const now = new Date()85 const min = toLocalInput(new Date(now.getTime() + 120_000))86 const max = toLocalInput(new Date(now.getTime() + maxDays * 86400_000))87 const submit = () => {88 const at = parseLocalDateTime(custom)89 if (!at) return setError(t("cmp.when.invalid"))90 if (at.getTime() < Date.now() + 60_000) return setError(t("cmp.when.past"))91 if (at.getTime() > Date.now() + maxDays * 86400_000) return setError(t("cmp.when.tooFar", { days: maxDays }))92 onConfirm(at)93 onClose()94 }95 return createPortal(96 <Dialog97 title={title}98 onClose={onClose}99 footer={100 <>101 <button type="button" className="btn" onClick={onClose}>102 {t("common.cancel")}103 </button>104 <button type="button" className="btn primary" onClick={submit} disabled={!custom}>105 {confirmLabel}106 </button>107 </>108 }109 >110 <div className="stack cmp-when">111 <p className="muted small">{hint}</p>112 <ul className="cmp-when-presets">113 {presets.map((p) => (114 <li key={p.id}>115 <button116 type="button"117 className="cmp-when-preset"118 data-preset={p.id}119 onClick={() => {120 onConfirm(p.at)121 onClose()122 }}123 >124 <span>{t(presetKey(p.id))}</span>125 <span className="muted small">{fmtDateTime(p.at.toISOString())}</span>126 </button>127 </li>128 ))}129 </ul>130 <Field label={t("cmp.when.custom")} id="cmp-when-custom" hint={t("cmp.when.zone", { zone: timeZoneLabel(parseLocalDateTime(custom) ?? now) })}>131 <input132 id="cmp-when-custom"133 className="input"134 type="datetime-local"135 value={custom}136 min={min}137 max={max}138 aria-invalid={!!error}139 aria-describedby={error ? "cmp-when-error" : undefined}140 onChange={(e) => {141 setCustom(e.target.value)142 setError(null)143 }}144 onKeyDown={(e) => {145 if (e.key === "Enter") {146 e.preventDefault()147 submit()148 }149 }}150 />151 </Field>152 {error ? (153 <p id="cmp-when-error" className="small cmp-field-error" role="alert">154 {error}155 </p>156 ) : null}157 {extra}158 </div>159 </Dialog>,160 document.body,161 )162}163164export function SendLaterDialog({ onConfirm, onClose }: { onConfirm: (at: Date) => void; onClose: () => void }) {165 return (166 <WhenDialog title={t("cmp.later.title")} hint={t("cmp.later.hint")} presets={sendLaterPresets()} presetKey={(id) => `cmp.when.${id}` as MsgKey} confirmLabel={t("cmp.later.confirm")} maxDays={SEND_AT_MAX_DAYS} onConfirm={onConfirm} onClose={onClose} />167 )168}169export function RemindDialog({ onConfirm, onClose }: { onConfirm: (at: Date, onlyIfNoReply: boolean) => void; onClose: () => void }) {170 const [only, setOnly] = useState(true)171 return (172 <WhenDialog173 title={t("cmp.remind.title")}174 hint={t("cmp.remind.hint")}175 presets={remindPresets()}176 presetKey={(id) => `cmp.when.${id}` as MsgKey}177 confirmLabel={t("cmp.remind.confirm")}178 maxDays={365}179 onConfirm={(at) => onConfirm(at, only)}180 onClose={onClose}181 extra={182 <label className="row cmp-check">183 <input type="checkbox" checked={only} onChange={(e) => setOnly(e.target.checked)} />184 <span>{t("cmp.remind.onlyIfNoReply")}</span>185 </label>186 }187 />188 )189}190