index.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. 'use client'
  2. import React, { useCallback, useState } from 'react'
  3. import Modal from '@/app/components/base/modal'
  4. import type { Dependency, Plugin, PluginManifestInMarket } from '../../types'
  5. import { InstallStep } from '../../types'
  6. import Install from './steps/install'
  7. import Installed from '../base/installed'
  8. import { useTranslation } from 'react-i18next'
  9. import useRefreshPluginList from '../hooks/use-refresh-plugin-list'
  10. import ReadyToInstallBundle from '../install-bundle/ready-to-install'
  11. import cn from '@/utils/classnames'
  12. import useHideLogic from '../hooks/use-hide-logic'
  13. const i18nPrefix = 'plugin.installModal'
  14. type InstallFromMarketplaceProps = {
  15. uniqueIdentifier: string
  16. manifest: PluginManifestInMarket | Plugin
  17. isBundle?: boolean
  18. dependencies?: Dependency[]
  19. onSuccess: () => void
  20. onClose: () => void
  21. }
  22. const InstallFromMarketplace: React.FC<InstallFromMarketplaceProps> = ({
  23. uniqueIdentifier,
  24. manifest,
  25. isBundle,
  26. dependencies,
  27. onSuccess,
  28. onClose,
  29. }) => {
  30. const { t } = useTranslation()
  31. // readyToInstall -> check installed -> installed/failed
  32. const [step, setStep] = useState<InstallStep>(InstallStep.readyToInstall)
  33. const [errorMsg, setErrorMsg] = useState<string | null>(null)
  34. const { refreshPluginList } = useRefreshPluginList()
  35. const {
  36. modalClassName,
  37. foldAnimInto,
  38. setIsInstalling,
  39. handleStartToInstall,
  40. } = useHideLogic(onClose)
  41. const getTitle = useCallback(() => {
  42. if (isBundle && step === InstallStep.installed)
  43. return t(`${i18nPrefix}.installComplete`)
  44. if (step === InstallStep.installed)
  45. return t(`${i18nPrefix}.installedSuccessfully`)
  46. if (step === InstallStep.installFailed)
  47. return t(`${i18nPrefix}.installFailed`)
  48. return t(`${i18nPrefix}.installPlugin`)
  49. }, [isBundle, step, t])
  50. const handleInstalled = useCallback((notRefresh?: boolean) => {
  51. setStep(InstallStep.installed)
  52. if (!notRefresh)
  53. refreshPluginList(manifest)
  54. setIsInstalling(false)
  55. }, [manifest, refreshPluginList, setIsInstalling])
  56. const handleFailed = useCallback((errorMsg?: string) => {
  57. setStep(InstallStep.installFailed)
  58. setIsInstalling(false)
  59. if (errorMsg)
  60. setErrorMsg(errorMsg)
  61. }, [setIsInstalling])
  62. return (
  63. <Modal
  64. isShow={true}
  65. onClose={foldAnimInto}
  66. wrapperClassName='z-[9999]'
  67. className={cn(modalClassName, 'shadows-shadow-xl flex min-w-[560px] flex-col items-start rounded-2xl border-[0.5px] border-components-panel-border bg-components-panel-bg p-0')}
  68. closable
  69. >
  70. <div className='flex items-start gap-2 self-stretch pb-3 pl-6 pr-14 pt-6'>
  71. <div className='title-2xl-semi-bold self-stretch text-text-primary'>
  72. {getTitle()}
  73. </div>
  74. </div>
  75. {
  76. isBundle ? (
  77. <ReadyToInstallBundle
  78. step={step}
  79. onStepChange={setStep}
  80. onStartToInstall={handleStartToInstall}
  81. setIsInstalling={setIsInstalling}
  82. onClose={onClose}
  83. allPlugins={dependencies!}
  84. isFromMarketPlace
  85. />
  86. ) : (<>
  87. {
  88. step === InstallStep.readyToInstall && (
  89. <Install
  90. uniqueIdentifier={uniqueIdentifier}
  91. payload={manifest!}
  92. onCancel={onClose}
  93. onInstalled={handleInstalled}
  94. onFailed={handleFailed}
  95. onStartToInstall={handleStartToInstall}
  96. />
  97. )}
  98. {
  99. [InstallStep.installed, InstallStep.installFailed].includes(step) && (
  100. <Installed
  101. payload={manifest!}
  102. isMarketPayload
  103. isFailed={step === InstallStep.installFailed}
  104. errMsg={errorMsg}
  105. onCancel={onSuccess}
  106. />
  107. )
  108. }
  109. </>
  110. )
  111. }
  112. </Modal >
  113. )
  114. }
  115. export default InstallFromMarketplace