import { headers } from "next/headers"; import { auth } from "@/lib/auth"; import { type CommentWithUser, getComments } from "@/lib/server/comments"; import { NewCommentForm } from "./comment-form"; import { CommentThread } from "./comment-thread"; import { SignIn } from "./sign-in"; const Comments = async ({ slug }: { slug: string }) => { const session = await auth.api.getSession({ headers: await headers(), }); const comments = await getComments(slug); const commentsByParentId = comments.reduce( (acc, comment) => { const parentId = comment.parentId || "root"; if (!acc[parentId]) { acc[parentId] = []; } acc[parentId].push(comment); return acc; }, {} as Record, ); const rootComments = commentsByParentId.root || []; return ( <> {session ? ( ) : (

Join the discussion by signing in:

)} {rootComments.length > 0 ? (
{rootComments.map((comment: CommentWithUser) => ( ))}
) : (
Be the first to comment!
)} ); }; export { Comments };