@@ -32,14 +32,19 @@ const bannerTypes: Record<BannerType, { primaryColour: string; textColour: strin
32
32
}
33
33
34
34
export const HeaderBanner : React . FC < { bannerContent ?: BannerContent } > = ( { bannerContent } ) => {
35
+ const [ cookieValue , setCookieValue ] = useState ( [ ] )
35
36
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
36
39
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 ) ) {
39
44
setIsDismissed ( false )
40
45
}
41
46
} , [ isDismissed ] )
42
- if ( ! bannerContent || isDismissed ) return null
47
+ if ( isDismissed ) return null
43
48
return (
44
49
< div
45
50
className = { clsx ( headerbanner . container , headerbannerCustom . container ) }
@@ -60,7 +65,8 @@ export const HeaderBanner: React.FC<{ bannerContent?: BannerContent }> = ({ bann
60
65
< button
61
66
className = { headerbannerCustom . dismiss }
62
67
onClick = { ( ) => {
63
- localStorage . setItem ( "headerBannerDismissed" , bannerContent . description )
68
+ const newCookieValue = [ ...cookieValue , bannerContent . description ]
69
+ setCookie ( "headerBannerDismissed" , JSON . stringify ( newCookieValue ) , 365 )
64
70
setIsDismissed ( true )
65
71
} }
66
72
>
@@ -69,3 +75,40 @@ export const HeaderBanner: React.FC<{ bannerContent?: BannerContent }> = ({ bann
69
75
</ div >
70
76
) as React . ReactElement // Explicitly assigning to ReactElement cause TS is confused otherwise
71
77
}
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