import { ChevronDownIcon, ChevronUpIcon } from "lucide-react"; import React, { useState } from "react"; export interface TableProps { headings: Heading[]; rows: JSX.Element[][]; } export interface Heading { title: string; type: "blank" | "normal"; sortable: boolean; } export const Table = ({ headings, rows }: TableProps): JSX.Element => { const [sortColumn, setSortColumn] = useState("Last Heard"); const [sortOrder, setSortOrder] = useState<"asc" | "desc">("desc"); const headingSort = (title: string) => { if (sortColumn === title) { setSortOrder(sortOrder === "asc" ? "desc" : "asc"); } else { setSortColumn(title); setSortOrder("asc"); } }; const sortedRows = rows.slice().sort((a, b) => { if (!sortColumn) return 0; const columnIndex = headings.findIndex((h) => h.title === sortColumn); const aValue = a[columnIndex].props.children; const bValue = b[columnIndex].props.children; // Custom comparison for 'Last Heard' column if (sortColumn === "Last Heard") { const aTimestamp = aValue.props.timestamp ?? 0; const bTimestamp = bValue.props.timestamp ?? 0; if (aTimestamp < bTimestamp) { return sortOrder === "asc" ? -1 : 1; } if (aTimestamp > bTimestamp) { return sortOrder === "asc" ? 1 : -1; } return 0; } // Default comparison for other columns if (aValue < bValue) { return sortOrder === "asc" ? -1 : 1; } if (aValue > bValue) { return sortOrder === "asc" ? 1 : -1; } return 0; }); return ( {headings.map((heading) => ( ))} {sortedRows.map((row, index) => ( // biome-ignore lint/suspicious/noArrayIndexKey: TODO: Once this table is sortable, this should get fixed. {row.map((item, index) => ( ))} ))}
heading.sortable && headingSort(heading.title)} onKeyUp={() => heading.sortable && headingSort(heading.title)} >
{heading.title} {sortColumn === heading.title && (sortOrder === "asc" ? ( ) : ( ))}
{item}
); };