From d181f8eb76c6560881133ae093c7ab74ecc11dcb Mon Sep 17 00:00:00 2001 From: atrincas Date: Mon, 13 Jan 2025 11:43:35 +0100 Subject: [PATCH] fix: ensure default sorting persists in overview table --- .../overview/table/view/overview/index.tsx | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/client/src/containers/overview/table/view/overview/index.tsx b/client/src/containers/overview/table/view/overview/index.tsx index 0766a04a..8fe8eea8 100644 --- a/client/src/containers/overview/table/view/overview/index.tsx +++ b/client/src/containers/overview/table/view/overview/index.tsx @@ -14,6 +14,7 @@ import { SortingState, useReactTable, TableState, + Updater, } from "@tanstack/react-table"; import { useAtom } from "jotai"; import { ChevronsUpDownIcon } from "lucide-react"; @@ -62,17 +63,23 @@ export interface TableStateWithMaximums extends TableState { }; } +const DEFAULT_SORTING: SortingState = [ + { + id: "projectName", + desc: true, + }, +]; + export function OverviewTable() { const [tableView] = useTableView(); const [filters] = useProjectOverviewFilters(); const [, setProjectDetails] = useAtom(projectDetailsAtom); - const [sorting, setSorting] = useState([ - { - id: "projectName", - desc: true, - }, - ]); - + const [sorting, setSorting] = useState(DEFAULT_SORTING); + const handleSortingChange = (updater: Updater) => { + const newSorting = + typeof updater === "function" ? updater(sorting) : updater; + setSorting(newSorting.length === 0 ? DEFAULT_SORTING : newSorting); + }; const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: Number.parseInt(PAGINATION_SIZE_OPTIONS[0]), @@ -149,7 +156,7 @@ export function OverviewTable() { pagination, maximums, } as TableStateWithMaximums, - onSortingChange: setSorting, + onSortingChange: handleSortingChange, onPaginationChange: setPagination, });