Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
1 line
7.8 KiB
Plaintext
1 line
7.8 KiB
Plaintext
{"version":3,"file":"index.js","names":["c","_c","React","createContext","use","useCallback","useEffect","useMemo","useControllableState","RootConfigContext","undefined","ConfigProvider","children","config","configFromProps","setConfig","collectionsBySlug","globalsBySlug","collection","collections","slug","global","globals","getEntityConfig","args","collectionSlug","globalSlug","value","_jsx","useConfig","PageConfigProvider","t0","$","rootConfig","setRootConfig","t1","t2","unauthenticated","t3"],"sources":["../../../src/providers/Config/index.tsx"],"sourcesContent":["/* eslint-disable perfectionist/sort-object-types */ // Need to disable this rule because the order of the overloads is important\n'use client'\nimport type {\n ClientCollectionConfig,\n ClientConfig,\n ClientGlobalConfig,\n CollectionSlug,\n GlobalSlug,\n} from 'payload'\n\nimport React, { createContext, use, useCallback, useEffect, useMemo } from 'react'\n\nimport { useControllableState } from '../../hooks/useControllableState.js'\n\ntype GetEntityConfigFn = {\n // Overload #1: collectionSlug only\n // @todo remove \"{} |\" in 4.0, which would be a breaking change\n (args: { collectionSlug: {} | CollectionSlug; globalSlug?: never }): ClientCollectionConfig\n\n // Overload #2: globalSlug only\n // @todo remove \"{} |\" in 4.0, which would be a breaking change\n (args: { collectionSlug?: never; globalSlug: {} | GlobalSlug }): ClientGlobalConfig\n\n // Overload #3: both/none (fall back to union | null)\n (args: {\n collectionSlug?: {} | CollectionSlug\n globalSlug?: {} | GlobalSlug\n }): ClientCollectionConfig | ClientGlobalConfig | null\n}\n\nexport type ClientConfigContext = {\n config: ClientConfig\n /**\n * Get a collection or global config by its slug. This is preferred over\n * using `config.collections.find` or `config.globals.find`, because\n * getEntityConfig uses a lookup map for O(1) lookups.\n */\n getEntityConfig: GetEntityConfigFn\n setConfig: (config: ClientConfig) => void\n}\n\nconst RootConfigContext = createContext<ClientConfigContext | undefined>(undefined)\n\nexport const ConfigProvider: React.FC<{\n readonly children: React.ReactNode\n readonly config: ClientConfig\n}> = ({ children, config: configFromProps }) => {\n // Need to update local config state if config from props changes, for HMR.\n // That way, config changes will be updated in the UI immediately without needing a refresh.\n // useControllableState handles this for us.\n const [config, setConfig] = useControllableState<ClientConfig>(configFromProps)\n\n // Build lookup maps for collections and globals so we can do O(1) lookups by slug\n const { collectionsBySlug, globalsBySlug } = useMemo(() => {\n const collectionsBySlug: Record<string, ClientCollectionConfig> = {}\n const globalsBySlug: Record<string, ClientGlobalConfig> = {}\n\n for (const collection of config.collections) {\n collectionsBySlug[collection.slug] = collection\n }\n\n for (const global of config.globals) {\n globalsBySlug[global.slug] = global\n }\n\n return { collectionsBySlug, globalsBySlug }\n }, [config])\n\n const getEntityConfig = useCallback<GetEntityConfigFn>(\n (args) => {\n if ('collectionSlug' in args && args.collectionSlug) {\n return collectionsBySlug[args.collectionSlug] ?? null\n }\n\n if ('globalSlug' in args && args.globalSlug) {\n return globalsBySlug[args.globalSlug] ?? null\n }\n\n return null as any\n },\n [collectionsBySlug, globalsBySlug],\n )\n\n const value = useMemo(\n () => ({ config, getEntityConfig, setConfig }),\n [config, getEntityConfig, setConfig],\n )\n\n return <RootConfigContext value={value}>{children}</RootConfigContext>\n}\n\nexport const useConfig = (): ClientConfigContext => use(RootConfigContext)\n\n/**\n * This provider shadows the `ConfigProvider` on the _page_ level, allowing us to\n * update the config when needed, e.g. after authentication.\n * The layout `ConfigProvider` is not updated on page navigation / authentication,\n * as the layout does not re-render in those cases.\n *\n * If the config here has the same reference as the config from the layout, we\n * simply reuse the context from the layout to avoid unnecessary re-renders.\n *\n * @experimental This component is experimental and may change or be removed in future releases. Use at your own risk.\n */\nexport const PageConfigProvider: React.FC<{\n readonly children: React.ReactNode\n readonly config: ClientConfig\n}> = ({ children, config: configFromProps }) => {\n const { config: rootConfig, setConfig: setRootConfig } = useConfig()\n\n /**\n * This `useEffect` is required in order for the _page_ to be able to refresh the client config,\n * which may have been cached on the _layout_ level, where the `ConfigProvider` is managed.\n * Since the layout does not re-render on page navigation / authentication, we need to manually\n * update the config, as the user may have been authenticated in the process, which affects the client config.\n */\n useEffect(() => {\n setRootConfig(configFromProps)\n }, [configFromProps, setRootConfig])\n\n // If this component receives a different config than what is in context from the layout, it is stale.\n // While stale, we instantiate a new context provider that provides the new config until the root context is updated.\n // Unfortunately, referential equality alone does not work bc the reference is lost during server/client serialization,\n // so we need to also compare the `unauthenticated` property.\n if (\n rootConfig !== configFromProps &&\n rootConfig.unauthenticated !== configFromProps.unauthenticated\n ) {\n return <ConfigProvider config={configFromProps}>{children}</ConfigProvider>\n }\n\n return children\n}\n"],"mappings":"AAAA,sDAAsD;AACtD;;AAAA,SAAAA,CAAA,IAAAC,EAAA;;AASA,OAAOC,KAAA,IAASC,aAAa,EAAEC,GAAG,EAAEC,WAAW,EAAEC,SAAS,EAAEC,OAAO,QAAQ;AAE3E,SAASC,oBAAoB,QAAQ;AA6BrC,MAAMC,iBAAA,gBAAoBN,aAAA,CAA+CO,SAAA;AAEzE,OAAO,MAAMC,cAAA,GAGRA,CAAC;EAAEC,QAAQ;EAAEC,MAAA,EAAQC;AAAe,CAAE;EACzC;EACA;EACA;EACA,MAAM,CAACD,MAAA,EAAQE,SAAA,CAAU,GAAGP,oBAAA,CAAmCM,eAAA;EAE/D;EACA,MAAM;IAAEE,iBAAiB,EAAjBA,mBAAiB;IAAEC,aAAa,EAAbA;EAAa,CAAE,GAAGV,OAAA,CAAQ;IACnD,MAAMS,iBAAA,GAA4D,CAAC;IACnE,MAAMC,aAAA,GAAoD,CAAC;IAE3D,KAAK,MAAMC,UAAA,IAAcL,MAAA,CAAOM,WAAW,EAAE;MAC3CH,iBAAiB,CAACE,UAAA,CAAWE,IAAI,CAAC,GAAGF,UAAA;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