import { useState, useEffect, useRef } from 'react' import { X, Send } from 'lucide-react' import { commentPost } from '../utils/api' import { hapticFeedback, getTelegramApp } from '../utils/telegram' 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 modalRef = useRef(null) useEffect(() => { const tg = getTelegramApp() if (tg) { // Фиксировать высоту при изменении viewport (клавиатура) const handleViewportChange = () => { if (modalRef.current) { const currentHeight = modalRef.current.offsetHeight modalRef.current.style.height = `${currentHeight}px` } } tg.onEvent('viewportChanged', handleViewportChange) return () => { if (tg.offEvent) { tg.offEvent('viewportChanged', handleViewportChange) } } } }, []) 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 (
Пока нет комментариев
Будьте первым!{c.content}