@scaleway/cookie-consent
Advanced tools
Comparing version 1.1.1 to 1.1.2
# @scaleway/cookie-consent | ||
## 1.1.2 | ||
### Patch Changes | ||
- [#1902](https://github.com/scaleway/scaleway-lib/pull/1902) [`6d410ae`](https://github.com/scaleway/scaleway-lib/commit/6d410ae22456909e84b2e2c332f2887e1ebc0509) Thanks [@philibea](https://github.com/philibea)! - Remove the default object All: false of integrations when integrations are set. This will only apply on empty integrations array to protect users | ||
## 1.1.1 | ||
@@ -4,0 +10,0 @@ |
import cookie from 'cookie'; | ||
import { createContext, useContext, useState, useEffect, useMemo, useCallback } from 'react'; | ||
import { createContext, useContext, useState, useMemo, useEffect, useCallback } from 'react'; | ||
import { uniq } from '../helpers/array.js'; | ||
@@ -25,2 +25,3 @@ import { stringToHash } from '../helpers/misc.js'; | ||
}; | ||
const getCookies = () => cookie.parse(document.cookie); | ||
const CookieConsentProvider = ({ | ||
@@ -37,3 +38,2 @@ children, | ||
const [needConsent, setNeedsConsent] = useState(false); | ||
const [cookies, setCookies] = useState(); | ||
const { | ||
@@ -43,5 +43,2 @@ integrations: segmentIntegrations, | ||
} = useSegmentIntegrations(config); | ||
useEffect(() => { | ||
setCookies(cookie.parse(document.cookie)); | ||
}, [needConsent]); | ||
const integrations = useMemo(() => uniq([...(segmentIntegrations ?? []), ...essentialIntegrations.map(integration => ({ | ||
@@ -53,13 +50,13 @@ name: integration, | ||
name | ||
}) => name), ...essentialIntegrations]).sort().join()), [segmentIntegrations, essentialIntegrations]); | ||
}) => name), ...essentialIntegrations]).sort().join(undefined)), [segmentIntegrations, essentialIntegrations]); | ||
useEffect(() => { | ||
setNeedsConsent(isConsentRequired && cookies?.[HASH_COOKIE] !== integrationsHash.toString() && segmentIntegrations !== undefined); | ||
}, [isConsentRequired, cookies, integrationsHash, segmentIntegrations]); | ||
const categories = useMemo(() => uniq([...(segmentIntegrations ?? []).map(({ | ||
setNeedsConsent(isConsentRequired && getCookies()[HASH_COOKIE] !== integrationsHash.toString() && segmentIntegrations !== undefined); | ||
}, [isConsentRequired, integrationsHash, segmentIntegrations]); | ||
const categories = useMemo(() => uniq((segmentIntegrations ?? []).map(({ | ||
category | ||
}) => category)]).sort(), [segmentIntegrations]); | ||
}) => category)).sort(undefined), [segmentIntegrations]); | ||
const cookieConsent = useMemo(() => categories.reduce((acc, category) => ({ | ||
...acc, | ||
[category]: isConsentRequired ? cookies?.[`${cookiePrefix}_${category}`] === 'true' : true | ||
}), {}), [isConsentRequired, categories, cookies, cookiePrefix]); | ||
[category]: isConsentRequired ? getCookies()[`${cookiePrefix}_${category}`] === 'true' : true | ||
}), {}), [isConsentRequired, categories, cookiePrefix]); | ||
const saveConsent = useCallback(categoriesConsent => { | ||
@@ -87,9 +84,8 @@ for (const [consentName, consentValue] of Object.entries(categoriesConsent)) { | ||
const isSegmentAllowed = useMemo(() => isConsentRequired ? !needConsent && !!segmentIntegrations?.some(integration => cookieConsent[integration.category]) : true, [isConsentRequired, segmentIntegrations, cookieConsent, needConsent]); | ||
const segmentEnabledIntegrations = useMemo(() => ({ | ||
All: false, | ||
...segmentIntegrations?.reduce((acc, integration) => ({ | ||
...acc, | ||
[integration.name]: cookieConsent[integration.category] | ||
}), {}) | ||
}), [cookieConsent, segmentIntegrations]); | ||
const segmentEnabledIntegrations = useMemo(() => segmentIntegrations?.length === 0 ? { | ||
All: !isConsentRequired | ||
} : (segmentIntegrations ?? []).reduce((acc, integration) => ({ | ||
...acc, | ||
[integration.name]: cookieConsent[integration.category] ?? false | ||
}), {}), [cookieConsent, isConsentRequired, segmentIntegrations]); | ||
const value = useMemo(() => ({ | ||
@@ -96,0 +92,0 @@ integrations, |
@@ -26,5 +26,3 @@ import * as react from 'react'; | ||
isSegmentIntegrationsLoaded: boolean; | ||
segmentIntegrations: { | ||
All: boolean; | ||
} & Record<string, boolean>; | ||
segmentIntegrations: Record<string, boolean>; | ||
categoriesConsent: Partial<Consent>; | ||
@@ -31,0 +29,0 @@ saveConsent: (categoriesConsent: Partial<Consent>) => void; |
{ | ||
"name": "@scaleway/cookie-consent", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "React provider to handle website end user consent cookie storage based on segment integrations", | ||
@@ -5,0 +5,0 @@ "type": "module", |
17905
197