'use client'; import { useQuery } from '@tanstack/react-query'; import { Loader2, MessageCircle } from 'lucide-react'; import { getComments } from '@/api/comments'; import { Comment } from '@/types'; import CommentForm from './CommentForm'; import CommentItem from './CommentItem'; interface CommentListProps { postSlug: string; } export default function CommentList({ postSlug }: CommentListProps) { const { data: comments, isLoading, error } = useQuery({ queryKey: ['comments', postSlug], queryFn: () => getComments(postSlug), }); const countComments = (list: Comment[]): number => { if (!list) return 0; return list.reduce((acc, curr) => acc + 1 + countComments(curr.children), 0); }; const totalCount = comments ? countComments(comments) : 0; if (isLoading) { return (
); } if (error) { return
댓글을 불러오는 데 실패했습니다.
; } return (

댓글 {totalCount}

{comments && comments.length > 0 ? ( comments.map((comment) => ( )) ) : (
아직 댓글이 없습니다. 첫 번째 댓글을 남겨보세요.
)}
); }