install.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useEffect, useMemo } from 'react'
  4. // import { RiInformation2Line } from '@remixicon/react'
  5. import { type Plugin, type PluginManifestInMarket, TaskStatus } from '../../../types'
  6. import Card from '../../../card'
  7. import { pluginManifestInMarketToPluginProps } from '../../utils'
  8. import Button from '@/app/components/base/button'
  9. import { useTranslation } from 'react-i18next'
  10. import { RiLoader2Line } from '@remixicon/react'
  11. import { useInstallPackageFromMarketPlace, usePluginDeclarationFromMarketPlace, useUpdatePackageFromMarketPlace } from '@/service/use-plugins'
  12. import checkTaskStatus from '../../base/check-task-status'
  13. import useCheckInstalled from '@/app/components/plugins/install-plugin/hooks/use-check-installed'
  14. import Version from '../../base/version'
  15. import { usePluginTaskList } from '@/service/use-plugins'
  16. import { gte } from 'semver'
  17. import { useAppContext } from '@/context/app-context'
  18. import useInstallPluginLimit from '../../hooks/use-install-plugin-limit'
  19. const i18nPrefix = 'plugin.installModal'
  20. type Props = {
  21. uniqueIdentifier: string
  22. payload: PluginManifestInMarket | Plugin
  23. onCancel: () => void
  24. onStartToInstall?: () => void
  25. onInstalled: (notRefresh?: boolean) => void
  26. onFailed: (message?: string) => void
  27. }
  28. const Installed: FC<Props> = ({
  29. uniqueIdentifier,
  30. payload,
  31. onCancel,
  32. onStartToInstall,
  33. onInstalled,
  34. onFailed,
  35. }) => {
  36. const { t } = useTranslation()
  37. const toInstallVersion = payload.version || payload.latest_version
  38. const pluginId = (payload as Plugin).plugin_id
  39. const { installedInfo, isLoading } = useCheckInstalled({
  40. pluginIds: [pluginId],
  41. enabled: !!pluginId,
  42. })
  43. const installedInfoPayload = installedInfo?.[pluginId]
  44. const installedVersion = installedInfoPayload?.installedVersion
  45. const hasInstalled = !!installedVersion
  46. const { mutateAsync: installPackageFromMarketPlace } = useInstallPackageFromMarketPlace()
  47. const { mutateAsync: updatePackageFromMarketPlace } = useUpdatePackageFromMarketPlace()
  48. const [isInstalling, setIsInstalling] = React.useState(false)
  49. const {
  50. check,
  51. stop,
  52. } = checkTaskStatus()
  53. const { handleRefetch } = usePluginTaskList(payload.category)
  54. useEffect(() => {
  55. if (hasInstalled && uniqueIdentifier === installedInfoPayload.uniqueIdentifier)
  56. onInstalled()
  57. }, [hasInstalled])
  58. const handleCancel = () => {
  59. stop()
  60. onCancel()
  61. }
  62. const handleInstall = async () => {
  63. if (isInstalling) return
  64. onStartToInstall?.()
  65. setIsInstalling(true)
  66. try {
  67. let taskId
  68. let isInstalled
  69. if (hasInstalled) {
  70. const {
  71. all_installed,
  72. task_id,
  73. } = await updatePackageFromMarketPlace({
  74. original_plugin_unique_identifier: installedInfoPayload.uniqueIdentifier,
  75. new_plugin_unique_identifier: uniqueIdentifier,
  76. })
  77. taskId = task_id
  78. isInstalled = all_installed
  79. }
  80. else {
  81. const {
  82. all_installed,
  83. task_id,
  84. } = await installPackageFromMarketPlace(uniqueIdentifier)
  85. taskId = task_id
  86. isInstalled = all_installed
  87. }
  88. if (isInstalled) {
  89. onInstalled()
  90. return
  91. }
  92. handleRefetch()
  93. const { status, error } = await check({
  94. taskId,
  95. pluginUniqueIdentifier: uniqueIdentifier,
  96. })
  97. if (status === TaskStatus.failed) {
  98. onFailed(error)
  99. return
  100. }
  101. onInstalled(true)
  102. }
  103. catch (e) {
  104. if (typeof e === 'string') {
  105. onFailed(e)
  106. return
  107. }
  108. onFailed()
  109. }
  110. }
  111. const { langGeniusVersionInfo } = useAppContext()
  112. const { data: pluginDeclaration } = usePluginDeclarationFromMarketPlace(uniqueIdentifier)
  113. const isDifyVersionCompatible = useMemo(() => {
  114. if (!pluginDeclaration || !langGeniusVersionInfo.current_version) return true
  115. return gte(langGeniusVersionInfo.current_version, pluginDeclaration?.manifest.meta.minimum_dify_version ?? '0.0.0')
  116. }, [langGeniusVersionInfo.current_version, pluginDeclaration])
  117. const { canInstall } = useInstallPluginLimit({ ...payload, from: 'marketplace' })
  118. return (
  119. <>
  120. <div className='flex flex-col items-start justify-center gap-4 self-stretch px-6 py-3'>
  121. <div className='system-md-regular text-text-secondary'>
  122. <p>{t(`${i18nPrefix}.readyToInstall`)}</p>
  123. {!isDifyVersionCompatible && (
  124. <p className='system-md-regular text-text-warning'>
  125. {t('plugin.difyVersionNotCompatible', { minimalDifyVersion: pluginDeclaration?.manifest.meta.minimum_dify_version })}
  126. </p>
  127. )}
  128. </div>
  129. <div className='flex flex-wrap content-start items-start gap-1 self-stretch rounded-2xl bg-background-section-burn p-2'>
  130. <Card
  131. className='w-full'
  132. payload={pluginManifestInMarketToPluginProps(payload as PluginManifestInMarket)}
  133. titleLeft={!isLoading && <Version
  134. hasInstalled={hasInstalled}
  135. installedVersion={installedVersion}
  136. toInstallVersion={toInstallVersion}
  137. />}
  138. limitedInstall={!canInstall}
  139. />
  140. </div>
  141. </div>
  142. {/* Action Buttons */}
  143. <div className='flex items-center justify-end gap-2 self-stretch p-6 pt-5'>
  144. {!isInstalling && (
  145. <Button variant='secondary' className='min-w-[72px]' onClick={handleCancel}>
  146. {t('common.operation.cancel')}
  147. </Button>
  148. )}
  149. <Button
  150. variant='primary'
  151. className='flex min-w-[72px] space-x-0.5'
  152. disabled={isInstalling || isLoading || !canInstall}
  153. onClick={handleInstall}
  154. >
  155. {isInstalling && <RiLoader2Line className='h-4 w-4 animate-spin-slow' />}
  156. <span>{t(`${i18nPrefix}.${isInstalling ? 'installing' : 'install'}`)}</span>
  157. </Button>
  158. </div>
  159. </>
  160. )
  161. }
  162. export default React.memo(Installed)