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.description}
)}