"use client" import { useState } from "react" import { Send } from "lucide-react" import { Button } from "@/components/ui/button" import { Textarea } from "@/components/ui/textarea" import { useAuth } from "@/lib/auth-context" import type { Comment } from "@/lib/types" import Link from "next/link" interface CommentSectionProps { comments: Comment[] novelId: string chapterId?: string } export function CommentSection({ comments: initialComments, novelId, chapterId }: CommentSectionProps) { const { user } = useAuth() const [comments, setComments] = useState(initialComments) const [content, setContent] = useState("") const [isSubmitting, setIsSubmitting] = useState(false) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!content.trim() || !user || isSubmitting) return setIsSubmitting(true) try { const res = await fetch(`/api/truyen/${novelId}/comments`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ content: content.trim(), chapterId }) }) if (res.ok) { const newComment = await res.json() setComments((prev) => [newComment, ...prev]) setContent("") } } catch (e) { console.error(e) } finally { setIsSubmitting(false) } } return (

Bình luận ({comments.length})

{/* Add comment form */} {user ? (
{user.avatarUrl ? ( {user.username} ) : (
{user.username.charAt(0).toUpperCase()}
)}