"use client"; import { useEffect, useRef } from "react"; import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react"; 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) 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, onPageChange, onPageSizeChange, pageSizeOptions = [10, 20, 50], }: TablePaginationProps) { const totalPages = Math.max(1, Math.ceil(total / pageSize)); const safePage = Math.min(Math.max(1, page), totalPages); const from = total === 0 ? 0 : (safePage - 1) * pageSize + 1; const to = Math.min(safePage * pageSize, total); const onPageChangeRef = useRef(onPageChange); onPageChangeRef.current = onPageChange; useEffect(() => { if (page !== safePage) onPageChangeRef.current(safePage); }, [page, safePage]); return (