lbry-desktop/web/component/ads/view.jsx

161 lines
4.8 KiB
React
Raw Normal View History

2020-01-24 19:31:49 +01:00
// @flow
import * as PAGES from 'constants/pages';
2020-01-23 17:21:36 +01:00
import React, { useEffect } from 'react';
2020-01-24 19:31:49 +01:00
import I18nMessage from 'component/i18nMessage';
import Button from 'component/button';
import PremiumPlusTile from 'component/premiumPlusTile';
2020-03-26 22:47:07 +01:00
import classnames from 'classnames';
import useShouldShowAds from 'effects/use-should-show-ads';
import { platform } from 'util/platform';
import Icon from 'component/common/icon';
import * as ICONS from 'constants/icons';
import { LocalStorage, LS } from 'util/storage';
2022-04-11 06:51:36 +02:00
const USE_ADNIMATION = true;
2022-03-07 07:31:53 +01:00
// prettier-ignore
const AD_CONFIGS = Object.freeze({
DEFAULT: {
url: 'https://cdn.vidcrunch.com/integrations/618bb4d28aac298191eec411/Lbry_Odysee.com_Responsive_Floating_DFP_Rev70_1011.js',
tag: 'vidcrunchJS537102317',
},
MOBILE: {
url: 'https://cdn.vidcrunch.com/integrations/618bb4d28aac298191eec411/Lbry_Odysee.com_Mobile_Floating_DFP_Rev70_1611.js',
tag: 'vidcrunchJS199212779',
},
EU: {
url: 'https://tg1.vidcrunch.com/api/adserver/spt?AV_TAGID=61dff05c599f1e20b01085d4&AV_PUBLISHERID=6182c8993c8ae776bd5635e9',
tag: 'AV61dff05c599f1e20b01085d4',
},
2022-04-11 06:51:36 +02:00
ADNIMATION: {
url: 'https://tg1.aniview.com/api/adserver/spt?AV_TAGID=6252bb6f28951333ec10a7a6&AV_PUBLISHERID=601d9a7f2e688a79e17c1265',
tag: 'AV6252bb6f28951333ec10a7a6',
},
2022-03-07 07:31:53 +01:00
});
2022-04-11 06:51:36 +02:00
// ****************************************************************************
// Helpers
// ****************************************************************************
function removeIfExists(querySelector) {
const element = document.querySelector(querySelector);
if (element) element.remove();
}
function resolveVidcrunchConfig() {
const mobileAds = platform.isAndroid() || platform.isIOS();
const isInEu = LocalStorage.getItem(LS.GDPR_REQUIRED) === 'true';
return isInEu ? AD_CONFIGS.EU : mobileAds ? AD_CONFIGS.MOBILE : AD_CONFIGS.DEFAULT;
}
// ****************************************************************************
// Ads
// ****************************************************************************
2020-01-24 19:31:49 +01:00
type Props = {
2020-03-26 22:47:07 +01:00
type: string,
tileLayout?: boolean,
small?: boolean,
className?: string,
noFallback?: boolean,
// --- redux ---
userHasPremiumPlus: boolean,
userCountry: string,
doSetAdBlockerFound: (boolean) => void,
2020-01-24 19:31:49 +01:00
};
function Ads(props: Props) {
const {
type = 'video',
tileLayout,
small,
userHasPremiumPlus,
userCountry,
className,
noFallback,
doSetAdBlockerFound,
} = props;
const shouldShowAds = useShouldShowAds(userHasPremiumPlus, userCountry, doSetAdBlockerFound);
2022-04-11 06:51:36 +02:00
const adConfig = USE_ADNIMATION ? AD_CONFIGS.ADNIMATION : resolveVidcrunchConfig();
// add script to DOM
2020-01-23 17:21:36 +01:00
useEffect(() => {
if (shouldShowAds) {
2021-01-22 00:57:29 +01:00
let script;
2020-03-26 22:47:07 +01:00
try {
script = document.createElement('script');
2022-03-07 07:31:53 +01:00
script.src = adConfig.url;
2020-03-26 22:47:07 +01:00
// $FlowFixMe
document.head.appendChild(script);
2020-03-26 22:47:07 +01:00
return () => {
// $FlowFixMe
document.head.removeChild(script);
// clear aniview state to allow ad reload
delete window.aniplayerPos;
delete window.storageAni;
delete window.__VIDCRUNCH_CONFIG_618bb4d28aac298191eec411__;
delete window.__player_618bb4d28aac298191eec411__;
// clean DOM elements from ad related elements
2022-04-11 06:51:36 +02:00
removeIfExists('[src^="https://player.avplayer.com"]');
removeIfExists('[src^="https://gum.criteo.com"]');
removeIfExists('[id^="AVLoaderaniview_slot"]');
};
} catch (e) {}
}
}, [shouldShowAds]);
2020-03-26 22:47:07 +01:00
const adsSignInDriver = (
<I18nMessage
tokens={{
sign_up_for_premium: (
<Button button="link" label={__('Get Odysee Premium+')} navigate={`/$/${PAGES.ODYSEE_MEMBERSHIP}`} />
2020-03-26 22:47:07 +01:00
),
}}
>
%sign_up_for_premium% for an ad free experience.
2020-03-26 22:47:07 +01:00
</I18nMessage>
);
2020-01-23 17:21:36 +01:00
if (type === 'video') {
if (shouldShowAds) {
return (
<div
className={classnames('ads ads__claim-item', className, {
'ads__claim-item--tile': tileLayout,
})}
>
<div className="ad__container">
<div id={adConfig.tag} />
</div>
<div
className={classnames('ads__claim-text', {
'ads__claim-text--small': small,
})}
>
<div className="ads__title">
{__('Ad')}
<br />
{__('Hate these?')}
{/* __('No ads, a custom badge and access to exclusive features, try Odysee Premium!') */}
</div>
<div className="ads__subtitle">
<Icon icon={ICONS.UPGRADE} />
{adsSignInDriver}
</div>
</div>
</div>
);
} else if (!noFallback) {
return <PremiumPlusTile tileLayout={tileLayout} />;
}
2021-01-22 00:57:29 +01:00
}
2022-01-04 18:43:35 +01:00
return null;
2022-01-04 18:43:35 +01:00
}
export default Ads;