|
| 1 | +import React, { useReducer, useEffect, type ReactNode } from "react"; |
| 2 | +import type { RegisteredLinkProps } from "../../link"; |
| 3 | +import type { ExtractFinalityFromFinalityDescription, FinalityConsent } from "../types"; |
| 4 | +import type { ProcessConsentChanges } from "../processConsentChanges"; |
| 5 | +import { FooterBottomItem } from "../../Footer"; |
| 6 | +import { createConsentBanner } from "./ConsentBanner"; |
| 7 | +import { createConsentManagement } from "./ConsentConsentManagement"; |
| 8 | +import { useTranslation } from "./translation"; |
| 9 | + |
| 10 | +export function createConsentBannerAndConsentManagement< |
| 11 | + FinalityDescription extends Record< |
| 12 | + string, |
| 13 | + { title: ReactNode; description?: ReactNode; subFinalities?: Record<string, ReactNode> } |
| 14 | + > |
| 15 | +>(params: { |
| 16 | + finalityDescription: ((params: { lang: string }) => FinalityDescription) | FinalityDescription; |
| 17 | + useFinalityConsent: () => |
| 18 | + | FinalityConsent<ExtractFinalityFromFinalityDescription<FinalityDescription>> |
| 19 | + | undefined; |
| 20 | + processConsentChanges: ProcessConsentChanges< |
| 21 | + ExtractFinalityFromFinalityDescription<FinalityDescription> |
| 22 | + >; |
| 23 | + personalDataPolicyLinkProps?: RegisteredLinkProps; |
| 24 | + finalities: ExtractFinalityFromFinalityDescription<FinalityDescription>[]; |
| 25 | +}) { |
| 26 | + const { |
| 27 | + finalityDescription, |
| 28 | + useFinalityConsent, |
| 29 | + processConsentChanges, |
| 30 | + personalDataPolicyLinkProps, |
| 31 | + finalities |
| 32 | + } = params; |
| 33 | + |
| 34 | + const { ConsentManagement, consentModalButtonProps, useIsConsentManagementOpen } = |
| 35 | + createConsentManagement({ |
| 36 | + finalityDescription, |
| 37 | + personalDataPolicyLinkProps, |
| 38 | + useFinalityConsent, |
| 39 | + processConsentChanges, |
| 40 | + finalities |
| 41 | + }); |
| 42 | + |
| 43 | + const { ConsentBanner } = createConsentBanner({ |
| 44 | + personalDataPolicyLinkProps, |
| 45 | + processConsentChanges, |
| 46 | + consentModalButtonProps |
| 47 | + }); |
| 48 | + |
| 49 | + const { FooterConsentManagementItem } = createFooterConsentManagementItem({ |
| 50 | + consentModalButtonProps |
| 51 | + }); |
| 52 | + |
| 53 | + function ConsentBannerAndConsentManagement() { |
| 54 | + const [isHydrated, setIsHydrated] = useReducer(() => true, false); |
| 55 | + |
| 56 | + useEffect(() => { |
| 57 | + processConsentChanges({ "type": "no changes but trigger consent callbacks" }); |
| 58 | + |
| 59 | + setIsHydrated(); |
| 60 | + }, []); |
| 61 | + |
| 62 | + const finalityConsent = useFinalityConsent(); |
| 63 | + |
| 64 | + const isConsentManagementOpen = useIsConsentManagementOpen(); |
| 65 | + |
| 66 | + if (!isHydrated) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + return ( |
| 71 | + <> |
| 72 | + {finalityConsent === undefined && !isConsentManagementOpen && <ConsentBanner />} |
| 73 | + <ConsentManagement /> |
| 74 | + </> |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + const { FooterPersonalDataPolicyItem } = createFooterPersonalDataPolicyItem({ |
| 79 | + personalDataPolicyLinkProps |
| 80 | + }); |
| 81 | + |
| 82 | + return { |
| 83 | + ConsentBannerAndConsentManagement, |
| 84 | + FooterConsentManagementItem, |
| 85 | + FooterPersonalDataPolicyItem |
| 86 | + }; |
| 87 | +} |
| 88 | + |
| 89 | +function createFooterConsentManagementItem(params: { |
| 90 | + consentModalButtonProps: { |
| 91 | + "aria-controls": string; |
| 92 | + "data-fr-opened": boolean; |
| 93 | + }; |
| 94 | +}) { |
| 95 | + const { consentModalButtonProps } = params; |
| 96 | + |
| 97 | + function FooterConsentManagementItem() { |
| 98 | + const { t } = useTranslation(); |
| 99 | + |
| 100 | + return ( |
| 101 | + <FooterBottomItem |
| 102 | + bottomItem={{ |
| 103 | + "buttonProps": consentModalButtonProps, |
| 104 | + "text": t("cookies management") |
| 105 | + }} |
| 106 | + /> |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + return { FooterConsentManagementItem }; |
| 111 | +} |
| 112 | + |
| 113 | +function createFooterPersonalDataPolicyItem(params: { |
| 114 | + personalDataPolicyLinkProps: RegisteredLinkProps | undefined; |
| 115 | +}) { |
| 116 | + const { personalDataPolicyLinkProps } = params; |
| 117 | + |
| 118 | + function FooterPersonalDataPolicyItem() { |
| 119 | + const { t } = useTranslation(); |
| 120 | + |
| 121 | + if (personalDataPolicyLinkProps === undefined) { |
| 122 | + throw new Error( |
| 123 | + [ |
| 124 | + "You should provide a personalDataPolicyLinkProps to createGdprApi if", |
| 125 | + "you want to add a link to the personal data policy in the footer" |
| 126 | + ].join(" ") |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + return ( |
| 131 | + <FooterBottomItem |
| 132 | + bottomItem={{ |
| 133 | + "text": t("personal data"), |
| 134 | + "linkProps": personalDataPolicyLinkProps |
| 135 | + }} |
| 136 | + /> |
| 137 | + ); |
| 138 | + } |
| 139 | + return { FooterPersonalDataPolicyItem }; |
| 140 | +} |
0 commit comments