index.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. 'use client'
  2. import React, { useCallback, useState } from 'react'
  3. import Modal from '@/app/components/base/modal'
  4. import type { Dependency, PluginDeclaration } from '../../types'
  5. import { InstallStep } from '../../types'
  6. import Uploading from './steps/uploading'
  7. import { useTranslation } from 'react-i18next'
  8. import useGetIcon from '@/app/components/plugins/install-plugin/base/use-get-icon'
  9. import ReadyToInstallPackage from './ready-to-install'
  10. import ReadyToInstallBundle from '../install-bundle/ready-to-install'
  11. import useHideLogic from '../hooks/use-hide-logic'
  12. import cn from '@/utils/classnames'
  13. const i18nPrefix = 'plugin.installModal'
  14. type InstallFromLocalPackageProps = {
  15. file: File
  16. onSuccess: () => void
  17. onClose: () => void
  18. }
  19. const InstallFromLocalPackage: React.FC<InstallFromLocalPackageProps> = ({
  20. file,
  21. onClose,
  22. }) => {
  23. const { t } = useTranslation()
  24. // uploading -> !uploadFailed -> readyToInstall -> installed/failed
  25. const [step, setStep] = useState<InstallStep>(InstallStep.uploading)
  26. const [uniqueIdentifier, setUniqueIdentifier] = useState<string | null>(null)
  27. const [manifest, setManifest] = useState<PluginDeclaration | null>(null)
  28. const [errorMsg, setErrorMsg] = useState<string | null>(null)
  29. const isBundle = file.name.endsWith('.difybndl')
  30. const [dependencies, setDependencies] = useState<Dependency[]>([])
  31. const {
  32. modalClassName,
  33. foldAnimInto,
  34. setIsInstalling,
  35. handleStartToInstall,
  36. } = useHideLogic(onClose)
  37. const getTitle = useCallback(() => {
  38. if (step === InstallStep.uploadFailed)
  39. return t(`${i18nPrefix}.uploadFailed`)
  40. if (isBundle && step === InstallStep.installed)
  41. return t(`${i18nPrefix}.installComplete`)
  42. if (step === InstallStep.installed)
  43. return t(`${i18nPrefix}.installedSuccessfully`)
  44. if (step === InstallStep.installFailed)
  45. return t(`${i18nPrefix}.installFailed`)
  46. return t(`${i18nPrefix}.installPlugin`)
  47. }, [isBundle, step, t])
  48. const { getIconUrl } = useGetIcon()
  49. const handlePackageUploaded = useCallback(async (result: {
  50. uniqueIdentifier: string
  51. manifest: PluginDeclaration
  52. }) => {
  53. const {
  54. manifest,
  55. uniqueIdentifier,
  56. } = result
  57. const icon = await getIconUrl(manifest!.icon)
  58. setUniqueIdentifier(uniqueIdentifier)
  59. setManifest({
  60. ...manifest,
  61. icon,
  62. })
  63. setStep(InstallStep.readyToInstall)
  64. }, [getIconUrl])
  65. const handleBundleUploaded = useCallback((result: Dependency[]) => {
  66. setDependencies(result)
  67. setStep(InstallStep.readyToInstall)
  68. }, [])
  69. const handleUploadFail = useCallback((errorMsg: string) => {
  70. setErrorMsg(errorMsg)
  71. setStep(InstallStep.uploadFailed)
  72. }, [])
  73. return (
  74. <Modal
  75. isShow={true}
  76. onClose={foldAnimInto}
  77. className={cn(modalClassName, 'flex min-w-[560px] p-0 flex-col items-start rounded-2xl border-[0.5px] border-components-panel-border bg-components-panel-bg shadows-shadow-xl')}
  78. closable
  79. >
  80. <div className='flex pt-6 pl-6 pb-3 pr-14 items-start gap-2 self-stretch'>
  81. <div className='self-stretch text-text-primary title-2xl-semi-bold'>
  82. {getTitle()}
  83. </div>
  84. </div>
  85. {step === InstallStep.uploading && (
  86. <Uploading
  87. isBundle={isBundle}
  88. file={file}
  89. onCancel={onClose}
  90. onPackageUploaded={handlePackageUploaded}
  91. onBundleUploaded={handleBundleUploaded}
  92. onFailed={handleUploadFail}
  93. />
  94. )}
  95. {isBundle ? (
  96. <ReadyToInstallBundle
  97. step={step}
  98. onStepChange={setStep}
  99. onStartToInstall={handleStartToInstall}
  100. setIsInstalling={setIsInstalling}
  101. onClose={onClose}
  102. allPlugins={dependencies}
  103. />
  104. ) : (
  105. <ReadyToInstallPackage
  106. step={step}
  107. onStepChange={setStep}
  108. onStartToInstall={handleStartToInstall}
  109. setIsInstalling={setIsInstalling}
  110. onClose={onClose}
  111. uniqueIdentifier={uniqueIdentifier}
  112. manifest={manifest}
  113. errorMsg={errorMsg}
  114. onError={setErrorMsg}
  115. />
  116. )}
  117. </Modal>
  118. )
  119. }
  120. export default InstallFromLocalPackage