import { useMemo, useState } from "react"; import { Package, ArrowUpCircle } from "lucide-react"; import { useTranslation } from "react-i18next"; import { toast } from "sonner"; import { Button } from "@/components/button"; import { managerPost } from "@/main-axios"; import { useManagerData, extractError } from "./useManagerData"; import { ManagerCardShell } from "./ManagerCardShell"; import { ManagerSearch } from "./ManagerToolbar"; interface UpgradablePackage { name: string; currentVersion?: string; newVersion?: string; } export function PackageManagerCard({ hostId }: { hostId: number | null }) { const { t } = useTranslation(); const { data, loading, error, refresh } = useManagerData<{ pkg: string | null; upgradable: UpgradablePackage[]; }>(hostId, "packages"); const [busy, setBusy] = useState(null); const [filter, setFilter] = useState(""); const all = useMemo(() => data?.upgradable ?? [], [data?.upgradable]); const pkgs = useMemo( () => all.filter( (p) => !filter || p.name.toLowerCase().includes(filter.toLowerCase()), ), [all, filter], ); const run = async (action: string, pkg?: string) => { if (hostId == null) return; setBusy(pkg ?? action); toast.loading(t("hostMetrics.managers.working"), { id: "pkg-op" }); try { const res = await managerPost<{ success: boolean; output: string }>( hostId, "packages", { action, pkg }, "action", ); toast[res.success ? "success" : "error"]( res.success ? t("hostMetrics.managers.actionDone", { name: pkg ?? "upgrade" }) : t("hostMetrics.managers.actionFailed"), { id: "pkg-op", description: res.output?.slice(-200) }, ); if (res.success) refresh(); } catch (e) { toast.error(extractError(e).message, { id: "pkg-op" }); } finally { setBusy(null); } }; return ( } loading={loading} error={error} onRefresh={refresh} empty={!loading && all.length === 0} emptyMessage={t("hostMetrics.managers.allUpToDate")} headerExtra={ all.length > 0 ? ( ) : undefined } > {all.length > 5 && ( )}
{pkgs.map((p) => (
{p.name} {p.currentVersion ? `${p.currentVersion} → ` : ""} {p.newVersion ?? ""}
))}
); }