"use client"; import { useEffect, useMemo, useRef, useState } from "react"; import { Search, Briefcase } from "lucide-react"; import { ColumnDef, DataTableLayout, Card, Input, } from "ikoncomponents"; import { getAllRoles } from "@/app/utils/api/companyData/roleApi.ts"; interface RoleData { id: string; role: string; } function RoleDataTable() { const [roleTableData, setRoleTableData] = useState([]); const [isLoading, setIsLoading] = useState(true); const [search, setSearch] = useState(""); // DataTableLayout defaults to list view and has no prop to start in grid, so // once mounted we click its built-in "Grid View" toggle once to default to cards. const tableContainerRef = useRef(null); const defaultedToGrid = useRef(false); const filteredRoleTableData = useMemo(() => { if (!search.trim()) return roleTableData; return roleTableData.filter((roleData) => roleData.role?.toLowerCase().includes(search.toLowerCase()), ); }, [roleTableData, search]); const fetchRoleTableData = async () => { setIsLoading(true); try { const roleData = await getAllRoles(); setRoleTableData(roleData || []); } catch (error) { console.error("Error fetching role data:", error); } finally { setIsLoading(false); } }; useEffect(() => { fetchRoleTableData(); }, []); // Switch DataTableLayout into grid (card) view as soon as it mounts, once. useEffect(() => { if (isLoading || defaultedToGrid.current) return; const gridButton = tableContainerRef.current?.querySelector( 'button[title="Grid View"]', ); if (gridButton) { gridButton.click(); defaultedToGrid.current = true; } }, [isLoading]); // ikoncomponents passes the row object directly to cell(), not { row }. const columns: ColumnDef[] = [ { accessorKey: "role", header: "Role", cell: (row) => {row.role || "n/a"}, }, ]; return (
{/* Header */}

Roles

{filteredRoleTableData.length} of {roleTableData.length} roles

row.id, totalPages: 1, currentPage: 1, isLoading, onReload: fetchRoleTableData, actionNode: (
setSearch(e.target.value)} />
), gridComponent: (data: RoleData[]) => (
{data.length > 0 ? ( data.map((roleItem) => ( {/* Left accent rail */}

Role

{roleItem.role || "-"}

)) ) : (
No roles found.
)}
), }} />
); } export default RoleDataTable;