Skip to content

Commit c0b421d

Browse files
committed
docs: banner mod
1 parent b4ff291 commit c0b421d

File tree

3 files changed

+74
-31
lines changed

3 files changed

+74
-31
lines changed

website/docusaurus.config.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@ const config = {
3131
{
3232
src: 'https://cloud.umami.is/script.js',
3333
'data-website-id': 'c15ead0d-e003-4fe0-8647-ddf0b560e38c',
34-
defer: true,
35-
},
36-
{
37-
src: 'https://media.ethicalads.io/media/client/ethicalads.min.js',
38-
async: true,
39-
type: 'text/javascript',
34+
defer: true
4035
}
4136
],
4237

website/src/css/custom.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ html[data-theme='dark'] .docusaurus-highlight-code-line {
5151
justify-content: space-between;
5252
}
5353

54+
#bwndw {
55+
transition: opacity 0.3s ease-in-out;
56+
opacity: 1;
57+
58+
&.bwndw-loading {
59+
opacity: 0;
60+
}
61+
}
62+
5463
.eab {
5564
margin: calc(var(--ifm-menu-link-padding-horizontal) + 0.5rem) 0 0 -0.5rem;
5665
}

website/src/theme/DocSidebar/index.js

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,57 @@ import Cookies from 'js-cookie';
66
const COOKIE_NAME = 'bwndw_fallback_cached';
77
const COOKIE_DURATION = 28; // days (4 weeks)
88

9-
async function shouldUseFallback() {
9+
function saveUseFallback(value) {
10+
Cookies.set(COOKIE_NAME, value.toString(), {
11+
expires: COOKIE_DURATION,
12+
sameSite: 'lax'
13+
});
14+
}
15+
16+
function readUseFallback() {
1017
const cached = Cookies.get(COOKIE_NAME);
1118

12-
if (cached !== undefined) {
13-
return cached === 'true';
14-
}
19+
return cached === undefined
20+
? null
21+
: cached === 'true';
22+
}
1523

16-
while (typeof ethicalads === 'undefined') {
17-
await new Promise((resolve) => setTimeout(resolve, 300));
18-
}
24+
let scriptPromise = null;
1925

20-
const placements = await ethicalads.wait;
21-
const useFallback = !placements.length || placements[0].response.campaign_type !== 'paid';
26+
async function loadEthicalAdsScript() {
27+
let script = document.getElementById('ethical-script');
2228

23-
Cookies.set(COOKIE_NAME, useFallback.toString(), {
24-
expires: COOKIE_DURATION,
25-
sameSite: 'lax'
26-
});
29+
if (!script) {
30+
script = document.createElement('script');
31+
script.id = 'ethical-script';
32+
script.src = 'https://media.ethicalads.io/media/client/ethicalads.min.js';
33+
script.async = true;
34+
document.head.appendChild(script);
35+
36+
scriptPromise = (async () => {
37+
while (typeof ethicalads === 'undefined') {
38+
await new Promise((resolve) => setTimeout(resolve, 100));
39+
}
40+
41+
return (await ethicalads.wait) || [];
42+
})()
43+
}
2744

28-
return useFallback;
45+
return scriptPromise
2946
}
3047

3148
function createEthicalAdsBlock(root) {
3249
const banner = document.createElement('div');
3350

34-
banner.className = 'eab flat horizontal'
51+
banner.className = 'eab flat horizontal bwndw-loading'
3552
banner.id = 'bwndw';
3653
banner.setAttribute('data-ea-publisher', 'react-chartjs-2jsorg');
3754
banner.setAttribute('data-ea-type', 'image');
3855

3956
root?.appendChild(banner);
4057

4158
if (typeof ethicalads !== 'undefined') {
42-
ethicalads.reload();
59+
ethicalads.load();
4360
}
4461

4562
return banner;
@@ -49,7 +66,7 @@ function createCarbonAdsBlock(root) {
4966
const banner = document.createElement('div');
5067
const script = document.createElement('script');
5168

52-
banner.className = 'crbn';
69+
banner.className = 'crbn bwndw-loading';
5370
banner.id = 'bwndw';
5471

5572
script.src = '//cdn.carbonads.com/carbon.js?serve=CWBDT53N&placement=react-chartjs-2jsorg&format=cover';
@@ -75,14 +92,36 @@ export default function DocSidebar(props) {
7592

7693
useEffect(() => {
7794
if (!document.getElementById('bwndw')) {
78-
shouldUseFallback().then((useFallback) => {
79-
if (!document.getElementById('bwndw')) {
80-
const root = document.querySelector('.theme-doc-sidebar-menu')?.parentElement;
81-
82-
bannerRef.current = useFallback ? createCarbonAdsBlock(root) : createEthicalAdsBlock(root);
83-
setColorMode(bannerRef.current, colorModeRef.current);
84-
}
85-
});
95+
const root = document.querySelector('.theme-doc-sidebar-menu')?.parentElement;
96+
let banner
97+
const showBanner = () => {
98+
setColorMode(banner, colorModeRef.current);
99+
bannerRef.current = banner;
100+
banner.classList.remove('bwndw-loading');
101+
}
102+
const cachedUseFallback = readUseFallback();
103+
104+
if (cachedUseFallback === true) {
105+
banner = createCarbonAdsBlock(root);
106+
showBanner();
107+
} else {
108+
banner = createEthicalAdsBlock(root);
109+
110+
loadEthicalAdsScript().then((placements) => {
111+
if (cachedUseFallback === null) {
112+
const useFallback = !placements.length || placements[0].response.campaign_type !== 'paid';
113+
114+
if (useFallback) {
115+
banner.remove();
116+
banner = createCarbonAdsBlock(root);
117+
}
118+
119+
saveUseFallback(useFallback);
120+
}
121+
122+
showBanner();
123+
})
124+
}
86125
}
87126
}, []);
88127

0 commit comments

Comments
 (0)