import { Plus, Play, Trash2, Zap } from "lucide-react"; import { useTranslation, type HostEditorSectionProps, } from "@termix/plugin-sdk/frontend"; import { Button, Input, SectionCard, Select2 } from "@termix/plugin-sdk/ui"; import { PLUGIN_ID, readStartupSnippetId } from "../shared/host-settings.js"; import { useSnippetOptions } from "./use-snippet-options"; type PluginSettingsBag = Record>; /** A quick action row while it is being edited: the snippet may be unset. */ interface QuickActionRow { name: string; snippetId: number | null; } function readRows(value: unknown): QuickActionRow[] { if (!Array.isArray(value)) return []; return value.map((entry) => { const row = (entry ?? {}) as { name?: unknown; snippetId?: unknown }; const id = Number(row.snippetId); return { name: typeof row.name === "string" ? row.name : "", snippetId: Number.isInteger(id) && id > 0 ? id : null, }; }); } /** * The host editor's Snippets tab: the snippet a terminal runs when it * connects, and the quick action buttons Host Metrics shows for the host. * Both are this plugin's host settings, saved with the host. */ export function HostSnippetsSection({ form, updateForm, adminTargetUserId, }: HostEditorSectionProps) { const { t } = useTranslation(); const { options } = useSnippetOptions(adminTargetUserId); const own = (form.pluginSettings as PluginSettingsBag | undefined)?.[PLUGIN_ID] ?? {}; const startupSnippetId = readStartupSnippetId(own.startupSnippetId); const quickActions = readRows(own.quickActions); const write = (values: Record) => updateForm((current) => { const all = (current.pluginSettings as PluginSettingsBag | undefined) ?? {}; return { ...current, pluginSettings: { ...all, [PLUGIN_ID]: { ...(all[PLUGIN_ID] ?? {}), ...values }, }, }; }); const setQuickActions = (rows: QuickActionRow[]) => write({ quickActions: rows }); return ( <> } >

{t("host.startupDescription")}

write({ startupSnippetId: e.target.value ? Number(e.target.value) : null, }) } className="flex h-9 w-full border border-border bg-background px-3 py-1 text-xs outline-none focus:ring-1 focus:ring-ring" > {options.map((snippet) => ( ))}
} action={ } >

{t("host.quickActionsDescription")}

{quickActions.length === 0 && (
{t("host.noQuickActions")}
)} {quickActions.map((row, index) => (
{ const next = [...quickActions]; next[index] = { ...row, name: e.target.value }; setQuickActions(next); }} /> { const next = [...quickActions]; next[index] = { ...row, snippetId: e.target.value ? Number(e.target.value) : null, }; setQuickActions(next); }} > {options.map((snippet) => ( ))}
))}
); }