import { Table, TableBody, TableCaption, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table" import { Skeleton } from "@/components/ui/skeleton" interface TableData { [key: string]: string | number; } interface TableProps { data: TableData[] | undefined; caption?: string; } export function GenericTable({ data, caption }: TableProps) { const headers = data && data.length > 0 ? ['#', ...Object.keys(data[0])] : []; const isLoading = !data; const isEmpty = data && data.length === 0; const renderSkeleton = (count: number) => ( [...Array(count)].map((_, index) => ( )) ); const renderHeaders = () => headers.map((header, index) => ( {header.toUpperCase()} )); const renderRows = () => { if (isLoading) { return [...Array(10)].map((_, index) => {renderSkeleton(5)}); } if (isEmpty) { return ( No results found ); } return data.map((row, index) => ( {index + 1} {Object.entries(row).map(([key, value]) => ( {value} ))} )); }; return ( {caption && {caption}} {isLoading ? renderSkeleton(5) : isEmpty ? null : renderHeaders()} {renderRows()}
); }