@adtogether/web-sdk 0.1.14 → 0.1.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +42 -37
- package/README.md +154 -154
- package/dist/react/index.js +17 -3
- package/dist/react/index.mjs +17 -3
- package/errors.txt +2 -2
- package/package.json +59 -59
- package/src/react/AdTogetherInterstitial.tsx +370 -358
- package/tsconfig.json +19 -19
|
@@ -1,358 +1,370 @@
|
|
|
1
|
-
import React, { useEffect, useRef, useState, useCallback } from 'react';
|
|
2
|
-
import { createPortal } from 'react-dom';
|
|
3
|
-
import { AdTogether } from '../core/AdTogether';
|
|
4
|
-
import { AdModel } from '../core/types';
|
|
5
|
-
|
|
6
|
-
export interface AdTogetherInterstitialProps {
|
|
7
|
-
adUnitId: string;
|
|
8
|
-
/** When true, the interstitial is shown (fetches and displays full-screen ad) */
|
|
9
|
-
isOpen: boolean;
|
|
10
|
-
/** Called when the user closes the interstitial */
|
|
11
|
-
onClose: () => void;
|
|
12
|
-
/** Called when the ad is successfully loaded */
|
|
13
|
-
onAdLoaded?: () => void;
|
|
14
|
-
/** Called when the ad fails to load */
|
|
15
|
-
onAdFailedToLoad?: (error: Error) => void;
|
|
16
|
-
/** Pass 'dark' to use dark mode, 'light' for light mode, or 'auto' (default) to respect system preference */
|
|
17
|
-
theme?: 'dark' | 'light' | 'auto';
|
|
18
|
-
/** Delay in seconds before the close button appears. Defaults to 3 */
|
|
19
|
-
closeDelay?: number;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const AdTogetherInterstitial: React.FC<AdTogetherInterstitialProps> = ({
|
|
23
|
-
adUnitId,
|
|
24
|
-
isOpen,
|
|
25
|
-
onClose,
|
|
26
|
-
onAdLoaded,
|
|
27
|
-
onAdFailedToLoad,
|
|
28
|
-
theme = 'auto',
|
|
29
|
-
closeDelay = 3,
|
|
30
|
-
}) => {
|
|
31
|
-
const [adData, setAdData] = useState<AdModel | null>(null);
|
|
32
|
-
const [isLoading, setIsLoading] = useState(false);
|
|
33
|
-
const [hasError, setHasError] = useState(false);
|
|
34
|
-
const [isDarkMode, setIsDarkMode] = useState(theme === 'dark');
|
|
35
|
-
const [canClose, setCanClose] = useState(false);
|
|
36
|
-
const [countdown, setCountdown] = useState(closeDelay);
|
|
37
|
-
|
|
38
|
-
const impressionTrackedRef = useRef(false);
|
|
39
|
-
const closeTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
40
|
-
const countdownRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
|
41
|
-
|
|
42
|
-
// Handle system theme changes
|
|
43
|
-
useEffect(() => {
|
|
44
|
-
if (theme === 'auto') {
|
|
45
|
-
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
|
|
46
|
-
setIsDarkMode(mediaQuery.matches);
|
|
47
|
-
|
|
48
|
-
const handler = (e: MediaQueryListEvent) => setIsDarkMode(e.matches);
|
|
49
|
-
mediaQuery.addEventListener('change', handler);
|
|
50
|
-
return () => mediaQuery.removeEventListener('change', handler);
|
|
51
|
-
} else {
|
|
52
|
-
setIsDarkMode(theme === 'dark');
|
|
53
|
-
}
|
|
54
|
-
}, [theme]);
|
|
55
|
-
|
|
56
|
-
// Fetch ad when opened
|
|
57
|
-
useEffect(() => {
|
|
58
|
-
if (!isOpen) {
|
|
59
|
-
// Reset state when closed
|
|
60
|
-
setAdData(null);
|
|
61
|
-
setIsLoading(false);
|
|
62
|
-
setHasError(false);
|
|
63
|
-
setCanClose(false);
|
|
64
|
-
setCountdown(closeDelay);
|
|
65
|
-
impressionTrackedRef.current = false;
|
|
66
|
-
|
|
67
|
-
if (closeTimerRef.current) clearTimeout(closeTimerRef.current);
|
|
68
|
-
if (countdownRef.current) clearInterval(countdownRef.current);
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
let isMounted = true;
|
|
73
|
-
setIsLoading(true);
|
|
74
|
-
|
|
75
|
-
AdTogether.fetchAd(adUnitId, 'interstitial')
|
|
76
|
-
.then((ad) => {
|
|
77
|
-
if (isMounted) {
|
|
78
|
-
setAdData(ad);
|
|
79
|
-
setIsLoading(false);
|
|
80
|
-
onAdLoaded?.();
|
|
81
|
-
}
|
|
82
|
-
})
|
|
83
|
-
.catch((err) => {
|
|
84
|
-
if (isMounted) {
|
|
85
|
-
console.error('AdTogether Failed to load interstitial:', err);
|
|
86
|
-
setHasError(true);
|
|
87
|
-
setIsLoading(false);
|
|
88
|
-
onAdFailedToLoad?.(err);
|
|
89
|
-
onClose();
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
return () => {
|
|
94
|
-
isMounted = false;
|
|
95
|
-
};
|
|
96
|
-
}, [isOpen, adUnitId]);
|
|
97
|
-
|
|
98
|
-
// Close delay countdown
|
|
99
|
-
useEffect(() => {
|
|
100
|
-
if (!isOpen || !adData) return;
|
|
101
|
-
|
|
102
|
-
setCountdown(closeDelay);
|
|
103
|
-
|
|
104
|
-
countdownRef.current = setInterval(() => {
|
|
105
|
-
setCountdown((prev) => {
|
|
106
|
-
if (prev <= 1) {
|
|
107
|
-
if (countdownRef.current) clearInterval(countdownRef.current);
|
|
108
|
-
setCanClose(true);
|
|
109
|
-
return 0;
|
|
110
|
-
}
|
|
111
|
-
return prev - 1;
|
|
112
|
-
});
|
|
113
|
-
}, 1000);
|
|
114
|
-
|
|
115
|
-
return () => {
|
|
116
|
-
if (countdownRef.current) clearInterval(countdownRef.current);
|
|
117
|
-
};
|
|
118
|
-
}, [isOpen, adData, closeDelay]);
|
|
119
|
-
|
|
120
|
-
// Impression tracking
|
|
121
|
-
useEffect(() => {
|
|
122
|
-
if (!adData || !isOpen || impressionTrackedRef.current) return;
|
|
123
|
-
impressionTrackedRef.current = true;
|
|
124
|
-
AdTogether.trackImpression(adData.id, adData.token);
|
|
125
|
-
}, [adData, isOpen]);
|
|
126
|
-
|
|
127
|
-
const handleAdClick = useCallback(() => {
|
|
128
|
-
if (!adData) return;
|
|
129
|
-
AdTogether.trackClick(adData.id, adData.token);
|
|
130
|
-
if (adData.clickUrl) {
|
|
131
|
-
window.open(adData.clickUrl, '_blank', 'noopener,noreferrer');
|
|
132
|
-
}
|
|
133
|
-
}, [adData]);
|
|
134
|
-
|
|
135
|
-
const handleClose = useCallback(() => {
|
|
136
|
-
if (canClose) {
|
|
137
|
-
onClose();
|
|
138
|
-
}
|
|
139
|
-
}, [canClose, onClose]);
|
|
140
|
-
|
|
141
|
-
// Block body scroll when open
|
|
142
|
-
useEffect(() => {
|
|
143
|
-
if (isOpen) {
|
|
144
|
-
document.body.style.overflow = 'hidden';
|
|
145
|
-
} else {
|
|
146
|
-
document.body.style.overflow = '';
|
|
147
|
-
}
|
|
148
|
-
return () => {
|
|
149
|
-
document.body.style.overflow = '';
|
|
150
|
-
};
|
|
151
|
-
}, [isOpen]);
|
|
152
|
-
|
|
153
|
-
if (!isOpen) return null;
|
|
154
|
-
|
|
155
|
-
const bgOverlay = 'rgba(0, 0, 0, 0.7)';
|
|
156
|
-
const cardBg = isDarkMode ? '#1F2937' : '#ffffff';
|
|
157
|
-
const borderColor = isDarkMode ? 'rgba(255,255,255,0.1)' : 'rgba(0,0,0,0.1)';
|
|
158
|
-
const textColor = isDarkMode ? '#F9FAFB' : '#111827';
|
|
159
|
-
const descColor = isDarkMode ? '#9CA3AF' : '#6B7280';
|
|
160
|
-
|
|
161
|
-
const content = (
|
|
162
|
-
<div
|
|
163
|
-
className="adtogether-interstitial-overlay"
|
|
164
|
-
style={{
|
|
165
|
-
position: 'fixed',
|
|
166
|
-
top: 0,
|
|
167
|
-
left: 0,
|
|
168
|
-
width: '100vw',
|
|
169
|
-
height: '100vh',
|
|
170
|
-
backgroundColor: bgOverlay,
|
|
171
|
-
display: 'flex',
|
|
172
|
-
alignItems: 'center',
|
|
173
|
-
justifyContent: 'center',
|
|
174
|
-
zIndex: 100000,
|
|
175
|
-
backdropFilter: 'blur(8px)',
|
|
176
|
-
animation: 'adtogether-fade-in 0.3s ease-out',
|
|
177
|
-
}}
|
|
178
|
-
onClick={(e) => {
|
|
179
|
-
if (e.target === e.currentTarget && canClose) handleClose();
|
|
180
|
-
}}
|
|
181
|
-
>
|
|
182
|
-
<style>{`
|
|
183
|
-
@keyframes adtogether-fade-in {
|
|
184
|
-
from { opacity: 0; }
|
|
185
|
-
to { opacity: 1; }
|
|
186
|
-
}
|
|
187
|
-
@keyframes adtogether-scale-in {
|
|
188
|
-
from { opacity: 0; transform: scale(0.9); }
|
|
189
|
-
to { opacity: 1; transform: scale(1); }
|
|
190
|
-
}
|
|
191
|
-
.adtogether-interstitial-card {
|
|
192
|
-
display: flex;
|
|
193
|
-
flex-direction: column;
|
|
194
|
-
}
|
|
195
|
-
.adtogether-interstitial-img-wrapper {
|
|
196
|
-
width: 100%;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
@media (orientation: landscape) and (max-height: 600px) {
|
|
200
|
-
.adtogether-interstitial-card {
|
|
201
|
-
flex-direction: row;
|
|
202
|
-
max-height: 90vh;
|
|
203
|
-
}
|
|
204
|
-
.adtogether-interstitial-img-wrapper {
|
|
205
|
-
width: 50%;
|
|
206
|
-
flex-shrink: 0;
|
|
207
|
-
display: flex;
|
|
208
|
-
}
|
|
209
|
-
.adtogether-interstitial-img-wrapper img {
|
|
210
|
-
height: 100% !important;
|
|
211
|
-
aspect-ratio: auto !important;
|
|
212
|
-
}
|
|
213
|
-
.adtogether-interstitial-content {
|
|
214
|
-
width: 50%;
|
|
215
|
-
overflow-y: auto;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
`}</style>
|
|
219
|
-
|
|
220
|
-
{isLoading ? (
|
|
221
|
-
<div style={{ color: '#fff', fontSize: '18px' }}>Loading Ad...</div>
|
|
222
|
-
) : adData ? (
|
|
223
|
-
<div
|
|
224
|
-
className="adtogether-interstitial-card"
|
|
225
|
-
style={{
|
|
226
|
-
position: 'relative',
|
|
227
|
-
maxWidth: '800px',
|
|
228
|
-
width: '95%',
|
|
229
|
-
backgroundColor: cardBg,
|
|
230
|
-
borderRadius: '24px',
|
|
231
|
-
border: `1px solid ${borderColor}`,
|
|
232
|
-
overflow: 'hidden',
|
|
233
|
-
boxShadow: '0 25px 50px rgba(0, 0, 0, 0.5)',
|
|
234
|
-
animation: 'adtogether-scale-in 0.3s ease-out',
|
|
235
|
-
}}
|
|
236
|
-
>
|
|
237
|
-
{/* Close / Countdown Button */}
|
|
238
|
-
<div style={{ position: 'absolute', top: '12px', right: '12px', zIndex: 10 }}>
|
|
239
|
-
{canClose ? (
|
|
240
|
-
<button
|
|
241
|
-
onClick={handleClose}
|
|
242
|
-
style={{
|
|
243
|
-
width: '36px',
|
|
244
|
-
height: '36px',
|
|
245
|
-
borderRadius: '50%',
|
|
246
|
-
backgroundColor: 'rgba(0,0,0,0.6)',
|
|
247
|
-
border: 'none',
|
|
248
|
-
color: '#fff',
|
|
249
|
-
fontSize: '18px',
|
|
250
|
-
cursor: 'pointer',
|
|
251
|
-
display: 'flex',
|
|
252
|
-
alignItems: 'center',
|
|
253
|
-
justifyContent: 'center',
|
|
254
|
-
backdropFilter: 'blur(4px)',
|
|
255
|
-
}}
|
|
256
|
-
aria-label="Close ad"
|
|
257
|
-
>
|
|
258
|
-
✕
|
|
259
|
-
</button>
|
|
260
|
-
) : (
|
|
261
|
-
<div
|
|
262
|
-
style={{
|
|
263
|
-
width: '36px',
|
|
264
|
-
height: '36px',
|
|
265
|
-
borderRadius: '50%',
|
|
266
|
-
backgroundColor: 'rgba(0,0,0,0.6)',
|
|
267
|
-
color: '#fff',
|
|
268
|
-
fontSize: '14px',
|
|
269
|
-
fontWeight: 'bold',
|
|
270
|
-
display: 'flex',
|
|
271
|
-
alignItems: 'center',
|
|
272
|
-
justifyContent: 'center',
|
|
273
|
-
backdropFilter: 'blur(4px)',
|
|
274
|
-
}}
|
|
275
|
-
>
|
|
276
|
-
{countdown}
|
|
277
|
-
</div>
|
|
278
|
-
)}
|
|
279
|
-
</div>
|
|
280
|
-
|
|
281
|
-
{/* Ad Image */}
|
|
282
|
-
{adData.imageUrl && (
|
|
283
|
-
<div
|
|
284
|
-
className="adtogether-interstitial-img-wrapper"
|
|
285
|
-
style={{ cursor: 'pointer', position: 'relative' }}
|
|
286
|
-
onClick={handleAdClick}
|
|
287
|
-
>
|
|
288
|
-
<img
|
|
289
|
-
src={adData.imageUrl}
|
|
290
|
-
alt={adData.title}
|
|
291
|
-
style={{
|
|
292
|
-
width: '100%',
|
|
293
|
-
|
|
294
|
-
objectFit: '
|
|
295
|
-
display: 'block',
|
|
296
|
-
}}
|
|
297
|
-
/>
|
|
298
|
-
</div>
|
|
299
|
-
)}
|
|
300
|
-
|
|
301
|
-
{/* Ad Content */}
|
|
302
|
-
<div
|
|
303
|
-
className="adtogether-interstitial-content"
|
|
304
|
-
style={{ padding: '20px', cursor: 'pointer' }}
|
|
305
|
-
onClick={handleAdClick}
|
|
306
|
-
>
|
|
307
|
-
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', marginBottom: '8px' }}>
|
|
308
|
-
<span style={{ fontWeight: 'bold', fontSize: '18px', color: textColor }}>
|
|
309
|
-
{adData.title}
|
|
310
|
-
</span>
|
|
311
|
-
<span
|
|
312
|
-
style={{
|
|
313
|
-
backgroundColor: '#FBBF24',
|
|
314
|
-
color: '#000',
|
|
315
|
-
fontSize: '10px',
|
|
316
|
-
fontWeight: 'bold',
|
|
317
|
-
padding: '3px 6px',
|
|
318
|
-
borderRadius: '4px',
|
|
319
|
-
marginLeft: '8px',
|
|
320
|
-
flexShrink: 0,
|
|
321
|
-
}}
|
|
322
|
-
>
|
|
323
|
-
AD
|
|
324
|
-
</span>
|
|
325
|
-
</div>
|
|
326
|
-
<p style={{ fontSize: '14px', color: descColor, margin: 0, lineHeight: 1.5 }}>
|
|
327
|
-
{adData.description}
|
|
328
|
-
</p>
|
|
329
|
-
|
|
330
|
-
{/* CTA Button */}
|
|
331
|
-
<button
|
|
332
|
-
style={{
|
|
333
|
-
marginTop: '16px',
|
|
334
|
-
width: '100%',
|
|
335
|
-
padding: '12px',
|
|
336
|
-
backgroundColor: '#F59E0B',
|
|
337
|
-
color: '#000',
|
|
338
|
-
fontWeight: 'bold',
|
|
339
|
-
fontSize: '14px',
|
|
340
|
-
border: 'none',
|
|
341
|
-
borderRadius: '12px',
|
|
342
|
-
cursor: 'pointer',
|
|
343
|
-
}}
|
|
344
|
-
onClick={(e) => {
|
|
345
|
-
e.stopPropagation();
|
|
346
|
-
handleAdClick();
|
|
347
|
-
}}
|
|
348
|
-
>
|
|
349
|
-
Learn More →
|
|
350
|
-
</button>
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
1
|
+
import React, { useEffect, useRef, useState, useCallback } from 'react';
|
|
2
|
+
import { createPortal } from 'react-dom';
|
|
3
|
+
import { AdTogether } from '../core/AdTogether';
|
|
4
|
+
import { AdModel } from '../core/types';
|
|
5
|
+
|
|
6
|
+
export interface AdTogetherInterstitialProps {
|
|
7
|
+
adUnitId: string;
|
|
8
|
+
/** When true, the interstitial is shown (fetches and displays full-screen ad) */
|
|
9
|
+
isOpen: boolean;
|
|
10
|
+
/** Called when the user closes the interstitial */
|
|
11
|
+
onClose: () => void;
|
|
12
|
+
/** Called when the ad is successfully loaded */
|
|
13
|
+
onAdLoaded?: () => void;
|
|
14
|
+
/** Called when the ad fails to load */
|
|
15
|
+
onAdFailedToLoad?: (error: Error) => void;
|
|
16
|
+
/** Pass 'dark' to use dark mode, 'light' for light mode, or 'auto' (default) to respect system preference */
|
|
17
|
+
theme?: 'dark' | 'light' | 'auto';
|
|
18
|
+
/** Delay in seconds before the close button appears. Defaults to 3 */
|
|
19
|
+
closeDelay?: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const AdTogetherInterstitial: React.FC<AdTogetherInterstitialProps> = ({
|
|
23
|
+
adUnitId,
|
|
24
|
+
isOpen,
|
|
25
|
+
onClose,
|
|
26
|
+
onAdLoaded,
|
|
27
|
+
onAdFailedToLoad,
|
|
28
|
+
theme = 'auto',
|
|
29
|
+
closeDelay = 3,
|
|
30
|
+
}) => {
|
|
31
|
+
const [adData, setAdData] = useState<AdModel | null>(null);
|
|
32
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
33
|
+
const [hasError, setHasError] = useState(false);
|
|
34
|
+
const [isDarkMode, setIsDarkMode] = useState(theme === 'dark');
|
|
35
|
+
const [canClose, setCanClose] = useState(false);
|
|
36
|
+
const [countdown, setCountdown] = useState(closeDelay);
|
|
37
|
+
|
|
38
|
+
const impressionTrackedRef = useRef(false);
|
|
39
|
+
const closeTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
40
|
+
const countdownRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
|
41
|
+
|
|
42
|
+
// Handle system theme changes
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
if (theme === 'auto') {
|
|
45
|
+
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
|
|
46
|
+
setIsDarkMode(mediaQuery.matches);
|
|
47
|
+
|
|
48
|
+
const handler = (e: MediaQueryListEvent) => setIsDarkMode(e.matches);
|
|
49
|
+
mediaQuery.addEventListener('change', handler);
|
|
50
|
+
return () => mediaQuery.removeEventListener('change', handler);
|
|
51
|
+
} else {
|
|
52
|
+
setIsDarkMode(theme === 'dark');
|
|
53
|
+
}
|
|
54
|
+
}, [theme]);
|
|
55
|
+
|
|
56
|
+
// Fetch ad when opened
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
if (!isOpen) {
|
|
59
|
+
// Reset state when closed
|
|
60
|
+
setAdData(null);
|
|
61
|
+
setIsLoading(false);
|
|
62
|
+
setHasError(false);
|
|
63
|
+
setCanClose(false);
|
|
64
|
+
setCountdown(closeDelay);
|
|
65
|
+
impressionTrackedRef.current = false;
|
|
66
|
+
|
|
67
|
+
if (closeTimerRef.current) clearTimeout(closeTimerRef.current);
|
|
68
|
+
if (countdownRef.current) clearInterval(countdownRef.current);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
let isMounted = true;
|
|
73
|
+
setIsLoading(true);
|
|
74
|
+
|
|
75
|
+
AdTogether.fetchAd(adUnitId, 'interstitial')
|
|
76
|
+
.then((ad) => {
|
|
77
|
+
if (isMounted) {
|
|
78
|
+
setAdData(ad);
|
|
79
|
+
setIsLoading(false);
|
|
80
|
+
onAdLoaded?.();
|
|
81
|
+
}
|
|
82
|
+
})
|
|
83
|
+
.catch((err) => {
|
|
84
|
+
if (isMounted) {
|
|
85
|
+
console.error('AdTogether Failed to load interstitial:', err);
|
|
86
|
+
setHasError(true);
|
|
87
|
+
setIsLoading(false);
|
|
88
|
+
onAdFailedToLoad?.(err);
|
|
89
|
+
onClose();
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
return () => {
|
|
94
|
+
isMounted = false;
|
|
95
|
+
};
|
|
96
|
+
}, [isOpen, adUnitId]);
|
|
97
|
+
|
|
98
|
+
// Close delay countdown
|
|
99
|
+
useEffect(() => {
|
|
100
|
+
if (!isOpen || !adData) return;
|
|
101
|
+
|
|
102
|
+
setCountdown(closeDelay);
|
|
103
|
+
|
|
104
|
+
countdownRef.current = setInterval(() => {
|
|
105
|
+
setCountdown((prev) => {
|
|
106
|
+
if (prev <= 1) {
|
|
107
|
+
if (countdownRef.current) clearInterval(countdownRef.current);
|
|
108
|
+
setCanClose(true);
|
|
109
|
+
return 0;
|
|
110
|
+
}
|
|
111
|
+
return prev - 1;
|
|
112
|
+
});
|
|
113
|
+
}, 1000);
|
|
114
|
+
|
|
115
|
+
return () => {
|
|
116
|
+
if (countdownRef.current) clearInterval(countdownRef.current);
|
|
117
|
+
};
|
|
118
|
+
}, [isOpen, adData, closeDelay]);
|
|
119
|
+
|
|
120
|
+
// Impression tracking
|
|
121
|
+
useEffect(() => {
|
|
122
|
+
if (!adData || !isOpen || impressionTrackedRef.current) return;
|
|
123
|
+
impressionTrackedRef.current = true;
|
|
124
|
+
AdTogether.trackImpression(adData.id, adData.token);
|
|
125
|
+
}, [adData, isOpen]);
|
|
126
|
+
|
|
127
|
+
const handleAdClick = useCallback(() => {
|
|
128
|
+
if (!adData) return;
|
|
129
|
+
AdTogether.trackClick(adData.id, adData.token);
|
|
130
|
+
if (adData.clickUrl) {
|
|
131
|
+
window.open(adData.clickUrl, '_blank', 'noopener,noreferrer');
|
|
132
|
+
}
|
|
133
|
+
}, [adData]);
|
|
134
|
+
|
|
135
|
+
const handleClose = useCallback(() => {
|
|
136
|
+
if (canClose) {
|
|
137
|
+
onClose();
|
|
138
|
+
}
|
|
139
|
+
}, [canClose, onClose]);
|
|
140
|
+
|
|
141
|
+
// Block body scroll when open
|
|
142
|
+
useEffect(() => {
|
|
143
|
+
if (isOpen) {
|
|
144
|
+
document.body.style.overflow = 'hidden';
|
|
145
|
+
} else {
|
|
146
|
+
document.body.style.overflow = '';
|
|
147
|
+
}
|
|
148
|
+
return () => {
|
|
149
|
+
document.body.style.overflow = '';
|
|
150
|
+
};
|
|
151
|
+
}, [isOpen]);
|
|
152
|
+
|
|
153
|
+
if (!isOpen) return null;
|
|
154
|
+
|
|
155
|
+
const bgOverlay = 'rgba(0, 0, 0, 0.7)';
|
|
156
|
+
const cardBg = isDarkMode ? '#1F2937' : '#ffffff';
|
|
157
|
+
const borderColor = isDarkMode ? 'rgba(255,255,255,0.1)' : 'rgba(0,0,0,0.1)';
|
|
158
|
+
const textColor = isDarkMode ? '#F9FAFB' : '#111827';
|
|
159
|
+
const descColor = isDarkMode ? '#9CA3AF' : '#6B7280';
|
|
160
|
+
|
|
161
|
+
const content = (
|
|
162
|
+
<div
|
|
163
|
+
className="adtogether-interstitial-overlay"
|
|
164
|
+
style={{
|
|
165
|
+
position: 'fixed',
|
|
166
|
+
top: 0,
|
|
167
|
+
left: 0,
|
|
168
|
+
width: '100vw',
|
|
169
|
+
height: '100vh',
|
|
170
|
+
backgroundColor: bgOverlay,
|
|
171
|
+
display: 'flex',
|
|
172
|
+
alignItems: 'center',
|
|
173
|
+
justifyContent: 'center',
|
|
174
|
+
zIndex: 100000,
|
|
175
|
+
backdropFilter: 'blur(8px)',
|
|
176
|
+
animation: 'adtogether-fade-in 0.3s ease-out',
|
|
177
|
+
}}
|
|
178
|
+
onClick={(e) => {
|
|
179
|
+
if (e.target === e.currentTarget && canClose) handleClose();
|
|
180
|
+
}}
|
|
181
|
+
>
|
|
182
|
+
<style>{`
|
|
183
|
+
@keyframes adtogether-fade-in {
|
|
184
|
+
from { opacity: 0; }
|
|
185
|
+
to { opacity: 1; }
|
|
186
|
+
}
|
|
187
|
+
@keyframes adtogether-scale-in {
|
|
188
|
+
from { opacity: 0; transform: scale(0.9); }
|
|
189
|
+
to { opacity: 1; transform: scale(1); }
|
|
190
|
+
}
|
|
191
|
+
.adtogether-interstitial-card {
|
|
192
|
+
display: flex;
|
|
193
|
+
flex-direction: column;
|
|
194
|
+
}
|
|
195
|
+
.adtogether-interstitial-img-wrapper {
|
|
196
|
+
width: 100%;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
@media (orientation: landscape) and (max-height: 600px) {
|
|
200
|
+
.adtogether-interstitial-card {
|
|
201
|
+
flex-direction: row;
|
|
202
|
+
max-height: 90vh;
|
|
203
|
+
}
|
|
204
|
+
.adtogether-interstitial-img-wrapper {
|
|
205
|
+
width: 50%;
|
|
206
|
+
flex-shrink: 0;
|
|
207
|
+
display: flex;
|
|
208
|
+
}
|
|
209
|
+
.adtogether-interstitial-img-wrapper img {
|
|
210
|
+
height: 100% !important;
|
|
211
|
+
aspect-ratio: auto !important;
|
|
212
|
+
}
|
|
213
|
+
.adtogether-interstitial-content {
|
|
214
|
+
width: 50%;
|
|
215
|
+
overflow-y: auto;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
`}</style>
|
|
219
|
+
|
|
220
|
+
{isLoading ? (
|
|
221
|
+
<div style={{ color: '#fff', fontSize: '18px' }}>Loading Ad...</div>
|
|
222
|
+
) : adData ? (
|
|
223
|
+
<div
|
|
224
|
+
className="adtogether-interstitial-card"
|
|
225
|
+
style={{
|
|
226
|
+
position: 'relative',
|
|
227
|
+
maxWidth: '800px',
|
|
228
|
+
width: '95%',
|
|
229
|
+
backgroundColor: cardBg,
|
|
230
|
+
borderRadius: '24px',
|
|
231
|
+
border: `1px solid ${borderColor}`,
|
|
232
|
+
overflow: 'hidden',
|
|
233
|
+
boxShadow: '0 25px 50px rgba(0, 0, 0, 0.5)',
|
|
234
|
+
animation: 'adtogether-scale-in 0.3s ease-out',
|
|
235
|
+
}}
|
|
236
|
+
>
|
|
237
|
+
{/* Close / Countdown Button */}
|
|
238
|
+
<div style={{ position: 'absolute', top: '12px', right: '12px', zIndex: 10 }}>
|
|
239
|
+
{canClose ? (
|
|
240
|
+
<button
|
|
241
|
+
onClick={handleClose}
|
|
242
|
+
style={{
|
|
243
|
+
width: '36px',
|
|
244
|
+
height: '36px',
|
|
245
|
+
borderRadius: '50%',
|
|
246
|
+
backgroundColor: 'rgba(0,0,0,0.6)',
|
|
247
|
+
border: 'none',
|
|
248
|
+
color: '#fff',
|
|
249
|
+
fontSize: '18px',
|
|
250
|
+
cursor: 'pointer',
|
|
251
|
+
display: 'flex',
|
|
252
|
+
alignItems: 'center',
|
|
253
|
+
justifyContent: 'center',
|
|
254
|
+
backdropFilter: 'blur(4px)',
|
|
255
|
+
}}
|
|
256
|
+
aria-label="Close ad"
|
|
257
|
+
>
|
|
258
|
+
✕
|
|
259
|
+
</button>
|
|
260
|
+
) : (
|
|
261
|
+
<div
|
|
262
|
+
style={{
|
|
263
|
+
width: '36px',
|
|
264
|
+
height: '36px',
|
|
265
|
+
borderRadius: '50%',
|
|
266
|
+
backgroundColor: 'rgba(0,0,0,0.6)',
|
|
267
|
+
color: '#fff',
|
|
268
|
+
fontSize: '14px',
|
|
269
|
+
fontWeight: 'bold',
|
|
270
|
+
display: 'flex',
|
|
271
|
+
alignItems: 'center',
|
|
272
|
+
justifyContent: 'center',
|
|
273
|
+
backdropFilter: 'blur(4px)',
|
|
274
|
+
}}
|
|
275
|
+
>
|
|
276
|
+
{countdown}
|
|
277
|
+
</div>
|
|
278
|
+
)}
|
|
279
|
+
</div>
|
|
280
|
+
|
|
281
|
+
{/* Ad Image */}
|
|
282
|
+
{adData.imageUrl && (
|
|
283
|
+
<div
|
|
284
|
+
className="adtogether-interstitial-img-wrapper"
|
|
285
|
+
style={{ cursor: 'pointer', position: 'relative', backgroundColor: isDarkMode ? '#111827' : '#F3F4F6' }}
|
|
286
|
+
onClick={handleAdClick}
|
|
287
|
+
>
|
|
288
|
+
<img
|
|
289
|
+
src={adData.imageUrl}
|
|
290
|
+
alt={adData.title}
|
|
291
|
+
style={{
|
|
292
|
+
width: '100%',
|
|
293
|
+
maxHeight: '350px',
|
|
294
|
+
objectFit: 'contain',
|
|
295
|
+
display: 'block',
|
|
296
|
+
}}
|
|
297
|
+
/>
|
|
298
|
+
</div>
|
|
299
|
+
)}
|
|
300
|
+
|
|
301
|
+
{/* Ad Content */}
|
|
302
|
+
<div
|
|
303
|
+
className="adtogether-interstitial-content"
|
|
304
|
+
style={{ padding: '20px', cursor: 'pointer' }}
|
|
305
|
+
onClick={handleAdClick}
|
|
306
|
+
>
|
|
307
|
+
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', marginBottom: '8px' }}>
|
|
308
|
+
<span style={{ fontWeight: 'bold', fontSize: '18px', color: textColor }}>
|
|
309
|
+
{adData.title}
|
|
310
|
+
</span>
|
|
311
|
+
<span
|
|
312
|
+
style={{
|
|
313
|
+
backgroundColor: '#FBBF24',
|
|
314
|
+
color: '#000',
|
|
315
|
+
fontSize: '10px',
|
|
316
|
+
fontWeight: 'bold',
|
|
317
|
+
padding: '3px 6px',
|
|
318
|
+
borderRadius: '4px',
|
|
319
|
+
marginLeft: '8px',
|
|
320
|
+
flexShrink: 0,
|
|
321
|
+
}}
|
|
322
|
+
>
|
|
323
|
+
AD
|
|
324
|
+
</span>
|
|
325
|
+
</div>
|
|
326
|
+
<p style={{ fontSize: '14px', color: descColor, margin: 0, lineHeight: 1.5 }}>
|
|
327
|
+
{adData.description}
|
|
328
|
+
</p>
|
|
329
|
+
|
|
330
|
+
{/* CTA Button */}
|
|
331
|
+
<button
|
|
332
|
+
style={{
|
|
333
|
+
marginTop: '16px',
|
|
334
|
+
width: '100%',
|
|
335
|
+
padding: '12px',
|
|
336
|
+
backgroundColor: '#F59E0B',
|
|
337
|
+
color: '#000',
|
|
338
|
+
fontWeight: 'bold',
|
|
339
|
+
fontSize: '14px',
|
|
340
|
+
border: 'none',
|
|
341
|
+
borderRadius: '12px',
|
|
342
|
+
cursor: 'pointer',
|
|
343
|
+
}}
|
|
344
|
+
onClick={(e) => {
|
|
345
|
+
e.stopPropagation();
|
|
346
|
+
handleAdClick();
|
|
347
|
+
}}
|
|
348
|
+
>
|
|
349
|
+
Learn More →
|
|
350
|
+
</button>
|
|
351
|
+
<p
|
|
352
|
+
style={{
|
|
353
|
+
marginTop: '12px',
|
|
354
|
+
marginBottom: 0,
|
|
355
|
+
fontSize: '10px',
|
|
356
|
+
color: isDarkMode ? 'rgba(255,255,255,0.3)' : 'rgba(0,0,0,0.3)',
|
|
357
|
+
textAlign: 'center',
|
|
358
|
+
letterSpacing: '0.3px',
|
|
359
|
+
}}
|
|
360
|
+
>
|
|
361
|
+
Powered by AdTogether
|
|
362
|
+
</p>
|
|
363
|
+
</div>
|
|
364
|
+
</div>
|
|
365
|
+
) : null}
|
|
366
|
+
</div>
|
|
367
|
+
);
|
|
368
|
+
|
|
369
|
+
return createPortal(content, document.body);
|
|
370
|
+
};
|