{"size": 1348, "ext": "js", "max_stars_repo_path": "ui/effects/use-ad-outbrain.js", "max_stars_repo_name": "david0178418/odysee-frontend", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "ui/effects/use-ad-outbrain.js", "max_issues_repo_name": "david0178418/odysee-frontend", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "ui/effects/use-ad-outbrain.js", "max_forks_repo_name": "david0178418/odysee-frontend", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 29.9555555556, "max_line_length": 90, "alphanum_fraction": 0.6735905045}
// @flow
import React from 'react';

function inIFrame() {
  try {
    return window.self !== window.top;
  } catch (e) {
    return true;
  }
}

const LOAD_AD_DELAY_MS = 3000; // Wait past boot-up and core-vitals period.
const OUTBRAIN_CONTAINER_KEY = 'outbrainSizeDiv';

let script;

export default function useAdOutbrain(hasPremiumPlus: boolean, isAuthenticated: boolean) {
  // Only look at authentication for now. Eventually, we'll only use 'hasPremiumPlus'.
  // Authenticated will return undefined if not yet populated, so wait and only show
  // when returned as false
  const isNotAuthenticated = isAuthenticated === false;

  React.useEffect(() => {
    if (!inIFrame() && isNotAuthenticated && !script) {
      const loadTimer = setTimeout(() => {
        script = document.createElement('script');
        script.src = 'https://adncdnend.azureedge.net/adtags/odysee.adn.js';
        script.async = true;

        // $FlowFixMe
        document.body.appendChild(script);
      }, LOAD_AD_DELAY_MS);

      return () => clearTimeout(loadTimer);
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps, (on mount only)
  }, [isNotAuthenticated]);

  React.useEffect(() => {
    if (isAuthenticated && window[OUTBRAIN_CONTAINER_KEY]) {
      window[OUTBRAIN_CONTAINER_KEY].style.display = 'none';
    }
  }, [isAuthenticated]);
}
