import type { Table as ReactTableType, RowData } from '@tanstack/react-table' import type { ReactNode } from 'react' import { ArrowDownIcon, ArrowUpIcon } from '@heroicons/react/24/solid' import { flexRender } from '@tanstack/react-table' import React, { useState } from 'react' import { classNames, Link, Table } from '..' import { LoadingOverlay } from '../loader' import { Tooltip } from '../tooltip' import { Typography } from '../typography' interface GenericTableProps { table: ReactTableType HoverElement?: React.FunctionComponent<{ row: C }> loading?: boolean placeholder: ReactNode pageSize: number linkFormatter?: (row: C) => string tdClassName?: string } declare module '@tanstack/react-table' { interface ColumnMeta { className?: string skeleton: ReactNode } } export function GenericTable({ table, HoverElement, loading, placeholder, pageSize, linkFormatter, tdClassName, }: GenericTableProps) { const [showOverlay, setShowOverlay] = useState(false) const [, setPopupInvisible] = useState(false) const headers = table.getFlatHeaders() return ( <> 5} style={{ minHeight: (pageSize + 1) * 52 }}> {table.getHeaderGroups().map(headerGroup => ( {headerGroup.headers.map(header => (
{flexRender(header.column.columnDef.header, header.getContext())} {{ asc: , desc: , }[header.column.getIsSorted() as string] ?? null}
))}
))}
{!loading && table.getRowModel().rows.map((row) => { if (HoverElement) { return ( } key={row.id} > { if (!e.ctrlKey && !e.shiftKey && !e.metaKey && !e.altKey) { if (!linkFormatter) return setPopupInvisible(true) setTimeout(() => setShowOverlay(true), 250) } }} > {row.getVisibleCells().map((cell, i) => { return ( {linkFormatter ? (
{flexRender(cell.column.columnDef.cell, cell.getContext())}
) : (
{flexRender(cell.column.columnDef.cell, cell.getContext())}
)}
) })}
) } return ( { if (!linkFormatter) return if (!e.ctrlKey && !e.shiftKey && !e.metaKey && !e.altKey) setShowOverlay(true) }} > {row.getVisibleCells().map((cell, i) => { return ( {linkFormatter ? (
{flexRender(cell.column.columnDef.cell, cell.getContext())}
) : (
{flexRender(cell.column.columnDef.cell, cell.getContext())}
)}
) })}
) })} {!loading && table.getRowModel().rows.length !== 0 && Array.from(Array(Math.max(pageSize - table.getRowModel().rows.length, 0))).map((el, index) => ( {table.getVisibleFlatColumns().map(column => ( ))} ))} {loading && Array.from(Array(pageSize)).map((el, index) => ( {table.getVisibleFlatColumns().map((column) => { return ( {column.columnDef.meta?.skeleton} ) })} ))} {!loading && table.getRowModel().rows.length === 0 && ( {placeholder} )}
) }