You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

install.tsx 5.1KB

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