'use client' import { useState } from 'react' import useSWR, { mutate } from 'swr' import Link from 'next/link' import { useSession } from 'next-auth/react' import { Button } from '@/components/ui/button' import { Textarea } from '@/components/ui/textarea' import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar' import { formatDistanceToNow } from 'date-fns' import { UserBadges } from './ui/user-badges' import { toast } from 'sonner' import { Trash2 } from 'lucide-react' interface Comment { _id: string userId: string userName: string userImage: string userVerified?: boolean userIsAdmin?: boolean userIsBetaTester?: boolean userIsBugHunter?: boolean text: string isHolder: boolean createdAt: string } const fetcher = (url: string) => fetch(url).then((res) => res.json()) export function Comments({ coinId, creatorId }: { coinId: string, creatorId?: string }) { const { data: session } = useSession() const { data: comments } = useSWR(`/api/comments?coinId=${coinId}`, fetcher) const [text, setText] = useState('') const [loading, setLoading] = useState(false) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!text.trim() || !session) return setLoading(true) try { await fetch('/api/comments', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ coinId, text }), }) setText('') mutate(`/api/comments?coinId=${coinId}`) } catch (error) { console.error('Failed to post comment', error) toast.error('Failed to post comment') } finally { setLoading(false) } } const handleDelete = async (commentId: string) => { try { const res = await fetch(`/api/comments?id=${commentId}`, { method: 'DELETE' }); if (res.ok) { mutate(`/api/comments?coinId=${coinId}`) toast.success('Comment deleted') } else { const data = await res.json() toast.error(data.error || 'Failed to delete') } } catch (e) { toast.error('Failed to delete comment') } } return (

Discussion

{session ? (