Skip to content

Commit eae362a

Browse files
avivkellerovflowdbmuenzenmeyermcollina
committed
chore: carbon ads
Co-authored-by: Claudio Wunder <cwunder@gnome.org> Co-authored-by: Brian Muenzenmeyer <brian.muenzenmeyer@gmail.com> Co-authored-by: Matteo Collina <hello@matteocollina.com>
1 parent 53d777b commit eae362a

14 files changed

Lines changed: 356 additions & 1 deletion

File tree

‎.changeset/smart-lilies-invent.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@node-core/ui-components': minor
3+
---
4+
5+
Adds a footer slot to the metabar
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use client';
2+
3+
import classNames from 'classnames';
4+
5+
import useCarbonAd, { adColors } from '#site/hooks/useCarbonAd';
6+
7+
import type { FC } from 'react';
8+
9+
import styles from './index.module.css';
10+
11+
const CarbonSidebar: FC = () => {
12+
const { ad, ref } = useCarbonAd('CWBI6KJU', '(min-width: 1280px)');
13+
14+
if (!ad) {
15+
return null;
16+
}
17+
18+
return (
19+
<aside
20+
ref={ref}
21+
className={styles.sideBar}
22+
style={adColors(ad)}
23+
aria-label="Advertisement"
24+
>
25+
<a
26+
href={ad.statlink}
27+
target="_blank"
28+
rel="sponsored noopener"
29+
className={styles.sideBarLink}
30+
>
31+
{/* eslint-disable-next-line @next/next/no-img-element */}
32+
<img src={ad.largeImage} alt={ad.company} className={styles.image} />
33+
<span className={styles.sideBarBody}>
34+
{ad.title && <strong>{ad.title}</strong>}
35+
<span>{ad.description}</span>
36+
<span className={styles.sideBarCta}>{ad.callToAction}</span>
37+
</span>
38+
</a>
39+
40+
<a
41+
href={ad.ad_via_link}
42+
className={classNames(styles.badge, styles.overlay)}
43+
>
44+
Ad
45+
</a>
46+
</aside>
47+
);
48+
};
49+
50+
export default CarbonSidebar;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use client';
2+
3+
import useCarbonAd, { adColors } from '#site/hooks/useCarbonAd';
4+
5+
import type { FC } from 'react';
6+
7+
import styles from './index.module.css';
8+
9+
const CarbonTopBar: FC = () => {
10+
const { ad, ref } = useCarbonAd('CWBI6KJY');
11+
12+
if (!ad) {
13+
return null;
14+
}
15+
16+
return (
17+
<aside
18+
ref={ref}
19+
className={styles.topBar}
20+
style={adColors(ad)}
21+
aria-label="Advertisement"
22+
>
23+
<a
24+
href={ad.statlink}
25+
target="_blank"
26+
rel="sponsored noopener"
27+
className={styles.topBarLink}
28+
>
29+
{/* eslint-disable-next-line @next/next/no-img-element */}
30+
<img src={ad.image} alt={ad.company} className={styles.logo} />
31+
<span>{ad.description}</span>
32+
<span className={styles.cta}>{ad.callToAction}</span>
33+
</a>
34+
35+
<a href={ad.ad_via_link} className={styles.badge}>
36+
Ad
37+
</a>
38+
</aside>
39+
);
40+
};
41+
42+
export default CarbonTopBar;
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
@reference "../../styles/index.css";
2+
3+
.topBar,
4+
.sideBar {
5+
@apply relative
6+
bg-(--ad-bg)
7+
text-(color:--ad-text)
8+
dark:bg-(--ad-bg-dark)
9+
dark:text-(color:--ad-text-dark);
10+
11+
& a {
12+
@apply text-inherit
13+
no-underline;
14+
}
15+
}
16+
17+
.badge {
18+
@apply shrink-0
19+
rounded-sm
20+
border
21+
border-current
22+
px-1.5
23+
text-xs
24+
leading-4
25+
opacity-70
26+
hover:opacity-100;
27+
}
28+
29+
.topBar {
30+
@apply mb-4
31+
flex
32+
items-center
33+
gap-3
34+
rounded-md
35+
px-4
36+
py-2
37+
text-sm;
38+
}
39+
40+
.topBarLink {
41+
@apply flex
42+
flex-1
43+
items-center
44+
gap-3;
45+
}
46+
47+
.logo {
48+
@apply size-8
49+
shrink-0
50+
rounded-sm;
51+
}
52+
53+
.cta {
54+
@apply hidden
55+
shrink-0
56+
rounded-md
57+
bg-(--ad-cta-bg)
58+
px-3
59+
py-1
60+
text-xs
61+
font-semibold
62+
whitespace-nowrap
63+
text-(color:--ad-cta-text)
64+
sm:block
65+
dark:bg-(--ad-cta-bg-dark)
66+
dark:text-(color:--ad-cta-text-dark);
67+
}
68+
69+
.sideBar {
70+
@apply w-44
71+
overflow-hidden
72+
rounded-lg;
73+
}
74+
75+
.sideBarLink {
76+
@apply flex
77+
flex-col;
78+
}
79+
80+
.image {
81+
@apply aspect-square
82+
w-full
83+
object-cover;
84+
}
85+
86+
.sideBarBody {
87+
@apply flex
88+
flex-col
89+
items-start
90+
gap-1
91+
p-3.5
92+
text-xs;
93+
}
94+
95+
.sideBarCta {
96+
@apply mt-2
97+
rounded-full
98+
border
99+
border-current
100+
px-3
101+
py-1
102+
font-semibold;
103+
}
104+
105+
.overlay {
106+
@apply absolute
107+
top-2
108+
right-2
109+
border-transparent
110+
bg-neutral-950/50
111+
text-white;
112+
}

‎apps/site/components/MetaBar.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { defaultLocale } from '@node-core/website-i18n';
66
import { useFormatter, useLocale, useTranslations } from 'next-intl';
77

88
import AvatarGroup from '#site/components/AvatarGroup';
9+
import CarbonSidebar from '#site/components/CarbonAds/Sidebar';
910
import Link from '#site/components/Link';
1011
import useClientContext from '#site/hooks/useClientContext';
1112
import useMediaQuery from '#site/hooks/useMediaQuery';
@@ -74,6 +75,7 @@ const MetaBar: FC = () => {
7475
),
7576
}}
7677
headings={{ items: headings }}
78+
footer={<CarbonSidebar />}
7779
/>
7880
);
7981
};

‎apps/site/hooks/useCarbonAd.ts‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
'use client';
2+
3+
import { usePathname } from 'next/navigation';
4+
import { useEffect, useRef, useState } from 'react';
5+
6+
import useMediaQuery from '#site/hooks/useMediaQuery';
7+
import {
8+
CARBON_ADS_COUNT_IMPRESSIONS,
9+
CARBON_ADS_URL,
10+
} from '#site/next.constants.mjs';
11+
12+
import type { CSSProperties } from 'react';
13+
14+
export type CarbonAd = Record<string, string>;
15+
16+
export const adColors = (ad: CarbonAd) =>
17+
({
18+
'--ad-bg': ad.backgroundColor,
19+
'--ad-bg-dark': ad.backgroundColorDark ?? ad.backgroundColor,
20+
'--ad-text': ad.textColor,
21+
'--ad-text-dark': ad.textColorDark ?? ad.textColor,
22+
'--ad-cta-bg': ad.ctaBackgroundColor,
23+
'--ad-cta-bg-dark': ad.ctaBackgroundColorDark ?? ad.ctaBackgroundColor,
24+
'--ad-cta-text': ad.ctaTextColor,
25+
'--ad-cta-text-dark': ad.ctaTextColorDark ?? ad.ctaTextColor,
26+
}) as CSSProperties;
27+
28+
const useCarbonAd = (zone: string, media?: string) => {
29+
const [ad, setAd] = useState<CarbonAd | null>(null);
30+
const ref = useRef<HTMLElement>(null);
31+
// Layouts persist across navigations, so refetch per page
32+
const pathname = usePathname();
33+
const matches = useMediaQuery(media ?? 'all');
34+
35+
useEffect(() => {
36+
if (!matches) {
37+
return;
38+
}
39+
40+
const controller = new AbortController();
41+
const url = new URL(`${zone}.json`, CARBON_ADS_URL);
42+
43+
if (!CARBON_ADS_COUNT_IMPRESSIONS) {
44+
url.searchParams.set('ignore', 'yes');
45+
}
46+
47+
fetch(url, { signal: controller.signal })
48+
.then(response => (response.ok ? response.json() : null))
49+
.then(data => data?.ads?.[0]?.statlink && setAd(data.ads[0]))
50+
.catch(() => {});
51+
52+
return () => {
53+
controller.abort();
54+
setAd(null);
55+
};
56+
}, [zone, matches, pathname]);
57+
58+
useEffect(() => {
59+
if (!ad?.statview || !ref.current) {
60+
return;
61+
}
62+
63+
let timer: ReturnType<typeof setTimeout>;
64+
65+
const observer = new IntersectionObserver(
66+
entries => {
67+
clearTimeout(timer);
68+
69+
if (entries.at(-1)!.intersectionRatio >= 0.5) {
70+
timer = setTimeout(() => {
71+
new Image().src = ad.statview;
72+
observer.disconnect();
73+
}, 1000);
74+
}
75+
},
76+
{ threshold: 0.5 }
77+
);
78+
79+
observer.observe(ref.current);
80+
81+
return () => {
82+
clearTimeout(timer);
83+
observer.disconnect();
84+
};
85+
}, [ad]);
86+
87+
return { ad, ref };
88+
};
89+
90+
export default useCarbonAd;

‎apps/site/layouts/About.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Article from '@node-core/ui-components/Containers/Article';
22

33
import Breadcrumbs from '#site/components/Breadcrumbs';
4+
import CarbonTopBar from '#site/components/CarbonAds/TopBar';
45
import Footer from '#site/components/Footer';
56
import MetaBar from '#site/components/MetaBar';
67
import NavBar from '#site/components/NavBar';
@@ -17,6 +18,8 @@ const AboutLayout: FC<PropsWithChildren> = ({ children }) => (
1718

1819
<div>
1920
<main id="main" tabIndex={-1}>
21+
<CarbonTopBar />
22+
2023
{children}
2124
</main>
2225

‎apps/site/layouts/ArticlePage.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Article from '@node-core/ui-components/Containers/Article';
22

3+
import CarbonTopBar from '#site/components/CarbonAds/TopBar';
34
import Footer from '#site/components/Footer';
45
import MetaBar from '#site/components/MetaBar';
56
import NavBar from '#site/components/NavBar';
@@ -16,6 +17,8 @@ const ArticlePageLayout: FC<PropsWithChildren> = ({ children }) => (
1617

1718
<div>
1819
<main id="main" tabIndex={-1}>
20+
<CarbonTopBar />
21+
1922
{children}
2023
</main>
2124

‎apps/site/layouts/Blog.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useTranslations } from 'next-intl';
33
import { getClientContext } from '#site/client-context';
44
import BlogHeader from '#site/components/Blog/BlogHeader';
55
import BlogCategories from '#site/components/BlogCategories';
6+
import CarbonTopBar from '#site/components/CarbonAds/TopBar';
67
import Footer from '#site/components/Footer';
78
import NavBar from '#site/components/NavBar';
89
import { getBlogData } from '#site/util/blog';
@@ -55,6 +56,8 @@ const BlogLayout: FC = () => {
5556

5657
<div className={styles.blogLayout}>
5758
<main id="main" tabIndex={-1}>
59+
<CarbonTopBar />
60+
5861
<BlogHeader category={blogData.category} />
5962

6063
<BlogCategories

‎apps/site/layouts/Post.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Preview from '@node-core/ui-components/Common/Preview';
33
import { getClientContext } from '#site/client-context';
44
import AvatarGroup from '#site/components/AvatarGroup';
55
import BlogCrossLinks from '#site/components/BlogCrossLinks';
6+
import CarbonTopBar from '#site/components/CarbonAds/TopBar';
67
import EOLAlert from '#site/components/EOL/EOLAlert';
78
import Footer from '#site/components/Footer';
89
import MetaBar from '#site/components/MetaBar';
@@ -32,6 +33,8 @@ const PostLayout: FC<PropsWithChildren> = ({ children }) => {
3233
<main id="main" tabIndex={-1}>
3334
{type === 'vulnerability' && <EOLAlert />}
3435

36+
<CarbonTopBar />
37+
3538
<h1>{frontmatter.title}</h1>
3639

3740
<section>

0 commit comments

Comments
 (0)