import { useState, useEffect } from "react"; import { useTranslation } from "react-i18next"; import { copyToClipboard } from "@/lib/clipboard"; import { Button } from "@/components/button"; import { Input } from "@/components/input"; import { Copy, Search, Terminal, Trash2 } from "lucide-react"; import { getCommandHistory, deleteCommandFromHistory, clearCommandHistory, } from "@/main-axios"; import type { Tab } from "@/types/ui-types"; export function HistoryPanel({ activeTabId, terminalTabs, }: { activeTabId: string; terminalTabs: Tab[]; }) { const { t } = useTranslation(); const [search, setSearch] = useState(""); const [commands, setCommands] = useState([]); const activeTab = terminalTabs.find((t) => t.id === activeTabId); const activeIsTerminal = !!activeTab; const hostId = activeTab?.host?.id ? parseInt(activeTab.host.id, 10) : null; const trackingEnabled = activeTab?.host?.enableCommandHistory !== false; useEffect(() => { if (!hostId || !trackingEnabled) { setCommands([]); return; } getCommandHistory(hostId) .then(setCommands) .catch(() => setCommands([])); }, [hostId, trackingEnabled]); if (activeIsTerminal && !trackingEnabled) { return (
{t("newUi.sidebar.history.trackingDisabled")} {t("newUi.sidebar.history.trackingDisabledHint")}
); } if (!activeIsTerminal) { return (
{t("newUi.sidebar.history.noTerminalSelected")} {t("newUi.sidebar.history.noTerminalSelectedHint")}
); } const filtered = search ? commands.filter((c) => c.toLowerCase().includes(search.toLowerCase())) : commands; async function handleDelete(cmd: string) { if (!hostId) return; try { await deleteCommandFromHistory(hostId, cmd); setCommands((prev) => prev.filter((c) => c !== cmd)); } catch { /* ignore */ } } return (
{activeTab.label}
setSearch(e.target.value)} className="pl-8" />
{filtered.length} command{filtered.length !== 1 ? "s" : ""}
{filtered.length === 0 && ( {t("newUi.sidebar.history.noHistoryEntries")} )} {filtered.map((cmd, i) => (
{cmd}
))}
); }