| 44 | } |
| 45 | |
| 46 | export const useAdQuery = ({ |
| 47 | queryKey, |
| 48 | enabled = true, |
| 49 | staleTime, |
| 50 | gcTime, |
| 51 | placement, |
| 52 | active, |
| 53 | retry, |
| 54 | onTransition, |
| 55 | refreshOnPageView = false, |
| 56 | isRefreshable = false, |
| 57 | }: UseAdQueryOptions) => { |
| 58 | const consent = useAdRequestConsent(enabled); |
| 59 | const { resolveAd } = useAdLifecycle(enabled); |
| 60 | const onFetchTransition = useAdSlotFetch(consent, onTransition); |
| 61 | const { isAuthReady, isLoggedIn, isAndroidApp } = useAuthContext(); |
| 62 | const { value: refreshEngine, isLoading: isRefreshEngineLoading } = |
| 63 | useConditionalFeature({ |
| 64 | feature: featureAdRefreshEngine, |
| 65 | shouldEvaluate: enabled && isRefreshable, |
| 66 | }); |
| 67 | const isRefreshEngine = |
| 68 | isRefreshable && refreshEngine === AdRefreshEngineVersion.V2; |
| 69 | const registered = isAuthReady ? isLoggedIn : undefined; |
| 70 | const pageViewId = useAdPageViewId(); |
| 71 | const fetchOptions = useMemo( |
| 72 | () => ({ |
| 73 | ...resolveAdFetchOptions({ |
| 74 | placement, |
| 75 | active, |
| 76 | consent: consent ?? undefined, |
| 77 | registered, |
| 78 | pageViewId, |
| 79 | }), |
| 80 | ...(isRefreshEngine && { |
| 81 | refresh: { platform: getAdRefreshPlatform(isAndroidApp), count: 0 }, |
| 82 | }), |
| 83 | }), |
| 84 | [ |
| 85 | placement, |
| 86 | active, |
| 87 | consent, |
| 88 | registered, |
| 89 | pageViewId, |
| 90 | isRefreshEngine, |
| 91 | isAndroidApp, |
| 92 | ], |
| 93 | ); |
| 94 | |
| 95 | return useQuery<Ad | null>({ |
| 96 | // the page-view id, for page-owned ads, so each page view gets a fresh |
| 97 | // ad. It is sent with every request either way. Then the consent |
| 98 | // fingerprint so ads refetch when the user answers the CMP banner, and |
| 99 | // login state so signing in swaps an anonymous-only ad |
| 100 | queryKey: [ |
| 101 | ...queryKey, |
| 102 | ...(refreshOnPageView ? [pageViewId] : []), |
| 103 | consent?.gdprApplies, |