import React, { useEffect, useState } from "react" import { Link } from "react-router-dom" import { Heart, MessageCircle, MoreHorizontal } from "lucide-react" import { getComments, postComment } from "../../api/deal/commentDeal" import { useAuth } from "../../context/AuthContext" import { timeAgo } from "../../utils/timeAgo" import type { Comment } from "../../models/comment/Comment" type DealCommentsProps = { dealId: number onRequireLogin: () => void } export default function DealComments({ dealId, onRequireLogin }: DealCommentsProps) { const [comments, setComments] = useState([]) const [newComment, setNewComment] = useState("") const [loading, setLoading] = useState(false) const { isAuthenticated } = useAuth() useEffect(() => { async function loadComments() { try { const data = await getComments(dealId) setComments(data) } catch (err) { console.error("Yorumlar alınamadı:", err) } } loadComments() }, [dealId]) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!newComment.trim()) return if (!isAuthenticated) return onRequireLogin() setLoading(true) try { const added = await postComment(dealId, newComment) setComments((prev) => [added, ...prev]) setNewComment("") } catch (err: any) { console.error(err) alert(err.message || "Sunucu hatası") } finally { setLoading(false) } } return (
{/* Header */}

Yorumlar

{comments.length}
{/* List: only desktop scroll */}
{comments.length > 0 ? (
{comments.map((c) => (
{c.user.username}
{c.user.username} {timeAgo(c.createdAt)}

{c.text}

))}
) : (
Henüz yorum yok
İlk yorumu sen yazabilirsin.
)}
{/* Footer (always visible) */}
{isAuthenticated ? (
setNewComment(e.target.value)} placeholder="Yorum ekle..." className="flex-1 rounded-xl border border-white/10 bg-background px-4 py-3 text-sm text-text placeholder:text-text-muted/70 outline-none focus:ring-2 focus:ring-primary/40" disabled={loading} />
) : ( )}
) }