Skip to content

Commit

Permalink
extract hook for client side pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
kantord committed Jan 17, 2025
1 parent 05c7da3 commit 7c7d044
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/components/AlertsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import { useAlertSearch } from "@/hooks/useAlertSearch";
import { useCallback } from "react";
import { useSearchParams } from "react-router-dom";
import { useFilteredAlerts } from "@/hooks/useAlertsData";

const PAGE_SIZE = 15;
import { useClientSidePagination } from "@/hooks/useClientSidePagination";

const wrapObjectOutput = (input: AlertConversation["trigger_string"]) => {
const data = getMaliciousPackage(input);
Expand Down Expand Up @@ -79,13 +78,11 @@ export function AlertsTable() {
const [searchParams, setSearchParams] = useSearchParams();
const { data: filteredAlerts = [] } = useFilteredAlerts();

const pageStart = page * PAGE_SIZE;
const pageEnd = page * PAGE_SIZE + PAGE_SIZE - 1;

const dataView = filteredAlerts.slice(pageStart, pageEnd);

const hasPreviousPage = page > 0;
const hasNextPage = pageEnd + 1 < filteredAlerts.length;
const { dataView, hasNextPage, hasPreviousPage } = useClientSidePagination(
filteredAlerts,
page,
15,
);

const handleToggleFilter = useCallback(
(isChecked: boolean) => {
Expand Down
15 changes: 15 additions & 0 deletions src/hooks/useClientSidePagination.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function useClientSidePagination<T>(
data: T[],
page: number,
pageSize: number,
) {
const pageStart = page * pageSize;
const pageEnd = page * pageSize + pageSize - 1;

const dataView = data.slice(pageStart, pageEnd);

const hasPreviousPage = page > 0;
const hasNextPage = pageEnd + 1 < data.length;

return { pageStart, pageEnd, dataView, hasPreviousPage, hasNextPage };
}

0 comments on commit 7c7d044

Please sign in to comment.