Skip to content

Commit e215ff7

Browse files
committed
refactor: switch to MRT for Config Insights Table
Fixes #2285
1 parent 527709c commit e215ff7

File tree

3 files changed

+73
-101
lines changed

3 files changed

+73
-101
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { useSearchParams } from "react-router-dom";
2+
import { useConfigInsightsQuery } from "./useConfigAnalysisQuery";
3+
4+
export default function useFetchConfigInsights(
5+
setIsLoading: (isLoading: boolean) => void,
6+
configId?: string
7+
) {
8+
const [params] = useSearchParams();
9+
10+
const status = params.get("status") ?? undefined;
11+
const severity = params.get("severity") ?? undefined;
12+
const type = params.get("type") ?? undefined;
13+
const configType = params.get("configType") ?? undefined;
14+
const analyzer = params.get("analyzer") ?? undefined;
15+
const component = params.get("component") ?? undefined;
16+
const pageSize = +(params.get("pageSize") ?? 50);
17+
const pageIndex = +(params.get("pageIndex") ?? 0);
18+
19+
return useConfigInsightsQuery(
20+
{
21+
status,
22+
severity: severity?.toLowerCase(),
23+
type,
24+
analyzer,
25+
component,
26+
configId,
27+
configType
28+
},
29+
{
30+
sortBy: params.get("sortBy") ?? undefined,
31+
sortOrder: params.get("sortOrder") as "asc" | "desc" | undefined
32+
},
33+
{
34+
pageIndex,
35+
pageSize
36+
},
37+
{
38+
keepPreviousData: true,
39+
onSuccess: () => setIsLoading(false)
40+
}
41+
);
42+
}
Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
import { ConfigAnalysis, ConfigItem } from "@flanksource-ui/api/types/configs";
2-
import { DateCell } from "@flanksource-ui/ui/DataTable/Cells/DateCells";
3-
import { ColumnDef } from "@tanstack/react-table";
2+
import { MRTDateCell } from "@flanksource-ui/ui/MRTDataTable/Cells/MRTDateCells";
3+
import { MRT_ColumnDef } from "mantine-react-table";
44
import { Link } from "react-router-dom";
55
import { ConfigIcon } from "../../../ui/Icons/ConfigIcon";
66
import ConfigInsightsIcon from "./ConfigInsightsIcon";
77
import ConfigInsightsSeverityIcons from "./ConfigInsightsSeverityIcons";
88

9-
export const ConfigInsightsColumns: ColumnDef<
10-
ConfigAnalysis & { config?: ConfigItem },
11-
any
9+
export const ConfigInsightsColumns: MRT_ColumnDef<
10+
ConfigAnalysis & { config?: ConfigItem }
1211
>[] = [
1312
{
1413
header: "Catalog",
1514
id: "catalog",
16-
aggregatedCell: "",
1715
enableHiding: true,
1816
size: 100,
19-
cell: ({ cell }) => {
17+
Cell: ({ cell }) => {
2018
const config = cell.row.original.config;
2119

2220
return (
@@ -32,10 +30,9 @@ export const ConfigInsightsColumns: ColumnDef<
3230
{
3331
header: "Type",
3432
id: "analysis_type",
35-
aggregatedCell: "",
3633
accessorKey: "analysis_type",
3734
size: 50,
38-
cell: ({ cell }) => {
35+
Cell: ({ cell }) => {
3936
const data = cell.row.original;
4037

4138
return (
@@ -52,14 +49,15 @@ export const ConfigInsightsColumns: ColumnDef<
5249
{
5350
header: "Analyzer",
5451
id: "analyzer",
55-
aggregatedCell: "",
5652
accessorKey: "analyzer",
5753
size: 100,
5854
enableGrouping: true,
59-
cell: ({ getValue }) => {
55+
Cell: ({ cell }) => {
56+
const value = cell.getValue<string>();
57+
6058
return (
6159
<span className="flex-1 overflow-hidden overflow-ellipsis">
62-
{getValue<string>()}
60+
{value}
6361
</span>
6462
);
6563
},
@@ -68,41 +66,39 @@ export const ConfigInsightsColumns: ColumnDef<
6866
{
6967
header: "Severity",
7068
id: "severity",
71-
aggregatedCell: "",
7269
accessorKey: "severity",
7370
size: 50,
7471
enableSorting: false,
75-
cell: ({ getValue }) => {
72+
Cell: ({ cell }) => {
73+
const value = cell.getValue<string>();
74+
7675
return (
7776
<div className="flex flex-1 flex-row items-center gap-1 overflow-hidden overflow-ellipsis capitalize">
78-
<ConfigInsightsSeverityIcons severity={getValue<string>()} />
79-
<span>{getValue<string>()}</span>
77+
<ConfigInsightsSeverityIcons severity={value} />
78+
<span>{value}</span>
8079
</div>
8180
);
8281
}
8382
},
8483
{
8584
header: "Status",
8685
id: "status",
87-
aggregatedCell: "",
8886
accessorKey: "status",
8987
size: 50,
9088
enableSorting: false
9189
},
9290
{
9391
header: "First Observed",
9492
id: "first_observed",
95-
aggregatedCell: "",
9693
accessorKey: "first_observed",
9794
size: 50,
98-
cell: DateCell
95+
Cell: MRTDateCell
9996
},
10097
{
10198
header: "Last Observed",
10299
id: "last_observed",
103-
aggregatedCell: "",
104100
accessorKey: "last_observed",
105101
size: 50,
106-
cell: DateCell
102+
Cell: MRTDateCell
107103
}
108104
];

src/components/Configs/Insights/ConfigInsightsList.tsx

Lines changed: 14 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { useConfigInsightsQuery } from "@flanksource-ui/api/query-hooks/useConfigAnalysisQuery";
1+
import useFetchConfigInsights from "@flanksource-ui/api/query-hooks/useFetchConfigInsights";
22
import { ConfigAnalysis } from "@flanksource-ui/api/types/configs";
3-
import { DataTable, PaginationOptions } from "@flanksource-ui/ui/DataTable";
4-
import useReactTableSortState from "@flanksource-ui/ui/DataTable/Hooks/useReactTableSortState";
5-
import { useMemo, useState } from "react";
3+
import MRTDataTable from "@flanksource-ui/ui/MRTDataTable/MRTDataTable";
4+
import { useEffect, useState } from "react";
65
import { useSearchParams } from "react-router-dom";
76
import { InfoMessage } from "../../InfoMessage";
87
import ConfigInsightsDetailsModal from "./ConfigAnalysisLink/ConfigInsightsDetailsModal";
@@ -21,47 +20,18 @@ export default function ConfigInsightsList({
2120
configId,
2221
columnsToHide = []
2322
}: Props) {
24-
const [params, setParams] = useSearchParams();
23+
const [params] = useSearchParams();
2524
const [clickedInsightItem, setClickedInsightItem] =
2625
useState<ConfigAnalysis>();
2726
const [isInsightDetailsModalOpen, setIsInsightDetailsModalOpen] =
2827
useState(false);
2928

30-
const status = params.get("status") ?? undefined;
31-
const severity = params.get("severity") ?? undefined;
32-
const type = params.get("type") ?? undefined;
33-
const configType = params.get("configType") ?? undefined;
34-
const analyzer = params.get("analyzer") ?? undefined;
35-
const component = params.get("component") ?? undefined;
3629
const pageSize = +(params.get("pageSize") ?? 50);
37-
const pageIndex = +(params.get("pageIndex") ?? 0);
3830

3931
const { data, isLoading, refetch, isRefetching, error } =
40-
useConfigInsightsQuery(
41-
{
42-
status,
43-
severity: severity?.toLowerCase(),
44-
type,
45-
analyzer,
46-
component,
47-
configId,
48-
configType
49-
},
50-
{
51-
sortBy: params.get("sortBy") ?? undefined,
52-
sortOrder: params.get("sortOrder") as "asc" | "desc" | undefined
53-
},
54-
{
55-
pageIndex,
56-
pageSize
57-
},
58-
{
59-
keepPreviousData: true,
60-
onSuccess: () => setIsLoading(false)
61-
}
62-
);
32+
useFetchConfigInsights(setIsLoading, configId);
6333

64-
useMemo(() => {
34+
useEffect(() => {
6535
setIsLoading(true);
6636
refetch();
6737
// we only want to trigger this effect when the triggerRefresh changes and
@@ -71,63 +41,27 @@ export default function ConfigInsightsList({
7141

7242
const configInsights = data?.data ?? [];
7343

74-
const totalEntries = (data as any)?.totalEntries;
44+
const totalEntries = data?.totalEntries;
7545
const pageCount = totalEntries ? Math.ceil(totalEntries / pageSize) : -1;
7646

77-
const [sortBy, updateSortBy] = useReactTableSortState();
78-
79-
const pagination = useMemo(() => {
80-
const pagination: PaginationOptions = {
81-
setPagination: (updater) => {
82-
const newParams =
83-
typeof updater === "function"
84-
? updater({
85-
pageIndex,
86-
pageSize
87-
})
88-
: updater;
89-
params.set("pageIndex", newParams.pageIndex.toString());
90-
params.set("pageSize", newParams.pageSize.toString());
91-
setParams(params);
92-
},
93-
pageIndex,
94-
pageSize,
95-
pageCount,
96-
remote: true,
97-
enable: true,
98-
loading: isLoading || isRefetching
99-
};
100-
return pagination;
101-
}, [
102-
pageIndex,
103-
pageSize,
104-
pageCount,
105-
isLoading,
106-
isRefetching,
107-
params,
108-
setParams
109-
]);
110-
11147
return (
11248
// eslint-disable-next-line react/jsx-no-useless-fragment
11349
<>
11450
{error ? (
11551
<InfoMessage message={error.message} />
11652
) : (
117-
<DataTable
118-
columns={configInsightsColumns}
53+
<MRTDataTable
11954
data={configInsights}
120-
isLoading={isLoading}
121-
stickyHead
122-
pagination={pagination}
55+
isLoading={isLoading || isRefetching}
12356
hiddenColumns={columnsToHide}
124-
handleRowClick={(row) => {
125-
setClickedInsightItem(row.original);
57+
onRowClick={(row) => {
58+
setClickedInsightItem(row);
12659
setIsInsightDetailsModalOpen(true);
12760
}}
12861
enableServerSideSorting
129-
tableSortByState={sortBy}
130-
onTableSortByChanged={updateSortBy}
62+
totalRowCount={totalEntries}
63+
manualPageCount={pageCount}
64+
columns={configInsightsColumns}
13165
/>
13266
)}
13367

0 commit comments

Comments
 (0)