From 8373eb64bfe39b484a249cacc34332ca95b8c119 Mon Sep 17 00:00:00 2001 From: atrincas Date: Wed, 18 Dec 2024 10:18:40 +0100 Subject: [PATCH] Added useTablePaginationReset hook for consistent search behavior --- .../overview/table/view/key-costs/index.tsx | 3 +++ .../overview/table/view/overview/index.tsx | 4 ++++ .../table/view/scorecard-prioritization/index.tsx | 3 +++ client/src/hooks/use-table-pagination-reset.ts | 15 +++++++++++++++ 4 files changed, 25 insertions(+) create mode 100644 client/src/hooks/use-table-pagination-reset.ts diff --git a/client/src/containers/overview/table/view/key-costs/index.tsx b/client/src/containers/overview/table/view/key-costs/index.tsx index 2c5bc46d..82e4a633 100644 --- a/client/src/containers/overview/table/view/key-costs/index.tsx +++ b/client/src/containers/overview/table/view/key-costs/index.tsx @@ -20,6 +20,8 @@ import { cn } from "@/lib/utils"; import { useGlobalFilters, useTableView } from "@/app/(overview)/url-store"; +import { useTablePaginationReset } from "@/hooks/use-table-pagination-reset"; + import { filtersToQueryParams, NO_DATA, @@ -51,6 +53,7 @@ export function KeyCostsTable() { pageIndex: 0, pageSize: Number.parseInt(PAGINATION_SIZE_OPTIONS[0]), }); + useTablePaginationReset(filters.keyword, setPagination); const queryKey = queryKeys.tables.all(tableView, projectsQuerySchema, { ...filters, diff --git a/client/src/containers/overview/table/view/overview/index.tsx b/client/src/containers/overview/table/view/overview/index.tsx index b314b170..d28e6032 100644 --- a/client/src/containers/overview/table/view/overview/index.tsx +++ b/client/src/containers/overview/table/view/overview/index.tsx @@ -23,6 +23,8 @@ import { cn } from "@/lib/utils"; import { projectDetailsAtom } from "@/app/(overview)/store"; import { useGlobalFilters, useTableView } from "@/app/(overview)/url-store"; +import { useTablePaginationReset } from "@/hooks/use-table-pagination-reset"; + import ProjectDetails from "@/containers/overview/project-details"; import { filtersToQueryParams, @@ -55,10 +57,12 @@ export function OverviewTable() { desc: true, }, ]); + const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: Number.parseInt(PAGINATION_SIZE_OPTIONS[0]), }); + useTablePaginationReset(filters.keyword, setPagination); const queryKey = queryKeys.tables.all(tableView, projectsQuerySchema, { ...filters, diff --git a/client/src/containers/overview/table/view/scorecard-prioritization/index.tsx b/client/src/containers/overview/table/view/scorecard-prioritization/index.tsx index 5c120ae1..fee28cd2 100644 --- a/client/src/containers/overview/table/view/scorecard-prioritization/index.tsx +++ b/client/src/containers/overview/table/view/scorecard-prioritization/index.tsx @@ -22,6 +22,8 @@ import { cn } from "@/lib/utils"; import { projectDetailsAtom } from "@/app/(overview)/store"; import { useGlobalFilters, useTableView } from "@/app/(overview)/url-store"; +import { useTablePaginationReset } from "@/hooks/use-table-pagination-reset"; + import ProjectDetails from "@/containers/overview/project-details"; import { filtersToQueryParams, @@ -62,6 +64,7 @@ export function ScoredCardPrioritizationTable() { sorting, pagination, }).queryKey; + useTablePaginationReset(filters.keyword, setPagination); const { data, isSuccess } = client.projects.getProjectsScorecard.useQuery( queryKey, diff --git a/client/src/hooks/use-table-pagination-reset.ts b/client/src/hooks/use-table-pagination-reset.ts new file mode 100644 index 00000000..3dedf42d --- /dev/null +++ b/client/src/hooks/use-table-pagination-reset.ts @@ -0,0 +1,15 @@ +import { useEffect } from "react"; + +import { PaginationState } from "@tanstack/react-table"; + +export function useTablePaginationReset( + keyword: string | undefined, + setPagination: (updater: (prev: PaginationState) => PaginationState) => void, +) { + useEffect(() => { + setPagination((prev) => ({ + ...prev, + pageIndex: 0, + })); + }, [keyword, setPagination]); +}