/* eslint-disable @typescript-eslint/no-explicit-any */ "use client"; import { useState } from "react"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { useRouter } from "next/navigation"; import { ColumnDef } from "@tanstack/react-table"; import { Badge } from "@/shared/components/ds/badge"; import { Button } from "@/shared/components/ui/button"; import { MoreHorizontal, Edit, Trash, ChevronsUpDown, ChevronUp, ChevronDown, } from "lucide-react"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/shared/components/ui/dropdown-menu"; import { Credential } from "@/shared/types/credential"; import credentialApi from "@/shared/services/credential"; import { toast } from "sonner"; import { hasPermission } from "@/shared/config/role"; import { useAuthSession } from "@/shared/hooks/use-session"; import { DeleteDialog } from "../../_components/delete-dialog"; interface ColumnConfig { id: string; header: string; accessor?: keyof Credential; accessorFn?: (row: Credential) => any; sortable?: boolean; cell?: (value: any) => React.ReactNode; } const COLUMN_CONFIGS: ColumnConfig[] = [ { id: "name", header: "Nama Kredensial", accessor: "name", sortable: true, }, { id: "credential_type", header: "Tipe Kredensial", accessor: "credential_type", sortable: true, }, { id: "environment", header: "Environment", accessorFn: (row) => row.credential_metadata.environment, sortable: true, }, { id: "version", header: "Versi", accessorFn: (row) => row.credential_metadata.version, sortable: true, }, { id: "is_default", header: "Default", accessor: "is_default", sortable: true, cell: (value) => ( {value ? "Ya" : "Tidak"} ), }, { id: "is_active", header: "Status", accessor: "is_active", sortable: true, cell: (value) => ( {value ? "Aktif" : "Tidak Aktif"} ), }, ]; export const useCredentialColumns = (): ColumnDef[] => { const router = useRouter(); const [credentialToDelete, setCredentialToDelete] = useState(null); const queryClient = useQueryClient(); const { session } = useAuthSession(); const userRole = session?.user?.role; const deleteMutation = useMutation({ mutationFn: async (id: string) => { return await credentialApi.deleteCredential(id); }, onSuccess: () => { toast.success("Berhasil menghapus credential"); queryClient.invalidateQueries({ queryKey: ["credentials"] }); setCredentialToDelete(null); }, onError: (error) => { toast.error("Gagal menghapus credential"); console.error("Error deleting credential:", error); }, }); const renderSortableHeader = (column: any, label: string) => ( ); const baseColumns = COLUMN_CONFIGS.map((config) => { const column: ColumnDef = { id: config.id, header: ({ column }) => config.sortable ? renderSortableHeader(column, config.header) : config.header, ...(config.accessor && { accessorKey: config.accessor }), ...(config.accessorFn && { accessorFn: config.accessorFn }), cell: ({ row }) => { const value = row.getValue(config.id); return config.cell ? ( config.cell(value) ) : (
{value as React.ReactNode}
); }, enableSorting: config.sortable !== false, enableHiding: config.id !== "select" && config.id !== "actions", }; return column; }); if ( userRole && (hasPermission(userRole, "credential", "read") || hasPermission(userRole, "credential", "update") || hasPermission(userRole, "credential", "delete")) ) { baseColumns.push({ id: "actions", enableHiding: false, cell: ({ row }) => { const credential = row.original; return ( <> Aksi {hasPermission(userRole, "credential", "update") && ( router.push(`/admin/credential/edit/${credential.id}`) } className="flex items-center gap-2" > Edit Credential )} {hasPermission(userRole, "credential", "delete") && ( <> setCredentialToDelete(credential)} className="flex items-center gap-2 text-destructive focus:text-destructive" > Hapus Credential )} {credentialToDelete?.id === credential.id && ( deleteMutation.mutate(credentialToDelete.id)} onCancel={() => setCredentialToDelete(null)} open={true} /> )} ); }, }); } return baseColumns; };