import React, { useState } from 'react'; import { useAuth } from '../../../contexts/AuthContext'; import { useCreateComment } from '../../../queries/comments'; import { Button } from '../../ui/button'; import { Textarea } from '../../ui/textarea'; import { Card, CardContent } from '../../ui/card'; import { format } from 'date-fns'; import { mk } from 'date-fns/locale'; import type { Comment } from '../../../lib/api'; interface CommentItemProps { comment: Comment; articleId?: string; liveBlogId?: string; depth?: number; } export function CommentItem({ comment, articleId, liveBlogId, depth = 0 }: CommentItemProps) { const { isAuthenticated } = useAuth(); const [showReplyForm, setShowReplyForm] = useState(false); const [replyContent, setReplyContent] = useState(''); const [isSubmittingReply, setIsSubmittingReply] = useState(false); const createCommentMutation = useCreateComment(); const handleSubmitReply = async (e: React.FormEvent) => { e.preventDefault(); if (!replyContent.trim() || !isAuthenticated) return; setIsSubmittingReply(true); try { await createCommentMutation.mutateAsync({ content: replyContent, articleId, liveBlogId, parentCommentId: comment.id, }); setReplyContent(''); setShowReplyForm(false); } catch (error) { console.error('Failed to post reply:', error); } finally { setIsSubmittingReply(false); } }; // Maximum depth to prevent infinite nesting (optional) const maxDepth = 5; const canReply = depth < maxDepth; return (
{comment.content}
{/* Reply button and form */} {isAuthenticated && canReply && (