From 80a5b641ce9956d9841bb033a2375b26ef55d1b0 Mon Sep 17 00:00:00 2001 From: SlimusMinus Date: Mon, 19 Oct 2026 01:24:03 +0300 Subject: [PATCH] fixed empty advertising slot --- src/components/AdSlot.jsx | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/components/AdSlot.jsx b/src/components/AdSlot.jsx index 94e898c..e77e72a 100644 --- a/src/components/AdSlot.jsx +++ b/src/components/AdSlot.jsx @@ -8,6 +8,8 @@ export default function AdSlot() { const slotRef = useRef(null); const [ad, setAd] = useState(null); const [closed, setClosed] = useState(false); + // Показывать нечего (204 или ошибка) — слот убирается из ленты целиком + const [empty, setEmpty] = useState(false); useEffect(() => { if (closed) return; @@ -25,18 +27,23 @@ export default function AdSlot() { headers: token ? { Authorization: `Bearer ${token}` } : undefined, }) .then((res) => { - // 204 No Content — свободных объявлений нет, оставляем пустой слот + // 204 No Content — свободных объявлений нет if (res.status === 204) return null; if (!res.ok) throw new Error(`Error ${res.status}`); return res.json(); }) .then((data) => { - if (!cancelled && data) { - if (data.mediaUrl) data.mediaUrl = normalizeMediaUrl(data.mediaUrl); - setAd(data); + if (cancelled) return; + if (!data) { + setEmpty(true); + return; } + if (data.mediaUrl) data.mediaUrl = normalizeMediaUrl(data.mediaUrl); + setAd(data); }) - .catch(() => {}); + .catch(() => { + if (!cancelled) setEmpty(true); + }); }, { rootMargin: '200px' } ); @@ -50,7 +57,9 @@ export default function AdSlot() { const handleClose = () => setClosed(true); - if (closed) return null; + // Слот грузится заранее (rootMargin 200px), т.е. ещё ниже экрана — + // его исчезновение не сдвигает видимую часть ленты + if (closed || empty) return null; if (ad) { return (