{"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,IACD,MAAMC,iBAAAA,GAAoB,CAACC,aAAAA,GAA0B,EAAE,GAAA;AACrD,IAAA,MAAM,CAACC,kBAAAA,EAAoBC,qBAAAA,CAAsB,GAAGC,QAAAA,CAASC,MAAAA,CAAOC,WAAW,CAACR,cAAAA,CAAAA,CAAAA;AAChF;;;MAIA,MAAMS,OAAAA,GAAUN,aAAAA,CAAcO,MAAM,CAAC,CAACC,GAAAA,GAAQ,CAACX,cAAAA,CAAeY,GAAG,CAACD,GAAAA,CAAAA,CAAAA;IAClE,MAAM,CAACE,SAASC,UAAAA,CAAW,GAAGR,SAAS,IAAM,CAAC,CAACG,OAAAA,CAAQM,MAAM,CAAA;IAC7D,MAAMC,cAAAA,GAAiBC,aAAa,mBAAA,EAAqB,CAACC,QAAUA,KAAAA,CAAMC,YAAY,CAACP,GAAG,CAAA;IAE1FQ,SAAAA,CAAU,IAAA;AACR,QAAA,MAAMC,WAAW,CAACC,KAAAA,GAAAA;YAChBjB,qBAAAA,CAAsBiB,KAAAA,CAAAA;YACtBR,UAAAA,CAAW,KAAA,CAAA;AACb,QAAA,CAAA;QAEA,MAAMS,kBAAAA,GAAqB,OACzBC,IAAAA,EACAC,UAAAA,GAAAA;AAEA,YAAA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