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

128 lines
3.4 KiB
React
Raw Normal View History

2020-01-24 19:31:49 +01:00
// @flow
2020-10-01 22:59:11 +02:00
import { DOMAIN, SHOW_ADS } from 'config';
2020-01-24 19:31:49 +01:00
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 { withRouter } from 'react-router';
import I18nMessage from 'component/i18nMessage';
import Button from 'component/button';
2020-03-26 22:47:07 +01:00
import classnames from 'classnames';
const ADS_URL = 'https://cdn.vidcrunch.com/integrations/618bb4d28aac298191eec411/Lbry_Odysee.com_Responsive_Floating_DFP_Rev70_1011.js';
const ADS_TAG = 'vidcrunchJS537102317';
const IOS_ADS_URL = 'https://cdn.vidcrunch.com/integrations/618bb4d28aac298191eec411/Lbry_Odysee.com_Mobile_Floating_DFP_Rev70_1611.js';
const IOS_ADS_TAG = 'vidcrunchJS199212779';
const HOMEPAGE_ADS_URL = 'https://cdn.vidcrunch.com/integrations/618bb4d28aac298191eec411/Lbry_Odysee.com_Responsive_Floating_300x169_DFP_Rev70_1211.js';
const HOMEPAGE_ADS_TAG = 'vidcrunchJS330442776';
const IS_IOS =
(/iPad|iPhone|iPod/.test(navigator.platform) ||
// for iOS 13+ , platform is MacIntel, so use this to test
(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)) &&
!window.MSStream;
2020-01-24 19:31:49 +01:00
type Props = {
location: { pathname: string },
2020-03-26 22:47:07 +01:00
type: string,
small: boolean,
2021-03-21 20:02:50 +01:00
claim: Claim,
2021-01-31 04:47:06 +01:00
isMature: boolean,
2020-01-24 19:31:49 +01:00
};
function Ads(props: Props) {
const {
location: { pathname },
type = 'video',
2020-03-26 22:47:07 +01:00
small,
2020-01-24 19:31:49 +01:00
} = props;
2020-03-26 22:47:07 +01:00
// load ad and tags here
let scriptUrlToUse;
let tagNameToUse;
if (type === 'video') {
if (IS_IOS) {
tagNameToUse = IOS_ADS_TAG;
scriptUrlToUse = IOS_ADS_URL;
} else {
tagNameToUse = ADS_TAG;
scriptUrlToUse = ADS_URL;
}
} else if (type === 'homepage') {
tagNameToUse = HOMEPAGE_ADS_TAG;
scriptUrlToUse = HOMEPAGE_ADS_URL;
}
// add script to DOM
2020-01-23 17:21:36 +01:00
useEffect(() => {
if (SHOW_ADS) {
2021-01-22 00:57:29 +01:00
let script;
2020-03-26 22:47:07 +01:00
try {
let fjs = document.getElementsByTagName('script')[0];
script = document.createElement('script');
script.src = scriptUrlToUse;
2020-03-26 22:47:07 +01:00
// $FlowFixMe
2021-01-22 00:57:29 +01:00
fjs.parentNode.insertBefore(script, fjs);
2020-03-26 22:47:07 +01:00
return () => {
// $FlowFixMe
document.head.removeChild(script);
};
} catch (e) {}
}
// TODO: remove the script when it exists?
2020-03-26 22:47:07 +01:00
}, [type]);
// display to say "sign up to not see these"
2020-03-26 22:47:07 +01:00
const adsSignInDriver = (
<I18nMessage
tokens={{
2021-01-03 14:55:24 +01:00
log_in_to_domain: (
2020-07-14 21:08:54 +02:00
<Button
button="link"
2021-01-03 14:55:24 +01:00
label={__('Log in to %domain%', { domain: DOMAIN })}
2020-07-14 21:08:54 +02:00
navigate={`/$/${PAGES.AUTH}?redirect=${pathname}`}
/>
2020-03-26 22:47:07 +01:00
),
}}
>
Hate these? %log_in_to_domain% for an ad free experience.
2020-03-26 22:47:07 +01:00
</I18nMessage>
);
2020-01-23 17:21:36 +01:00
// ad shown in the related videos area
2021-01-22 00:57:29 +01:00
const videoAd = (
2020-03-26 22:47:07 +01:00
<div className="ads__claim-item">
<div id={tagNameToUse} className="ads__injected-video" style={{display: 'none'}} />
2020-03-26 22:47:07 +01:00
<div
className={classnames('ads__claim-text', {
'ads__claim-text--small': small,
})}
>
<div>Ad</div>
<p>{adsSignInDriver}</p>
</div>
</div>
2021-01-22 00:57:29 +01:00
);
// homepage ad in a card
const homepageCardAd = (
<div className="homepageAdContainer media__thumb" style={{display: 'none'}}>
<div id={tagNameToUse} className="homepageAdDiv media__thumb" style={{display: 'none'}} />
</div>
);
2021-06-22 19:16:02 +02:00
if (!SHOW_ADS) {
2021-01-22 00:57:29 +01:00
return false;
}
if (type === 'video') {
return videoAd;
}
if (type === 'homepage') {
return homepageCardAd;
}
2020-01-23 19:44:55 +01:00
}
2020-01-23 17:21:36 +01:00
2020-01-24 19:31:49 +01:00
export default withRouter(Ads);