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 (
0 ? 'ml-8 mt-4 border-l-2 border-border pl-4' : ''}>
{comment.user?.username || 'Анонимен корисник'}
{format(new Date(comment.createdAt), 'dd MMMM yyyy, HH:mm', { locale: mk })}
{comment.user?.role === 'admin' && ( Администратор )}

{comment.content}

{/* Reply button and form */} {isAuthenticated && canReply && (
{!showReplyForm ? ( ) : (