-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathindex.jsx
More file actions
76 lines (64 loc) · 1.99 KB
/
index.jsx
File metadata and controls
76 lines (64 loc) · 1.99 KB
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
import { ArrowUpRightIcon } from '@heroicons/react/24/outline';
import Banner from '@node-core/ui-components/Common/Banner';
import { useEffect, useState } from 'preact/hooks';
import { isBannerActive } from '../../utils/banner.mjs';
/** @import { BannerEntry, RemoteConfig } from './types.d.ts' */
/**
* Asynchronously fetches and displays announcement banners from the remote config.
* Global banners are rendered above version-specific ones.
* Non-blocking: silently ignores fetch/parse failures.
*
* @param {{ remoteConfig: string, versionMajor: number | null }} props
*/
export default ({ remoteConfig, versionMajor }) => {
const [banners, setBanners] = useState(/** @type {BannerEntry[]} */ ([]));
useEffect(() => {
if (!remoteConfig) {
return;
}
fetch(remoteConfig, {
signal: AbortSignal.timeout(2500),
})
.then(async res => {
if (!res.ok) {
return;
}
/** @type {RemoteConfig} */
const config = await res.json();
const active = [];
const globalBanner = config.global?.banner;
if (globalBanner && isBannerActive(globalBanner)) {
active.push(globalBanner);
}
if (versionMajor != null) {
const versionBanner = config[`v${versionMajor}`]?.banner;
if (versionBanner && isBannerActive(versionBanner)) {
active.push(versionBanner);
}
}
setBanners(active);
})
.catch(error => {
console.error(error);
});
}, []);
if (!banners.length) {
return null;
}
return (
<div role="region" aria-label="Announcements">
{banners.map(banner => (
<Banner key={banner.text ?? banner.text} type={banner.type}>
{banner.link ? (
<a href={banner.link} target="_blank" rel="noopener noreferrer">
{banner.text}
<ArrowUpRightIcon />
</a>
) : (
banner.text
)}
</Banner>
))}
</div>
);
};