1 line
5.0 KiB
Plaintext
1 line
5.0 KiB
Plaintext
{"version":3,"file":"useLazyComponents.mjs","sources":["../../../admin/src/hooks/useLazyComponents.ts"],"sourcesContent":["import { ComponentType, useCallback, useEffect, useState } from 'react';\n\nimport { StrapiAppContextValue, useStrapiApp } from '@strapi/admin/strapi-admin';\n\nconst componentStore = new Map<string, ComponentType | undefined>();\n\ntype LazyComponentStore = Record<string, ComponentType | undefined>;\n\ntype CustomField = NonNullable<ReturnType<StrapiAppContextValue['customFields']['get']>>;\n\ninterface UseLazyComponentsReturn {\n isLazyLoading: boolean;\n lazyComponentStore: LazyComponentStore;\n cleanup: () => void;\n}\n\n/**\n * @description A hook to lazy load custom field components\n */\nconst useLazyComponents = (componentUids: string[] = []): UseLazyComponentsReturn => {\n const [lazyComponentStore, setLazyComponentStore] = useState(Object.fromEntries(componentStore));\n /**\n * Start loading only if there are any components passed in\n * and there are some new to load\n */\n const newUids = componentUids.filter((uid) => !componentStore.get(uid));\n const [loading, setLoading] = useState(() => !!newUids.length);\n const getCustomField = useStrapiApp('useLazyComponents', (state) => state.customFields.get);\n\n useEffect(() => {\n const setStore = (store: Record<string, ComponentType | undefined>) => {\n setLazyComponentStore(store);\n setLoading(false);\n };\n\n const lazyLoadComponents = async (\n uids: string[],\n components: Array<ReturnType<CustomField['components']['Input']>>\n ) => {\n const modules = await Promise.all(components);\n\n uids.forEach((uid, index) => {\n componentStore.set(uid, modules[index].default);\n });\n\n setStore(Object.fromEntries(componentStore));\n };\n\n if (newUids.length > 0) {\n setLoading(true);\n\n const componentPromises = newUids.reduce<\n Array<ReturnType<CustomField['components']['Input']>>\n >((arrayOfPromises, uid) => {\n const customField = getCustomField(uid);\n\n if (customField) {\n arrayOfPromises.push(customField.components.Input());\n }\n\n return arrayOfPromises;\n }, []);\n\n if (componentPromises.length > 0) {\n lazyLoadComponents(newUids, componentPromises);\n }\n }\n }, [newUids, getCustomField]);\n\n /**\n * Wrap this in a callback so it can be used in\n * effects to cleanup the cached store if required\n */\n const cleanup = useCallback(() => {\n componentStore.clear();\n setLazyComponentStore({});\n }, []);\n\n return { isLazyLoading: loading, lazyComponentStore, cleanup };\n};\n\nexport { useLazyComponents };\nexport type { UseLazyComponentsReturn, LazyComponentStore };\n"],"names":["componentStore","Map","useLazyComponents","componentUids","lazyComponentStore","setLazyComponentStore","useState","Object","fromEntries","newUids","filter","uid","get","loading","setLoading","length","getCustomField","useStrapiApp","state","customFields","useEffect","setStore","store","lazyLoadComponents","uids","components","modules","Promise","all","forEach","index","set","default","componentPromises","reduce","arrayOfPromises","customField","push","Input","cleanup","useCallback","clear","isLazyLoading"],"mappings":";;;AAIA,MAAMA,iBAAiB,IAAIC,GAAAA,EAAAA;AAY3B;;AAEC,IACKC,MAAAA,iBAAAA,GAAoB,CAACC,aAAAA,GAA0B,EAAE,GAAA;AACrD,IAAA,MAAM,CAACC,kBAAoBC,EAAAA,qBAAAA,CAAsB,GAAGC,QAASC,CAAAA,MAAAA,CAAOC,WAAW,CAACR,cAAAA,CAAAA,CAAAA;AAChF;;;MAIA,MAAMS,OAAUN,GAAAA,aAAAA,CAAcO,MAAM,CAAC,CAACC,GAAQ,GAAA,CAACX,cAAeY,CAAAA,GAAG,CAACD,GAAAA,CAAAA,CAAAA;IAClE,MAAM,CAACE,SAASC,UAAW,CAAA,GAAGR,SAAS,IAAM,CAAC,CAACG,OAAAA,CAAQM,MAAM,CAAA;IAC7D,MAAMC,cAAAA,GAAiBC,aAAa,mBAAqB,EAAA,CAACC,QAAUA,KAAMC,CAAAA,YAAY,CAACP,GAAG,CAAA;IAE1FQ,SAAU,CAAA,IAAA;AACR,QAAA,MAAMC,WAAW,CAACC,KAAAA,GAAAA;YAChBjB,qBAAsBiB,CAAAA,KAAAA,CAAAA;YACtBR,UAAW,CAAA,KAAA,CAAA;AACb,SAAA;QAEA,MAAMS,kBAAAA,GAAqB,OACzBC,IACAC,EAAAA,UAAAA,GAAAA;AAEA,YAAA,MAAMC,OAAU,GAAA,MAAMC,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