MCPcopy Create free account
hub / github.com/dailydotdev/apps / useAutoRotatingAds

Function useAutoRotatingAds

packages/shared/src/hooks/feed/useAutoRotatingAds.ts:24–134  ·  view source on GitHub ↗
(
  ad: Ad,
  index: number,
  feedIndex: number,
  ref: InViewRef,
  disabled = false,
  target?: AdRotationTarget,
)

Source from the content-addressed store, hash-verified

22export type InViewRef = InViewHookResponse['ref'];
23
24export const useAutoRotatingAds = (
25 ad: Ad,
26 index: number,
27 feedIndex: number,
28 ref: InViewRef,
29 disabled = false,
30 target?: AdRotationTarget,
31): {
32 ref: InViewRef;
33 refetch: QueryObserverBaseResult<Ad | null>['refetch'];
34 isRefetching: QueryObserverBaseResult<Ad | null>['isRefetching'];
35} => {
36 const autorotateFlag = useFeature(featureAutorotateAds);
37 const autorotateAds = disabled ? 0 : (autorotateFlag ?? 0);
38 const { logEventEnd } = useLogContext();
39 const { queryKey: feedQueryKey = [] } = useContext(ActiveFeedContext);
40 const queryClient = useQueryClient();
41 const { ref: inViewRef, inView } = useInView({
42 threshold: 0.5,
43 skip: !autorotateAds,
44 });
45
46 const rotationTime = (autorotateAds ?? 0) * 1_000;
47
48 const refs = useCallback(
49 (node?: Element | null) => {
50 ref?.(node);
51 inViewRef(node);
52 },
53 [inViewRef, ref],
54 );
55
56 const { fetchAd, isAdConsentReady } = useFetchAd(!!autorotateAds);
57 const queryKey = useMemo(
58 () => target?.queryKey ?? [RequestKey.Ads, ...(feedQueryKey ?? [])],
59 [feedQueryKey, target?.queryKey],
60 );
61
62 const fetchNewAd = useCallback(
63 async ({ signal }: QueryFunctionContext) => {
64 const newAd = await fetchAd({
65 placement: AdPlacement.Feed,
66 active: true,
67 });
68 if (!newAd || signal.aborted) {
69 return null;
70 }
71
72 if (target) {
73 target.replace(newAd);
74 return newAd;
75 }
76
77 // End the impression event for the old ad
78 logEventEnd(generateAdLogEventKey(feedIndex));
79
80 queryClient.setQueryData(
81 queryKey,

Callers 3

useFeedAdRefreshFunction · 0.90
renderRotationFunction · 0.90
Iubenda.spec.tsxFile · 0.90

Calls 8

useFeatureFunction · 0.90
useLogContextFunction · 0.90
useFetchAdFunction · 0.90
generateAdLogEventKeyFunction · 0.90
fetchAdFunction · 0.85
logEventEndFunction · 0.85
replaceMethod · 0.80
nowMethod · 0.80

Tested by 1

renderRotationFunction · 0.72