-
Notifications
You must be signed in to change notification settings - Fork 12
feat(cookie): init cookie consent package #1494
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e1efa62
feat(cookie): init cookie consent package
971fb63
chore(clean): remove comments
8da7023
Update README.md
Axel-KIRK 522a404
Update README.md
Axel-KIRK 23a60ed
Update README.md
Axel-KIRK 3a31eec
docs: add usage documentation
Slashgear 903645b
refactor(options): add props cookie config
1a6f647
refactor(options): add props cookie config
8eb4f23
refactor(options): add props cookie config
9decfce
refactor(options): add props cookie config
def9b0c
fix: review suggestions
Slashgear 25a584d
fix: fix typing and test
Slashgear 0ee93c0
fix: remove useless react typings inside package
Slashgear beda660
docs: reference new package in global README
Slashgear 58abc3d
Merge branch 'main' into test-branch
Slashgear File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@scaleway/cookie-consent': major | ||
| --- | ||
|
|
||
| Extract internal cookie consent provider strategy to shared package |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| const { join } = require('path') | ||
|
|
||
| module.exports = { | ||
| rules: { | ||
| 'import/no-extraneous-dependencies': [ | ||
| 'error', | ||
| { packageDir: [__dirname, join(__dirname, '../../')] }, | ||
| ], | ||
| }, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| **/__tests__/** | ||
| examples/ | ||
| src | ||
| .eslintrc.cjs | ||
| !.npmignore |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| # Shire - Cookie Consent | ||
|
|
||
| This package is an helper to handle cookie consents with Segment integrations. | ||
| It will handle the cookie consent for each categories. | ||
|
|
||
| This package does not contain design element to display a cookie consent modal, | ||
| it only handle the storage and the init of cookie consent in a React provider. | ||
|
|
||
| ## QuickStart | ||
|
|
||
| In order to use it, first you need to provide a context at the top level of your application | ||
|
|
||
| ```tsx | ||
| import { PropsWithChildren } from 'react' | ||
|
|
||
| const MyApp = ({ children }: PropsWithChildren) => { | ||
| return ( | ||
| <CookieConsentProvider | ||
| isConsentRequired // Switch off consents if not defined (usefull for E2E testing) | ||
| essentialIntegrations={[]} // List of mandatory integrations | ||
| config={{ | ||
| segment, // Segment configuration used to get dynamically the integration used | ||
| }} | ||
| // not required | ||
| cookiePrefix="_scw_rgpd" // default value | ||
| consentMaxAge={13 * 30 * 24 * 60 * 60} // default value (appx 13 months) | ||
| consentAdvertisingMaxAge={6 * 30 * 24 * 60 * 60} // default value (appx 6 months) | ||
| cookiesOptions={{ sameSite: 'strict', secure: true, path: '/' }} // default value | ||
| > | ||
| {children} | ||
| </CookieConsentProvider> | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| Then in your cookie modal component you could simply use exposed hook to get and modify consents | ||
|
|
||
| ```tsx | ||
| export function PanelConsent() { | ||
| const { saveConsent, categoriesConsent } = useCookieConsent() | ||
|
|
||
| const setAllConsents = ({ | ||
| categoriesConsent, | ||
| value, | ||
| }: { | ||
| categoriesConsent: Partial<Consent> | ||
| value: boolean | ||
| }) => | ||
| Object.keys(categoriesConsent).reduce( | ||
| (acc, category) => ({ ...acc, [category]: value }), | ||
| {}, | ||
| ) | ||
|
|
||
| const handleClick = (consentForAll: boolean) => () => { | ||
| saveConsent(setAllConsents({ categoriesConsent, value: consentForAll })) | ||
| } | ||
|
|
||
| const onAgreeAll = handleClick(true) | ||
|
|
||
| const onReject = handleClick(false) | ||
|
|
||
| return ( | ||
| <div className={styles.consent}> | ||
| <div>Do you accept consents ?</div> | ||
| <div> | ||
| <Button onClick={onAgreeAll} autoFocus> | ||
| Accept | ||
| </Button> | ||
| <Button onClick={onReject}>Decline</Button> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| ### User flow | ||
|
|
||
| ```mermaid | ||
| flowchart TD | ||
| Z[Application boot] -..-> A | ||
| subgraph "Cookie Consent booting" | ||
| A[First user navigation in app] --> B{isConsentRequired} | ||
| B --> |false| C[do nothing with cookies] | ||
| B --> |true| D[Fetch segment integrations configuration] | ||
| D --> E[Generate hash of integration and define required consent categories depending on integration] | ||
| E -..->F[Set needConsent to true] | ||
| end | ||
| subgraph "Consent storage" | ||
| F -..-> | | G[/User saveConsent with categories/] | ||
| G --> H[Hash of integration is stored in _scw_rgpd_hash cookie with storage of 6 months] | ||
| G --> I[_scw_rgpd_$category$ cookie is stored for each accepted cookie consent category, 6 months for ad consent, 13 month for others] | ||
| H & I --> J[needConsent is set to false] | ||
| end | ||
| subgraph "User come back on website in futur (within cookie duration)" | ||
| J -..-> K[Application boot] | ||
| K -..-> L[Check value fo cookies _scw_rgpd_hash and _scw_rgpd_$categorie$] | ||
| L --> M[Load in context accepted categories] | ||
| end | ||
| subgraph "User come back after 6 months" | ||
| J -...-> N[Application boot] | ||
| N -..-> O[Check value fo cookies _scw_rgpd_hash and _scw_rgpd_$categorie$] | ||
| O --> B | ||
| end | ||
| ``` | ||
|
|
||
| ## How to contribute ? | ||
|
|
||
| ### Prerequisites | ||
|
|
||
| ``` | ||
| $ cd packages/cookie-consent | ||
| ``` | ||
|
|
||
| ### Start | ||
|
|
||
| ```bash | ||
| $ pnpm build # Build the package | ||
| $ pnpm watch # Build the package and watch for changes | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| { | ||
| "name": "@scaleway/cookie-consent", | ||
| "version": "0.1.0", | ||
| "description": "React provider to handle website end user consent cookie storage based on segment integrations", | ||
| "type": "module", | ||
| "sideEffects": false, | ||
| "exports": { | ||
| "types": "./dist/index.d.ts", | ||
| "default": "./dist/index.js" | ||
| }, | ||
| "publishConfig": { | ||
| "access": "public" | ||
| }, | ||
| "keywords": [ | ||
| "react", | ||
| "reactjs", | ||
| "hooks", | ||
| "segment", | ||
| "cookies", | ||
| "gdpr" | ||
| ], | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/scaleway/scaleway-lib", | ||
| "directory": "packages/cookie-consent" | ||
| }, | ||
| "dependencies": { | ||
| "cookie": "0.5.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/cookie": "0.5.1", | ||
| "react": "18.2.0" | ||
| }, | ||
| "peerDependencies": { | ||
| "react": "18.x || 18" | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.