import { notFound } from "next/navigation" import { BookOpen, Eye, BookMarked, User, Clock, Layers } from "lucide-react" import { formatViews } from "@/lib/utils" import { GenreBadge } from "@/components/genre-badge" import { StarRating } from "@/components/star-rating" import { ChapterList } from "@/components/chapter-list" import { CommentSection } from "@/components/comment-section" import { NovelDetailActions } from "./novel-detail-actions" import { prisma } from "@/lib/prisma" import connectToMongoDB from "@/lib/mongoose" import { Chapter } from "@/lib/models/chapter" export default async function NovelDetailPage({ params, searchParams }: { params: Promise<{ slug: string }>, searchParams: Promise<{ page?: string }> }) { const { slug } = await params const { page } = await searchParams const currentPage = parseInt(page || "1") const limit = 20 const novel = await prisma.novel.findUnique({ where: { slug }, include: { genres: { include: { genre: true } } } }) if (!novel) { notFound() } // Increment view quietly prisma.novel.update({ where: { id: novel.id }, data: { views: { increment: 1 } } }).catch(e => console.error("Error incrementing view:", e)) // Fetch chapters from MongoDB await connectToMongoDB() const skip = (currentPage - 1) * limit const [chapters, totalChapters] = await Promise.all([ Chapter.find({ novelId: novel.id }) .sort({ number: 1 }) .skip(skip) .limit(limit) .select("id novelId number title createdAt views") .lean(), Chapter.countDocuments({ novelId: novel.id }) ]) const totalPages = Math.ceil(totalChapters / limit) // Convert Mongoose documents to plain objects for Server Component const formattedChapters = chapters.map(c => ({ id: c._id.toString(), novelId: c.novelId, number: c.number, title: c.title, createdAt: (c.createdAt as Date).toISOString(), views: c.views || 0, content: "" // We don't fetch content for the list })) const commentsData = await prisma.comment.findMany({ where: { novelId: novel.id, chapterId: null }, include: { user: true }, orderBy: { createdAt: "desc" } }) // Format explicitly as the CommentProp type const comments = commentsData.map(c => ({ id: c.id, userId: c.user.id, username: c.user.name || "User", avatarColor: c.user.image || "bg-primary", novelId: c.novelId, content: c.content, createdAt: c.createdAt.toISOString().split("T")[0] })) const novelGenres = novel.genres.map(ng => ng.genre) || [] return (
{novel.description}