Merge pull request #102 from SlimusMinus/fix-advert_slot
All checks were successful
deploy-frontend / deploy (push) Successful in 44s

fixed empty advertising slot
This commit is contained in:
SlimusMinus
2026-10-19 01:24:38 +03:00
committed by GitHub

View File

@@ -8,6 +8,8 @@ export default function AdSlot() {
const slotRef = useRef(null); const slotRef = useRef(null);
const [ad, setAd] = useState(null); const [ad, setAd] = useState(null);
const [closed, setClosed] = useState(false); const [closed, setClosed] = useState(false);
// Показывать нечего (204 или ошибка) — слот убирается из ленты целиком
const [empty, setEmpty] = useState(false);
useEffect(() => { useEffect(() => {
if (closed) return; if (closed) return;
@@ -25,18 +27,23 @@ export default function AdSlot() {
headers: token ? { Authorization: `Bearer ${token}` } : undefined, headers: token ? { Authorization: `Bearer ${token}` } : undefined,
}) })
.then((res) => { .then((res) => {
// 204 No Content — свободных объявлений нет, оставляем пустой слот // 204 No Content — свободных объявлений нет
if (res.status === 204) return null; if (res.status === 204) return null;
if (!res.ok) throw new Error(`Error ${res.status}`); if (!res.ok) throw new Error(`Error ${res.status}`);
return res.json(); return res.json();
}) })
.then((data) => { .then((data) => {
if (!cancelled && data) { if (cancelled) return;
if (data.mediaUrl) data.mediaUrl = normalizeMediaUrl(data.mediaUrl); if (!data) {
setAd(data); setEmpty(true);
return;
} }
if (data.mediaUrl) data.mediaUrl = normalizeMediaUrl(data.mediaUrl);
setAd(data);
}) })
.catch(() => {}); .catch(() => {
if (!cancelled) setEmpty(true);
});
}, },
{ rootMargin: '200px' } { rootMargin: '200px' }
); );
@@ -50,7 +57,9 @@ export default function AdSlot() {
const handleClose = () => setClosed(true); const handleClose = () => setClosed(true);
if (closed) return null; // Слот грузится заранее (rootMargin 200px), т.е. ещё ниже экрана —
// его исчезновение не сдвигает видимую часть ленты
if (closed || empty) return null;
if (ad) { if (ad) {
return ( return (