2022-04-14 15:21:37 +02:00
|
|
|
// @flow
|
|
|
|
import React from 'react';
|
|
|
|
import { SHOW_ADS } from 'config';
|
|
|
|
|
2022-05-19 17:09:24 +02:00
|
|
|
const NO_COUNTRY_CHECK = true;
|
2022-04-14 15:21:37 +02:00
|
|
|
const GOOGLE_AD_URL = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js';
|
|
|
|
|
2022-05-19 17:09:24 +02:00
|
|
|
export default function useShouldShowAds(
|
|
|
|
hasPremiumPlus: boolean,
|
|
|
|
userCountry: string,
|
2022-05-30 12:50:15 +02:00
|
|
|
isAdBlockerFound: ?boolean,
|
2022-05-19 17:09:24 +02:00
|
|
|
doSetAdBlockerFound: (boolean) => void
|
|
|
|
) {
|
2022-04-14 15:21:37 +02:00
|
|
|
const [shouldShowAds, setShouldShowAds] = React.useState(resolveAdVisibility());
|
|
|
|
|
|
|
|
function resolveAdVisibility() {
|
2022-05-30 08:11:12 +02:00
|
|
|
// 'ad_blocker_detected' and 'hasPremiumPlus' will be undefined until
|
|
|
|
// fetched. Only show when it is exactly 'false'.
|
|
|
|
return (
|
2022-05-30 12:50:15 +02:00
|
|
|
SHOW_ADS && (NO_COUNTRY_CHECK || userCountry === 'US') && isAdBlockerFound === false && hasPremiumPlus === false
|
2022-05-30 08:11:12 +02:00
|
|
|
);
|
2022-04-14 15:21:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
2022-05-30 12:50:15 +02:00
|
|
|
if (isAdBlockerFound === undefined) {
|
2022-04-14 15:21:37 +02:00
|
|
|
fetch(GOOGLE_AD_URL)
|
|
|
|
.then((response) => {
|
|
|
|
const detected = response.redirected === true;
|
2022-05-30 12:50:15 +02:00
|
|
|
doSetAdBlockerFound(detected);
|
2022-04-14 15:21:37 +02:00
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
doSetAdBlockerFound(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
2022-05-30 08:11:12 +02:00
|
|
|
React.useEffect(() => {
|
|
|
|
setShouldShowAds(resolveAdVisibility());
|
2022-05-30 12:50:15 +02:00
|
|
|
}, [hasPremiumPlus, isAdBlockerFound]);
|
2022-05-30 08:11:12 +02:00
|
|
|
|
2022-04-14 15:21:37 +02:00
|
|
|
return shouldShowAds;
|
|
|
|
}
|