"use client"; import { useEffect, useRef } from "react"; import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react"; import { useTranslation } from "react-i18next"; import { Button } from "@/components/ui/button"; import { Pagination, PaginationContent, PaginationEllipsis, PaginationItem } from "@/components/ui/pagination"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; interface TablePaginationProps { page: number; // 1-based pageSize: number; total: number; // total (after filtering) totalApproximate?: boolean; totalCapped?: boolean; hasMore?: boolean; onPageChange: (p: number) => void; onPageSizeChange: (s: number) => void; pageSizeOptions?: number[]; } // pageWindows returns the sequence of page numbers / ellipsis to render. function pageWindows(page: number, total: number): (number | "...")[] { if (total <= 7) return Array.from({ length: total }, (_, i) => i + 1); const out: (number | "...")[] = [1]; if (page > 3) out.push("..."); const lo = Math.max(2, page - 1); const hi = Math.min(total - 1, page + 1); for (let i = lo; i <= hi; i++) out.push(i); if (page < total - 2) out.push("..."); out.push(total); return out; } export function TablePagination({ page, pageSize, total, totalApproximate = false, totalCapped = false, hasMore = false, onPageChange, onPageSizeChange, pageSizeOptions = [10, 20, 50], }: TablePaginationProps) { const { t } = useTranslation(); const totalPages = Math.max(1, Math.ceil(total / pageSize), hasMore ? page + 1 : 0); const safePage = Math.min(Math.max(1, page), totalPages); const hasItems = total > 0 || hasMore; const from = hasItems ? (safePage - 1) * pageSize + 1 : 0; const to = hasMore ? safePage * pageSize : Math.min(safePage * pageSize, total); let totalLabel: string | number = total; if (totalCapped) totalLabel = `${total}+`; else if (totalApproximate) totalLabel = `~${total}`; const onPageChangeRef = useRef(onPageChange); onPageChangeRef.current = onPageChange; useEffect(() => { if (page !== safePage) onPageChangeRef.current(safePage); }, [page, safePage]); return (