import React, { useState } from "react"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui"; import { Scale, Pencil, Trash2, FileText } from "lucide-react"; import type { ReplicationControllerInfo } from "@/lib/tauriCommands"; import { scaleReplicationcontrollerCmd, deleteResourceCmd, getResourceYamlCmd, } from "@/lib/tauriCommands"; import { ResourceActionMenu } from "./ResourceActionMenu"; import { ConfirmDeleteDialog } from "./ConfirmDeleteDialog"; import { ScaleModal } from "./ScaleModal"; import { EditResourceModal } from "./EditResourceModal"; import { WorkloadLogsModal } from "./WorkloadLogsModal"; interface ReplicationControllerListProps { items: ReplicationControllerInfo[]; clusterId: string; namespace: string; onRefresh?: () => void; } type ActiveModal = | { type: "scale"; rc: ReplicationControllerInfo } | { type: "logs"; rc: ReplicationControllerInfo } | { type: "edit"; rc: ReplicationControllerInfo; yaml: string } | { type: "delete"; rc: ReplicationControllerInfo } | null; export function ReplicationControllerList({ items, clusterId, namespace: _namespace, onRefresh, }: ReplicationControllerListProps) { const [activeModal, setActiveModal] = useState(null); const [isActing, setIsActing] = useState(false); const [actionError, setActionError] = useState(null); const openEdit = async (rc: ReplicationControllerInfo) => { setActionError(null); try { const yaml = await getResourceYamlCmd(clusterId, "replicationcontrollers", rc.namespace, rc.name); setActiveModal({ type: "edit", rc, yaml }); } catch (err) { setActionError(err instanceof Error ? err.message : String(err)); } }; const handleDelete = async () => { if (activeModal?.type !== "delete") return; setIsActing(true); try { await deleteResourceCmd(clusterId, "replicationcontrollers", activeModal.rc.namespace, activeModal.rc.name); setActiveModal(null); onRefresh?.(); } finally { setIsActing(false); } }; // Convert "X/Y" string to number (for current replicas) const getDesiredReplicas = (rc: ReplicationControllerInfo): number => { return rc.desired; }; return ( <> {actionError && (

{actionError}

)}
Name Namespace Desired Current Ready Age Actions {items.length === 0 ? ( No replication controllers found ) : ( items.map((rc) => ( {rc.name} {rc.namespace} {rc.desired} {rc.current} {rc.ready} {rc.age} setActiveModal({ type: "scale", rc }), }, { label: "Logs", icon: FileText, onClick: () => setActiveModal({ type: "logs", rc }), }, { label: "Edit", icon: Pencil, onClick: () => openEdit(rc), }, { label: "Delete", icon: Trash2, variant: "destructive", onClick: () => setActiveModal({ type: "delete", rc }), }, ]} /> )) )}
{activeModal?.type === "logs" && ( { if (!o) setActiveModal(null); }} clusterId={clusterId} namespace={activeModal.rc.namespace} workloadType="replicationcontroller" workloadName={activeModal.rc.name} labels={{}} /> )} {activeModal?.type === "scale" && ( { if (!o) setActiveModal(null); }} resourceType="ReplicationController" resourceName={activeModal.rc.name} currentReplicas={getDesiredReplicas(activeModal.rc)} onScale={(replicas) => scaleReplicationcontrollerCmd(clusterId, activeModal.rc.namespace, activeModal.rc.name, replicas).then(() => { setActiveModal(null); onRefresh?.(); }) } /> )} {activeModal?.type === "edit" && ( { setActiveModal(null); onRefresh?.(); }} /> )} {activeModal?.type === "delete" && ( { if (!o) setActiveModal(null); }} resourceType="ReplicationController" resourceName={activeModal.rc.name} isLoading={isActing} onConfirm={handleDelete} /> )} ); }