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