@@ -33,18 +33,31 @@ const bannerTypes: Record<BannerType, { primaryColour: string; textColour: strin
33
33
34
34
export const HeaderBanner : React . FC < { bannerContent ?: BannerContent } > = ( { bannerContent } ) => {
35
35
const [ cookieValue , setCookieValue ] = useState ( [ ] )
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
36
+ const [ isBannerShown , setBannerShown ] = useState ( false ) // Change to false to show banner later to prevent flasing on page load for users who have already dismissed it
37
+
38
+ if ( ! bannerContent ) {
39
+ return null
40
+ }
37
41
38
- if ( ! bannerContent ) return null
39
42
useEffect ( ( ) => {
40
43
const dismissedBannersCookie = getCookie ( "headerBannerDismissed" )
41
44
const parsedCookieValue = JSON . parse ( dismissedBannersCookie || "[]" )
42
45
setCookieValue ( parsedCookieValue )
43
46
if ( ! parsedCookieValue . includes ( bannerContent . description ) ) {
44
- setIsDismissed ( false )
47
+ setBannerShown ( true )
45
48
}
46
- } , [ isDismissed ] )
47
- if ( isDismissed ) return null
49
+ } , [ isBannerShown ] )
50
+
51
+ if ( ! isBannerShown ) {
52
+ return null
53
+ }
54
+
55
+ const handleCloseBanner = ( ) => {
56
+ const newCookieValue = [ ...cookieValue , bannerContent . description ]
57
+ setCookie ( "headerBannerDismissed" , JSON . stringify ( newCookieValue ) , 365 )
58
+ setBannerShown ( false )
59
+ }
60
+
48
61
return (
49
62
< div
50
63
className = { clsx ( headerbanner . container , headerbannerCustom . container ) }
@@ -62,21 +75,14 @@ export const HeaderBanner: React.FC<{ bannerContent?: BannerContent }> = ({ bann
62
75
</ a >
63
76
) }
64
77
</ p >
65
- < button
66
- className = { headerbannerCustom . dismiss }
67
- onClick = { ( ) => {
68
- const newCookieValue = [ ...cookieValue , bannerContent . description ]
69
- setCookie ( "headerBannerDismissed" , JSON . stringify ( newCookieValue ) , 365 )
70
- setIsDismissed ( true )
71
- } }
72
- >
78
+ < button className = { headerbannerCustom . dismiss } onClick = { handleCloseBanner } >
73
79
< CloseIcon />
74
80
</ button >
75
81
</ div >
76
82
) as React . ReactElement // Explicitly assigning to ReactElement cause TS is confused otherwise
77
83
}
78
84
79
- function getCookie ( cname ) {
85
+ function getCookie ( cname : string ) {
80
86
const name = cname + "="
81
87
const decodedCookie = decodeURIComponent ( document . cookie )
82
88
const ca = decodedCookie . split ( ";" )
@@ -91,15 +97,15 @@ function getCookie(cname) {
91
97
}
92
98
return undefined
93
99
}
94
- function setCookie ( cname , cvalue , exdays = 365 ) {
100
+ function setCookie ( cname : string , cvalue : string , exdays = 365 ) {
95
101
const baseDomain = getBaseDomain ( window . location . href )
96
102
const d = new Date ( )
97
103
d . setTime ( d . getTime ( ) + exdays * 24 * 60 * 60 * 1000 )
98
104
const expires = "expires=" + d . toUTCString ( )
99
105
document . cookie = `${ cname } =${ cvalue } ;${ expires } ;path=/;domain=${ baseDomain } `
100
106
}
101
107
102
- function getBaseDomain ( url ) {
108
+ function getBaseDomain ( url : string ) {
103
109
try {
104
110
const hostname = new URL ( url ) . hostname
105
111
const parts = hostname . split ( "." ) . reverse ( )
0 commit comments