import React, { useState } from "react"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui"; import { Pencil, Trash2 } from "lucide-react"; import type { RoleInfo } from "@/lib/tauriCommands"; import { deleteResourceCmd, getResourceYamlCmd } from "@/lib/tauriCommands"; import { ResourceActionMenu } from "./ResourceActionMenu"; import { ConfirmDeleteDialog } from "./ConfirmDeleteDialog"; import { EditResourceModal } from "./EditResourceModal"; interface RoleListProps { roles: RoleInfo[]; clusterId?: string; _clusterId?: string; namespace?: string; _namespace?: string; onRefresh?: () => void; } type ActiveModal = | { type: "edit"; role: RoleInfo; yaml: string } | { type: "delete"; role: RoleInfo } | null; export function RoleList({ roles, clusterId, _clusterId, namespace, _namespace, onRefresh, }: RoleListProps) { const cid = clusterId ?? _clusterId ?? ""; const ns = namespace ?? _namespace ?? ""; const [activeModal, setActiveModal] = useState(null); const [isDeleting, setIsDeleting] = useState(false); const [actionError, setActionError] = useState(null); const openEdit = async (role: RoleInfo) => { setActionError(null); try { const yaml = await getResourceYamlCmd(cid, "roles", ns, role.name); setActiveModal({ type: "edit", role, 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, "roles", ns, activeModal.role.name); setActiveModal(null); onRefresh?.(); } finally { setIsDeleting(false); } }; return ( <> {actionError && (

{actionError}

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