"use client"; import { useState } from "react"; interface Comment { id: string; body: string; nickname: string | null; createdByAnonId: string; createdAt: string; } function timeAgo(date: string): string { const seconds = Math.floor( (Date.now() - new Date(date).getTime()) / 1000 ); if (seconds < 60) return "just now"; const minutes = Math.floor(seconds / 60); if (minutes < 60) return `${minutes}m ago`; const hours = Math.floor(minutes / 60); if (hours < 24) return `${hours}h ago`; const days = Math.floor(hours / 24); return `${days}d ago`; } export function CommentSection({ ideaId, initialComments, }: { ideaId: string; initialComments: Comment[]; }) { const [comments, setComments] = useState(initialComments); const [body, setBody] = useState(""); const [submitting, setSubmitting] = useState(false); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); if (!body.trim() || submitting) return; setSubmitting(true); try { const res = await fetch(`/api/ideas/${ideaId}/comments`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ body: body.trim() }), }); if (res.ok) { const d = await res.json(); setComments([d.data, ...comments]); setBody(""); } } finally { setSubmitting(false); } } return (
setBody(e.target.value)} placeholder="Add a comment..." maxLength={2000} className="flex-1 px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500" />
{comments.length === 0 ? (

No comments yet. Be the first!

) : (
{comments.map((comment) => (
{comment.nickname || `anon-${comment.createdByAnonId.slice(0, 6)}`} {timeAgo(comment.createdAt)}

{comment.body}

))}
)}
); }