import React from 'react'; import type { RowData, Table as TanStackTable } from '@tanstack/react-table'; import { ChevronLeft, ChevronRight, ChevronsLeft, ChevronsRight } from 'lucide-react'; import Button from '@/components/elements/Button'; import { cn } from '@/lib/cn'; import { useTranslation } from '@/sdk/localization'; export type PaginationRange = { start: number; end: number; count: number; }; interface Props { table: TanStackTable; total: number; count: number; itemLabel?: string; summary?: (range: PaginationRange) => string; className?: string; } const valuePattern = /(.*?)<\/value>/g; const plainSummary = (summary: string) => summary.replace(valuePattern, '$1'); const highlightedSummary = (summary: string) => React.Children.toArray( summary .split(valuePattern) .map((part, index) => index % 2 === 1 ? {part} : part ) ); const PageButton = ({ label, children, ...props }: React.ComponentProps & { label: string }) => ( {children} ); export default function DataTablePagination({ table, total, count, itemLabel, summary, className, }: Props) { const { t } = useTranslation(); const { pageIndex, pageSize } = table.getState().pagination; const pageCount = table.getPageCount(); const start = total === 0 ? 0 : pageIndex * pageSize + 1; const end = total === 0 ? 0 : Math.min(total, start + count - 1); const showPager = pageCount > 1 || pageIndex > 0; if (total === 0) { return null; } const range = { start, end, count: total }; const pageText = t('Page {{page}} of {{pages}}', { page: pageIndex + 1, pages: Math.max(pageCount, 1), }); const defaultSummary = (shown: PaginationRange): string => itemLabel ? t('Showing {{start}} to {{end}} of {{total}} {{items}}', { start: shown.start, end: shown.end, total: shown.count, items: itemLabel, }) : t('Showing {{start}} to {{end}} of {{count}} items', shown); const text = (summary ?? defaultSummary)(range); return (

{highlightedSummary(text)}

{showPager ? (
table.setPageIndex(0)} > table.previousPage()} > {highlightedSummary(pageText)} table.nextPage()} > table.setPageIndex(Math.max(pageCount - 1, 0))} >
) : null}
); }