requestsolution / components /CommentSection.tsx
Jacksuyu's picture
Claude Opus 4.6
Initial release: Community Demand Board V0.1
8e944c6
Raw History Blame Contribute Delete
3.03 kB
"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 (
<div className="space-y-4">
<form onSubmit={handleSubmit} className="flex gap-2">
<input
type="text"
value={body}
onChange={(e) => 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"
/>
<button
type="submit"
disabled={!body.trim() || submitting}
className="px-4 py-2 bg-indigo-600 text-white rounded-lg text-sm font-medium hover:bg-indigo-700 disabled:opacity-50"
>
Post
</button>
</form>
{comments.length === 0 ? (
<p className="text-sm text-gray-400 py-4">
No comments yet. Be the first!
</p>
) : (
<div className="space-y-3">
{comments.map((comment) => (
<div
key={comment.id}
className="bg-white border border-gray-200 rounded-lg p-3"
>
<div className="flex items-center gap-2 mb-1">
<span className="text-xs font-medium text-gray-700">
{comment.nickname ||
`anon-${comment.createdByAnonId.slice(0, 6)}`}
</span>
<span className="text-xs text-gray-400">
{timeAgo(comment.createdAt)}
</span>
</div>
<p className="text-sm text-gray-700">{comment.body}</p>
</div>
))}
</div>
)}
</div>
);
}