Skip to content

Commit

Permalink
add support for expiration date
Browse files Browse the repository at this point in the history
  • Loading branch information
a-hariti committed Nov 7, 2024
1 parent bb25cd2 commit c3f0b01
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/components/banner/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,22 @@ import {useEffect, useState} from 'react';
import styles from './banner.module.scss';

type BannerType = {
/** This is an array of strings or RegExps to feed into new RegExp() */
appearsOn: (string | RegExp)[];
/** String that is the label for the call to action button */
linkText: string;
/** String that is the destination url of the call to action button */
linkURL: string;
/** String for the text of the banner */
text: string;
/** Optional ISO Date string that will hide the banner after this date without the need for a rebuild */
expiresOn?: string;
};

// BANNERS is an array of banner objects. You can add as many as you like. If
// you need to disable all banners, set BANNERS to an empty array. Each banner
// is evaluated in order, and the first one that matches will be shown.
//
// Banner Object Properties:
//
// - appearsOn: An array of RegExps or strings to feed into new RegExp()
// - text: String for the text of the banner
// - linkURL: String that is the destination url of the call to action button
// - linkText: String that is the label for the call to action button
//
// Example:
//
// Examples:
Expand Down Expand Up @@ -68,6 +67,14 @@ const BANNERS: BannerType[] = [
linkURL: 'https://sentry.io/thought-leadership',
linkText: 'Get webinarly',
},
// example with an expiration date
{
appearsOn: ['^/platforms/javascript/guides/aws-lambda/'],
text: "This banner should appear on the AWS Lambda guide, but won't because it's expired",
linkURL: 'https://sentry.io/thought-leadership',
linkText: 'Get webinarly',
expiresOn: '2024-01-01T00:00:00Z',
},
// generic javascript example
{
// we can constrain it to the javascript platform page only
Expand Down Expand Up @@ -120,8 +127,12 @@ export function Banner() {
);
});

// Bail if no banner matches this page
if (!matchingBanner) {
// Bail if no banner matches this page or if the banner has expired
if (
!matchingBanner ||
(matchingBanner.expiresOn &&
new Date() > new Date(matchingBanner.expiresOn ?? null))
) {
return;
}

Expand Down

0 comments on commit c3f0b01

Please sign in to comment.