|
| 1 | +"use client"; |
| 2 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 3 | + |
| 4 | +import React, { useMemo, useEffect } from "react"; |
| 5 | +import type { ReactNode } from "react"; |
| 6 | +import { isBrowser } from "../tools/isBrowser"; |
| 7 | +import { SsrIsDarkProvider } from "../useIsDark/server"; |
| 8 | +import type { DefaultColorScheme } from "./zz_internal/defaultColorScheme"; |
| 9 | +import { setUseLang } from "../i18n"; |
| 10 | +import { setLink } from "../link"; |
| 11 | +import { start } from "../start"; |
| 12 | + |
| 13 | +export type DsfrProviderProps = { |
| 14 | + children: ReactNode; |
| 15 | + lang: string | undefined; |
| 16 | + /** Default: false */ |
| 17 | + verbose?: boolean; |
| 18 | + /** |
| 19 | + * When true, the nonce of the script tag will be checked, fetched from {@link DsfrHead} component and injected in react-dsfr scripts. |
| 20 | + * |
| 21 | + * @see https://developer.mozilla.org/fr/docs/Web/HTML/Global_attributes/nonce |
| 22 | + * @default false |
| 23 | + */ |
| 24 | + doCheckNonce?: boolean; |
| 25 | + /** |
| 26 | + * Enable Trusted Types with a custom policy name. |
| 27 | + * |
| 28 | + * Don't forget to also add the policy name in {@link DsfrHead} component. |
| 29 | + * |
| 30 | + * `<trustedTypesPolicyName>` and `<trustedTypesPolicyName>-asap` should be set in your Content-Security-Policy header. |
| 31 | + * |
| 32 | + * For example: |
| 33 | + * ```txt |
| 34 | + * With a policy name of "react-dsfr": |
| 35 | + * Content-Security-Policy: |
| 36 | + * require-trusted-types-for 'script'; |
| 37 | + * trusted-types react-dsfr react-dsfr-asap nextjs nextjs#bundler; |
| 38 | + * ``` |
| 39 | + * |
| 40 | + * @see https://developer.mozilla.org/fr/docs/Web/HTTP/Headers/Content-Security-Policy/trusted-types |
| 41 | + * @see {@link DEFAULT_TRUSTED_TYPES_POLICY_NAME} |
| 42 | + * @default "react-dsfr" |
| 43 | + */ |
| 44 | + trustedTypesPolicyName?: string; |
| 45 | +}; |
| 46 | + |
| 47 | +export function DsfrProviderBase( |
| 48 | + props: DsfrProviderProps & { |
| 49 | + Link: Function; |
| 50 | + defaultColorScheme: DefaultColorScheme; |
| 51 | + } |
| 52 | +) { |
| 53 | + const { |
| 54 | + children, |
| 55 | + lang, |
| 56 | + Link, |
| 57 | + defaultColorScheme, |
| 58 | + verbose = false, |
| 59 | + doCheckNonce = false, |
| 60 | + trustedTypesPolicyName = "react-dsfr" |
| 61 | + } = props; |
| 62 | + |
| 63 | + /* |
| 64 | + useEffect(() => { |
| 65 | + dsfrEffect(); |
| 66 | + }, []); |
| 67 | + */ |
| 68 | + |
| 69 | + useMemo(() => { |
| 70 | + if (!isBrowser) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + start({ |
| 75 | + defaultColorScheme, |
| 76 | + verbose, |
| 77 | + doCheckNonce, |
| 78 | + trustedTypesPolicyName, |
| 79 | + "nextParams": { |
| 80 | + "doPersistDarkModePreferenceWithCookie": false, |
| 81 | + "registerEffectAction": action => { |
| 82 | + console.log("registerEffectAction", action); |
| 83 | + |
| 84 | + if (isAfterFirstEffect) { |
| 85 | + console.log("run now"); |
| 86 | + action(); |
| 87 | + } else { |
| 88 | + console.log("push"); |
| 89 | + actions.push(action); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + }); |
| 94 | + }, []); |
| 95 | + |
| 96 | + useMemo(() => { |
| 97 | + if (lang === undefined) { |
| 98 | + return; |
| 99 | + } |
| 100 | + setUseLang({ useLang: () => lang }); |
| 101 | + }, [lang]); |
| 102 | + |
| 103 | + useMemo(() => { |
| 104 | + setLink({ Link: Link as any }); |
| 105 | + }, [Link]); |
| 106 | + |
| 107 | + if (isBrowser) { |
| 108 | + return <>{children}</>; |
| 109 | + } |
| 110 | + |
| 111 | + const isDark = defaultColorScheme === "dark" ? true : false; |
| 112 | + |
| 113 | + return <SsrIsDarkProvider value={isDark}>{children}</SsrIsDarkProvider>; |
| 114 | +} |
| 115 | + |
| 116 | +let isAfterFirstEffect = false; |
| 117 | +const actions: (() => void)[] = []; |
| 118 | + |
| 119 | +function dsfrEffect(): void { |
| 120 | + if (isAfterFirstEffect) { |
| 121 | + return; |
| 122 | + } |
| 123 | + isAfterFirstEffect = true; |
| 124 | + actions.forEach(action => { |
| 125 | + console.log("running action", action); |
| 126 | + action(); |
| 127 | + }); |
| 128 | +} |
| 129 | + |
| 130 | +export function StartDsfrOnHydration() { |
| 131 | + useEffect(() => { |
| 132 | + console.log("wesh hydratation!"); |
| 133 | + |
| 134 | + dsfrEffect(); |
| 135 | + }, []); |
| 136 | + |
| 137 | + return null; |
| 138 | +} |
0 commit comments