import { useState, useEffect } from "react"; import { useTranslation } from "react-i18next"; import { API_URL } from "../config/authConstants"; import { apiFetch } from "../api/apiClient"; import { useAuthContext } from "../context/AuthContext"; import PostOnFeed from "../pages/PostOnFeed"; import CloseIcon from "./icons/CloseIcon"; /** * Модалка с полным объявлением, догружаемым по id. * * compact — компактная карточка (без адреса и даты), как при открытии с карты; * hideContact — без кнопки «Написать» (модалка открыта из самого чата); * fallbackTitle — что показать над сообщением, если объявление удалено или скрыто; * footer(post) — дополнительные действия под карточкой. * * @param {{ * postId: any, onClose: () => void, * onLocationClick?: (ad: any) => void, onAuthRequired?: () => void, * compact?: boolean, hideContact?: boolean, * fallbackTitle?: string, footer?: (post: any) => any, * }} props */ export default function PostDetailModal({ postId, onClose, onLocationClick, onAuthRequired, compact = true, hideContact = false, fallbackTitle, footer, }) { const { t } = useTranslation(); const { user } = useAuthContext(); const [post, setPost] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { if (!postId) return; let cancelled = false; const fetchPost = async () => { setLoading(true); setError(null); try { const res = await apiFetch(`${API_URL}/api/v1/posts/${encodeURIComponent(postId)}`); // 404 — объявление удалено или снято с публикации (модерация, отклонено) if (res.status === 404) throw new Error("notFound"); if (!res.ok) throw new Error("Failed to load post"); const data = await res.json(); if (!cancelled) { setPost({ ...data, type: data.type ? data.type.toLowerCase() : "loss" }); } } catch (e) { if (!cancelled) setError(e.message); } finally { if (!cancelled) setLoading(false); } }; fetchPost(); return () => { cancelled = true; }; }, [postId]); const isOwner = post && user && (post.authorId === user.id || post.userId === user.id); return (