Support new backend contracts: single-flight refresh, ad impression tokens, error handling
- 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>
This commit is contained in:
71
src/hooks/useAdViewability.js
Normal file
71
src/hooks/useAdViewability.js
Normal file
@@ -0,0 +1,71 @@
|
||||
import { useEffect, useRef } from "react";
|
||||
|
||||
// Стандарт видимости IAB/MRC для дисплейной рекламы: ≥ 50% площади
|
||||
// в зоне видимости не меньше 1 секунды подряд.
|
||||
export const VIEWABLE_RATIO = 0.5;
|
||||
export const VIEWABLE_MS = 1000;
|
||||
|
||||
/**
|
||||
* Вызывает onViewable один раз, когда элемент непрерывно виден хотя бы на
|
||||
* VIEWABLE_RATIO своей площади в течение VIEWABLE_MS. Вкладка в фоне
|
||||
* (document.hidden) не считается: таймер сбрасывается.
|
||||
*
|
||||
* @param {React.RefObject<Element>} ref
|
||||
* @param {() => void} onViewable
|
||||
* @param {boolean} [enabled=true]
|
||||
*/
|
||||
export function useAdViewability(ref, onViewable, enabled = true) {
|
||||
const onViewableRef = useRef(onViewable);
|
||||
useEffect(() => {
|
||||
onViewableRef.current = onViewable;
|
||||
}, [onViewable]);
|
||||
|
||||
useEffect(() => {
|
||||
const node = ref.current;
|
||||
if (!enabled || !node || typeof IntersectionObserver === "undefined") return;
|
||||
|
||||
let fired = false;
|
||||
let inView = false;
|
||||
let timerId = null;
|
||||
|
||||
const stop = () => {
|
||||
clearTimeout(timerId);
|
||||
timerId = null;
|
||||
};
|
||||
|
||||
const start = () => {
|
||||
if (fired || timerId || !inView || document.hidden) return;
|
||||
timerId = setTimeout(() => {
|
||||
timerId = null;
|
||||
if (fired || !inView || document.hidden) return;
|
||||
fired = true;
|
||||
observer.disconnect();
|
||||
document.removeEventListener("visibilitychange", onVisibility);
|
||||
onViewableRef.current?.();
|
||||
}, VIEWABLE_MS);
|
||||
};
|
||||
|
||||
const observer = new IntersectionObserver(
|
||||
([entry]) => {
|
||||
inView = entry.isIntersecting && entry.intersectionRatio >= VIEWABLE_RATIO;
|
||||
if (inView) start();
|
||||
else stop();
|
||||
},
|
||||
{ threshold: [0, VIEWABLE_RATIO, 1] }
|
||||
);
|
||||
|
||||
const onVisibility = () => {
|
||||
if (document.hidden) stop();
|
||||
else start();
|
||||
};
|
||||
|
||||
observer.observe(node);
|
||||
document.addEventListener("visibilitychange", onVisibility);
|
||||
|
||||
return () => {
|
||||
stop();
|
||||
observer.disconnect();
|
||||
document.removeEventListener("visibilitychange", onVisibility);
|
||||
};
|
||||
}, [ref, enabled]);
|
||||
}
|
||||
Reference in New Issue
Block a user