import Link from "next/link" import { ArrowRight, BookOpen, Sparkles, Flame, Heart, Swords, Building2, Rocket, Crown, Laugh, Search, Shield } from "lucide-react" import { NovelCard } from "@/components/novel-card" import { getNovelStatusBadgeClass } from "@/lib/novel-status" import { prisma } from "@/lib/prisma" const iconMap: Record = { Sparkles: , Flame: , Heart: , Sword: , Building: , Rocket: , Crown: , Laugh: , Search: , Shield: , } export const dynamic = "force-dynamic" function collapseSeriesRows(rows: T[]): T[] { const pickedSeries = new Set() const output: T[] = [] for (const row of rows) { if (!row.seriesId) { output.push(row) continue } if (pickedSeries.has(row.seriesId)) continue pickedSeries.add(row.seriesId) output.push(row) } return output } export default async function HomePage() { let popularNovels: any[] = [] let latestNovels: any[] = [] let topRated: any[] = [] let genres: any[] = [] let featured = null try { popularNovels = await prisma.novel.findMany({ take: 100, select: { id: true, slug: true, title: true, authorName: true, coverColor: true, coverUrl: true, rating: true, views: true, totalChapters: true, status: true, description: true, seriesId: true, }, orderBy: { views: "desc" }, }) popularNovels = collapseSeriesRows(popularNovels).slice(0, 20) latestNovels = await prisma.novel.findMany({ take: 100, select: { id: true, slug: true, title: true, authorName: true, coverColor: true, coverUrl: true, rating: true, views: true, totalChapters: true, status: true, description: true, seriesId: true, }, orderBy: { updatedAt: "desc" }, }) latestNovels = collapseSeriesRows(latestNovels).slice(0, 20) topRated = await prisma.novel.findMany({ take: 20, select: { id: true, slug: true, title: true, authorName: true, coverUrl: true, rating: true, totalChapters: true, seriesId: true, }, orderBy: { rating: "desc" }, }) topRated = collapseSeriesRows(topRated).slice(0, 4) genres = await prisma.genre.findMany({ take: 8, }) featured = popularNovels.length > 0 ? popularNovels[0] : null } catch (error) { console.error("Failed to fetch data for homepage during build/runtime", error) } return (
{/* Hero / Featured Novel */} {featured && (
{featured.title}
Truyện Nổi Bật

{featured.title}

Tác giả: {featured.authorName}

{featured.description}

{featured.totalChapters} chương {featured.status} {featured.rating}
)} {/* Popular Novels */}

Truyện Hot

Xem tất cả
{popularNovels.length > 0 ? popularNovels.map((novel) => ( )) :

Chưa có truyện nào trong hệ thống.

}
{/* Latest Updated */}

Mới Cập Nhật

Xem tất cả
{latestNovels.length > 0 ? latestNovels.map((novel) => ( )) :

Chưa có truyện nào được cập nhật.

}
{/* Two columns: Top Rated + Genres */}
{/* Top Rated */}

Đánh Giá Cao

{topRated.length > 0 ? topRated.map((novel, idx) => ( {idx + 1} {novel.title}

{novel.title}

{novel.authorName} - Ch. {novel.totalChapters}

{novel.rating}
)) :

Chưa có đánh giá.

}
{/* Genres */}

Thể Loại

Xem tất cả
{genres.slice(0, 8).map((genre) => ( {genre.icon && iconMap[genre.icon] ? iconMap[genre.icon] : }

{genre.name}

{genre.description}

))}
) }