Skip to content

Commit

Permalink
Added useTablePaginationReset hook for consistent search behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
atrincas committed Dec 18, 2024
1 parent be3da98 commit 8373eb6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions client/src/containers/overview/table/view/key-costs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions client/src/containers/overview/table/view/overview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -55,10 +57,12 @@ export function OverviewTable() {
desc: true,
},
]);

const [pagination, setPagination] = useState<PaginationState>({
pageIndex: 0,
pageSize: Number.parseInt(PAGINATION_SIZE_OPTIONS[0]),
});
useTablePaginationReset(filters.keyword, setPagination);

const queryKey = queryKeys.tables.all(tableView, projectsQuerySchema, {
...filters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -62,6 +64,7 @@ export function ScoredCardPrioritizationTable() {
sorting,
pagination,
}).queryKey;
useTablePaginationReset(filters.keyword, setPagination);

const { data, isSuccess } = client.projects.getProjectsScorecard.useQuery(
queryKey,
Expand Down
15 changes: 15 additions & 0 deletions client/src/hooks/use-table-pagination-reset.ts
Original file line number Diff line number Diff line change
@@ -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]);
}

0 comments on commit 8373eb6

Please sign in to comment.