import Link from "next/link" import { Eye } from "lucide-react" import type { Chapter } from "@/lib/types" import { formatViews } from "@/lib/utils" interface ChapterListProps { chapters: { id: string novelId: string number: number title: string createdAt: string views: number }[] novelSlug: string currentPage: number totalPages: number totalChapters: number } const generatePagination = (currentPage: number, totalPages: number) => { if (totalPages <= 7) { return Array.from({ length: totalPages }, (_, i) => i + 1) } if (currentPage <= 3) { return [1, 2, 3, 4, '...', totalPages] } if (currentPage >= totalPages - 2) { return [1, '...', totalPages - 3, totalPages - 2, totalPages - 1, totalPages] } return [1, '...', currentPage - 1, currentPage, currentPage + 1, '...', totalPages] } export function ChapterList({ chapters, novelSlug, currentPage, totalPages, totalChapters }: ChapterListProps) { return (
{chapters.map((chapter) => (
Ch. {chapter.number} {chapter.title}
{formatViews(chapter.views)} {chapter.createdAt}
))} {totalPages > 1 && (
Trang {currentPage} / {totalPages} (Tổng {totalChapters} chương)
1 ? `/truyen/${novelSlug}?page=${currentPage - 1}` : '#'} className={`inline-flex h-9 items-center justify-center whitespace-nowrap rounded-md border border-input px-3 text-sm font-medium shadow-sm transition-colors ${currentPage <= 1 ? 'pointer-events-none opacity-50 bg-muted/50 text-muted-foreground' : 'bg-background hover:bg-accent hover:text-accent-foreground'}`} aria-disabled={currentPage <= 1} > Trước {generatePagination(currentPage, totalPages).map((p, i) => (
{p === '...' ? ( ... ) : ( {p} )}
))} = totalPages ? 'pointer-events-none opacity-50 bg-muted/50 text-muted-foreground' : 'bg-background hover:bg-accent hover:text-accent-foreground'}`} aria-disabled={currentPage >= totalPages} > Sau
)}
) }