import { useState } from "react"; import Stack from "@moduletrace/twui/layout/Stack"; import { Wrench } from "lucide-react"; import IconTile from "@/src/components/general/icon-tile"; import { CustomToolKinds } from "@/src/dict/custom-tool-dict"; import postAdminApi from "@/src/layouts/bots/(functions)/post-admin-api"; import type { CustomToolsOverviewPayload, ExtensionTargets, PublicCustomTool } from "@/src/types"; import ExtensionAccessModal from "../../(partials)/extension-access-modal"; import SettingsListRow from "../../(partials)/settings-list-row"; import SettingsRowAction from "../../(partials)/settings-row-action"; import extensionAccessLabel from "../../(utils)/extension-access-label"; import CustomToolModal from "./custom-tool-modal"; import CustomToolTryModal from "./custom-tool-try-modal"; type Props = { tool: PublicCustomTool; targets: ExtensionTargets; onChanged: (overview: CustomToolsOverviewPayload, msg: string) => void; onError: (msg: string) => void; }; /** * One custom tool: its name, kind and who gets it. Edit, Access and Try * open in a modal. */ export default function CustomToolRow({ tool, targets, onChanged, onError }: Props) { const [open, setOpen] = useState<"edit" | "access" | "try">(); const [busy, setBusy] = useState(false); const toggle = (panel: "edit" | "access" | "try") => setOpen(open == panel ? undefined : panel); const saved = (overview: CustomToolsOverviewPayload, msg: string) => { setOpen(undefined); onChanged(overview, msg); }; const remove = async () => { if (busy || !window.confirm(`Delete ${tool.name}? Bots lose it at once.`)) return; setBusy(true); const res = await postAdminApi({ path: "tools/delete-tool", body: { custom_tool_id: tool.id }, }); setBusy(false); if (res.success && res.singleRes) onChanged(res.singleRes, res.msg || "Deleted."); else onError(res.msg || "Couldn't delete the tool."); }; return ( } title={tool.name} badge={tool.enabled ? undefined : "Off"} subtitle={[ CustomToolKinds.find((k) => k.value == tool.kind)?.title, extensionAccessLabel({ access: tool.access }), tool.description, ] .filter(Boolean) .join(" ยท ")} > {targets.can_edit ? ( toggle("edit")} /> ) : null} toggle("access")} /> {targets.can_edit ? ( toggle("try")} /> ) : null} {targets.can_edit ? ( ) : null} {open == "edit" ? ( setOpen(undefined)} /> ) : null} {open == "access" ? ( id={tool.id} name={tool.name} access={tool.access} targets={targets} path="tools/set-tool-access" onSaved={saved} onCancel={() => setOpen(undefined)} /> ) : null} {open == "try" ? setOpen(undefined)} /> : null} ); }