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 { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "@/components/ui/alert-dialog"; import { format } from "date-fns"; import { Comment } from "@/lib/qna/types"; import { MessageCircle, Trash2, Edit, Check, X, User, Plus } from "lucide-react"; interface ImprovedCommentSectionProps { answerId: string | number; comments: Comment[]; onAddComment: (content: string) => Promise; onDeleteComment: (commentId: string | number) => Promise; onUpdateComment?: (commentId: string | number, content: string) => Promise; } export function ImprovedCommentSection({ answerId, comments, onAddComment, onDeleteComment, onUpdateComment }: ImprovedCommentSectionProps) { 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 [showCommentForm, setShowCommentForm] = React.useState(false); // 댓글 작성 const handleSubmit = async () => { if (!content.trim() || !session?.user) return; setIsSubmitting(true); try { await onAddComment(content); setContent(""); setShowCommentForm(false); } catch (error) { console.error("댓글 작성 실패:", error); } finally { setIsSubmitting(false); } }; // 댓글 수정 시작 const handleEditStart = (comment: Comment) => { setEditingId(comment.id); setEditContent(comment.content); }; // 댓글 수정 취소 const handleEditCancel = () => { setEditingId(null); setEditContent(""); }; // 댓글 수정 저장 const handleEditSave = async (commentId: string | number) => { if (!editContent.trim() || !onUpdateComment) return; try { await onUpdateComment(commentId, editContent); setEditingId(null); setEditContent(""); } catch (error) { console.error("댓글 수정 실패:", error); } }; return (
{/* 헤더 */}
댓글 {comments.length > 0 && ( {comments.length} )}
{session?.user && !showCommentForm && ( )}
{/* 댓글 목록 */} {comments.length > 0 && (
{comments.map((comment) => (
{/* 아바타 */} {/* 댓글 내용 */}
{/* 작성자 정보 */}
{comment.authorName || comment.author} {format(new Date(comment.createdAt), "MM월 dd일 HH:mm")}
{/* 댓글 텍스트 */} {editingId === comment.id ? (