import { useState } from "react"; import { Box, HStack, Image, Stack, Table, Text } from "@chakra-ui/react"; import { PaginationItems, PaginationNextTrigger, PaginationPrevTrigger, PaginationRoot, } from "./ui/pagination"; import EmptyFile from "../assets/empty-file.png"; // import { // PaginationItems, // PaginationNextTrigger, // PaginationPrevTrigger, // PaginationRoot, // } from "./ui/pagination"; interface TableProps { tableHeadRow: string[]; data: Record[]; sortableColumns?: string[]; // Specify which columns are sortable paginationData?: { current_page: number; last_page: number; per_page: number; total: number; }; onPageChange?: (page: number) => void; } const DataTable: React.FC = ({ tableHeadRow, data, sortableColumns = [], paginationData, onPageChange, }: TableProps) => { const { current_page = 1, last_page = 1 } = paginationData || {}; const [sortConfig, setSortConfig] = useState<{ key: string; direction: "asc" | "desc"; } | null>(null); const handlePageChange = (details: { page: number }) => { const newPage = details.page; if (newPage >= 1 && newPage <= last_page) { onPageChange?.(newPage); } }; const handleSort = (column: string) => { if (!sortableColumns.includes(column)) return; let direction: "asc" | "desc" = "asc"; if ( sortConfig && sortConfig.key === column && sortConfig.direction === "asc" ) { direction = "desc"; } const newSortConfig = { key: column, direction }; setSortConfig(newSortConfig); onPageChange?.(1); }; return ( {tableHeadRow.map((item, index) => ( handleSort(item)} cursor={ sortableColumns.includes(item) ? "pointer" : "default" } _hover={ sortableColumns.includes(item) ? { textDecoration: "underline" } : {} } > {item} {sortableColumns.includes(item) && sortConfig?.key === item && ( {sortConfig.direction === "asc" ? "▲" : "▼"} )} ))} {data?.length === 0 ? ( No data We do not have any records ) : ( {data.map((item: any, index) => ( {tableHeadRow.map((heading, colIndex) => ( {(() => { const words = item[heading]?.toString().split(" ") || []; return words.length > 5 ? `${words.slice(0, 5).join(" ")}...` : item[heading]; })()} ))} ))} )} {last_page > 1 && ( handlePageChange({ page: current_page - 1 })} disabled={current_page === 1} /> handlePageChange({ page: current_page + 1 })} disabled={current_page === last_page} /> )} ); }; export default DataTable;