From 46f546c2938797daef14729f0a14d84f0a933ef0 Mon Sep 17 00:00:00 2001 From: SlimusMinus Date: Tue, 13 Oct 2026 01:31:36 +0300 Subject: [PATCH] hide "write" button on own ads, log chat creation errors The feed card showed the contact button on the user's own ads (isOwner is false in the main feed), and the backend rejects a chat with yourself with 400. The button is now hidden when ad.userId is the current user, and createOrGetChat logs the backend error message. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/hooks/useChats.js | 8 +++++++- src/pages/PostOnFeed.jsx | 15 ++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/hooks/useChats.js b/src/hooks/useChats.js index 996d7c2..236c6f5 100644 --- a/src/hooks/useChats.js +++ b/src/hooks/useChats.js @@ -47,7 +47,13 @@ export function useChats() { if (postId) url.searchParams.set("postId", postId); const res = await apiFetch(url, { method: "POST" }); - if (!res.ok) throw new Error(t('chats.createError')); + if (!res.ok) { + // Причину отказа (чат с собой, чужое объявление и т.п.) пишем в консоль, + // иначе 400 выглядит как необъяснимый сбой + const body = await res.json().catch(() => null); + console.warn("createOrGetChat failed:", res.status, body?.message); + throw new Error(t('chats.createError')); + } const chat = await res.json(); // Объявление чата берём из ответа бэкенда: если он вернул существующий чат по // другому объявлению, postInfo с карточки привязал бы к нему чужую картинку. diff --git a/src/pages/PostOnFeed.jsx b/src/pages/PostOnFeed.jsx index 08c77cd..9cf29c7 100644 --- a/src/pages/PostOnFeed.jsx +++ b/src/pages/PostOnFeed.jsx @@ -25,7 +25,10 @@ export default function PostOnFeed({ ad, onEdit, onDelete, onMarkFound, onReopen const rewardAnchorRef = useRef(null); const descRef = useRef(null); const imgTouchStartRef = useRef(null); - const { isAuthenticated } = useAuthContext(); + const { isAuthenticated, user } = useAuthContext(); + // Своё объявление: писать самому себе нельзя (бэкенд отвечает 400), кнопку не показываем. + // isOwner тут не подходит — в общей ленте он false и для своих объявлений. + const isOwnAd = user?.id != null && ad.userId != null && String(ad.userId) === String(user.id); const { contactSeller, error: contactError, clearError } = useContactSeller(ad); useEffect(() => { @@ -301,10 +304,12 @@ export default function PostOnFeed({ ad, onEdit, onDelete, onMarkFound, onReopen )}
- + {!isOwnAd && ( + + )}
{onMarkFound && isOwner && !isClosed && (