'use client' import * as React from 'react' import type { Table } from '@tanstack/react-table' import { ProgressiveBlur } from '@admin/components/custom/progressive-blur' import { Button } from '@admin/components/ui/button' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@admin/components/ui/select' import { cn } from '@admin/utils/shared/cn' const PAGE_SIZE_OPTIONS = [ { value: '10', label: '10' }, { value: '20', label: '20' }, { value: '50', label: '50' }, { value: '100', label: '100' }, { value: 'all', label: 'All' } ] interface DataTablePaginationBaseProps { table: Table pageSize: number setPageSize: (value: number | null) => void className?: string } interface ControlledDataTablePaginationProps { paginationMode: 'controlled' pageIndex: number pageCount: number setPageIndex: (value: number) => void } interface UncontrolledDataTablePaginationProps { paginationMode: 'uncontrolled' pageIndex?: never pageCount?: never setPageIndex?: never } type DataTablePaginationProps = DataTablePaginationBaseProps & (ControlledDataTablePaginationProps | UncontrolledDataTablePaginationProps) export function DataTablePagination(props: DataTablePaginationProps) { const { table, pageSize, setPageSize, className } = props const controlledSetPageIndex = props.paginationMode === 'controlled' ? props.setPageIndex : undefined const resolvedPageIndex = props.paginationMode === 'controlled' ? props.pageIndex : table.getState().pagination.pageIndex const resolvedPageCount = (props.paginationMode === 'controlled' ? props.pageCount : table.getPageCount()) || 1 const canGoPrevious = resolvedPageIndex > 0 const canGoNext = resolvedPageIndex < resolvedPageCount - 1 const updatePageIndex = React.useCallback( (nextPageIndex: number) => { React.startTransition(() => { if (controlledSetPageIndex) { controlledSetPageIndex(nextPageIndex) } else { table.setPageIndex(nextPageIndex) } }) }, [controlledSetPageIndex, table] ) const handlePageSizeChange = React.useCallback( (value: string | null) => { if (value === null) return React.startTransition(() => { if (value === 'all') { setPageSize(-1) } else { setPageSize(Number(value)) } if (controlledSetPageIndex) { controlledSetPageIndex(0) } else { table.setPageIndex(0) } }) }, [controlledSetPageIndex, setPageSize, table] ) return (
Rows per page
Page {resolvedPageIndex + 1} of {resolvedPageCount}
) }