Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: customize localStorage key for analytics consents #302 #310

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/consentManagement/createConsentManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useRerenderOnChange } from "../tools/StatefulObservable/hooks";
import { createConsentBannerAndConsentManagement } from "./ConsentBannerAndConsentManagement";
import { isBrowser } from "../tools/isBrowser";

export const localStorageKeyPrefix = "@codegouvfr/react-dsfr finalityConsent";
export const defaultLocalStorageKeyPrefix = "@codegouvfr/react-dsfr finalityConsent";

export function createConsentManagement<
FinalityDescription extends Record<
Expand All @@ -21,10 +21,16 @@ export function createConsentManagement<
consentCallback?: ConsentCallback<ExtractFinalityFromFinalityDescription<FinalityDescription>>;
/** Optional: If you have a dedicated page that provides comprehensive information about your website's GDPR policies. */
personalDataPolicyLinkProps?: RegisteredLinkProps;
localStorageKeyPrefix?: string;
}) {
type Finality = ExtractFinalityFromFinalityDescription<FinalityDescription>;

const { finalityDescription, personalDataPolicyLinkProps, consentCallback } = params;
const {
finalityDescription,
personalDataPolicyLinkProps,
consentCallback,
localStorageKeyPrefix
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
localStorageKeyPrefix
localStorageKeyPrefix = defaultLocalStorageKeyPrefix

} = params;

const finalities = getFinalitiesFromFinalityDescription({
"finalityDescription":
Expand All @@ -33,7 +39,9 @@ export function createConsentManagement<
: finalityDescription
});

const localStorageKey = `${localStorageKeyPrefix} ${finalities.join("-")}`;
const localStorageKey = `${
localStorageKeyPrefix ?? defaultLocalStorageKeyPrefix
} ${finalities.join("-")}`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const localStorageKey = `${
localStorageKeyPrefix ?? defaultLocalStorageKeyPrefix
} ${finalities.join("-")}`;
const localStorageKey = `${localStorageKeyPrefix} ${finalities.join("-")}`;


const $finalityConsent = createStatefulObservable<FinalityConsent<Finality> | undefined>(() => {
if (!isBrowser) {
Expand Down Expand Up @@ -98,7 +106,8 @@ export function createConsentManagement<
useConsent,
ConsentBannerAndConsentManagement,
FooterConsentManagementItem,
FooterPersonalDataPolicyItem
FooterPersonalDataPolicyItem,
consentLocalStorageKey: localStorageKey
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
FooterPersonalDataPolicyItem,
consentLocalStorageKey: localStorageKey
FooterPersonalDataPolicyItem,
"consentLocalStorageKey": localStorageKey

Will you actually use this value?
Because if there is no identified usecase for it no need to increace the API surface.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah so I see, you exported it for Storybook.
The storybook needs shouldn't affect the API.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok after thinking, the use case we have won't be covered by just returning the computed key. So, I removed it from the return.

};
}

Expand Down
18 changes: 13 additions & 5 deletions stories/ConsentManagement.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react";
import { sectionName } from "./sectionName";
import { getStoryFactory } from "./getStory";
import { createConsentManagement } from "../dist/consentManagement";
import { localStorageKeyPrefix } from "../dist/consentManagement/createConsentManagement";
import { defaultLocalStorageKeyPrefix } from "../dist/consentManagement/createConsentManagement";
import { Placeholder } from "../dist/consentManagement/Placeholder";
import { Footer } from "../dist/Footer";
import { Button } from "../dist/Button";
Expand Down Expand Up @@ -34,7 +34,8 @@ export const {
ConsentBannerAndConsentManagement,
FooterConsentManagementItem,
FooterPersonalDataPolicyItem,
useConsent
useConsent,
consentLocalStorageKey
} = createConsentManagement({
/*
Can be an object or a function that take the current language as argument.
Expand Down Expand Up @@ -121,7 +122,13 @@ export const {
}
*/

}
},

/*
This optional parameter let's you personalise the key that is used to store user's consents in the localStorage.
The default value is "${defaultLocalStorageKeyPrefix}"
*/
"localStorageKeyPrefix": "company-name/app-name"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to keep this undocumented.
It's a niche thing that is relevent in your usecase but for most app it won't be.
Pepole don't read documentation. If they see the doc is a little bit too long they'd rather implement their own solution.
So let's keep only the essential info.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I removed all mentions in the documentation.

});
\`\`\`

Expand Down Expand Up @@ -213,7 +220,8 @@ const {
ConsentBannerAndConsentManagement,
FooterConsentManagementItem,
FooterPersonalDataPolicyItem,
useConsent
useConsent,
consentLocalStorageKey
} = createConsentManagement({
"finalityDescription": {
"advertising": {
Expand Down Expand Up @@ -284,7 +292,7 @@ function Story() {
<Button
onClick={() => {
Object.keys(localStorage)
.filter(key => key.startsWith(localStorageKeyPrefix))
.filter(key => key.startsWith(consentLocalStorageKey))
.forEach(key => localStorage.removeItem(key));

location.reload();
Expand Down
6 changes: 4 additions & 2 deletions test/integration/vite/src/consentManagement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export const {
ConsentBannerAndConsentManagement,
FooterConsentManagementItem,
FooterPersonalDataPolicyItem,
useConsent
useConsent,
consentLocalStorageKey
} = createConsentManagement({
"finalityDescription": ({ lang }) => ({
"advertising": {
Expand Down Expand Up @@ -41,5 +42,6 @@ export const {
}

console.log("callback from gdpr hook");
}
},
"localStorageKeyPrefix": "company-name/app-name finalityConsent"
});
Loading