import { useMemo, useState } from "react"; import { Cog, Play, Square, RotateCw, Power } from "lucide-react"; import { useTranslation } from "react-i18next"; import { toast } from "sonner"; import { managerPost } from "@/main-axios"; import { useManagerData, extractError } from "./useManagerData"; import { ManagerCardShell } from "./ManagerCardShell"; import { ManagerSearch } from "./ManagerToolbar"; interface SystemdService { unit: string; active: string; sub: string; description: string; } export function ServiceManagerCard({ hostId }: { hostId: number | null }) { const { t } = useTranslation(); const { data, loading, error, refresh } = useManagerData<{ services: SystemdService[]; }>(hostId, "services"); const [filter, setFilter] = useState(""); const [busy, setBusy] = useState(null); const services = useMemo( () => (data?.services ?? []).filter( (s) => !filter || s.unit.toLowerCase().includes(filter.toLowerCase()) || s.description.toLowerCase().includes(filter.toLowerCase()), ), [data?.services, filter], ); const act = async (unit: string, action: string) => { if (hostId == null) return; setBusy(`${unit}:${action}`); try { const res = await managerPost<{ success: boolean; output: string }>( hostId, "services", { unit, action }, "action", ); if (res.success) { toast.success(t("hostMetrics.managers.actionDone", { name: unit })); refresh(); } else { toast.error(res.output || t("hostMetrics.managers.actionFailed")); } } catch (e) { toast.error(extractError(e).message); } finally { setBusy(null); } }; return ( } loading={loading} error={error} onRefresh={refresh} empty={!loading && services.length === 0} >
{services.map((s) => (
{s.unit.replace(/\.service$/, "")} {s.sub}
act(s.unit, "start")} busy={busy === `${s.unit}:start`} title={t("hostMetrics.managers.start")} > act(s.unit, "restart")} busy={busy === `${s.unit}:restart`} title={t("hostMetrics.managers.restart")} > act(s.unit, "stop")} busy={busy === `${s.unit}:stop`} title={t("hostMetrics.managers.stop")} > act(s.unit, s.sub === "running" ? "disable" : "enable") } busy={ busy === `${s.unit}:enable` || busy === `${s.unit}:disable` } title={t("hostMetrics.managers.enableDisable")} >
))}
); } function ActionBtn({ onClick, busy, title, children, }: { onClick: () => void; busy: boolean; title: string; children: React.ReactNode; }) { return ( ); }