{text}
))}import Link from "next/link" import { notFound } from "next/navigation" import { ChevronLeft, ChevronRight, List } from "lucide-react" import { Button } from "@/components/ui/button" import { CommentSection } from "@/components/comment-section" import { ReaderFAB } from "@/components/reader-fab" import { ChapterReaderProgress } from "./chapter-reader-progress" import { prisma } from "@/lib/prisma" import connectToMongoDB from "@/lib/mongoose" import { Chapter as ChapterModel } from "@/lib/models/chapter" export const dynamic = "force-dynamic" export default async function ChapterReaderPage({ params }: { params: Promise<{ slug: string; chapterId: string }> }) { const { slug, chapterId } = await params const chapterNumber = parseInt(chapterId, 10) if (isNaN(chapterNumber)) { notFound() } const novel = await prisma.novel.findUnique({ where: { slug } }) if (!novel) { notFound() } await connectToMongoDB() const chapter = await ChapterModel.findOne({ novelId: novel.id, number: chapterNumber }).lean() if (!chapter) { notFound() } const maxChapter = await ChapterModel.countDocuments({ novelId: novel.id }) const commentsData = await prisma.comment.findMany({ where: { novelId: novel.id, chapterId: chapter._id.toString() }, include: { user: true }, orderBy: { createdAt: "desc" } }) 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, chapterId: c.chapterId || undefined, content: c.content, createdAt: c.createdAt.toISOString().split("T")[0] })) // Increment chapter views quietly (fire and forget to not block render) ChapterModel.updateOne({ _id: chapter._id }, { $inc: { views: 1 } }) .catch(e => console.error("Error updating chapter views:", e)) const hasPrev = chapterNumber > 1 const hasNext = chapterNumber < maxChapter // Extract paragraphs for TTS const paragraphs = chapter.content.split("\n").map((p: string) => p.trim()).filter(Boolean) const chapterLabel = (chapter as any).volumeChapterNumber ? `Chương ${(chapter as any).volumeChapterNumber}` : `Chương ${chapter.number}` const volumeLabel = (chapter as any).volumeTitle || ((chapter as any).volumeNumber ? `Quyển ${(chapter as any).volumeNumber}` : null) return (
{text}
))}