Skip to content

Commit 4533792

Browse files
committed
refactor(data-table): refactor table tables to @TanStack
1 parent 913fa8f commit 4533792

File tree

10 files changed

+440
-520
lines changed

10 files changed

+440
-520
lines changed

components/DataTable/DataTable.tsx

Lines changed: 182 additions & 184 deletions
Large diffs are not rendered by default.

features/DatasetsTable/DatasetDetails/VersionActionsSection/DatasetSchemaListItem/DatasetSchemaViewModal/DatasetSchemaViewModal.tsx

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import type { FC } from "react";
22
import { useMemo } from "react";
3-
import type { Cell, Column } from "react-table";
43

54
import { Alert, Typography } from "@mui/material";
5+
import type { CoreOptions } from "@tanstack/react-table";
6+
import { createColumnHelper } from "@tanstack/react-table";
67

78
import { CenterLoader } from "../../../../../../components/CenterLoader";
8-
import { DataTable } from "../../../../../../components/DataTable";
9+
import { DataTable } from "../../../../../../components/DataTable/DataTable";
910
import { ModalWrapper } from "../../../../../../components/modals/ModalWrapper";
1011
import { getErrorMessage } from "../../../../../../utils/next/orvalError";
1112
import { JSON_SCHEMA_TYPES } from "./constants";
@@ -46,6 +47,8 @@ export interface DatasetSchemaViewModalProps {
4647
onClose: () => void;
4748
}
4849

50+
const columnHelper = createColumnHelper<TableSchemaView>();
51+
4952
/**
5053
* Displays the schema of a version of a dataset in a tabular format.
5154
*/
@@ -88,17 +91,13 @@ export const DatasetSchemaViewModal: FC<DatasetSchemaViewModalProps> = ({
8891
return undefined;
8992
}, [schema, originalSchema]);
9093

91-
const columns: Column<TableSchemaView>[] = useMemo(
94+
const columns = useMemo(
9295
() => [
93-
{
94-
accessor: "name",
95-
Header: "Field Name",
96-
},
97-
{
96+
columnHelper.accessor("name", { header: "Field Name" }),
97+
columnHelper.accessor((row) => row.description.current, {
9898
id: "description",
99-
accessor: (row) => row.description.current,
100-
Header: "Description",
101-
Cell: ({ row }: Cell<TableSchemaView>) => {
99+
header: "Description",
100+
cell: ({ row }) => {
102101
const {
103102
name,
104103
description: { original, current },
@@ -113,12 +112,11 @@ export const DatasetSchemaViewModal: FC<DatasetSchemaViewModalProps> = ({
113112
/>
114113
);
115114
},
116-
},
117-
{
115+
}),
116+
columnHelper.accessor((row) => row.type.current, {
118117
id: "type",
119-
accessor: (row) => row.type.current,
120-
Header: "Type",
121-
Cell: ({ row }: Cell<TableSchemaView>) => {
118+
header: "Type",
119+
cell: ({ row }) => {
122120
const {
123121
name,
124122
type: { original, current },
@@ -134,7 +132,7 @@ export const DatasetSchemaViewModal: FC<DatasetSchemaViewModalProps> = ({
134132
/>
135133
);
136134
},
137-
},
135+
}),
138136
],
139137
[setSchemaField],
140138
);
@@ -164,6 +162,8 @@ export const DatasetSchemaViewModal: FC<DatasetSchemaViewModalProps> = ({
164162
);
165163
}
166164

165+
const getRowId: CoreOptions<TableSchemaView>["getRowId"] = (row) => row.name;
166+
167167
return (
168168
<>
169169
{isSavingError &&
@@ -184,7 +184,7 @@ export const DatasetSchemaViewModal: FC<DatasetSchemaViewModalProps> = ({
184184
},
185185
}}
186186
data={fields}
187-
getRowId={(row) => row.name}
187+
getRowId={getRowId}
188188
tableContainer={false}
189189
ToolbarChild={
190190
<DatasetSchemaDescriptionInput

features/DatasetsTable/DatasetsTable.tsx

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { useCallback, useMemo } from "react";
2-
import type { Column, Row } from "react-table";
32

43
import { useGetDatasets } from "@squonk/data-manager-client/dataset";
54

65
import { CircularProgress } from "@mui/material";
6+
import type { CoreOptions, Row } from "@tanstack/react-table";
7+
import { createColumnHelper } from "@tanstack/react-table";
78
import dynamic from "next/dynamic";
89

910
import { Chips } from "../../components/Chips";
10-
import { DataTable } from "../../components/DataTable";
11+
import { DataTable } from "../../components/DataTable/DataTable";
1112
import { LabelChip } from "../../components/labels/LabelChip";
1213
import { combineLabels } from "../../utils/app/labels";
1314
import { getErrorMessage } from "../../utils/next/orvalError";
@@ -43,54 +44,47 @@ const editorsSorter = (rowA: Row<TableDataset>, rowB: Row<TableDataset>) => {
4344
return -1;
4445
};
4546

47+
const columnHelper = createColumnHelper<TableDataset>();
48+
4649
/**
4750
* MuiTable managed by react-table that displays datasets viewable by the user with option to see
4851
* further details of a dataset.
4952
*/
5053
export const DatasetsTable = () => {
51-
const columns: Column<TableDataset>[] = useMemo(
54+
const columns = useMemo(
5255
() => [
53-
{
54-
accessor: "fileName",
55-
Header: "File Name",
56-
Cell: ({ row }) => {
57-
return (
58-
<DatasetDetails
59-
dataset={row.original.datasetSummary}
60-
datasetName={row.original.datasetSummary.versions[0].file_name}
61-
version={row.original.datasetVersion}
62-
/>
63-
);
64-
},
65-
},
66-
{
67-
accessor: "labels",
68-
Cell: ({ value: labels }) => (
56+
columnHelper.accessor("fileName", {
57+
header: "File Name",
58+
cell: ({ row }) => (
59+
<DatasetDetails
60+
dataset={row.original.datasetSummary}
61+
datasetName={row.original.datasetSummary.versions[0].file_name}
62+
version={row.original.datasetVersion}
63+
/>
64+
),
65+
}),
66+
columnHelper.accessor("labels", {
67+
header: "Labels",
68+
cell: ({ getValue }) => (
6969
<Chips>
70-
{Object.entries(labels).map(([label, values]) => (
70+
{Object.entries(getValue()).map(([label, values]) => (
7171
<LabelChip key={label} label={label} values={values} />
7272
))}
7373
</Chips>
7474
),
75-
Header: "Labels",
76-
},
77-
{
78-
accessor: "editors",
79-
Header: "Editors",
80-
sortType: editorsSorter,
81-
Cell: ({ value: editors }) => {
82-
return editors.join(", ");
83-
},
84-
},
85-
{
75+
}),
76+
columnHelper.accessor("editors", {
77+
header: "Editors",
78+
sortingFn: editorsSorter,
79+
cell: ({ getValue }) => getValue().join(", "),
80+
}),
81+
columnHelper.accessor((row) => row.subRows.length || "", {
8682
id: "versions",
87-
accessor: (row) => row.subRows.length || "",
88-
Header: "Versions",
89-
},
90-
{
91-
accessor: (row) => row.numberOfProjects,
92-
Header: "Number of projects",
93-
},
83+
header: "Versions",
84+
}),
85+
columnHelper.accessor("numberOfProjects", {
86+
header: "Number of projects",
87+
}),
9488
],
9589
[],
9690
);
@@ -135,7 +129,10 @@ export const DatasetsTable = () => {
135129
const { selectedDatasets, onSelection } = useSelectedDatasets(datasets);
136130

137131
const { owner, editor, fileType, labels } = filter;
138-
const getRowId = useCallback((row) => `${row.dataset_id}#${row.version}`, []);
132+
const getRowId: CoreOptions<TableDataset>["getRowId"] = useCallback(
133+
(row) => `${row.dataset_id}#${row.version}`,
134+
[],
135+
);
139136

140137
return (
141138
<DataTable
Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,48 @@
1-
import { useCallback, useMemo } from "react";
2-
import type { CellProps, Column, PluginHook } from "react-table";
1+
import { useMemo } from "react";
32

43
import type { ProductDmStorage } from "@squonk/account-server-client";
54

5+
import { createColumnHelper } from "@tanstack/react-table";
6+
67
import { DataTable } from "../../../components/DataTable";
78
import { AdjustProjectProduct } from "../../../components/products/AdjustProjectProduct";
89
import { ChargesLinkIconButton } from "../../../components/products/ChargesLinkIconButton";
910
import { DeleteProductButton } from "../../../components/products/DeleteProductButton";
10-
import { sharedProductColumns } from "../columns";
11+
import { getSharedColumns } from "../columns";
1112

1213
export interface DatasetProductTableProps {
1314
products: ProductDmStorage[];
1415
}
1516

17+
const columnHelper = createColumnHelper<ProductDmStorage>();
18+
1619
export const DatasetProductTable = ({ products }: DatasetProductTableProps) => {
17-
const columns: Column<ProductDmStorage>[] = useMemo(
20+
const columns = useMemo(
1821
() => [
19-
...(sharedProductColumns as Column<ProductDmStorage>[]),
20-
{ id: "size", Header: "Size", accessor: (row) => row.storage.size.current },
21-
{ id: "coins", Header: "Coins", accessor: (row) => row.storage.coins.used },
22+
...getSharedColumns<ProductDmStorage>(),
23+
columnHelper.accessor((row) => row.storage.size.current, { id: "size", header: "Size" }),
24+
columnHelper.accessor((row) => row.storage.coins.used, { id: "coins", header: "Coins" }),
25+
columnHelper.display({
26+
id: "actions",
27+
header: "Actions",
28+
enableGrouping: false,
29+
cell: ({ row }) => (
30+
<>
31+
<ChargesLinkIconButton productId={row.original.product.id} />
32+
<AdjustProjectProduct
33+
allowance={row.original.coins.allowance}
34+
product={row.original.product}
35+
/>
36+
<DeleteProductButton
37+
product={row.original.product}
38+
tooltip="Delete product permanently"
39+
/>
40+
</>
41+
),
42+
}),
2243
],
2344
[],
2445
);
2546

26-
// react-table plugin to add actions buttons for project files
27-
const useActionsColumnPlugin: PluginHook<ProductDmStorage> = useCallback((hooks) => {
28-
hooks.visibleColumns.push((columns) => {
29-
return [
30-
...columns,
31-
{
32-
id: "actions",
33-
groupByBoundary: true, // Ensure normal columns can't be ordered before this
34-
Header: "Actions",
35-
Cell: ({ row }: CellProps<ProductDmStorage, any>) => (
36-
<>
37-
<ChargesLinkIconButton productId={row.original.product.id} />
38-
<AdjustProjectProduct
39-
allowance={row.original.coins.allowance}
40-
product={row.original.product}
41-
/>
42-
<DeleteProductButton
43-
product={row.original.product}
44-
tooltip="Delete product permanently"
45-
/>
46-
</>
47-
),
48-
},
49-
];
50-
});
51-
}, []);
52-
53-
return (
54-
<DataTable columns={columns} data={products} useActionsColumnPlugin={useActionsColumnPlugin} />
55-
);
47+
return <DataTable columns={columns} data={products} />;
5648
};

0 commit comments

Comments
 (0)