{"version":3,"file":"components/Manifest/data.mjs","sources":["../../../src/components/Manifest/data.tsx"],"sourcesContent":["import { Manifest } from '@rsdoctor/types';\nimport { Skeleton } from 'antd';\nimport React, { useEffect, useState } from 'react';\nimport { useDataLoader } from '../../utils';\nimport { PageState } from '../../constants';\nimport { FailedStatus } from '../Status';\nimport { BaseDataLoader } from '../../utils/data/base';\n\ntype ExtractAlias<T> = T extends [property: string, alias: infer P][] ? `${P extends string ? P : never}` : never;\n\n/**\n * @deprecated\n */\nexport function ConnectManifestData<\n  Props extends object,\n  Keys extends\n    | [property: `${Manifest.RsdoctorManifestMappingKeys}`, alias: keyof Props][]\n    | [property: string, alias: keyof Props][],\n>(\n  manifestLoader: () => Promise<Manifest.RsdoctorManifestWithShardingFiles>,\n  keys: Keys,\n  Component: React.FC<Props>,\n): React.FC<Omit<Props, ExtractAlias<Keys>>> {\n  const promise = manifestLoader();\n\n  return function _() {\n    const [manifest, setManifest] = useState<Manifest.RsdoctorManifestWithShardingFiles>();\n    const [state, setState] = useState(PageState.Pending);\n    const [props, setProps] = useState<Props>({} as Props);\n\n    const { loader } = useDataLoader(manifest);\n\n    useEffect(() => {\n      ensureManifest(promise).then(() => {\n        executeLoader(loader);\n      });\n    }, [loader]);\n\n    function ensureManifest(pro: Promise<Manifest.RsdoctorManifestWithShardingFiles>) {\n      return pro\n        .then((manifest) => {\n          setManifest(manifest);\n        })\n        .catch(() => {\n          setState(PageState.Fail);\n        });\n    }\n\n    function executeLoader(loader: BaseDataLoader | void) {\n      if (!loader) return;\n      Promise.all(keys.map(([key]) => loader.loadData(key)))\n        .then((e) => {\n          const p = e.reduce<Props>((t, val, i) => {\n            const [_key, alias] = keys[i];\n            t[alias] = val as Props[keyof Props];\n            return t;\n          }, {} as Props);\n\n          // if (process.env.NODE_ENV === 'development') {\n          //   console.log('[ConnectManifestData] props: ', p, loader);\n          // }\n\n          setProps(p);\n          setState(PageState.Success);\n        })\n        .catch((err) => {\n          console.error(err);\n          setState(PageState.Fail);\n        });\n    }\n\n    if (state === PageState.Pending) {\n      return <Skeleton active />;\n    }\n\n    if (state === PageState.Fail) {\n      return (\n        <FailedStatus\n          retry={() => {\n            setState(PageState.Pending);\n            promise\n              .then(() => {\n                executeLoader(loader);\n              })\n              .catch(() => {\n                ensureManifest(manifestLoader());\n              });\n          }}\n        />\n      );\n    }\n\n    return <Component {...props} />;\n  };\n}\n"],"names":["ConnectManifestData","manifestLoader","keys","Component","promise","manifest","setManifest","useState","state","setState","PageState","props","setProps","loader","useDataLoader","useEffect","ensureManifest","executeLoader","pro","Promise","key","e","p","t","val","i","_key","alias","err","console","Skeleton","FailedStatus"],"mappings":";;;;;;AAaO,SAASA,oBAMdC,cAAyE,EACzEC,IAAU,EACVC,SAA0B;IAE1B,MAAMC,UAAUH;IAEhB,OAAO;QACL,MAAM,CAACI,UAAUC,YAAY,GAAGC;QAChC,MAAM,CAACC,OAAOC,SAAS,GAAGF,SAASG,UAAU,OAAO;QACpD,MAAM,CAACC,OAAOC,SAAS,GAAGL,SAAgB,CAAC;QAE3C,MAAM,EAAEM,MAAM,EAAE,GAAGC,cAAcT;QAEjCU,UAAU;YACRC,eAAeZ,SAAS,IAAI,CAAC;gBAC3Ba,cAAcJ;YAChB;QACF,GAAG;YAACA;SAAO;QAEX,SAASG,eAAeE,GAAwD;YAC9E,OAAOA,IACJ,IAAI,CAAC,CAACb;gBACLC,YAAYD;YACd,GACC,KAAK,CAAC;gBACLI,SAASC,UAAU,IAAI;YACzB;QACJ;QAEA,SAASO,cAAcJ,MAA6B;YAClD,IAAI,CAACA,QAAQ;YACbM,QAAQ,GAAG,CAACjB,KAAK,GAAG,CAAC,CAAC,CAACkB,IAAI,GAAKP,OAAO,QAAQ,CAACO,OAC7C,IAAI,CAAC,CAACC;gBACL,MAAMC,IAAID,EAAE,MAAM,CAAQ,CAACE,GAAGC,KAAKC;oBACjC,MAAM,CAACC,MAAMC,MAAM,GAAGzB,IAAI,CAACuB,EAAE;oBAC7BF,CAAC,CA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