From 86a72687adaeaf71e0cbc828da211833d4d581e4 Mon Sep 17 00:00:00 2001 From: aphilibeaux Date: Fri, 12 Sep 2025 14:32:42 +0200 Subject: [PATCH] feat(cookie-consent): export allowed, denied consents --- .changeset/beige-bars-wonder.md | 5 ++++ .changeset/giant-states-ask.md | 5 ++++ .../src/analytics/useAnalytics.tsx | 5 ++-- .../cookies-consent/CookieConsentProvider.tsx | 28 ++++++++++++++++++- 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 .changeset/beige-bars-wonder.md create mode 100644 .changeset/giant-states-ask.md diff --git a/.changeset/beige-bars-wonder.md b/.changeset/beige-bars-wonder.md new file mode 100644 index 000000000..1668a0e15 --- /dev/null +++ b/.changeset/beige-bars-wonder.md @@ -0,0 +1,5 @@ +--- +"@scaleway/use-analytics": patch +--- + +Add allowedConsents and denied consent on cookie consent provider diff --git a/.changeset/giant-states-ask.md b/.changeset/giant-states-ask.md new file mode 100644 index 000000000..da33cabfe --- /dev/null +++ b/.changeset/giant-states-ask.md @@ -0,0 +1,5 @@ +--- +"@scaleway/use-analytics": patch +--- + +Fix readyness state diff --git a/packages/use-analytics/src/analytics/useAnalytics.tsx b/packages/use-analytics/src/analytics/useAnalytics.tsx index e2429cb48..6d5aaf300 100644 --- a/packages/use-analytics/src/analytics/useAnalytics.tsx +++ b/packages/use-analytics/src/analytics/useAnalytics.tsx @@ -91,7 +91,7 @@ export function AnalyticsProvider({ // This effect will unlock the case where we have a failure with the load of the analytics.load as rudderstack doesn't provider any solution for this case. useEffect(() => { let timer: ReturnType | undefined - if (!isAnalyticsReady && !internalAnalytics && timeout) { + if (!isAnalyticsReady && timeout) { if (shouldRenderOnlyWhenReady) { timer = setTimeout(() => setIsAnalyticsReady(true), timeout) onError?.(new Error('Analytics Setup Timeout')) @@ -138,8 +138,7 @@ export function AnalyticsProvider({ }) onLoaded(rudderAnalytics) - - setIsAnalyticsReady(true) + setAnalytics(analytics) }, ...loadOptions, }) diff --git a/packages/use-analytics/src/cookies-consent/CookieConsentProvider.tsx b/packages/use-analytics/src/cookies-consent/CookieConsentProvider.tsx index 691a4d959..874c30ba4 100644 --- a/packages/use-analytics/src/cookies-consent/CookieConsentProvider.tsx +++ b/packages/use-analytics/src/cookies-consent/CookieConsentProvider.tsx @@ -22,7 +22,13 @@ import { uniq } from '../helpers/array' import { IS_CLIENT } from '../helpers/isClient' import { stringToHash } from '../helpers/misc' import { isCategoryKind } from '../types' -import type { Config, Consent, Destination, Destinations } from '../types' +import type { + CategoryKind, + Config, + Consent, + Destination, + Destinations, +} from '../types' type Context = { destinations: Destinations @@ -185,12 +191,30 @@ export const CookieConsentProvider = ({ ], ) + const allowedConsents = useMemo( + () => + Object.keys(cookieConsent).filter( + key => cookieConsent[key as CategoryKind], + ) as CategoryKind[], + [cookieConsent], + ) + + const deniedConsents = useMemo( + () => + Object.keys(cookieConsent).filter( + key => !allowedConsents.includes(key as CategoryKind), + ) as CategoryKind[], + [cookieConsent, allowedConsents], + ) + const value = useMemo( () => ({ destinations, needConsent, isDestinationsLoaded, categoriesConsent: cookieConsent, + allowedConsents, + deniedConsents, saveConsent, categories: CATEGORIES, cookies, @@ -199,6 +223,8 @@ export const CookieConsentProvider = ({ destinations, isDestinationsLoaded, needConsent, + allowedConsents, + deniedConsents, cookieConsent, saveConsent, cookies,