import { prisma } from "@/lib/prisma"; import { notFound } from "next/navigation"; export const dynamic = "force-dynamic"; interface Props { params: { category: string }; searchParams: { sort?: string; page?: string }; } export default async function CategoryPage({ params, searchParams }: Props) { const category = await prisma.forumCategory.findUnique({ where: { slug: params.category }, }); if (!category) notFound(); const childCategories = await prisma.forumCategory.findMany({ where: { parentId: category.id }, select: { id: true, name: true, slug: true }, }); const sort = searchParams.sort || "latest"; const page = parseInt(searchParams.page || "1", 10); const pageSize = 20; const orderBy: any = {}; if (sort === "latest") orderBy.createdAt = "desc"; else if (sort === "hot") orderBy.viewCount = "desc"; else if (sort === "active") orderBy.lastReplyAt = "desc"; else orderBy.createdAt = "desc"; const [topics, totalCount] = await Promise.all([ prisma.forumTopic.findMany({ where: { categoryId: category.id }, include: { category: { select: { name: true, slug: true, icon: true } }, user: { select: { id: true, name: true, email: true, avatarUrl: true, level: true, points: true, isBot: true } }, _count: { select: { posts: true } }, }, orderBy: [{ isPinned: "desc" }, orderBy], skip: (page - 1) * pageSize, take: pageSize, }), prisma.forumTopic.count({ where: { categoryId: category.id } }), ]); return (
← 返回社区

{category.name}

{category.description && (

{category.description}

)}
{["latest", "hot", "active"].map((s) => ( {s === "latest" ? "最新" : s === "hot" ? "热门" : "活跃"} ))}
{childCategories.length > 0 && (
全部 {childCategories.map((child) => ( {child.name} ))}
)}
{topics.map((topic) => { const initial = topic.user.name?.charAt(0).toUpperCase() || "?"; return ( {topic.user.avatarUrl ? ( ) : (
{initial}
)}
{topic.isPinned && ( 置顶 )} {topic.isLocked && ( 已锁定 )}

{topic.title}

{topic.user.name || topic.user.email?.split("@")[0]}{topic.user.isBot && AI} {topic.createdAt?.toLocaleDateString("zh-CN")} {topic._count.posts}回复 {topic.viewCount}浏览
); })}
{totalCount > pageSize && (
{Array.from({ length: Math.ceil(totalCount / pageSize) }, (_, i) => i + 1).map((p) => ( {p} ))}
)}
); }