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

Function useAutoRotatingAds

packages/shared/src/hooks/feed/useAutoRotatingAds.ts:22–118  ·  view source on GitHub ↗
(
  ad: Ad,
  index: number,
  feedIndex: number,
  ref: InViewRef,
)

Source from the content-addressed store, hash-verified

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

Callers 3

AdGrid.tsxFile · 0.90
AdList.tsxFile · 0.90
SignalAdList.tsxFile · 0.90

Calls 6

useFeatureFunction · 0.90
useLogContextFunction · 0.90
useFetchAdFunction · 0.90
generateAdLogEventKeyFunction · 0.90
fetchAdFunction · 0.85
logEventEndFunction · 0.85

Tested by

no test coverage detected