-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathCarbonAdNative.js
182 lines (154 loc) · 4.47 KB
/
CarbonAdNative.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import { Label } from 'semantic-ui-react'
import { makeDebugger } from '../../../../src/lib'
const debug = makeDebugger('carbon-ad-native')
const MAX_FAILED_ADS = 10
class CarbonAdNative extends PureComponent {
state = {}
componentDidMount() {
debug('componentDidMount', { mounted: this.mounted })
this.mounted = true
this.failedAds = 0
this.getAd()
}
// eslint-disable-next-line camelcase
UNSAFE_componentWillUpdate() {
const shouldGetAd = Date.now() - this.timeOfLastAd > 10000
debug('componentWillUpdate', { mounted: this.mounted, shouldGetAd })
if (shouldGetAd) {
this.getAd()
}
}
componentWillUnmount() {
this.mounted = false
this.cleanup()
}
cleanup = () => {
debug('cleanup', { script: this.script, mounted: this.mounted })
if (!this.script) return
document.getElementsByTagName('head')[0].removeChild(this.script)
this.script = null
}
getAd = () => {
debug('getAd', { mounted: this.mounted })
if (!this.mounted) return
window._handleNativeJSON = this.handleNativeJSON
this.timeOfLastAd = Date.now()
this.cleanup()
this.script = document.createElement('script')
this.script.src = `https://srv.buysellads.com/ads/CK7DC2QW.json?${[
'segment=placement:reactsemanticuicom',
'callback=_handleNativeJSON',
`v=${Date.now()}`,
].join('&')}`
document.getElementsByTagName('head')[0].appendChild(this.script)
}
handleNativeJSON = (json) => {
debug('handleNativeJSON', { mounted: this.mounted })
try {
const sanitizedAd = json.ads
.filter((ad) => Object.keys(ad).length > 0)
.filter((ad) => !!ad.statlink)
.filter(Boolean)[0]
debug('handleNativeJSON sanitizedAd', sanitizedAd)
if (!sanitizedAd) {
this.failedAds += 1
if (this.failedAds < MAX_FAILED_ADS) {
this.getAd()
}
} else if (this.mounted) {
this.setState({ ad: sanitizedAd })
this.failedAds = 0
}
} catch (err) {
// eslint-disable-next-line no-console
console.error(err)
}
}
render() {
const { inverted } = this.props
const { ad } = this.state
debug('render', ad)
if (!ad) return null
const id = `carbon-native-${ad.timestamp}`
const colors = inverted
? {
divider: '#333',
background: '#222',
backgroundHover: '#1d1d1d',
color: '#999',
colorHover: '#ccc',
}
: {
divider: '#eee',
background: '#fff',
backgroundHover: 'whitesmoke',
color: '#555',
colorHover: '#333',
}
return (
<a id={id} href={ad.statlink} target='_blank' rel='noopener noreferrer'>
<img src={ad.image} alt="Ad" />
<span>{ad.company}</span>
{' — '}
<span>{ad.description}</span>
<Label
content='Ad'
basic={!inverted}
color={inverted ? 'black' : undefined}
horizontal
style={{ position: 'absolute', right: '1rem', opacity: 0.5 }}
/>
{/* Impression */}
<img alt="Ad" src={`${ad.statimp}`} style={{ display: 'none' }} />
{/* Pixel */}
{ad.pixel &&
ad.pixel
.split('||')
.map((pixel, i) => (
<img
alt="Ad"
key={i}
src={`${pixel.replace('[timestamp]', ad.timestamp)}`}
style={{ display: 'none' }}
/>
))}
<style>
{`
#${id} {
display: block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
padding: 1rem 5rem 1rem 1rem;
width: 100%;
min-height: 3.5rem;
border-top: 1px solid ${colors.divider};
background: ${colors.background};
color: ${colors.color};
cursor: pointer;
}
#${id} > img {
vertical-align: middle;
width: 20px;
margin-right: 0.5rem;
opacity: 0.8;
}
#${id}:hover {
background: ${colors.backgroundHover};
color: ${colors.colorHover};
}
#${id}:hover > img {
opacity: 1;
}
`}
</style>
</a>
)
}
}
CarbonAdNative.propTypes = {
inverted: PropTypes.bool,
}
export default CarbonAdNative