Repository Public
1// Réglages › Extraits : textes réutilisables, insérés par le sélecteur de la rédaction ou en tapant « ;raccourci ».2import { useEffect, useId, useState, type FormEvent } from "react"3import { t } from "../../i18n"4import { HttpError, errorDetail } from "../../jmap/errors"5import { htmlToText } from "../../mail/compose"6import { toast } from "../../state/app"7import { sanitizeComposerHtml } from "../../state/composer"8import { useStore } from "../../state/store"9import { D, deleteSnippet, loadComposeData, saveSnippet, type Snippet } from "../compose/data"10import { RichEditor } from "../compose/RichEditor"11import "../compose/compose.css"12import { Field, Icon, Spinner, confirmAsync } from "../kit"13import { Section } from "../Page"1415const SHORTCUT = /^[a-z0-9_-]{1,24}$/1617function SnippetForm({ snippet, onDone }: { snippet: Snippet | null; onDone: () => void }) {18 const [name, setName] = useState(snippet?.name ?? "")19 const [shortcut, setShortcut] = useState(snippet?.shortcut ?? "")20 const [subject, setSubject] = useState(snippet?.subject ?? "")21 const [html, setHtml] = useState(snippet?.html ?? "")22 const [busy, setBusy] = useState(false)23 const [error, setError] = useState<string | null>(null)24 const id = useId()25 const key = shortcut.trim().toLowerCase().replace(/^;/, "")26 const save = async (e: FormEvent) => {27 e.preventDefault()28 if (busy) return29 if (!name.trim()) return setError(t("cmp.set.snip.needName"))30 if (key && !SHORTCUT.test(key)) return setError(t("cmp.set.snip.badShortcut"))31 const clean = sanitizeComposerHtml(html)32 if (!htmlToText(clean).trim()) return setError(t("cmp.set.snip.needBody"))33 setBusy(true)34 setError(null)35 try {36 await saveSnippet(snippet?.id ?? null, { name: name.trim(), shortcut: key || null, subject: subject.trim() || null, html: clean, text: htmlToText(clean) })37 toast({ kind: "success", text: t("common.saved") })38 onDone()39 } catch (err) {40 setError(err instanceof HttpError && err.code === "shortcut_taken" ? t("cmp.set.snip.shortcutTaken") : errorDetail(err) || t("cmp.set.saveFailed"))41 setBusy(false)42 }43 }44 return (45 <form className="stack set-form cmp-set-form" onSubmit={(e) => void save(e)} aria-label={snippet ? t("cmp.set.snip.editTitle", { name: snippet.name }) : t("cmp.set.snip.newTitle")}>46 <div className="set-grid2">47 <Field id={`${id}-name`} label={t("cmp.set.snip.name")}>48 <input id={`${id}-name`} className="input" value={name} maxLength={80} onChange={(e) => setName(e.target.value)} autoFocus />49 </Field>50 <Field id={`${id}-key`} label={t("cmp.set.snip.shortcut")} hint={t("cmp.set.snip.shortcutHint")}>51 <div className="cmp-set-shortcut">52 <span aria-hidden="true">;</span>53 <input id={`${id}-key`} className="input" value={shortcut} maxLength={25} autoCapitalize="none" autoComplete="off" spellCheck={false} onChange={(e) => setShortcut(e.target.value)} />54 </div>55 </Field>56 </div>57 <Field id={`${id}-subject`} label={t("cmp.set.snip.subject")} hint={t("cmp.set.snip.subjectHint")}>58 <input id={`${id}-subject`} className="input" value={subject} maxLength={300} onChange={(e) => setSubject(e.target.value)} />59 </Field>60 <div className="field">61 <span className="label">{t("cmp.set.snip.body")}</span>62 <div className="cmp-set-editor">63 <RichEditor docKey={snippet?.id ?? "new"} rev={0} html={html} onChange={setHtml} label={t("cmp.set.snip.body")} compact />64 </div>65 </div>66 {error ? (67 <p className="set-error" role="alert">68 {error}69 </p>70 ) : null}71 <div className="row wrap">72 <button type="submit" className="btn primary" disabled={busy}>73 {t("common.save")}74 </button>75 <button type="button" className="btn" disabled={busy} onClick={onDone}>76 {t("common.cancel")}77 </button>78 </div>79 </form>80 )81}8283export function SnippetsSection() {84 const status = useStore(D, (d) => d.status)85 const snippets = useStore(D, (d) => d.snippets)86 const [editing, setEditing] = useState<string | "new" | null>(null)87 useEffect(() => {88 void loadComposeData(true)89 }, [])90 const remove = async (s: Snippet) => {91 if (!(await confirmAsync({ title: t("cmp.set.snip.deleteTitle"), body: t("cmp.set.snip.deleteBody", { name: s.name }), confirmLabel: t("common.delete"), danger: true }))) return92 try {93 await deleteSnippet(s.id)94 toast({ kind: "info", text: t("cmp.set.snip.deleted", { name: s.name }) })95 } catch (err) {96 toast({ kind: "error", text: t("cmp.set.saveFailed"), detail: errorDetail(err) || undefined })97 }98 }99 return (100 <Section101 title={t("cmp.set.snip.title")}102 description={t("cmp.set.snip.desc")}103 actions={104 editing === null ? (105 <button type="button" className="btn primary" onClick={() => setEditing("new")}>106 <Icon name="plus" />107 <span>{t("cmp.set.snip.add")}</span>108 </button>109 ) : null110 }111 >112 {editing === "new" ? <SnippetForm snippet={null} onDone={() => setEditing(null)} /> : null}113 {status === "loading" && !snippets.length ? <Spinner label={t("common.loading")} /> : null}114 {status === "error" ? (115 <div className="banner danger" role="alert">116 <Icon name="warn" small />117 <span>{t("cmp.set.loadFailed")}</span>118 </div>119 ) : null}120 {snippets.length ? (121 <ul className="set-list" data-testid="snippet-list">122 {snippets.map((s) => (123 <li key={s.id} className="set-item" data-snippet={s.name}>124 {editing === s.id ? (125 <SnippetForm snippet={s} onDone={() => setEditing(null)} />126 ) : (127 <div className="set-item-row">128 <div className="set-item-main">129 <strong className="set-wrap">{s.name}</strong> {s.shortcut ? <kbd className="cmp-kbd">;{s.shortcut}</kbd> : null}130 {s.subject ? <div className="small muted set-wrap">{t("cmp.set.snip.subjectIs", { subject: s.subject })}</div> : null}131 <pre className="set-signature muted">{s.text}</pre>132 </div>133 <div className="row wrap">134 <button type="button" className="btn small" aria-label={t("cmp.set.editLabel", { name: s.name })} onClick={() => setEditing(s.id)}>135 <Icon name="edit" small />136 <span>{t("common.edit")}</span>137 </button>138 <button type="button" className="btn small danger" aria-label={t("cmp.set.deleteLabel", { name: s.name })} onClick={() => void remove(s)}>139 <Icon name="trash" small />140 <span>{t("common.delete")}</span>141 </button>142 </div>143 </div>144 )}145 </li>146 ))}147 </ul>148 ) : status === "ready" && editing !== "new" ? (149 <p className="muted">{t("cmp.set.snip.empty")}</p>150 ) : null}151 </Section>152 )153}154