import { useState } from 'react' import { X, Send } from 'lucide-react' import { commentPost } from '../utils/api' import { hapticFeedback } from '../utils/telegram' import { decodeHtmlEntities } from '../utils/htmlEntities' import './CommentsModal.css' export default function CommentsModal({ post, onClose, onUpdate }) { const [comment, setComment] = useState('') const [loading, setLoading] = useState(false) const [comments, setComments] = useState(post.comments || []) const handleSubmit = async () => { if (!comment.trim()) return try { setLoading(true) hapticFeedback('light') const result = await commentPost(post._id, comment) setComments(result.comments) setComment('') hapticFeedback('success') onUpdate() } catch (error) { console.error('Ошибка добавления комментария:', error) hapticFeedback('error') } finally { setLoading(false) } } const formatDate = (date) => { const d = new Date(date) const now = new Date() const diff = Math.floor((now - d) / 1000) // секунды if (diff < 60) return 'только что' if (diff < 3600) return `${Math.floor(diff / 60)} мин` if (diff < 86400) return `${Math.floor(diff / 3600)} ч` return d.toLocaleDateString('ru-RU', { day: 'numeric', month: 'short' }) } const handleOverlayClick = (e) => { // Закрывать только при клике на overlay, не на контент if (e.target === e.currentTarget) { onClose() } } return (
e.stopPropagation()} onTouchStart={(e) => e.stopPropagation()} onClick={handleOverlayClick} >
e.stopPropagation()}> {/* Хедер */}

Комментарии

{/* Пост */}
{post.author?.username { e.target.src = '/default-avatar.png' }} />
{post.author?.firstName || ''} {post.author?.lastName || ''} {!post.author?.firstName && !post.author?.lastName && 'Пользователь'}
@{post.author?.username || post.author?.firstName || 'user'}
{post.content && (
{decodeHtmlEntities(post.content)}
)} {((post.images && post.images.length > 0) || post.imageUrl) && (
Post
)}
{/* Список комментариев */}
{comments.length === 0 ? (

Пока нет комментариев

Будьте первым!
) : ( comments .filter(c => c.author) // Фильтруем комментарии без автора .map((c, index) => (
{c.author?.username { e.target.src = '/default-avatar.png' }} />
{c.author?.firstName || ''} {c.author?.lastName || ''} {!c.author?.firstName && !c.author?.lastName && 'Пользователь'} {formatDate(c.createdAt)}

{decodeHtmlEntities(c.content)}

)) )}
{/* Форма добавления комментария */}
setComment(e.target.value)} onKeyPress={e => e.key === 'Enter' && handleSubmit()} maxLength={500} />
) }