@@ -52,8 +52,6 @@ export const useCookieConsent = (): Context => {
5252 return context
5353}
5454
55- const getCookies = ( ) => cookie . parse ( document . cookie )
56-
5755export const CookieConsentProvider = ( {
5856 children,
5957 isConsentRequired,
@@ -73,6 +71,9 @@ export const CookieConsentProvider = ({
7371 cookiesOptions ?: CookieSerializeOptions
7472} > ) => {
7573 const [ needConsent , setNeedsConsent ] = useState ( false )
74+ const [ cookies , setCookies ] = useState < Record < string , string > > (
75+ cookie . parse ( document . cookie ) ,
76+ )
7677
7778 const {
7879 integrations : segmentIntegrations ,
@@ -113,10 +114,10 @@ export const CookieConsentProvider = ({
113114 // to false after receiving segment answer and flicker the UI
114115 setNeedsConsent (
115116 isConsentRequired &&
116- getCookies ( ) [ HASH_COOKIE ] !== integrationsHash . toString ( ) &&
117+ cookies [ HASH_COOKIE ] !== integrationsHash . toString ( ) &&
117118 segmentIntegrations !== undefined ,
118119 )
119- } , [ isConsentRequired , integrationsHash , segmentIntegrations ] )
120+ } , [ isConsentRequired , integrationsHash , segmentIntegrations , cookies ] )
120121
121122 // We store unique categories names in an array
122123 const categories = useMemo (
@@ -135,13 +136,14 @@ export const CookieConsentProvider = ({
135136 categories . reduce < Partial < Consent > > (
136137 ( acc , category ) => ( {
137138 ...acc ,
138- [ category ] : isConsentRequired
139- ? getCookies ( ) [ `${ cookiePrefix } _${ category } ` ] === 'true'
140- : true ,
139+ [ category ] :
140+ isConsentRequired || needConsent
141+ ? cookies [ `${ cookiePrefix } _${ category } ` ] === 'true'
142+ : true ,
141143 } ) ,
142144 { } ,
143145 ) ,
144- [ isConsentRequired , categories , cookiePrefix ] ,
146+ [ isConsentRequired , categories , cookiePrefix , needConsent , cookies ] ,
145147 )
146148
147149 const saveConsent = useCallback (
@@ -163,7 +165,7 @@ export const CookieConsentProvider = ({
163165 } )
164166 } else {
165167 document . cookie = cookie . serialize (
166- ` ${ cookiePrefix } _ ${ consentCategoryName } ` ,
168+ cookieName ,
167169 consentValue . toString ( ) ,
168170 {
169171 ...cookiesOptions ,
@@ -174,6 +176,10 @@ export const CookieConsentProvider = ({
174176 } ,
175177 )
176178 }
179+ setCookies ( prevCookies => ( {
180+ ...prevCookies ,
181+ [ cookieName ] : consentValue ? 'true' : 'false' ,
182+ } ) )
177183 }
178184 // We set the hash cookie to the current consented integrations
179185 document . cookie = cookie . serialize (
@@ -185,6 +191,10 @@ export const CookieConsentProvider = ({
185191 maxAge : consentAdvertisingMaxAge ,
186192 } ,
187193 )
194+ setCookies ( prevCookies => ( {
195+ ...prevCookies ,
196+ [ HASH_COOKIE ] : integrationsHash . toString ( ) ,
197+ } ) )
188198 setNeedsConsent ( false )
189199 } ,
190200 [
@@ -206,7 +216,6 @@ export const CookieConsentProvider = ({
206216 : true ,
207217 [ isConsentRequired , segmentIntegrations , cookieConsent , needConsent ] ,
208218 )
209-
210219 // 'All': false tells Segment not to send data to any Destinations by default, unless they’re explicitly listed as true in the next lines.
211220 // In this case we should not have any integration, so we protect the user. Maybe unecessary as we always set true of false for an integration.
212221 const segmentEnabledIntegrations = useMemo (
0 commit comments