SPB Git

Repository Public

Composing.tsx 5.4 KB 135 lines tsx

1// Réglages › Rédaction : fenêtre d'annulation d'envoi, destinataires sensibles, avertissements avant l'envoi.2import { useEffect, useId, useState } from "react"3import { t } from "../../i18n"4import { errorDetail } from "../../jmap/errors"5import { UNDO_CHOICES } from "../../mail/compose"6import { toast } from "../../state/app"7import { useStore } from "../../state/store"8import { D, loadComposeData, saveComposePrefs, type ComposePrefs } from "../compose/data"9import "../compose/compose.css"10import { Icon, Spinner } from "../kit"11import { Section } from "../Page"1213const SENSITIVE = /^(@[a-z0-9.-]+\.[a-z]{2,}|[^\s@]+@[a-z0-9.-]+\.[a-z]{2,})$/1415export function ComposingSection() {16  const prefs = useStore(D, (d) => d.prefs)17  const loaded = useStore(D, (d) => d.prefsLoaded)18  const status = useStore(D, (d) => d.status)19  const [entry, setEntry] = useState("")20  const [bad, setBad] = useState(false)21  const [busy, setBusy] = useState(false)22  const id = useId()23  useEffect(() => {24    void loadComposeData(true)25  }, [])26  const save = async (patch: Partial<ComposePrefs>) => {27    setBusy(true)28    try {29      await saveComposePrefs(patch)30      toast({ kind: "success", text: t("common.saved") })31    } catch (err) {32      toast({ kind: "error", text: t("cmp.set.saveFailed"), detail: errorDetail(err) || undefined })33    } finally {34      setBusy(false)35    }36  }37  const addSensitive = () => {38    const values = entry.split(/[\s,;]+/).map((x) => x.trim().toLowerCase()).filter(Boolean)39    if (!values.length) return40    if (values.some((v) => !SENSITIVE.test(v))) return setBad(true)41    setEntry("")42    void save({ sensitive: [...new Set([...prefs.sensitive, ...values])] })43  }44  if (!loaded && status !== "error") return <Spinner label={t("common.loading")} />45  return (46    <>47      {status === "error" && !loaded ? (48        <div className="banner danger" role="alert">49          <Icon name="warn" small />50          <span>{t("cmp.set.loadFailed")}</span>51        </div>52      ) : null}53      <Section title={t("cmp.set.undo.title")} description={t("cmp.set.undo.desc")}>54        <div className="cmp-segmented" role="radiogroup" aria-label={t("cmp.set.undo.title")}>55          {UNDO_CHOICES.map((n) => (56            <button key={n} type="button" role="radio" aria-checked={prefs.undo_seconds === n} className="cmp-chip-btn" data-undo={n} disabled={busy} onClick={() => void save({ undo_seconds: n })}>57              {n === 0 ? t("cmp.set.undo.off") : t("cmp.set.undo.seconds", { n })}58            </button>59          ))}60        </div>61      </Section>62      <Section title={t("cmp.set.warn.title")} description={t("cmp.set.warn.desc")}>63        <div className="stack">64          <label className="row cmp-check">65            <input type="checkbox" checked={prefs.warn_missing_attachment} disabled={busy} onChange={(e) => void save({ warn_missing_attachment: e.target.checked })} />66            <span>67              {t("cmp.set.warn.attachment")}68              <span className="muted small set-block">{t("cmp.set.warn.attachmentHint")}</span>69            </span>70          </label>71          <label className="row cmp-check">72            <input type="checkbox" checked={prefs.warn_external} disabled={busy} onChange={(e) => void save({ warn_external: e.target.checked })} />73            <span>74              {t("cmp.set.warn.external")}75              <span className="muted small set-block">{t("cmp.set.warn.externalHint")}</span>76            </span>77          </label>78        </div>79      </Section>80      <Section title={t("cmp.set.sensitive.title")} description={t("cmp.set.sensitive.desc")}>81        <form82          className="set-form stack"83          onSubmit={(e) => {84            e.preventDefault()85            addSensitive()86          }}87        >88          <div className="field">89            <label htmlFor={`${id}-s`}>{t("cmp.set.sensitive.add")}</label>90            <div className="set-inline">91              <input92                id={`${id}-s`}93                className="input"94                value={entry}95                placeholder="nom@exemple.org, @exemple.org"96                autoCapitalize="none"97                autoComplete="off"98                spellCheck={false}99                aria-invalid={bad}100                aria-describedby={bad ? `${id}-e` : undefined}101                onChange={(e) => {102                  setEntry(e.target.value)103                  setBad(false)104                }}105              />106              <button type="submit" className="btn" disabled={busy || !entry.trim()}>107                {t("common.add")}108              </button>109            </div>110            {bad ? (111              <span id={`${id}-e`} className="set-error" role="alert">112                {t("cmp.set.sensitive.invalid")}113              </span>114            ) : null}115          </div>116        </form>117        {prefs.sensitive.length ? (118          <ul className="cmp-set-chips" aria-label={t("cmp.set.sensitive.title")}>119            {prefs.sensitive.map((s) => (120              <li key={s} className="cmp-set-chip">121                <span className="set-wrap">{s}</span>122                <button type="button" aria-label={t("cmp.set.sensitive.remove", { value: s })} disabled={busy} onClick={() => void save({ sensitive: prefs.sensitive.filter((x) => x !== s) })}>123                  <Icon name="close" small />124                </button>125              </li>126            ))}127          </ul>128        ) : (129          <p className="muted small set-note">{t("cmp.set.sensitive.empty")}</p>130        )}131      </Section>132    </>133  )134}135
Files main
Menu

Theme