import { Button, HostDefaultBadge, Input, SectionCard, SettingRow, FakeSwitch, useIsDefaultsEditor, } from "@termix/plugin-sdk/ui"; import { useState } from "react"; import { useTranslation, type HostEditorSectionProps, } from "@termix/plugin-sdk/frontend"; import { HardDrive, LayoutDashboard, Plus, Server, Trash2 } from "lucide-react"; import { readHostMetricsSettings } from "../shared/stats-widgets.js"; const PLUGIN_ID = "host-metrics"; type PluginSettingsBag = Record>; /** * The Host Metrics section of the host editor. Its values are this plugin's * host settings, carried on form.pluginSettings and saved by the editor after * the host itself. */ export function HostStatsTab({ form, updateForm }: HostEditorSectionProps) { const { t } = useTranslation(); const [newMount, setNewMount] = useState(""); const [newMonitoredPath, setNewMonitoredPath] = useState(""); const [newMonitoredLabel, setNewMonitoredLabel] = useState(""); const bag = (form.pluginSettings as PluginSettingsBag | undefined)?.[ PLUGIN_ID ]; const settings = readHostMetricsSettings(bag); const { excludedMounts, monitoredMounts } = settings; const defaultsEditor = useIsDefaultsEditor(); const patch = (values: Record) => updateForm((current) => { const all = (current.pluginSettings as PluginSettingsBag | undefined) ?? {}; return { pluginSettings: { ...all, [PLUGIN_ID]: { ...(all[PLUGIN_ID] ?? {}), ...values }, }, }; }); const addExcludedMount = () => { const value = newMount.trim(); if (!value || excludedMounts.includes(value)) { setNewMount(""); return; } patch({ excludedMounts: [...excludedMounts, value] }); setNewMount(""); }; const addMonitoredMount = () => { const path = newMonitoredPath.trim(); if (!path || monitoredMounts.some((entry) => entry.path === path)) return; patch({ monitoredMounts: [ ...monitoredMounts, { path, label: newMonitoredLabel.trim() || undefined }, ], }); setNewMonitoredPath(""); setNewMonitoredLabel(""); }; return ( <> } >
patch({ metricsEnabled: v })} /> {settings.metricsEnabled && ( patch({ metricsInterval: v ? null : 30 })} /> )} {settings.metricsEnabled && settings.metricsInterval !== null && ( patch({ metricsInterval: Number(e.target.value) }) } className="w-20 h-7 text-xs text-right [appearance:textfield] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none" /> )}
} >

{t("hosts.monitoredMountsDesc")}

setNewMonitoredPath(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") { e.preventDefault(); addMonitoredMount(); } }} /> setNewMonitoredLabel(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") { e.preventDefault(); addMonitoredMount(); } }} />
{monitoredMounts.map((entry) => (
{entry.path} {entry.label && ( {entry.label} )}
))}
} >

{t("hosts.excludedMountsDesc")}

setNewMount(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") { e.preventDefault(); addExcludedMount(); } }} />
{excludedMounts.length === 0 && (
{t("hosts.excludedMountsEmpty")}
)} {excludedMounts.map((mount, i) => (
{mount}
))}
{!defaultsEditor && ( } >

{t("hosts.widgetsMovedToHostMetrics")}

)} ); }