import Link from "next/link" import { ChevronLeft } from "lucide-react" import { NovelCard } from "@/components/novel-card" import { notFound } from "next/navigation" import { readerApiFetch, readerApiFetchNullable } from "@/lib/server-api" export const dynamic = "force-dynamic" const PAGE_SIZE = 24 type GenreItem = { id: string name: string slug: string description: string | null } type BrowseNovel = { id: string slug: string title: string authorName: string coverColor: string | null coverUrl: string | null rating: number views: number totalChapters: number status: string } type BrowseResponse = { items: BrowseNovel[] totalCount: number totalPages: number currentPage: number } export default async function GenreDetailPage({ params, searchParams, }: { params: Promise<{ slug: string }> searchParams: Promise<{ [key: string]: string | undefined }> }) { const { slug } = await params const resolved = await searchParams const sort = resolved.sort || "latest" const requestedPage = Math.max(1, Number(resolved.page || "1") || 1) const genre = await readerApiFetchNullable(`/api/genres/${encodeURIComponent(slug)}`) if (!genre) { notFound() } const browse = await readerApiFetch( `/api/novels/browse?genre=${encodeURIComponent(slug)}&sort=${sort}&page=${requestedPage}&limit=${PAGE_SIZE}` ) const totalPages = Math.max(1, browse.totalPages || 1) const currentPage = Math.min(requestedPage, totalPages) const pageRangeStart = Math.max(1, currentPage - 2) const pageRangeEnd = Math.min(totalPages, currentPage + 2) const pageNumbers = Array.from( { length: pageRangeEnd - pageRangeStart + 1 }, (_, i) => pageRangeStart + i ) const buildPageHref = (page: number) => { const p = new URLSearchParams() if (sort !== "latest") p.set("sort", sort) p.set("page", String(page)) return `/the-loai/${slug}?${p.toString()}` } return (
Thể Loại

{genre.name}

{genre.description && (

{genre.description}

)}

{browse.totalCount} truyện {totalPages > 1 && `(Trang ${currentPage}/${totalPages})`}

{browse.items.length === 0 ? (

Chưa có truyện nào

Thể loại này chưa có truyện, hãy quay lại sau.

) : (
{browse.items.map((novel) => ( ))}
)} {totalPages > 1 && (
Trước {pageNumbers.map((page) => ( {page} ))} = totalPages ? "pointer-events-none opacity-50" : "hover:bg-muted"}`} > Sau
)}
) }