"use client"; import { MessageSquare, Trash2 } from "lucide-react"; import { useEffect, useState, useTransition } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { addComment, deleteComment, listComments, type CommentDto } from "@/modules/_core/comments"; type Props = { entityType: string; entityId: string; currentUserId: string; compact?: boolean; }; export function EntityComments({ entityType, entityId, currentUserId, compact = false }: Props) { const [open, setOpen] = useState(!compact); const [comments, setComments] = useState([]); const [draft, setDraft] = useState(""); const [isPending, startTransition] = useTransition(); useEffect(() => { if (!open) return; listComments(entityType, entityId) .then(setComments) .catch(() => setComments([])); }, [entityType, entityId, open]); function submitComment() { const body = draft.trim(); if (!body) return; startTransition(async () => { const created = await addComment({ entityType, entityId, body }); setComments((current) => [...current, created]); setDraft(""); }); } function removeComment(id: string) { startTransition(async () => { await deleteComment({ id }); setComments((current) => current.filter((comment) => comment.id !== id)); }); } return (
{compact ? ( ) : (
Comments
)} {open && (
{comments.length === 0 ? (

No comments yet.

) : (
    {comments.map((comment) => (
  • {comment.authorName ?? "Someone"} · {formatCommentTime(comment.createdAt)}

    {comment.body}

    {comment.authorId === currentUserId ? ( ) : null}
  • ))}
)}
setDraft(e.target.value)} placeholder="Add a comment…" disabled={isPending} onKeyDown={(e) => { if (e.key === "Enter") { e.preventDefault(); submitComment(); } }} />
)}
); } function formatCommentTime(iso: string): string { return new Date(iso).toLocaleString(undefined, { month: "short", day: "numeric", hour: "numeric", minute: "2-digit", }); }