From 587fc0f228458b8c0a1537f5eea6d0f1ce31a320 Mon Sep 17 00:00:00 2001 From: Boki Date: Wed, 18 Jun 2025 08:17:00 -0400 Subject: [PATCH] fixed up tables to use virtualized --- .../src/components/ui/DataTable/DataTable.tsx | 195 +++++++++++------- .../exchanges/components/ExchangesTable.tsx | 1 - 2 files changed, 116 insertions(+), 80 deletions(-) diff --git a/apps/web-app/src/components/ui/DataTable/DataTable.tsx b/apps/web-app/src/components/ui/DataTable/DataTable.tsx index 9aef247..6e91197 100644 --- a/apps/web-app/src/components/ui/DataTable/DataTable.tsx +++ b/apps/web-app/src/components/ui/DataTable/DataTable.tsx @@ -11,6 +11,7 @@ import { useReactTable, } from '@tanstack/react-table'; import { Fragment, useState } from 'react'; +import { TableVirtuoso } from 'react-virtuoso'; interface DataTableProps { data: T[]; @@ -19,6 +20,8 @@ interface DataTableProps { className?: string; getRowCanExpand?: (row: Row) => boolean; renderSubComponent?: (props: { row: Row }) => React.ReactElement; + onRowClick?: (row: T) => void; + height?: number; } export function DataTable({ @@ -28,6 +31,8 @@ export function DataTable({ className = '', getRowCanExpand, renderSubComponent, + onRowClick, + height, }: DataTableProps) { const [sorting, setSorting] = useState([]); @@ -54,89 +59,121 @@ export function DataTable({ ); } + const { rows } = table.getRowModel(); + + // For expanded rows, we need to create a flattened list + const flatRows = rows.reduce }>>((acc, row) => { + acc.push({ type: 'row', row }); + if (row.getIsExpanded() && renderSubComponent) { + acc.push({ type: 'expanded', row }); + } + return acc; + }, []); + return ( -
- - - {table.getHeaderGroups().map(headerGroup => ( - - {headerGroup.headers.map(header => ( - + ))} + + )) + } + /> ); } \ No newline at end of file diff --git a/apps/web-app/src/features/exchanges/components/ExchangesTable.tsx b/apps/web-app/src/features/exchanges/components/ExchangesTable.tsx index 3ea3245..1380f79 100644 --- a/apps/web-app/src/features/exchanges/components/ExchangesTable.tsx +++ b/apps/web-app/src/features/exchanges/components/ExchangesTable.tsx @@ -368,7 +368,6 @@ export function ExchangesTable() { data={exchanges || []} columns={columns} loading={loading} - className="max-h-[calc(100vh-300px)]" getRowCanExpand={() => true} renderSubComponent={renderSubComponent} />
( + + ), + TableRow: props => { + const index = props['data-index'] as number; + const item = flatRows[index]; + + if (!item) return null; + + if (item.type === 'expanded') { + return ( + + + + ); + } + + return ( + onRowClick?.(item.row.original)} + > + {item.row.getVisibleCells().map(cell => ( + ))} - ))} - - - {table.getRowModel().rows.map(row => ( - - - {row.getVisibleCells().map(cell => ( - + {headerGroup.headers.map(header => ( + - {row.getIsExpanded() && renderSubComponent && ( - - - - )} - - ))} - -
+ {renderSubComponent?.({ row: item.row })} +
- {header.isPlaceholder ? null : ( - <> -
- - {flexRender(header.column.columnDef.header, header.getContext())} - - {header.column.getCanSort() && ( -
- {header.column.getIsSorted() === 'asc' ? ( - - ) : header.column.getIsSorted() === 'desc' ? ( - - ) : ( -
- )} -
- )} -
- {/* Column resizer */} - {header.column.getCanResize() && ( -
- )} - - )} - +
+ {flexRender(cell.column.columnDef.cell, cell.getContext())} +
+
-
- {flexRender(cell.column.columnDef.cell, cell.getContext())} + ); + }, + }} + fixedHeaderContent={() => + table.getHeaderGroups().map(headerGroup => ( +
+ {header.isPlaceholder ? null : ( + <> +
+ + {flexRender(header.column.columnDef.header, header.getContext())} + + {header.column.getCanSort() && ( +
+ {header.column.getIsSorted() === 'asc' ? ( + + ) : header.column.getIsSorted() === 'desc' ? ( + + ) : ( +
+ )} +
+ )}
- - ))} -
- {renderSubComponent({ row })} -
- + {/* Column resizer */} + {header.column.getCanResize() && ( +
+ )} + + )} +