Repository Public
1// SP Mail Next — rédaction : assistant d'écriture (améliorer, corriger, raccourcir, développer, ton, traduire, objet). Le résultat est2// montré en texte brut et n'entre dans le brouillon que sur « Remplacer » ; rien n'est envoyé.3import { useId, useRef, useState } from "react"4import { createPortal } from "react-dom"5import { t } from "../../i18n"6import type { MsgKey } from "../../i18n"7import { aiFailure, aiSettings, aiWrite, WRITE_ACTIONS, WRITE_LANGS, WRITE_MAX, WRITE_TONES, type AiFailure, type AiWriteResult, type WriteAction, type WriteLang, type WriteTone } from "../../next/ai"8import { isAbort } from "../../next/api"9import { S } from "../../state/app"10import type { ComposerToolProps } from "../../state/registry"11import { useStore } from "../../state/store"12import { Dialog, Icon } from "../kit"13import { AiByLine, AiProblem } from "./AiParts"14import "./ai.css"1516const ACTION: Record<WriteAction, MsgKey> = {17 improve: "nx.ai.write.improve", grammar: "nx.ai.write.grammar", shorten: "nx.ai.write.shorten", expand: "nx.ai.write.expand", tone: "nx.ai.write.tone", translate: "nx.ai.write.translate", subject: "nx.ai.write.subject",18}19const TONE: Record<WriteTone, MsgKey> = { concise: "nx.ai.tone.concise", professional: "nx.ai.tone.professional", detailed: "nx.ai.tone.detailed", friendly: "nx.ai.tone.friendly", formal: "nx.ai.tone.formal" }20const LANG: Record<WriteLang, MsgKey> = { fr: "nx.ai.lang.fr", en: "nx.ai.lang.en", es: "nx.ai.lang.es", de: "nx.ai.lang.de", it: "nx.ai.lang.it", pt: "nx.ai.lang.pt" }2122type State = { status: "pick" } | { status: "loading"; action: WriteAction } | { status: "ready"; action: WriteAction; data: AiWriteResult } | { status: "failed"; action: WriteAction; failure: AiFailure }2324export function AiWriteMenu({ getText, replaceText, setSubject }: ComposerToolProps) {25 const [open, setOpen] = useState(false)26 const [state, setState] = useState<State>({ status: "pick" })27 const [tone, setTone] = useState<WriteTone>("professional")28 const [target, setTarget] = useState<WriteLang>("en")29 const [note, setNote] = useState<string | null>(null)30 const online = useStore(S, (s) => s.online)31 const gen = useRef(0)32 const ctl = useRef<AbortController | null>(null)33 const toneId = useId()34 const langId = useId()35 // la portée (sélection, sinon texte rédigé) est relevée à l'ouverture, avant que le dialogue ne prenne le focus36 const source = useRef("")3738 const close = () => {39 gen.current++40 ctl.current?.abort()41 setOpen(false)42 setState({ status: "pick" })43 setNote(null)44 }45 const run = async (action: WriteAction) => {46 const text = source.current.trim()47 if (!text) return setNote(t("nx.ai.write.emptyText"))48 if (text.length > WRITE_MAX) return setNote(t("nx.ai.write.tooLong"))49 setNote(null)50 const my = ++gen.current51 ctl.current?.abort()52 ctl.current = new AbortController()53 setState({ status: "loading", action })54 try {55 const data = await aiWrite({ action, text, tone, targetLanguage: target }, ctl.current.signal)56 if (my !== gen.current) return57 setState({ status: "ready", action, data })58 } catch (e) {59 if (my !== gen.current || isAbort(e)) return60 const failure = aiFailure(e)61 setState({ status: "failed", action, failure })62 if (failure.resync) void aiSettings.refresh()63 }64 }6566 return (67 <span className="nx-ai-write" data-nx-ai="write">68 <button type="button" className="btn small ghost nx-ai-ask" aria-haspopup="dialog"69 onClick={() => {70 source.current = getText()71 setOpen(true)72 }} title={t("nx.ai.write.title")}>73 <Icon name="sparkle" small />74 <span>{t("nx.ai.write.button")}</span>75 </button>76 {open77 ? createPortal(78 <Dialog79 title={t("nx.ai.write.title")}80 onClose={close}81 footer={82 state.status === "ready" && state.action !== "subject" ? (83 <>84 <button type="button" className="btn" onClick={() => setState({ status: "pick" })}>85 {t("common.cancel")}86 </button>87 <button88 type="button"89 className="btn primary"90 onClick={() => {91 replaceText(state.data.text)92 close()93 }}94 >95 {t("nx.ai.write.replace")}96 </button>97 </>98 ) : state.status !== "pick" ? (99 <button type="button" className="btn" onClick={() => setState({ status: "pick" })}>100 {t("nx.ai.write.back")}101 </button>102 ) : undefined103 }104 >105 <div className="stack nx-ai-write-body" data-nx-ai="write-dialog">106 {state.status === "pick" ? (107 <>108 <p className="nx-note">{t("nx.ai.write.intro")}</p>109 {!online ? <p className="nx-note">{t("nx.offline")}</p> : null}110 {note ? (111 <p className="nx-ai-problem" role="status">112 <Icon name="warn" small />113 <span>{note}</span>114 </p>115 ) : null}116 <div className="nx-ai-write-options">117 <label className="nx-ai-lang" htmlFor={toneId}>118 <span>{t("nx.ai.tone.label")}</span>119 <select id={toneId} className="select" value={tone} onChange={(e) => setTone(e.target.value as WriteTone)}>120 {WRITE_TONES.map((x) => (121 <option key={x} value={x}>122 {t(TONE[x])}123 </option>124 ))}125 </select>126 </label>127 <label className="nx-ai-lang" htmlFor={langId}>128 <span>{t("nx.ai.write.target")}</span>129 <select id={langId} className="select" value={target} onChange={(e) => setTarget(e.target.value as WriteLang)}>130 {WRITE_LANGS.map((x) => (131 <option key={x} value={x}>132 {t(LANG[x])}133 </option>134 ))}135 </select>136 </label>137 </div>138 <ul className="nx-ai-write-actions">139 {WRITE_ACTIONS.map((a) => (140 <li key={a}>141 <button type="button" className="btn block" disabled={!online} onClick={() => void run(a)}>142 {t(ACTION[a])}143 </button>144 </li>145 ))}146 </ul>147 </>148 ) : null}149 {state.status === "loading" ? (150 <div className="nx-ai-wait" role="status">151 <span className="sr-only">{t("nx.ai.write.loading")}</span>152 <span className="skeleton nx-ai-line" />153 <span className="skeleton nx-ai-line" />154 <span className="skeleton nx-ai-line short" />155 </div>156 ) : null}157 {state.status === "failed" ? <AiProblem failure={state.failure} onRetry={() => void run(state.action)} /> : null}158 {state.status === "ready" ? (159 <div className="nx-ai nx-ai-panel nx-ai-preview">160 <div className="nx-ai-head">161 <Icon name="sparkle" small />162 <h3>{t(ACTION[state.action])}</h3>163 <AiByLine by={state.data} />164 </div>165 {state.action === "subject" ? (166 state.data.subjects.length ? (167 <ul className="nx-ai-write-actions">168 {state.data.subjects.map((s, i) => (169 <li key={i}>170 <button171 type="button"172 className="btn block nx-ai-subject"173 onClick={() => {174 setSubject(s)175 close()176 }}177 >178 {s}179 </button>180 </li>181 ))}182 </ul>183 ) : (184 <p className="nx-note">{t("nx.ai.write.noSubject")}</p>185 )186 ) : (187 <p className="nx-ai-text">{state.data.text}</p>188 )}189 </div>190 ) : null}191 </div>192 </Dialog>,193 document.body,194 )195 : null}196 </span>197 )198}199export default AiWriteMenu200