import { ChevronLeft, ChevronRight } from "lucide-react"; import { Button } from "~/components/ui/button"; /** Build a compact page number list with ellipsis, e.g. 1 … 4 5 6 … 20 */ function visiblePages(current: number, total: number): Array { if (total <= 7) { return Array.from({ length: total }, (_, index) => index + 1); } const pages = new Set([1, total, current, current - 1, current + 1]); if (current <= 3) { pages.add(2); pages.add(3); pages.add(4); } if (current >= total - 2) { pages.add(total - 1); pages.add(total - 2); pages.add(total - 3); } const sorted = [...pages].filter((p) => p >= 1 && p <= total).sort((a, b) => a - b); const result: Array = []; for (const page of sorted) { const prev = result[result.length - 1]; if (typeof prev === "number" && page - prev > 1) { result.push("ellipsis"); } result.push(page); } return result; } export function AdminPagination({ page, totalPages, totalCount, loading, hasMore, canGoToPage, onPrev, onNext, onGoToPage, }: { page: number; totalPages: number; totalCount: number; loading?: boolean; hasMore: boolean; canGoToPage: (page: number) => boolean; onPrev: () => void; onNext: () => void; onGoToPage: (page: number) => void; }) { if (totalCount === 0) return null; const pages = visiblePages(page, totalPages); return (
{totalCount} 条,第 {page}/{totalPages} 页
{pages.map((item, index) => item === "ellipsis" ? ( ) : ( ), )}
); }