@scaleway/cookie-consent
Advanced tools
Comparing version 1.0.7 to 1.1.0
# @scaleway/cookie-consent | ||
## 1.1.0 | ||
### Minor Changes | ||
- [#1726](https://github.com/scaleway/scaleway-lib/pull/1726) [`7669906`](https://github.com/scaleway/scaleway-lib/commit/76699066084b09de9c5da877c6f3be71a47024a5) Thanks [@ModuloM](https://github.com/ModuloM)! - Add isSegmentIntegrationsLoading state to manage Segment integrations loading status | ||
- [#1738](https://github.com/scaleway/scaleway-lib/pull/1738) [`63b01a2`](https://github.com/scaleway/scaleway-lib/commit/63b01a24418ea68889db60e6a6efb48651310917) Thanks [@ModuloM](https://github.com/ModuloM)! - refactor(useCookieConsent): change isSegmentIntegrationsLoaded output | ||
## 1.0.7 | ||
@@ -4,0 +12,0 @@ |
@@ -25,16 +25,18 @@ import cookie from 'cookie'; | ||
}; | ||
const CookieConsentProvider = _ref => { | ||
let { | ||
children, | ||
isConsentRequired, | ||
essentialIntegrations, | ||
config, | ||
cookiePrefix = COOKIE_PREFIX, | ||
consentMaxAge = CONSENT_MAX_AGE, | ||
consentAdvertisingMaxAge = CONSENT_ADVERTISING_MAX_AGE, | ||
cookiesOptions = COOKIES_OPTIONS | ||
} = _ref; | ||
const CookieConsentProvider = ({ | ||
children, | ||
isConsentRequired, | ||
essentialIntegrations, | ||
config, | ||
cookiePrefix = COOKIE_PREFIX, | ||
consentMaxAge = CONSENT_MAX_AGE, | ||
consentAdvertisingMaxAge = CONSENT_ADVERTISING_MAX_AGE, | ||
cookiesOptions = COOKIES_OPTIONS | ||
}) => { | ||
const [needConsent, setNeedsConsent] = useState(false); | ||
const [cookies, setCookies] = useState(); | ||
const segmentIntegrations = useSegmentIntegrations(config); | ||
const { | ||
integrations: segmentIntegrations, | ||
isLoaded: isSegmentIntegrationsLoaded | ||
} = useSegmentIntegrations(config); | ||
useEffect(() => { | ||
@@ -47,17 +49,11 @@ setCookies(cookie.parse(document.cookie)); | ||
}))]), [segmentIntegrations, essentialIntegrations]); | ||
const integrationsHash = useMemo(() => stringToHash(uniq([...(segmentIntegrations ?? []).map(_ref2 => { | ||
let { | ||
name | ||
} = _ref2; | ||
return name; | ||
}), ...essentialIntegrations]).sort().join()), [segmentIntegrations, essentialIntegrations]); | ||
const integrationsHash = useMemo(() => stringToHash(uniq([...(segmentIntegrations ?? []).map(({ | ||
name | ||
}) => name), ...essentialIntegrations]).sort().join()), [segmentIntegrations, essentialIntegrations]); | ||
useEffect(() => { | ||
setNeedsConsent(isConsentRequired && cookies?.[HASH_COOKIE] !== integrationsHash.toString() && segmentIntegrations !== undefined); | ||
}, [isConsentRequired, cookies, integrationsHash, segmentIntegrations]); | ||
const categories = useMemo(() => uniq([...(segmentIntegrations ?? []).map(_ref3 => { | ||
let { | ||
category | ||
} = _ref3; | ||
return category; | ||
})]).sort(), [segmentIntegrations]); | ||
const categories = useMemo(() => uniq([...(segmentIntegrations ?? []).map(({ | ||
category | ||
}) => category)]).sort(), [segmentIntegrations]); | ||
const cookieConsent = useMemo(() => categories.reduce((acc, category) => ({ | ||
@@ -100,6 +96,7 @@ ...acc, | ||
isSegmentAllowed, | ||
isSegmentIntegrationsLoaded, | ||
segmentIntegrations: segmentEnabledIntegrations, | ||
categoriesConsent: cookieConsent, | ||
saveConsent | ||
}), [integrations, cookieConsent, saveConsent, needConsent, isSegmentAllowed, segmentEnabledIntegrations]); | ||
}), [integrations, needConsent, isSegmentAllowed, isSegmentIntegrationsLoaded, segmentEnabledIntegrations, cookieConsent, saveConsent]); | ||
return jsx(CookieConsentContext.Provider, { | ||
@@ -106,0 +103,0 @@ value: value, |
@@ -21,13 +21,10 @@ import { useState, useEffect } from 'react'; | ||
}; | ||
const transformSegmentIntegrationsToIntegrations = segmentIntegrations => [defaultSegmentIoIntegration, ...segmentIntegrations].map(_ref => { | ||
let { | ||
category, | ||
creationName, | ||
name | ||
} = _ref; | ||
return { | ||
name: name === 'Google Ads (Gtag)' ? creationName : name, | ||
category: CATEGORY_MATCH[category] ?? 'marketing' | ||
}; | ||
}); | ||
const transformSegmentIntegrationsToIntegrations = segmentIntegrations => [defaultSegmentIoIntegration, ...segmentIntegrations].map(({ | ||
category, | ||
creationName, | ||
name | ||
}) => ({ | ||
name: name === 'Google Ads (Gtag)' ? creationName : name, | ||
category: CATEGORY_MATCH[category] ?? 'marketing' | ||
})); | ||
const useSegmentIntegrations = config => { | ||
@@ -55,5 +52,8 @@ const [integrations, setIntegrations] = useState(undefined); | ||
}, [setIntegrations, config.segment]); | ||
return integrations; | ||
return { | ||
integrations, | ||
isLoaded: integrations !== undefined | ||
}; | ||
}; | ||
export { useSegmentIntegrations }; |
@@ -25,2 +25,3 @@ import * as react from 'react'; | ||
isSegmentAllowed: boolean; | ||
isSegmentIntegrationsLoaded: boolean; | ||
segmentIntegrations: { | ||
@@ -27,0 +28,0 @@ All: boolean; |
{ | ||
"name": "@scaleway/cookie-consent", | ||
"version": "1.0.7", | ||
"version": "1.1.0", | ||
"description": "React provider to handle website end user consent cookie storage based on segment integrations", | ||
@@ -5,0 +5,0 @@ "type": "module", |
17262
203