mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-06-27 17:05:42 -04:00
65 lines
2.0 KiB
TypeScript
65 lines
2.0 KiB
TypeScript
import { headers } from "next/headers";
|
|
import Form from "./comment-form";
|
|
import Thread from "./comment-thread";
|
|
import SignIn from "./sign-in";
|
|
import { auth } from "@/lib/auth";
|
|
import { getComments, type CommentWithUser } from "@/lib/server/comments";
|
|
|
|
const Comments = async ({ slug, closed = false }: { slug: string; closed?: boolean }) => {
|
|
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<string, CommentWithUser[]>
|
|
);
|
|
|
|
const rootComments = commentsByParentId["root"] || [];
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
{closed ? (
|
|
<div className="bg-muted/40 flex min-h-32 items-center justify-center rounded-lg p-6">
|
|
<p className="text-center font-medium">Comments are closed for this post.</p>
|
|
</div>
|
|
) : !session ? (
|
|
<div className="bg-muted/40 flex flex-col items-center justify-center rounded-lg p-6">
|
|
<p className="mb-4 text-center font-medium">Join the discussion by signing in:</p>
|
|
<SignIn callbackPath={`/${slug}#comments`} />
|
|
</div>
|
|
) : (
|
|
<Form slug={slug} />
|
|
)}
|
|
|
|
{!closed && rootComments.length === 0 ? (
|
|
<div className="text-foreground/80 py-8 text-center text-lg font-medium tracking-tight">
|
|
Be the first to comment!
|
|
</div>
|
|
) : (
|
|
<div className="space-y-6">
|
|
{rootComments.map((comment: CommentWithUser) => (
|
|
<Thread
|
|
key={comment.id}
|
|
comment={comment}
|
|
replies={commentsByParentId[comment.id] || []}
|
|
allComments={commentsByParentId}
|
|
/>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default Comments;
|