import * as React from "react"; import { useSession } from "next-auth/react"; import { Button } from "@/components/ui/button"; import { Textarea } from "@/components/ui/textarea"; import { Badge } from "@/components/ui/badge"; import { format } from "date-fns"; import { Comment } from "@/lib/qna/types"; import { Trash2, Pencil, Check, X } from "lucide-react"; interface CommentSectionProps { answerId: string; comments: Comment[]; onAddComment: (content: string) => Promise; onDeleteComment: (commentId: string) => Promise; onUpdateComment?: (commentId: string, content: string) => Promise; } export function CommentSection({ answerId, comments, onAddComment, onDeleteComment, onUpdateComment }: CommentSectionProps) { const { data: session } = useSession(); const [content, setContent] = React.useState(""); const [isSubmitting, setIsSubmitting] = React.useState(false); const [editingId, setEditingId] = React.useState(null); const [editContent, setEditContent] = React.useState(""); const handleSubmit = async () => { if (!content.trim() || !session?.user?.name) return; setIsSubmitting(true); try { await onAddComment(content); setContent(""); } finally { setIsSubmitting(false); } }; const handleEditStart = (comment: Comment) => { setEditingId(comment.id); setEditContent(comment.content); }; const handleEditCancel = () => { setEditingId(null); setEditContent(""); }; const handleEditSave = async (commentId: string) => { if (!editContent.trim() || !onUpdateComment) return; try { await onUpdateComment(commentId, editContent); setEditingId(null); } catch (error) { console.error("댓글 수정 실패:", error); } }; return (

댓글

{comments.length > 0 && ( {comments.length} )}
{/* 댓글 목록 */}
{comments.map((comment) => (
{comment.author} {format(new Date(comment.createdAt), "yyyy.MM.dd HH:mm")}
{editingId === comment.id ? (