-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathCarbonAd.js
75 lines (60 loc) · 1.8 KB
/
CarbonAd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import PropTypes from 'prop-types'
import React, { Component } from 'react'
import { withRouter } from 'react-router-dom'
import { isBrowser } from 'src/lib'
let isLoading = true
let script
const adExist = () => !!document.querySelector('#docs-carbonads #carbonads')
// Heads up!
// We render docs with React-Static which performs SSR rendering.
if (isBrowser()) {
script = document.createElement('script')
script.async = true
script.id = '_carbonads_js'
script.type = 'text/javascript'
script.src = '//cdn.carbonads.com/carbon.js?serve=CK7DT23J&placement=reactsemanticuicom'
script.onload = () => {
isLoading = false
}
}
const waitForLoad = () => {
if (adExist()) isLoading = false
else setTimeout(waitForLoad, 50)
}
class CarbonAd extends Component {
shouldComponentUpdate(nextProps) {
return this.props.location.pathname !== nextProps.location.pathname
}
componentDidMount() {
this.loadAd()
}
componentDidUpdate() {
this.loadAd()
}
loadAd = () => {
this.ifRef((ref) => {
// always add the script as it is used to insert the ad
ref.appendChild(script)
// On the first mount, the script fetches the first ad itself, so do nothing
// On subsequent mounts, we need to call refresh to insert a new ad
// Ensure we don't call refresh during an existing refresh or we'll get a double ad insert
// https://github.com/Semantic-Org/Semantic-UI-React/pull/3215
if (!isLoading) {
isLoading = true
window._carbonads.refresh?.()
waitForLoad()
}
})
}
ifRef = (cb) => {
const ref = document.querySelector('#docs-carbonads')
if (ref) cb(ref)
}
render() {
return <div id='docs-carbonads' />
}
}
CarbonAd.propTypes = {
location: PropTypes.object.isRequired,
}
export default withRouter(CarbonAd)