2022-03-18 01:58:06 +01:00
|
|
|
// @flow
|
|
|
|
import React from 'react';
|
2022-04-05 14:28:51 +02:00
|
|
|
import * as PAGES from 'constants/pages';
|
2022-03-18 01:58:06 +01:00
|
|
|
|
|
|
|
function inIFrame() {
|
|
|
|
try {
|
|
|
|
return window.self !== window.top;
|
|
|
|
} catch (e) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-05 14:28:51 +02:00
|
|
|
const EXCLUDED_PATHS = Object.freeze([`/$/${PAGES.AUTH}`, `/$/${PAGES.AUTH_SIGNIN}`, `/$/${PAGES.AUTH_VERIFY}`]);
|
|
|
|
|
2022-03-18 01:58:06 +01:00
|
|
|
const LOAD_AD_DELAY_MS = 3000; // Wait past boot-up and core-vitals period.
|
2022-03-30 16:24:19 +02:00
|
|
|
const OUTBRAIN_CONTAINER_KEY = 'outbrainSizeDiv';
|
2022-03-18 01:58:06 +01:00
|
|
|
let script;
|
|
|
|
|
2022-04-05 14:28:51 +02:00
|
|
|
/**
|
|
|
|
* @param hasPremiumPlus
|
|
|
|
* @param isAuthenticated
|
|
|
|
* @param pathname Reminder: the component using this effect must be listening
|
|
|
|
* to path changes (e.g. useHistory, etc.). This value must not
|
|
|
|
* come from window.location.pathname, which doesn't spark an
|
|
|
|
* update.
|
|
|
|
*/
|
|
|
|
export default function useAdOutbrain(hasPremiumPlus: boolean, isAuthenticated: boolean, pathname: string) {
|
2022-03-18 01:58:06 +01:00
|
|
|
// Only look at authentication for now. Eventually, we'll only use 'hasPremiumPlus'.
|
2022-03-18 19:31:59 +01:00
|
|
|
// Authenticated will return undefined if not yet populated, so wait and only show
|
|
|
|
// when returned as false
|
|
|
|
const isNotAuthenticated = isAuthenticated === false;
|
2022-03-18 01:58:06 +01:00
|
|
|
|
2022-04-06 05:26:42 +02:00
|
|
|
const propRef = React.useRef({ isAuthenticated, pathname });
|
|
|
|
propRef.current = { isAuthenticated, pathname };
|
|
|
|
|
|
|
|
function resolveVisibility() {
|
|
|
|
if (window[OUTBRAIN_CONTAINER_KEY]) {
|
|
|
|
if (propRef.current.isAuthenticated) {
|
|
|
|
window[OUTBRAIN_CONTAINER_KEY].style.display = 'none';
|
|
|
|
} else {
|
|
|
|
window[OUTBRAIN_CONTAINER_KEY].style.display = EXCLUDED_PATHS.includes(propRef.current.pathname) ? 'none' : '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-18 01:58:06 +01:00
|
|
|
React.useEffect(() => {
|
2022-03-18 19:31:59 +01:00
|
|
|
if (!inIFrame() && isNotAuthenticated && !script) {
|
2022-03-18 01:58:06 +01:00
|
|
|
const loadTimer = setTimeout(() => {
|
|
|
|
script = document.createElement('script');
|
|
|
|
script.src = 'https://adncdnend.azureedge.net/adtags/odysee.adn.js';
|
|
|
|
script.async = true;
|
2022-04-06 05:26:42 +02:00
|
|
|
script.addEventListener('load', resolveVisibility);
|
2022-03-18 01:58:06 +01:00
|
|
|
|
|
|
|
// $FlowFixMe
|
|
|
|
document.body.appendChild(script);
|
|
|
|
}, LOAD_AD_DELAY_MS);
|
|
|
|
|
|
|
|
return () => clearTimeout(loadTimer);
|
|
|
|
}
|
2022-04-06 05:26:42 +02:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2022-03-18 19:31:59 +01:00
|
|
|
}, [isNotAuthenticated]);
|
2022-03-18 01:58:06 +01:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
2022-04-06 05:26:42 +02:00
|
|
|
resolveVisibility();
|
2022-04-05 14:28:51 +02:00
|
|
|
}, [isAuthenticated, pathname]);
|
2022-03-18 01:58:06 +01:00
|
|
|
}
|