import { X, Trash2, AlertCircle, Flag, Edit2 } from 'lucide-react'
import { useState } from 'react'
import { createPortal } from 'react-dom'
import { reportPost, editPost } from '../utils/api'
import { hapticFeedback, showConfirm } from '../utils/telegram'
import './PostMenu.css'
export default function PostMenu({ post, currentUser, onClose, onDelete, onUpdate, buttonPosition }) {
const [showReportModal, setShowReportModal] = useState(false)
const [showEditModal, setShowEditModal] = useState(false)
const [reportReason, setReportReason] = useState('')
const [editContent, setEditContent] = useState(post.content || '')
const [submitting, setSubmitting] = useState(false)
const isOwnPost = post.author._id === currentUser.id
const isModerator = currentUser.role === 'moderator' || currentUser.role === 'admin'
const handleReport = async () => {
if (!reportReason.trim()) {
alert('Укажите причину жалобы')
return
}
try {
setSubmitting(true)
hapticFeedback('light')
await reportPost(post._id, reportReason)
hapticFeedback('success')
alert('Жалоба отправлена')
onClose()
} catch (error) {
console.error('Ошибка отправки жалобы:', error)
hapticFeedback('error')
alert('Ошибка отправки жалобы')
} finally {
setSubmitting(false)
}
}
if (showReportModal) {
const handleReportOverlayClick = (e) => {
if (e.target === e.currentTarget) {
setShowReportModal(false)
}
}
return createPortal(
e.stopPropagation()}
onTouchStart={(e) => e.stopPropagation()}
onClick={handleReportOverlayClick}
>
e.stopPropagation()}>
Пожаловаться
e.stopPropagation()}>
,
document.body
)
}
const handleEdit = async () => {
if (!editContent.trim()) {
alert('Текст поста не может быть пустым')
return
}
try {
setSubmitting(true)
hapticFeedback('light')
await editPost(post._id, { content: editContent })
hapticFeedback('success')
setShowEditModal(false)
onClose()
if (onUpdate) {
onUpdate()
}
} catch (error) {
console.error('Ошибка редактирования поста:', error)
hapticFeedback('error')
alert('Ошибка редактирования поста')
} finally {
setSubmitting(false)
}
}
const handleOverlayClick = (e) => {
// Закрывать только при клике на overlay, не на контент
if (e.target === e.currentTarget) {
onClose()
}
}
if (showEditModal) {
return createPortal(
e.stopPropagation()}
onTouchStart={(e) => e.stopPropagation()}
onClick={handleOverlayClick}
>
e.stopPropagation()}>
Редактировать пост
,
document.body
)
}
const menuStyle = buttonPosition ? {
position: 'fixed',
top: `${buttonPosition.bottom + 4}px`,
left: `${buttonPosition.left + (buttonPosition.right - buttonPosition.left) / 2}px`,
transform: 'translateX(-50%)',
width: 'auto',
minWidth: '140px'
} : {}
return createPortal(
e.stopPropagation()}
onTouchStart={(e) => e.stopPropagation()}
onClick={handleOverlayClick}
>
e.stopPropagation()}>
e.stopPropagation()}>
{isOwnPost || isModerator ? (
<>
>
) : (
)}
,
document.body
)
}