import { Link } from '@tanstack/react-router' import { cn } from '~/lib/utils' type Props = { page: number itemsPerPage: number totalItems: number limit?: number buildSearch: (nextPage: number) => Record } export function Pagination({ page, itemsPerPage, totalItems, limit = 3, buildSearch }: Props) { const totalPages = Math.max(1, Math.ceil(totalItems / itemsPerPage)) if (totalPages === 1) return null const pages: number[] = [] for (let i = 0; i < totalPages; i++) { if (i < limit || i >= totalPages - limit || (i >= page - limit && i < page + limit - 1)) { pages.push(i + 1) } } const prev = Math.max(1, page - 1) const next = Math.min(totalPages, page + 1) return (
← Previous
{pages.map((p, idx) => (
{idx > 0 && p - 1 !== pages[idx - 1] && } {p}
))}
Next →
) }