Skip to content

Commit 21d7644

Browse files
committed
WIP 383
1 parent ca7d013 commit 21d7644

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

src/features/notifications/components/HeaderBanner.tsx

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,19 @@ const bannerTypes: Record<BannerType, { primaryColour: string; textColour: strin
3232
}
3333

3434
export const HeaderBanner: React.FC<{ bannerContent?: BannerContent }> = ({ bannerContent }) => {
35+
const [cookieValue, setCookieValue] = useState([])
3536
const [isDismissed, setIsDismissed] = useState(true) // Change to false to show banner later to prevent flasing on page load for users who have already dismissed it
37+
38+
if (!bannerContent) return null
3639
useEffect(() => {
37-
const isDismissedLocalStorage = localStorage.getItem("headerBannerDismissed")
38-
if (!isDismissedLocalStorage || isDismissedLocalStorage !== bannerContent?.description) {
40+
const dismissedBannersCookie = getCookie("headerBannerDismissed")
41+
const parsedCookieValue = JSON.parse(dismissedBannersCookie || "[]")
42+
setCookieValue(parsedCookieValue)
43+
if (!parsedCookieValue.includes(bannerContent.description)) {
3944
setIsDismissed(false)
4045
}
4146
}, [isDismissed])
42-
if (!bannerContent || isDismissed) return null
47+
if (isDismissed) return null
4348
return (
4449
<div
4550
className={clsx(headerbanner.container, headerbannerCustom.container)}
@@ -60,7 +65,8 @@ export const HeaderBanner: React.FC<{ bannerContent?: BannerContent }> = ({ bann
6065
<button
6166
className={headerbannerCustom.dismiss}
6267
onClick={() => {
63-
localStorage.setItem("headerBannerDismissed", bannerContent.description)
68+
const newCookieValue = [...cookieValue, bannerContent.description]
69+
setCookie("headerBannerDismissed", JSON.stringify(newCookieValue), 365)
6470
setIsDismissed(true)
6571
}}
6672
>
@@ -69,3 +75,40 @@ export const HeaderBanner: React.FC<{ bannerContent?: BannerContent }> = ({ bann
6975
</div>
7076
) as React.ReactElement // Explicitly assigning to ReactElement cause TS is confused otherwise
7177
}
78+
79+
function getCookie(cname) {
80+
const name = cname + "="
81+
const decodedCookie = decodeURIComponent(document.cookie)
82+
const ca = decodedCookie.split(";")
83+
for (let i = 0; i < ca.length; i++) {
84+
let c = ca[i]
85+
while (c.charAt(0) === " ") {
86+
c = c.substring(1)
87+
}
88+
if (c.indexOf(name) === 0) {
89+
return c.substring(name.length, c.length)
90+
}
91+
}
92+
return undefined
93+
}
94+
function setCookie(cname, cvalue, exdays = 365) {
95+
const baseDomain = getBaseDomain(window.location.href)
96+
const d = new Date()
97+
d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000)
98+
const expires = "expires=" + d.toUTCString()
99+
document.cookie = `${cname}=${cvalue};${expires};path=/;domain=${baseDomain}`
100+
}
101+
102+
function getBaseDomain(url) {
103+
try {
104+
const hostname = new URL(url).hostname
105+
const parts = hostname.split(".").reverse()
106+
if (parts.length >= 2) {
107+
return `${parts[1]}.${parts[0]}`
108+
}
109+
return hostname
110+
} catch (error) {
111+
console.error("Invalid URL:", error)
112+
return ""
113+
}
114+
}

0 commit comments

Comments
 (0)