- apiFetch: proactive refresh, cross-tab single-flight via Web Locks, no logout on transient errors - AuthContext: save tokens returned on own login change, revoke refresh on logout, sync tabs - Ads: X-Session-Id, impressionToken in impression/click, IAB viewability, safe http(s) targetUrl - STOMP: refresh before CONNECT, stop reconnect loop on dead session, /user/queue/errors behind flag - Auth form: password 8 chars / 72 bytes; 429/413 messages via describeApiError - My posts with auth + moderation badges; post form step validation and field limits - Map: escape user text in Yandex hint/template Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
64 lines
2.2 KiB
JavaScript
64 lines
2.2 KiB
JavaScript
import { useEffect, useRef, useState } from 'react';
|
||
import { getNextAd } from '../api/adsApi';
|
||
import AdCard from './AdCard';
|
||
|
||
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;
|
||
const node = slotRef.current;
|
||
if (!node) return;
|
||
|
||
let cancelled = false;
|
||
const controller = new AbortController();
|
||
const observer = new IntersectionObserver(
|
||
([entry]) => {
|
||
if (!entry.isIntersecting) return;
|
||
observer.disconnect();
|
||
|
||
// Выдача — ещё не показ: показ засчитывается в AdCard, когда
|
||
// баннер реально виден (useAdViewability).
|
||
getNextAd(controller.signal)
|
||
.then((data) => {
|
||
if (cancelled) return;
|
||
// null — 204 (нет кандидатов, лимит, пауза между показами) или 429
|
||
if (!data) setEmpty(true);
|
||
else setAd(data);
|
||
})
|
||
.catch(() => {
|
||
if (!cancelled) setEmpty(true);
|
||
});
|
||
},
|
||
{ rootMargin: '200px' }
|
||
);
|
||
|
||
observer.observe(node);
|
||
return () => {
|
||
cancelled = true;
|
||
controller.abort();
|
||
observer.disconnect();
|
||
};
|
||
}, [closed]);
|
||
|
||
const handleClose = () => setClosed(true);
|
||
|
||
// Слот грузится заранее (rootMargin 200px), т.е. ещё ниже экрана —
|
||
// его исчезновение не сдвигает видимую часть ленты
|
||
if (closed || empty) return null;
|
||
|
||
if (ad) {
|
||
return (
|
||
<div ref={slotRef} className="ad-slot">
|
||
<AdCard ad={ad} onClose={handleClose} />
|
||
</div>
|
||
);
|
||
}
|
||
|
||
// Зарезервированная высота, чтобы место под объявление не «дёргало» ленту
|
||
return <div ref={slotRef} className="ad-slot-placeholder" />;
|
||
} |