import { Pagination, PaginationContent, PaginationItem, PaginationLink, Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@repo/shadcn-ui/components"; import { cn } from "@repo/shadcn-ui/lib/utils"; import type { Table } from "@tanstack/react-table"; import { ChevronLeftIcon, ChevronRightIcon, ChevronsLeftIcon, ChevronsRightIcon, } from "lucide-react"; import { useTranslation } from "../../locales/i18n.ts"; import type { DataTableMeta } from "./data-table.tsx"; interface DataTablePaginationProps { table: Table; className?: string; } export function DataTablePagination({ table, className }: DataTablePaginationProps) { const { t } = useTranslation(); const { pageIndex: rawIndex, pageSize: rawSize } = table.getState().pagination; const meta = table.options.meta as DataTableMeta; const totalRows = meta?.totalItems ?? table.getFilteredRowModel().rows.length; const pageIndex = Number.isFinite(rawIndex) && rawIndex >= 0 ? rawIndex : 0; const pageSize = Number.isFinite(rawSize) && rawSize > 0 ? rawSize : 10; const pageCount = Math.max(1, Math.ceil(totalRows / pageSize)); const hasSelected = table.getFilteredSelectedRowModel().rows.length > 0; const start = totalRows > 0 ? pageIndex * pageSize + 1 : 0; const end = totalRows > 0 ? Math.min(start + pageSize - 1, totalRows) : 0; const gotoPage = (index: number) => { const nextIndex = Math.max(0, Math.min(index, pageCount - 1)); table.setPageIndex(nextIndex); }; const handlePageSizeChange = (size: string) => { table.setPageSize(Number(size)); }; return (
{t("components.datatable.pagination.showing_range", { start, end, total: totalRows })} {hasSelected && ( {t("components.datatable.pagination.rows_selected", { count: table.getFilteredSelectedRowModel().rows.length, total: table.getFilteredRowModel().rows.length, })} )}
{t("components.datatable.pagination.rows_per_page")}
0} onClick={() => pageIndex > 0 && gotoPage(0)} size="sm" > 0} onClick={() => pageIndex > 0 && gotoPage(pageIndex - 1)} size="sm" > {t("components.datatable.pagination.page_of", { page: pageIndex + 1, of: pageCount, })} pageIndex < pageCount - 1 && gotoPage(pageIndex + 1)} size="sm" > pageIndex < pageCount - 1 && gotoPage(pageCount - 1)} size="sm" >
); }