-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
014feba
commit a700570
Showing
6 changed files
with
146 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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,32 @@ | ||
import { useGlobalBanners } from "@/hooks/useGlobalBanners"; | ||
import { Banner } from "./Banner"; | ||
import { X } from "react-feather"; | ||
import tw from "twin.macro"; | ||
|
||
export const GlobalBanners = () => { | ||
const { currentBanner, dismissGlobalBanner } = useGlobalBanners(); | ||
|
||
if (currentBanner == null) return null; | ||
|
||
return ( | ||
<Banner | ||
variant={currentBanner.variant} | ||
tw="rounded-none" | ||
textContainerStyles={tw`relative w-full flex justify-center`} | ||
> | ||
<p>{currentBanner.message}</p> | ||
|
||
<button | ||
type="button" | ||
title="Dismiss" | ||
css={[ | ||
tw`absolute top-0 right-0`, | ||
tw`focus:outline-none text-gray-500 hover:text-gray-700`, | ||
]} | ||
onClick={() => dismissGlobalBanner(currentBanner.id)} | ||
> | ||
<X tw="h-5 w-5" /> | ||
</button> | ||
</Banner> | ||
); | ||
}; |
This file contains 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,98 @@ | ||
import { BannerVariant } from "@/components/Banner"; | ||
import { Link } from "@/components/Link"; | ||
import { useMemo } from "react"; | ||
import useLocalStorageState from "use-local-storage-state"; | ||
import tw from "twin.macro"; | ||
|
||
export interface GlobalBanner { | ||
id: string; | ||
message: string | React.ReactElement; | ||
variant?: BannerVariant; | ||
} | ||
|
||
const LaunchWeekBanner = ({ | ||
linkText, | ||
isFirstStop, | ||
isLastStop, | ||
}: { | ||
linkText: string; | ||
isFirstStop?: boolean; | ||
isLastStop?: boolean; | ||
}) => ( | ||
<> | ||
<span role="img">🚅</span>{" "} | ||
<span> | ||
<strong>Launch Week 01</strong> is now{" "} | ||
{isFirstStop ? "boarding" : "in service"}.{" "} | ||
{isFirstStop ? "First" : isLastStop ? "Last" : "Next"} stop:{" "} | ||
<Link href="https://railway.app/launch-week-01" tw="underline"> | ||
{linkText} | ||
</Link> | ||
! | ||
</span> | ||
</> | ||
); | ||
|
||
const GLOBAL_BANNERS_KEY = "@railway/globalBanners"; | ||
const useGlobalBannersLocalStorage = () => | ||
useLocalStorageState<Record<string, boolean>>(GLOBAL_BANNERS_KEY, { | ||
defaultValue: {}, | ||
}); | ||
|
||
export const allGlobalBanners: GlobalBanner[] = [ | ||
// Day 1 | ||
{ | ||
id: "launch-week-day-1", | ||
message: <LaunchWeekBanner linkText="Regions" isFirstStop />, | ||
}, | ||
// Day 2 | ||
// { | ||
// id: "launch-week-day-2", | ||
// message: <LaunchWeekBanner linkText="NO SPOILERS" />, | ||
// }, | ||
// Day 3 | ||
// { | ||
// id: "launch-week-day-3", | ||
// message: <LaunchWeekBanner linkText="NO SPOILERS" />, | ||
// }, | ||
// Day 4 | ||
// { | ||
// id: "launch-week-day-4", | ||
// message: <LaunchWeekBanner linkText="NO SPOILERS" />, | ||
// }, | ||
// Day 5 | ||
// { | ||
// id: "launch-week-day-5", | ||
// message: <LaunchWeekBanner linkText="NO SPOILERS" isLastStop />, | ||
// }, | ||
]; | ||
|
||
export const useGlobalBanners = () => { | ||
const [viewedGlobalBanners, setViewedGlobalBanners] = | ||
useGlobalBannersLocalStorage(); | ||
|
||
const dismissGlobalBanner = (id: string) => { | ||
setViewedGlobalBanners(currentBanners => ({ | ||
...currentBanners, | ||
[id]: true, | ||
})); | ||
}; | ||
|
||
// Oldest banner that should be visible | ||
const currentBanner = useMemo(() => { | ||
const viewedBanners = Object.entries(viewedGlobalBanners) | ||
.filter(([, viewed]) => viewed) | ||
.map(([id]) => id); | ||
|
||
const unviewedBanners = allGlobalBanners.filter( | ||
banner => !viewedBanners.includes(banner.id), | ||
); | ||
|
||
return unviewedBanners[0] ?? null; | ||
}, [allGlobalBanners, viewedGlobalBanners]); | ||
|
||
return { | ||
currentBanner, | ||
dismissGlobalBanner, | ||
}; | ||
}; |
This file contains 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 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