Skip to content

Commit b48e471

Browse files
committed
fix: use server side sorting for config summary
1 parent 704dbb2 commit b48e471

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/components/Configs/ConfigSummary/ConfigSummaryList.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
import { Badge } from "@flanksource-ui/ui/Badge/Badge";
77
import { CountBadge } from "@flanksource-ui/ui/Badge/CountBadge";
88
import { DataTable } from "@flanksource-ui/ui/DataTable";
9+
import useReactTableSortState from "@flanksource-ui/ui/DataTable/Hooks/useReactTableSortState";
910
import { CellContext, ColumnDef, Row } from "@tanstack/react-table";
1011
import { useCallback, useMemo } from "react";
1112
import { BiLabel } from "react-icons/bi";
@@ -318,6 +319,8 @@ export default function ConfigSummaryList({
318319
return [...newColumns, ...configSummaryColumns];
319320
}, [groupBy, groupByTags]);
320321

322+
const [sortState, updateSortState] = useReactTableSortState();
323+
321324
return (
322325
<DataTable
323326
stickyHead
@@ -330,6 +333,9 @@ export default function ConfigSummaryList({
330333
hiddenColumns={
331334
groupBy.length > 1 ? groupBy.slice(0, groupBy.length - 1) : undefined
332335
}
336+
enableServerSideSorting
337+
onTableSortByChanged={updateSortState}
338+
tableSortByState={sortState}
333339
handleRowClick={handleRowClick}
334340
tableStyle={{ borderSpacing: "0" }}
335341
isLoading={isLoading}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { SortingState, Updater } from "@tanstack/react-table";
2+
import { useCallback, useMemo } from "react";
3+
import { useSearchParams } from "react-router-dom";
4+
5+
/**
6+
*
7+
* useReactTableSortState is a custom hook that manages the sorting state of a
8+
* react-table instance. It uses the URL search params to store the sorting
9+
* state. It returns the sorting state and a function to update the sorting state.
10+
*
11+
*/
12+
export default function useReactTableSortState(
13+
sortByKey = "sortBy",
14+
sortOrderKey = "sortOrder"
15+
): [SortingState, (newSortBy: Updater<SortingState>) => void] {
16+
const [searchParams, setSearchParams] = useSearchParams();
17+
18+
const sortBy = searchParams.get(sortByKey) || undefined;
19+
const sortOrder = searchParams.get(sortOrderKey) || undefined;
20+
21+
const tableSortByState = useMemo(() => {
22+
if (!sortBy || !sortOrder) {
23+
return [];
24+
}
25+
return [
26+
{
27+
id: sortBy,
28+
desc: sortOrder === "desc"
29+
}
30+
] satisfies SortingState;
31+
}, [sortBy, sortOrder]);
32+
33+
console.log(tableSortByState);
34+
35+
const updateSortByFn = useCallback(
36+
(newSortBy: Updater<SortingState>) => {
37+
const sort = typeof newSortBy === "function" ? newSortBy([]) : newSortBy;
38+
if (sort.length === 0) {
39+
searchParams.delete(sortByKey);
40+
searchParams.delete(sortOrderKey);
41+
} else {
42+
searchParams.set(sortByKey, sort[0]?.id);
43+
searchParams.set(sortOrderKey, sort[0].desc ? "desc" : "asc");
44+
}
45+
setSearchParams(searchParams);
46+
},
47+
[searchParams, setSearchParams, sortByKey, sortOrderKey]
48+
);
49+
50+
return [tableSortByState, updateSortByFn];
51+
}

0 commit comments

Comments
 (0)