Repository Public
1// Réglages : profil, identités, réponse automatique, filtres, apparence, confidentialité, sécurité, export, stockage.2import { Suspense, useId, useState, type FormEvent, type ReactNode } from "react"3import { t } from "../../i18n"4import { isValidEmail } from "../../mail/address"5import { S, type Account, type Density, type Lang, type PreviewLines, type ReadingFont, type TextSize, type Theme } from "../../state/app"6import type { Layout } from "../../state/device"7import { signOut, updateDevice, updatePrefs } from "../../state/session"8import { useStore } from "../../state/store"9import { Field, Icon, type IconName } from "../kit"10import { Page, Section, type PageTab } from "../Page"11import { ExportSection, StorageSection } from "./Export"12import { FiltersSection } from "./Filters"13import { IdentitiesSection } from "./Identities"14import { useNextTabs } from "./NextTabs"15import { SecuritySection } from "./Security"16import { ComposingSection } from "./Composing"17import { SignaturesSection } from "./Signatures"18import { SnippetsSection } from "./Snippets"19import { AccountGate, MailboxPicker } from "./shared"20import { VacationSection } from "./Vacation"21import "./settings.css"2223const TAB_IDS = ["profile", "identities", "signatures", "snippets", "composing", "vacation", "filters", "appearance", "privacy", "security", "export", "storage"] as const24type TabId = (typeof TAB_IDS)[number]25const TAB_ICONS: Record<TabId, IconName> = { profile: "user", identities: "edit", signatures: "draft", snippets: "bolt", composing: "send", vacation: "clock", filters: "filter", appearance: "eye", privacy: "shield", security: "key", export: "download", storage: "server" }2627// libellés relus à chaque rendu : ils suivent la langue28function tabs(): PageTab[] {29 const labels: Record<TabId, string> = {30 profile: t("settings.tab.profile"),31 identities: t("settings.tab.identities"),32 signatures: t("cmp.set.tab.signatures"),33 snippets: t("cmp.set.tab.snippets"),34 composing: t("cmp.set.tab.composing"),35 vacation: t("settings.tab.vacation"),36 filters: t("settings.tab.filters"),37 appearance: t("settings.tab.appearance"),38 privacy: t("settings.tab.privacy"),39 security: t("settings.tab.security"),40 export: t("settings.tab.export"),41 storage: t("settings.tab.storage"),42 }43 return TAB_IDS.map((id) => ({ id, label: labels[id], icon: TAB_ICONS[id] }))44}4546export default function Settings() {47 const route = useStore(S, (s) => (s.route.name === "settings" ? s.route.tab : "profile"))48 useStore(S, (s) => s.lang) // redessine au changement de langue49 // onglets de la version « Next » (NextTabs.tsx) : présents seulement si le serveur les sert50 const nextTabs = useNextTabs()51 const next = nextTabs.find((x) => x.id === route)52 const tab: TabId = (TAB_IDS as readonly string[]).includes(route) ? (route as TabId) : "profile"53 return (54 <Page title={t("settings.title")} base="/settings" tabs={[...tabs(), ...nextTabs.map((x) => ({ id: x.id, label: x.label(), icon: x.icon }))]} current={next ? next.id : tab}>55 <div className="stack set-root" data-settings-tab={next ? next.id : tab}>56 {next ? (57 <Suspense fallback={null}>58 <next.component />59 </Suspense>60 ) : (61 <TabContent tab={tab} />62 )}63 </div>64 </Page>65 )66}6768function TabContent({ tab }: { tab: TabId }) {69 switch (tab) {70 case "profile":71 return <Profile />72 case "identities":73 return <PerMailbox>{(mb, account) => <IdentitiesSection key={mb} mb={mb} account={account} />}</PerMailbox>74 case "signatures":75 return <SignaturesSection />76 case "snippets":77 return <SnippetsSection />78 case "composing":79 return <ComposingSection />80 case "vacation":81 return <PerMailbox>{(mb, account) => <VacationSection key={mb} mb={mb} account={account} />}</PerMailbox>82 case "filters":83 return <PerMailbox>{(mb, account) => <FiltersSection key={mb} mb={mb} account={account} />}</PerMailbox>84 case "appearance":85 return <Appearance />86 case "privacy":87 return <Privacy />88 case "security":89 return <SecuritySection />90 case "export":91 return <PerMailbox>{(mb, account) => <ExportSection key={mb} mb={mb} account={account} />}</PerMailbox>92 case "storage":93 return <PerMailbox>{(mb, account) => <StorageSection key={mb} mb={mb} account={account} />}</PerMailbox>94 }95}9697/** Boîte choisie dans les réglages : conservée d'un onglet à l'autre pendant la visite. */98let chosenMailbox: string | null = null99100function PerMailbox({ children }: { children: (mb: string, account: Account) => ReactNode }) {101 const mailboxes = useStore(S, (s) => s.me?.mailboxes)102 const [choice, setChoice] = useState<string | null>(() => chosenMailbox ?? S.get().mb)103 if (!mailboxes || mailboxes.length === 0) return <p className="muted">{t("settings.noMailbox")}</p>104 const mb = mailboxes.some((m) => m.id === choice) ? (choice as string) : mailboxes[0].id105 return (106 <>107 <MailboxPicker108 mb={mb}109 onChange={(next) => {110 chosenMailbox = next111 setChoice(next)112 }}113 />114 <AccountGate mb={mb}>{(account) => children(mb, account)}</AccountGate>115 </>116 )117}118119// ---------------------------------------------------------------- profil120function Profile() {121 const me = useStore(S, (s) => s.me)122 const [busy, setBusy] = useState(false)123 if (!me) return null124 const leave = async () => {125 setBusy(true)126 try {127 await signOut()128 } finally {129 setBusy(false)130 }131 }132 return (133 <>134 <Section title={t("settings.profile.title")}>135 <dl className="set-kv">136 <dt>{t("settings.profile.name")}</dt>137 <dd className="set-wrap">{me.user.display_name || "—"}</dd>138 <dt>{t("settings.profile.handle")}</dt>139 <dd className="mono set-wrap">{me.user.handle}</dd>140 <dt>{t("settings.profile.role")}</dt>141 <dd>142 <span className={`badge ${me.user.role === "admin" ? "accent" : ""}`}>{me.user.role === "admin" ? t("settings.profile.roleAdmin") : t("settings.profile.roleMember")}</span>143 </dd>144 <dt>{t("settings.profile.version")}</dt>145 <dd className="mono">{me.version}</dd>146 </dl>147 <p className="muted small set-note">{t("settings.profile.nameNote")}</p>148 </Section>149 <Section title={t("settings.profile.mailboxes")}>150 {me.mailboxes.length === 0 ? (151 <p className="muted">{t("settings.noMailbox")}</p>152 ) : (153 <ul className="set-list">154 {me.mailboxes.map((m) => (155 <li key={m.id} className="set-item">156 <div className="set-wrap">157 <strong>{m.address}</strong>158 </div>159 {m.display_name ? <div className="muted small set-wrap">{m.display_name}</div> : null}160 </li>161 ))}162 </ul>163 )}164 </Section>165 <Section title={t("settings.profile.session")} description={t("settings.profile.sessionDesc")}>166 <button type="button" className="btn" disabled={busy} onClick={() => void leave()}>167 <Icon name="signout" small />168 <span>{t("settings.signOut")}</span>169 </button>170 </Section>171 </>172 )173}174175// ---------------------------------------------------------------- apparence176function Segmented<T extends string>({ label, value, options, onChange }: { label: string; value: T; options: { value: T; label: string }[]; onChange: (value: T) => void }) {177 const id = useId()178 return (179 <div className="field">180 <span className="label" id={id}>181 {label}182 </span>183 <div>184 <div className="segmented set-segmented" role="group" aria-labelledby={id}>185 {options.map((o) => (186 <button key={o.value} type="button" aria-pressed={o.value === value} onClick={() => onChange(o.value)}>187 {o.label}188 </button>189 ))}190 </div>191 </div>192 </div>193 )194}195196function Toggle({ label, hint, checked, onChange }: { label: string; hint?: string; checked: boolean; onChange: (checked: boolean) => void }) {197 const id = useId()198 return (199 <div className="set-toggle">200 <label className="check" htmlFor={id}>201 <input id={id} type="checkbox" checked={checked} onChange={(e) => onChange(e.target.checked)} aria-describedby={hint ? `${id}-hint` : undefined} />202 <span>{label}</span>203 </label>204 {hint ? (205 <p id={`${id}-hint`} className="muted small set-indent">206 {hint}207 </p>208 ) : null}209 </div>210 )211}212213function Appearance() {214 const prefs = useStore(S, (s) => s.prefs)215 const device = useStore(S, (s) => s.device)216 const systemReduces = typeof matchMedia === "function" && matchMedia("(prefers-reduced-transparency: reduce)").matches217 return (218 <>219 <Section title={t("settings.appearance.title")} description={t("settings.appearance.desc")}>220 <div className="stack set-form">221 <Segmented<Density>222 label={t("settings.appearance.density")}223 value={prefs.density}224 onChange={(density) => updatePrefs({ density })}225 options={[226 { value: "comfortable", label: t("settings.appearance.densityComfortable") },227 { value: "cozy", label: t("settings.appearance.densityCozy") },228 { value: "compact", label: t("settings.appearance.densityCompact") },229 ]}230 />231 <Segmented<`${PreviewLines}`>232 label={t("settings.appearance.preview")}233 value={`${prefs.previewLines}`}234 onChange={(v) => updatePrefs({ previewLines: Number(v) as PreviewLines })}235 options={[236 { value: "0", label: t("settings.appearance.preview0") },237 { value: "1", label: t("settings.appearance.preview1") },238 { value: "2", label: t("settings.appearance.preview2") },239 ]}240 />241 <Segmented<TextSize>242 label={t("settings.appearance.textSize")}243 value={prefs.textSize}244 onChange={(textSize) => updatePrefs({ textSize })}245 options={[246 { value: "small", label: t("settings.appearance.textSmall") },247 { value: "default", label: t("settings.appearance.textDefault") },248 { value: "large", label: t("settings.appearance.textLarge") },249 { value: "xlarge", label: t("settings.appearance.textXLarge") },250 ]}251 />252 <Segmented<ReadingFont>253 label={t("settings.appearance.readingFont")}254 value={prefs.readingFont}255 onChange={(readingFont) => updatePrefs({ readingFont })}256 options={[257 { value: "system", label: t("settings.appearance.fontSystem") },258 { value: "serif", label: t("settings.appearance.fontSerif") },259 { value: "mono", label: t("settings.appearance.fontMono") },260 ]}261 />262 <Toggle label={t("settings.appearance.avatars")} hint={t("settings.appearance.avatarsHint")} checked={prefs.avatars} onChange={(avatars) => updatePrefs({ avatars })} />263 <Toggle label={t("settings.appearance.darkMessages")} hint={t("settings.appearance.darkMessagesHint")} checked={prefs.darkMessages} onChange={(darkMessages) => updatePrefs({ darkMessages })} />264 <Segmented<Lang | "auto">265 label={t("settings.appearance.language")}266 value={prefs.lang ?? "auto"}267 onChange={(lang) => updatePrefs({ lang: lang === "auto" ? null : lang })}268 options={[269 { value: "auto", label: t("settings.appearance.langAuto") },270 { value: "fr", label: "Français" },271 { value: "en", label: "English" },272 ]}273 />274 </div>275 </Section>276 <Section title={t("settings.appearance.deviceTitle")} description={t("settings.appearance.deviceDesc")}>277 <div className="stack set-form">278 <Segmented<Theme>279 label={t("settings.appearance.theme")}280 value={prefs.theme}281 onChange={(theme) => updatePrefs({ theme })}282 options={[283 { value: "system", label: t("settings.appearance.themeSystem") },284 { value: "light", label: t("settings.appearance.themeLight") },285 { value: "dark", label: t("settings.appearance.themeDark") },286 { value: "midnight", label: t("settings.appearance.themeMidnight") },287 ]}288 />289 <Segmented<Layout>290 label={t("settings.appearance.layout")}291 value={device.layout}292 onChange={(layout) => updateDevice({ layout })}293 options={[294 { value: "right", label: t("settings.appearance.layoutRight") },295 { value: "below", label: t("settings.appearance.layoutBelow") },296 { value: "none", label: t("settings.appearance.layoutNone") },297 ]}298 />299 <Toggle300 label={t("settings.appearance.reduceTransparency")}301 hint={t("settings.appearance.reduceTransparencyHint")}302 checked={device.reduceTransparency ?? systemReduces}303 onChange={(reduceTransparency) => updateDevice({ reduceTransparency })}304 />305 </div>306 </Section>307 </>308 )309}310311// ---------------------------------------------------------------- confidentialité312function Privacy() {313 const prefs = useStore(S, (s) => s.prefs)314 const [email, setEmail] = useState("")315 const [error, setError] = useState<string | null>(null)316 const id = useId()317318 const add = (e: FormEvent) => {319 e.preventDefault()320 const address = email.trim().toLowerCase()321 if (!isValidEmail(address)) return setError(t("settings.invalidEmail"))322 setError(null)323 setEmail("")324 if (!prefs.trustedSenders.some((x) => x.toLowerCase() === address)) updatePrefs({ trustedSenders: [...prefs.trustedSenders, address] })325 }326327 return (328 <>329 <Section title={t("settings.privacy.images")}>330 <div className="stack set-form">331 <fieldset className="set-fieldset">332 <legend>{t("settings.privacy.policy")}</legend>333 <div className="stack">334 <label className="check">335 <input type="radio" name={`${id}-img`} value="block" checked={prefs.remoteImages === "block"} onChange={() => updatePrefs({ remoteImages: "block" })} />336 <span>{t("settings.privacy.block")}</span>337 </label>338 <label className="check">339 <input type="radio" name={`${id}-img`} value="allow" checked={prefs.remoteImages === "allow"} onChange={() => updatePrefs({ remoteImages: "allow" })} />340 <span>{t("settings.privacy.allow")}</span>341 </label>342 </div>343 </fieldset>344 <p className="muted small">{t("settings.privacy.explain")}</p>345 </div>346 </Section>347 <Section title={t("settings.privacy.trusted")} description={t("settings.privacy.trustedDesc")}>348 <div className="stack set-form">349 {prefs.trustedSenders.length === 0 ? (350 <p className="muted">{t("settings.privacy.trustedEmpty")}</p>351 ) : (352 <ul className="set-list" aria-label={t("settings.privacy.trusted")}>353 {prefs.trustedSenders.map((address) => (354 <li key={address} className="set-item set-item-row">355 <span className="set-item-main set-wrap">{address}</span>356 <button type="button" className="btn small" aria-label={t("settings.privacy.removeLabel", { email: address })} onClick={() => updatePrefs({ trustedSenders: prefs.trustedSenders.filter((x) => x !== address) })}>357 {t("common.remove")}358 </button>359 </li>360 ))}361 </ul>362 )}363 <form className="stack" onSubmit={add} noValidate>364 <Field id={`${id}-add`} label={t("settings.privacy.addLabel")}>365 <div className="set-inline">366 <input367 id={`${id}-add`}368 className="input"369 type="email"370 value={email}371 onChange={(e) => setEmail(e.target.value)}372 maxLength={254}373 autoComplete="off"374 spellCheck={false}375 aria-invalid={error ? true : undefined}376 aria-describedby={error ? `${id}-err` : undefined}377 />378 <button type="submit" className="btn" disabled={email.trim() === ""}>379 {t("common.add")}380 </button>381 </div>382 </Field>383 {error ? (384 <p id={`${id}-err`} className="set-error" role="alert">385 {error}386 </p>387 ) : null}388 </form>389 </div>390 </Section>391 </>392 )393}394