"use client" import { useState, useEffect } from "react" import { Button } from "@/components/ui/button" import { Textarea } from "@/components/ui/textarea" import { Card, CardContent } from "@/components/ui/card" import { formatDistanceToNow } from "date-fns" import { getApiUrl } from "@/lib/api-config" import { MessageCircle, CornerDownRight, Heart } from "lucide-react" import { cn } from "@/lib/utils" interface Comment { id: number user_id: number content: string created_at: string parent_id: number | null } interface CommentSectionProps { entityType: "show" | "venue" | "song" | "tour" | "performance" | "year" entityId: number } export function CommentSection({ entityType, entityId }: CommentSectionProps) { const [comments, setComments] = useState([]) const [newComment, setNewComment] = useState("") const [loading, setLoading] = useState(false) useEffect(() => { fetchComments() }, [entityType, entityId]) const fetchComments = async () => { try { const res = await fetch(`${getApiUrl()}/social/comments?${entityType}_id=${entityId}&limit=100`) if (res.ok) { const data = await res.json() setComments(data) } } catch (err) { console.error("Failed to fetch comments", err) } } const handleSubmit = async (e: React.FormEvent, parentId: number | null = null, content: string = newComment) => { e.preventDefault() if (!content.trim()) return const token = localStorage.getItem("token") if (!token) { alert("Please log in to comment.") return } setLoading(true) try { const body: any = { content: content, parent_id: parentId } body[`${entityType}_id`] = entityId const res = await fetch(`${getApiUrl()}/social/comments`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}` }, body: JSON.stringify(body) }) if (!res.ok) throw new Error("Failed to post comment") const savedComment = await res.json() setComments(prev => [savedComment, ...prev]) setNewComment("") } catch (err) { console.error(err) alert("Error posting comment") } finally { setLoading(false) } } // Tree builder const rootComments = comments.filter(c => c.parent_id === null) const getReplies = (parentId: number) => comments.filter(c => c.parent_id === parentId).sort((a, b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime()) return (

Discussion

{/* Root Post Form */}
handleSubmit(e)} className="space-y-4">