|
1 |
| -import Avatar from '@mui/material/Avatar'; |
2 |
| -import type { HeaderGroup, Row } from '@tanstack/react-table'; |
| 1 | +import { useMemo } from 'react'; |
3 | 2 | import {
|
4 | 3 | createColumnHelper,
|
5 |
| - flexRender, |
6 | 4 | getCoreRowModel,
|
7 | 5 | getFilteredRowModel,
|
8 | 6 | getSortedRowModel,
|
9 | 7 | useReactTable,
|
10 | 8 | } from '@tanstack/react-table';
|
| 9 | +import Avatar from '@mui/material/Avatar'; |
11 | 10 | import stringToColor from 'string-to-color';
|
12 |
| -import { memo, useMemo } from 'react'; |
13 | 11 | import Typography from '@mui/material/Typography';
|
14 |
| -import Paper from '@mui/material/Paper'; |
15 |
| -import TableContainer from '@mui/material/TableContainer'; |
16 |
| -import Table from '@mui/material/Table'; |
17 |
| -import TableBody from '@mui/material/TableBody'; |
18 |
| -import TableHead from '@mui/material/TableHead'; |
19 |
| -import TableRow from '@mui/material/TableRow'; |
20 |
| -import TableCell from '@mui/material/TableCell'; |
21 |
| -import TableSortLabel from '@mui/material/TableSortLabel'; |
22 | 12 | import TextField from '@mui/material/TextField';
|
23 | 13 | import InputAdornment from '@mui/material/InputAdornment';
|
24 |
| -import flags from '@lib/flags'; |
25 | 14 | import useFiltersFromUrl from '@lib/useFiltersFromUrl';
|
26 | 15 | import useSortFromUrl from '@lib/useSortFromUrl';
|
27 | 16 | import type { ExchangeRate } from '@server/exchangeRates/types';
|
| 17 | +import flags from '@lib/flags'; |
28 | 18 | import { formatDate } from '@lib/format';
|
29 | 19 |
|
30 | 20 | export const DefaultSort = { id: 'ticker', desc: true };
|
31 |
| - |
32 | 21 | const columnHelper = createColumnHelper<ExchangeRate>();
|
33 |
| -const ExchangeRateTableHead = memo(ExchangeRateTableHeadBase); |
34 |
| -const ExchangeRateTableRow = memo(ExchangeRateTableRowBase); |
35 |
| - |
36 |
| -type Props = { |
37 |
| - rates: ExchangeRate[]; |
38 |
| - onUpdateRate: (idx: number, rate: ExchangeRate) => void; |
39 |
| -}; |
40 |
| - |
41 |
| -export default function ExchangeRatesTableTable({ |
42 |
| - rates, |
43 |
| - onUpdateRate, |
44 |
| -}: Props) { |
45 |
| - const table = useExchangeRatesTable(rates, onUpdateRate); |
46 |
| - |
47 |
| - return ( |
48 |
| - <Paper> |
49 |
| - <TableContainer> |
50 |
| - <Table size="small"> |
51 |
| - <ExchangeRateTableHead headerGroups={table.getHeaderGroups()} /> |
52 |
| - <TableBody> |
53 |
| - {table.getRowModel().rows.map((row) => ( |
54 |
| - <ExchangeRateTableRow key={row.id} row={row} /> |
55 |
| - ))} |
56 |
| - </TableBody> |
57 |
| - </Table> |
58 |
| - </TableContainer> |
59 |
| - </Paper> |
60 |
| - ); |
61 |
| -} |
62 | 22 |
|
63 |
| -function useExchangeRatesTable( |
| 23 | +export default function useExchangeRatesTable( |
64 | 24 | rates: ExchangeRate[],
|
65 |
| - onUpdateRate: Props['onUpdateRate'], |
| 25 | + onUpdateRate: (idx: number, rate: ExchangeRate) => void, |
66 | 26 | ) {
|
67 | 27 | const { sorting } = useSortFromUrl(DefaultSort);
|
68 | 28 | const { filtersByField } = useFiltersFromUrl();
|
69 | 29 |
|
70 | 30 | const columns = useMemo(
|
71 | 31 | () => [
|
72 | 32 | columnHelper.display({
|
| 33 | + enableSorting: false, |
73 | 34 | id: 'avatar',
|
74 | 35 | header: '',
|
75 | 36 | cell: (info) => (
|
@@ -151,62 +112,3 @@ function useExchangeRatesTable(
|
151 | 112 |
|
152 | 113 | return table;
|
153 | 114 | }
|
154 |
| - |
155 |
| -type ExchangeRateTableHeadProps = { |
156 |
| - headerGroups: HeaderGroup<ExchangeRate>[]; |
157 |
| -}; |
158 |
| - |
159 |
| -function ExchangeRateTableHeadBase({ |
160 |
| - headerGroups, |
161 |
| -}: ExchangeRateTableHeadProps) { |
162 |
| - const { toggleSort } = useSortFromUrl(DefaultSort); |
163 |
| - return ( |
164 |
| - <TableHead> |
165 |
| - {headerGroups.map((headerGroup) => ( |
166 |
| - <TableRow key={headerGroup.id}> |
167 |
| - {headerGroup.headers.map((header) => ( |
168 |
| - <TableCell |
169 |
| - key={header.id} |
170 |
| - sortDirection={header.column.getIsSorted()} |
171 |
| - align={ |
172 |
| - header.getContext().column.columnDef.meta?.numeric |
173 |
| - ? 'right' |
174 |
| - : 'left' |
175 |
| - } |
176 |
| - > |
177 |
| - <TableSortLabel |
178 |
| - active={!!header.column.getIsSorted()} |
179 |
| - direction={header.column.getIsSorted() || undefined} |
180 |
| - onClick={() => toggleSort(header.column.id)} |
181 |
| - > |
182 |
| - {flexRender( |
183 |
| - header.column.columnDef.header, |
184 |
| - header.getContext(), |
185 |
| - )} |
186 |
| - </TableSortLabel> |
187 |
| - </TableCell> |
188 |
| - ))} |
189 |
| - </TableRow> |
190 |
| - ))} |
191 |
| - </TableHead> |
192 |
| - ); |
193 |
| -} |
194 |
| - |
195 |
| -type ExchangeRateTableRowProps = { |
196 |
| - row: Row<ExchangeRate>; |
197 |
| -}; |
198 |
| - |
199 |
| -function ExchangeRateTableRowBase({ row }: ExchangeRateTableRowProps) { |
200 |
| - return ( |
201 |
| - <TableRow> |
202 |
| - {row.getVisibleCells().map((cell) => ( |
203 |
| - <TableCell |
204 |
| - key={cell.id} |
205 |
| - align={cell.column.columnDef.meta?.numeric ? 'right' : 'left'} |
206 |
| - > |
207 |
| - {flexRender(cell.column.columnDef.cell, cell.getContext())} |
208 |
| - </TableCell> |
209 |
| - ))} |
210 |
| - </TableRow> |
211 |
| - ); |
212 |
| -} |
0 commit comments