import React, { useState } from "react"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui"; import { Pencil, Trash2 } from "lucide-react"; import type { ClusterRoleInfo } from "@/lib/tauriCommands"; import { deleteResourceCmd, getResourceYamlCmd } from "@/lib/tauriCommands"; import { ResourceActionMenu } from "./ResourceActionMenu"; import { ConfirmDeleteDialog } from "./ConfirmDeleteDialog"; import { EditResourceModal } from "./EditResourceModal"; interface ClusterRoleListProps { clusterRoles: ClusterRoleInfo[]; clusterId?: string; _clusterId?: string; onRefresh?: () => void; } type ActiveModal = | { type: "edit"; cr: ClusterRoleInfo; yaml: string } | { type: "delete"; cr: ClusterRoleInfo } | null; export function ClusterRoleList({ clusterRoles, clusterId, _clusterId, onRefresh, }: ClusterRoleListProps) { const cid = clusterId ?? _clusterId ?? ""; const [activeModal, setActiveModal] = useState(null); const [isDeleting, setIsDeleting] = useState(false); const [actionError, setActionError] = useState(null); const openEdit = async (cr: ClusterRoleInfo) => { setActionError(null); try { const yaml = await getResourceYamlCmd(cid, "clusterroles", "", cr.name); setActiveModal({ type: "edit", cr, yaml }); } catch (err) { setActionError(err instanceof Error ? err.message : String(err)); } }; const handleDelete = async () => { if (activeModal?.type !== "delete") return; setIsDeleting(true); try { await deleteResourceCmd(cid, "clusterroles", "", activeModal.cr.name); setActiveModal(null); onRefresh?.(); } finally { setIsDeleting(false); } }; return ( <> {actionError && (

{actionError}

)}
Name Age Actions {clusterRoles.length === 0 ? ( No cluster roles found ) : ( clusterRoles.map((cr) => ( {cr.name} {cr.age} openEdit(cr), }, { label: "Delete", icon: Trash2, variant: "destructive", onClick: () => setActiveModal({ type: "delete", cr }), }, ]} /> )) )}
{activeModal?.type === "edit" && ( { setActiveModal(null); onRefresh?.(); }} /> )} {activeModal?.type === "delete" && ( { if (!o) setActiveModal(null); }} resourceType="ClusterRole" resourceName={activeModal.cr.name} isLoading={isDeleting} onConfirm={handleDelete} /> )} ); }