import { useEffect, useState } from "react"; import { get, put } from "../../api"; import { useT } from "../../i18n"; import { toast } from "../../dialogs"; import { Icon } from "../Icon"; export interface Snippet { id?: string; name: string; text: string; } /** Personal prompt snippets: typed as `/name` in the composer. */ export function Snippets() { const t = useT(); const [items, setItems] = useState(); useEffect(() => { get("/account/snippets") .then(setItems) .catch((e) => toast(e.message)); }, []); if (!items) return null; const set = (i: number, patch: Partial) => setItems(items.map((s, j) => (j === i ? { ...s, ...patch } : s))); const save = () => { const clean = items.filter((s) => s.name.trim() || s.text.trim()).map((s) => ({ ...s, name: s.name.trim().replace(/^\/+/, "").replace(/\s+/g, "-") })); put("/account/snippets", clean) .then((r) => (setItems(r), toast(t("settings.saved"), "success"))) .catch((e) => toast(e.message)); }; return (

{t("snippets.title")}

{t("snippets.hint")}
{items.map((s, i) => (
/ set(i, { name: e.target.value })} aria-label={t("snippets.name")} />