2022-05-31 11:19:21 +02:00
|
|
|
// @flow
|
|
|
|
import React from 'react';
|
|
|
|
import { useHistory } from 'react-router-dom';
|
|
|
|
import useShouldShowAds from 'effects/use-should-show-ads';
|
2022-06-01 15:47:14 +02:00
|
|
|
import { platform } from 'util/platform';
|
2022-05-31 11:19:21 +02:00
|
|
|
|
|
|
|
// ****************************************************************************
|
|
|
|
// AdsSticky
|
|
|
|
// ****************************************************************************
|
|
|
|
|
|
|
|
const OUTBRAIN_CONTAINER_KEY = 'outbrainSizeDiv';
|
|
|
|
let gScript;
|
|
|
|
|
|
|
|
type Props = {
|
2022-06-02 15:56:12 +02:00
|
|
|
uri: ?string,
|
|
|
|
// --- redux ---
|
|
|
|
isContentClaim: boolean,
|
|
|
|
isChannelClaim: boolean,
|
|
|
|
authenticated: ?boolean,
|
2022-05-31 11:19:21 +02:00
|
|
|
isAdBlockerFound: ?boolean,
|
|
|
|
userHasPremiumPlus: boolean,
|
|
|
|
userCountry: string,
|
2022-06-01 15:47:14 +02:00
|
|
|
homepageData: any,
|
2022-05-31 11:19:21 +02:00
|
|
|
doSetAdBlockerFound: (boolean) => void,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function AdsSticky(props: Props) {
|
2022-06-02 15:56:12 +02:00
|
|
|
const {
|
|
|
|
isContentClaim,
|
|
|
|
isChannelClaim,
|
|
|
|
authenticated,
|
|
|
|
isAdBlockerFound,
|
|
|
|
userHasPremiumPlus,
|
|
|
|
userCountry,
|
|
|
|
homepageData,
|
|
|
|
doSetAdBlockerFound,
|
|
|
|
} = props;
|
|
|
|
|
2022-06-01 15:47:14 +02:00
|
|
|
const { location } = useHistory();
|
2022-06-01 06:26:12 +02:00
|
|
|
const [refresh, setRefresh] = React.useState(0);
|
|
|
|
|
2022-06-02 15:56:12 +02:00
|
|
|
// Global condition on whether ads should be activated:
|
2022-06-01 15:47:14 +02:00
|
|
|
const shouldShowAds = useShouldShowAds(userHasPremiumPlus, userCountry, isAdBlockerFound, doSetAdBlockerFound);
|
2022-06-02 15:56:12 +02:00
|
|
|
// Global conditions aside, should the Sticky be shown for this path:
|
|
|
|
const inAllowedPath = shouldShowAdsForPath(location.pathname, isContentClaim, isChannelClaim, authenticated);
|
|
|
|
// Final answer:
|
2022-06-15 11:00:23 +02:00
|
|
|
const shouldLoadSticky = shouldShowAds && !gScript && !inIFrame() && !platform.isMobile();
|
2022-06-01 15:47:14 +02:00
|
|
|
|
2022-06-02 15:56:12 +02:00
|
|
|
function shouldShowAdsForPath(pathname, isContentClaim, isChannelClaim, authenticated) {
|
2022-06-01 15:47:14 +02:00
|
|
|
// $FlowIssue: mixed type
|
2022-06-02 15:56:12 +02:00
|
|
|
const pathIsCategory = Object.values(homepageData).some((x) => pathname.startsWith(`/$/${x?.name}`));
|
2022-06-15 11:02:49 +02:00
|
|
|
return pathIsCategory || isChannelClaim || (isContentClaim && !authenticated) || pathname === '/';
|
2022-06-01 06:26:12 +02:00
|
|
|
}
|
2022-05-31 11:19:21 +02:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
2022-06-02 15:56:12 +02:00
|
|
|
if (shouldLoadSticky) {
|
2022-05-31 11:19:21 +02:00
|
|
|
gScript = document.createElement('script');
|
|
|
|
gScript.src = 'https://adncdnend.azureedge.net/adtags/odysee.adn.js';
|
|
|
|
gScript.async = true;
|
2022-06-01 06:26:12 +02:00
|
|
|
gScript.addEventListener('load', () => setRefresh(Date.now()));
|
2022-05-31 11:19:21 +02:00
|
|
|
// $FlowFixMe
|
|
|
|
document.body.appendChild(gScript);
|
|
|
|
}
|
2022-06-02 15:56:12 +02:00
|
|
|
}, [shouldLoadSticky]);
|
2022-05-31 11:19:21 +02:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
const container = window[OUTBRAIN_CONTAINER_KEY];
|
|
|
|
if (container) {
|
2022-06-01 06:26:12 +02:00
|
|
|
container.style.display = inAllowedPath ? '' : 'none';
|
2022-05-31 11:19:21 +02:00
|
|
|
}
|
2022-06-01 06:26:12 +02:00
|
|
|
}, [inAllowedPath, refresh]);
|
2022-05-31 11:19:21 +02:00
|
|
|
|
2022-06-02 15:56:12 +02:00
|
|
|
return null; // Nothing for us to mount; the ad script will handle everything.
|
2022-05-31 11:19:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ****************************************************************************
|
|
|
|
// Helpers
|
|
|
|
// ****************************************************************************
|
|
|
|
|
|
|
|
function inIFrame() {
|
|
|
|
try {
|
|
|
|
return window.self !== window.top;
|
|
|
|
} catch (e) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|