import { ChevronLeft, ChevronRight, ChevronsLeft, ChevronsRight, } from "lucide-react"; import { type Table } from "@tanstack/react-table"; import { Button } from "@/src/components/ui/button"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/src/components/ui/select"; import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture"; import { LoaderCircle } from "lucide-react"; import { Input } from "@/src/components/ui/input"; import { useEffect, useState } from "react"; interface DataTablePaginationProps { table: Table; isLoading: boolean; paginationOptions?: number[]; hideTotalCount?: boolean; canJumpPages?: boolean; // if we need a cursor (last_item_id), we can't jump pages } export function DataTablePagination({ table, isLoading, paginationOptions = [10, 20, 30, 40, 50], hideTotalCount = false, canJumpPages = true, }: DataTablePaginationProps) { const capture = usePostHogClientCapture(); const currentPage = table.getState().pagination.pageIndex + 1; const [inputState, setInputState] = useState(currentPage); useEffect(() => { setInputState(currentPage); }, [currentPage]); const pageCount = table.getPageCount(); const setPageIndex = table.setPageIndex; useEffect(() => { if (currentPage > pageCount && pageCount > 0) { setPageIndex(0); } }, [currentPage, pageCount, setPageIndex]); const handlePageNavigation = (newValue: string) => { if (newValue === "") { table.setPageIndex(0); setInputState(1); return; } // if nan, reset to current page if (isNaN(Number(newValue))) { setInputState(currentPage); return; } const newPageIndex = Number(newValue) - 1; if (newPageIndex < 0 || newPageIndex >= pageCount) { setInputState(currentPage); return; } table.setPageIndex(newPageIndex); setInputState(newPageIndex + 1); }; return (
{/* {table.getFilteredSelectedRowModel().rows.length} of{" "} {table.getFilteredRowModel().rows.length} row(s) selected. */}

Rows

Rows per page

{table.getPageCount() !== -1 ? ( <> Page {canJumpPages && ( { setInputState(e.target.value); }} onKeyDown={(e) => { if (e.key === "Enter") { e.preventDefault(); handlePageNavigation(e.currentTarget.value); } }} onBlur={(e) => { handlePageNavigation(e.target.value); }} className="h-8 appearance-none" style={{ width: `${3 + Math.max(1, pageCount.toString().length)}ch`, }} /> )} {!canJumpPages && {currentPage}} ) : ( `Page ${currentPage}` )} {!hideTotalCount && ( <> {pageCount !== -1 ? ( of {pageCount} ) : ( of{" "} {isLoading ? ( ) : ( 1 )} )} )}
{canJumpPages && ( )} {canJumpPages && ( )}
); }