-
Notifications
You must be signed in to change notification settings - Fork 31
feat: create announcements banner #617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
araujogui
wants to merge
5
commits into
main
Choose a base branch
from
feat/banners
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
src/generators/web/ui/components/AnnouncementBanner/__tests__/fetchBanners.test.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { describe, it } from 'node:test'; | ||
|
|
||
| import { fetchBanners } from '../fetchBanners.mjs'; | ||
|
|
||
| const PAST = new Date(Date.now() - 86_400_000).toISOString(); // yesterday | ||
| const FUTURE = new Date(Date.now() + 86_400_000).toISOString(); // tomorrow | ||
|
|
||
| const makeResponse = (banners, ok = true) => ({ | ||
| ok, | ||
| json: async () => ({ websiteBanners: banners }), | ||
| }); | ||
|
|
||
| describe('fetchBanners', () => { | ||
| describe('fetch behavior', () => { | ||
| it('fetches from the given URL', async t => { | ||
| t.mock.method(global, 'fetch', () => Promise.resolve(makeResponse({}))); | ||
|
|
||
| await fetchBanners('https://example.com/site.json', null); | ||
|
|
||
| assert.equal(global.fetch.mock.calls.length, 1); | ||
| assert.equal( | ||
| global.fetch.mock.calls[0].arguments[0], | ||
| 'https://example.com/site.json' | ||
| ); | ||
| }); | ||
|
|
||
| it('returns an empty array on non-ok response', async t => { | ||
| t.mock.method(global, 'fetch', () => | ||
| Promise.resolve(makeResponse({}, false)) | ||
| ); | ||
|
|
||
| const result = await fetchBanners('https://example.com/site.json', null); | ||
|
|
||
| assert.deepEqual(result, []); | ||
| }); | ||
|
|
||
| it('propagates fetch errors to the caller', async t => { | ||
| t.mock.method(global, 'fetch', () => | ||
| Promise.reject(new Error('Network error')) | ||
| ); | ||
|
|
||
| await assert.rejects( | ||
| () => fetchBanners('https://example.com/site.json', null), | ||
| { message: 'Network error' } | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('banner selection', () => { | ||
| it('returns the active global (index) banner', async t => { | ||
| const banner = { text: 'Global banner', type: 'warning' }; | ||
| t.mock.method(global, 'fetch', () => | ||
| Promise.resolve(makeResponse({ index: banner })) | ||
| ); | ||
|
|
||
| const result = await fetchBanners('https://example.com/site.json', null); | ||
|
|
||
| assert.deepEqual(result, [banner]); | ||
| }); | ||
|
|
||
| it('returns the active version-specific banner', async t => { | ||
| const banner = { text: 'v20 banner', type: 'warning' }; | ||
| t.mock.method(global, 'fetch', () => | ||
| Promise.resolve(makeResponse({ v20: banner })) | ||
| ); | ||
|
|
||
| const result = await fetchBanners('https://example.com/site.json', 20); | ||
|
|
||
| assert.deepEqual(result, [banner]); | ||
| }); | ||
|
|
||
| it('returns both global and version banners when both are active', async t => { | ||
| const globalBanner = { text: 'Global banner', type: 'warning' }; | ||
| const versionBanner = { text: 'v20 banner', type: 'error' }; | ||
| t.mock.method(global, 'fetch', () => | ||
| Promise.resolve( | ||
| makeResponse({ index: globalBanner, v20: versionBanner }) | ||
| ) | ||
| ); | ||
|
|
||
| const result = await fetchBanners('https://example.com/site.json', 20); | ||
|
|
||
| assert.deepEqual(result, [globalBanner, versionBanner]); | ||
| }); | ||
|
|
||
| it('returns global banner first, version banner second', async t => { | ||
| const globalBanner = { text: 'Global', type: 'warning' }; | ||
| const versionBanner = { text: 'v22', type: 'error' }; | ||
| t.mock.method(global, 'fetch', () => | ||
| Promise.resolve( | ||
| makeResponse({ index: globalBanner, v22: versionBanner }) | ||
| ) | ||
| ); | ||
|
|
||
| const result = await fetchBanners('https://example.com/site.json', 22); | ||
|
|
||
| assert.equal(result[0], globalBanner); | ||
| assert.equal(result[1], versionBanner); | ||
| }); | ||
|
|
||
| it('does not include the version banner when versionMajor is null', async t => { | ||
| const globalBanner = { text: 'Global banner', type: 'warning' }; | ||
| const versionBanner = { text: 'v20 banner', type: 'error' }; | ||
| t.mock.method(global, 'fetch', () => | ||
| Promise.resolve( | ||
| makeResponse({ index: globalBanner, v20: versionBanner }) | ||
| ) | ||
| ); | ||
|
|
||
| const result = await fetchBanners('https://example.com/site.json', null); | ||
|
|
||
| assert.deepEqual(result, [globalBanner]); | ||
| }); | ||
|
|
||
| it('returns an empty array when websiteBanners is absent', async t => { | ||
| t.mock.method(global, 'fetch', () => | ||
| Promise.resolve({ ok: true, json: async () => ({}) }) | ||
| ); | ||
|
|
||
| const result = await fetchBanners('https://example.com/site.json', null); | ||
|
|
||
| assert.deepEqual(result, []); | ||
| }); | ||
| }); | ||
|
|
||
| describe('date filtering', () => { | ||
| it('excludes a banner whose endDate has passed', async t => { | ||
| const banner = { text: 'Expired', type: 'warning', endDate: PAST }; | ||
| t.mock.method(global, 'fetch', () => | ||
| Promise.resolve(makeResponse({ index: banner })) | ||
| ); | ||
|
|
||
| const result = await fetchBanners('https://example.com/site.json', null); | ||
|
|
||
| assert.deepEqual(result, []); | ||
| }); | ||
|
|
||
| it('excludes a banner whose startDate is in the future', async t => { | ||
| const banner = { text: 'Upcoming', type: 'warning', startDate: FUTURE }; | ||
| t.mock.method(global, 'fetch', () => | ||
| Promise.resolve(makeResponse({ index: banner })) | ||
| ); | ||
|
|
||
| const result = await fetchBanners('https://example.com/site.json', null); | ||
|
|
||
| assert.deepEqual(result, []); | ||
| }); | ||
|
|
||
| it('includes a banner within its active date range', async t => { | ||
| const banner = { | ||
| text: 'Active', | ||
| type: 'warning', | ||
| startDate: PAST, | ||
| endDate: FUTURE, | ||
| }; | ||
| t.mock.method(global, 'fetch', () => | ||
| Promise.resolve(makeResponse({ index: banner })) | ||
| ); | ||
|
|
||
| const result = await fetchBanners('https://example.com/site.json', null); | ||
|
|
||
| assert.deepEqual(result, [banner]); | ||
| }); | ||
| }); | ||
| }); |
38 changes: 38 additions & 0 deletions
38
src/generators/web/ui/components/AnnouncementBanner/fetchBanners.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /** @import { BannerEntry, RemoteConfig } from './types.d.ts' */ | ||
|
|
||
| import { isBannerActive } from '../../utils/banner.mjs'; | ||
|
|
||
| /** | ||
| * Fetches and returns active banners for the given version from the remote config. | ||
| * Returns an empty array on any fetch or parse failure. | ||
| * | ||
| * @param {string} remoteConfig | ||
| * @param {number | null} versionMajor | ||
| * @returns {Promise<BannerEntry[]>} | ||
| */ | ||
| export const fetchBanners = async (remoteConfig, versionMajor) => { | ||
| const res = await fetch(remoteConfig, { signal: AbortSignal.timeout(2500) }); | ||
|
|
||
| if (!res.ok) { | ||
| return []; | ||
| } | ||
|
|
||
| /** @type {RemoteConfig} */ | ||
| const config = await res.json(); | ||
|
|
||
| const active = []; | ||
|
|
||
| const globalBanner = config.websiteBanners?.index; | ||
| if (globalBanner && isBannerActive(globalBanner)) { | ||
| active.push(globalBanner); | ||
| } | ||
|
|
||
| if (versionMajor != null) { | ||
| const versionBanner = config.websiteBanners?.[`v${versionMajor}`]; | ||
| if (versionBanner && isBannerActive(versionBanner)) { | ||
| active.push(versionBanner); | ||
| } | ||
| } | ||
|
|
||
| return active; | ||
| }; | ||
49 changes: 49 additions & 0 deletions
49
src/generators/web/ui/components/AnnouncementBanner/index.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { ArrowUpRightIcon } from '@heroicons/react/24/outline'; | ||
| import Banner from '@node-core/ui-components/Common/Banner'; | ||
| import { useEffect, useState } from 'preact/hooks'; | ||
|
|
||
| import { fetchBanners } from './fetchBanners.mjs'; | ||
|
|
||
| /** @import { BannerEntry } 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; | ||
| } | ||
|
|
||
| fetchBanners(remoteConfig, versionMajor) | ||
| .then(setBanners) | ||
| .catch(console.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} | ||
| </a> | ||
| ) : ( | ||
| banner.text | ||
| )} | ||
| {banner.link && <ArrowUpRightIcon />} | ||
| </Banner> | ||
| ))} | ||
| </div> | ||
| ); | ||
| }; |
13 changes: 13 additions & 0 deletions
13
src/generators/web/ui/components/AnnouncementBanner/types.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import type { BannerProps } from '@node-core/ui-components/Common/Banner'; | ||
|
|
||
| export type BannerEntry = { | ||
| startDate?: string; | ||
| endDate?: string; | ||
| text: string; | ||
| link?: string; | ||
| type?: BannerProps['type']; | ||
| }; | ||
|
|
||
| export type RemoteConfig = { | ||
| websiteBanners?: Record<string, BannerEntry | undefined>; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { describe, it } from 'node:test'; | ||
|
|
||
| import { isBannerActive } from '../banner.mjs'; | ||
|
|
||
| const PAST = new Date(Date.now() - 86_400_000).toISOString(); // yesterday | ||
| const FUTURE = new Date(Date.now() + 86_400_000).toISOString(); // tomorrow | ||
|
|
||
| const banner = (overrides = {}) => ({ | ||
| text: 'Test banner', | ||
| ...overrides, | ||
| }); | ||
|
|
||
| describe('isBannerActive', () => { | ||
| describe('no startDate, no endDate', () => { | ||
| it('is always active', () => { | ||
| assert.equal(isBannerActive(banner()), true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('startDate only', () => { | ||
| it('is active when startDate is in the past', () => { | ||
| assert.equal(isBannerActive(banner({ startDate: PAST })), true); | ||
| }); | ||
|
|
||
| it('is not active when startDate is in the future', () => { | ||
| assert.equal(isBannerActive(banner({ startDate: FUTURE })), false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('endDate only', () => { | ||
| it('is active when endDate is in the future', () => { | ||
| assert.equal(isBannerActive(banner({ endDate: FUTURE })), true); | ||
| }); | ||
|
|
||
| it('is not active when endDate is in the past', () => { | ||
| assert.equal(isBannerActive(banner({ endDate: PAST })), false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('startDate and endDate', () => { | ||
| it('is active when now is within the range', () => { | ||
| assert.equal( | ||
| isBannerActive(banner({ startDate: PAST, endDate: FUTURE })), | ||
| true | ||
| ); | ||
| }); | ||
|
|
||
| it('is not active when now is before the range', () => { | ||
| assert.equal( | ||
| isBannerActive(banner({ startDate: FUTURE, endDate: FUTURE })), | ||
| false | ||
| ); | ||
| }); | ||
|
|
||
| it('is not active when now is after the range', () => { | ||
| assert.equal( | ||
| isBannerActive(banner({ startDate: PAST, endDate: PAST })), | ||
| false | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can have
config.websiteBanners?.indexandconfig.websiteBanners?.all, not sure which one to use here