Buttons, fields, drop-downs, check boxes, tabs, dialogs and notices are Ant Design components; their tokens come from the theme variables (web/src/ui). Every factory board column has its own vertical scrollbar. ChatView rendered fewer hooks once its chat was deleted. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
81 lines
3.2 KiB
TypeScript
81 lines
3.2 KiB
TypeScript
import { Button, Input } from "antd";
|
|
import { useEffect, useState } from "react";
|
|
import { confirmDialog, toast } from "../../dialogs";
|
|
import { useT } from "../../i18n";
|
|
import { loadTemplates, placeholders, saveTemplates, type Template, type TemplateLists } from "../../templates";
|
|
import { Icon } from "../Icon";
|
|
|
|
function List({ scope, initial }: { scope: "personal" | "global"; initial: Template[] }) {
|
|
const t = useT();
|
|
const [list, setList] = useState<Template[]>(initial);
|
|
const [dirty, setDirty] = useState(false);
|
|
const set = (i: number, patch: Partial<Template>) => {
|
|
setList((l) => l.map((x, j) => (j === i ? { ...x, ...patch } : x)));
|
|
setDirty(true);
|
|
};
|
|
const save = () =>
|
|
saveTemplates(scope, list.filter((x) => x.name.trim() || x.text.trim()))
|
|
.then((l) => (setList(l), setDirty(false), toast(t("settings.saved"), "success")))
|
|
.catch((e) => toast(e.message));
|
|
const remove = async (i: number) => {
|
|
if (!(await confirmDialog({ message: t("templates.deleteConfirm", { name: list[i].name || "?" }), danger: true, confirmLabel: t("common.delete") }))) return;
|
|
setList((l) => l.filter((_, j) => j !== i));
|
|
setDirty(true);
|
|
};
|
|
|
|
return (
|
|
<div className="panel">
|
|
<h3>
|
|
<Icon name={scope === "global" ? "users" : "user"} /> {t(scope === "global" ? "templates.globalTitle" : "templates.personalTitle")}
|
|
</h3>
|
|
<div className="hint" style={{ marginBottom: 10 }}>
|
|
{t(scope === "global" ? "templates.globalHint" : "templates.personalHint")}
|
|
</div>
|
|
{list.map((x, i) => (
|
|
<div key={x.id || `new${i}`} className="tpl-edit">
|
|
<div className="row">
|
|
<Input className="grow" value={x.name} placeholder={t("templates.name")} onChange={(e) => set(i, { name: e.target.value })} />
|
|
<Button type="text" size="small" onClick={() => remove(i)} aria-label={t("common.delete")}>
|
|
<Icon name="trash" />
|
|
</Button>
|
|
</div>
|
|
<Input.TextArea rows={4} value={x.text} placeholder={t("templates.textPlaceholder")} onChange={(e) => set(i, { text: e.target.value })} />
|
|
{placeholders(x.text).length > 0 && (
|
|
<div className="tpl-ph">
|
|
{placeholders(x.text).map((p) => (
|
|
<span key={p.name} className="chip">
|
|
{p.name}
|
|
</span>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
))}
|
|
<div className="row" style={{ marginTop: 8 }}>
|
|
<Button onClick={() => (setList([...list, { id: "", name: "", text: "" }]), setDirty(true))}>
|
|
<Icon name="plus" /> {t("templates.add")}
|
|
</Button>
|
|
<span className="grow" />
|
|
<Button type="primary" disabled={!dirty} onClick={save}>
|
|
{t("common.save")}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** Prompt templates: personal ones and (admins) the ones every user sees. */
|
|
export function Templates() {
|
|
const [lists, setLists] = useState<TemplateLists>();
|
|
useEffect(() => {
|
|
loadTemplates().then(setLists).catch((e) => toast(e.message));
|
|
}, []);
|
|
if (!lists) return null;
|
|
return (
|
|
<>
|
|
<List scope="personal" initial={lists.personal} />
|
|
{lists.can_edit_global && <List scope="global" initial={lists.global} />}
|
|
</>
|
|
);
|
|
}
|