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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. // eslint-disable-next-line react-hooks/exhaustive-deps
  48. }, [hasInstalled])
  49. const [isInstalling, setIsInstalling] = React.useState(false)
  50. const { mutateAsync: installPackageFromLocal } = useInstallPackageFromLocal()
  51. const {
  52. check,
  53. stop,
  54. } = checkTaskStatus()
  55. const handleCancel = () => {
  56. stop()
  57. onCancel()
  58. }
  59. const { handleRefetch } = usePluginTaskList(payload.category)
  60. const handleInstall = async () => {
  61. if (isInstalling) return
  62. setIsInstalling(true)
  63. onStartToInstall?.()
  64. try {
  65. if (hasInstalled)
  66. await uninstallPlugin(installedInfoPayload.installedId)
  67. const {
  68. all_installed,
  69. task_id,
  70. } = await installPackageFromLocal(uniqueIdentifier)
  71. const taskId = task_id
  72. const isInstalled = all_installed
  73. if (isInstalled) {
  74. onInstalled()
  75. return
  76. }
  77. handleRefetch()
  78. const { status, error } = await check({
  79. taskId,
  80. pluginUniqueIdentifier: uniqueIdentifier,
  81. })
  82. if (status === TaskStatus.failed) {
  83. onFailed(error)
  84. return
  85. }
  86. onInstalled(true)
  87. }
  88. catch (e) {
  89. if (typeof e === 'string') {
  90. onFailed(e)
  91. return
  92. }
  93. onFailed()
  94. }
  95. }
  96. const { langeniusVersionInfo } = useAppContext()
  97. const isDifyVersionCompatible = useMemo(() => {
  98. if (!langeniusVersionInfo.current_version)
  99. return true
  100. return gte(langeniusVersionInfo.current_version, payload.meta.minimum_dify_version ?? '0.0.0')
  101. }, [langeniusVersionInfo.current_version, payload.meta.minimum_dify_version])
  102. return (
  103. <>
  104. <div className='flex flex-col items-start justify-center gap-4 self-stretch px-6 py-3'>
  105. <div className='system-md-regular text-text-secondary'>
  106. <p>{t(`${i18nPrefix}.readyToInstall`)}</p>
  107. <p>
  108. <Trans
  109. i18nKey={`${i18nPrefix}.fromTrustSource`}
  110. components={{ trustSource: <span className='system-md-semibold' /> }}
  111. />
  112. </p>
  113. {!isDifyVersionCompatible && (
  114. <p className='system-md-regular flex items-center gap-1 text-text-warning'>
  115. {t('plugin.difyVersionNotCompatible', { minimalDifyVersion: payload.meta.minimum_dify_version })}
  116. </p>
  117. )}
  118. </div>
  119. <div className='flex flex-wrap content-start items-start gap-1 self-stretch rounded-2xl bg-background-section-burn p-2'>
  120. <Card
  121. className='w-full'
  122. payload={pluginManifestToCardPluginProps(payload)}
  123. titleLeft={!isLoading && <Version
  124. hasInstalled={hasInstalled}
  125. installedVersion={installedVersion}
  126. toInstallVersion={toInstallVersion}
  127. />}
  128. />
  129. </div>
  130. </div>
  131. {/* Action Buttons */}
  132. <div className='flex items-center justify-end gap-2 self-stretch p-6 pt-5'>
  133. {!isInstalling && (
  134. <Button variant='secondary' className='min-w-[72px]' onClick={handleCancel}>
  135. {t('common.operation.cancel')}
  136. </Button>
  137. )}
  138. <Button
  139. variant='primary'
  140. className='flex min-w-[72px] space-x-0.5'
  141. disabled={isInstalling || isLoading}
  142. onClick={handleInstall}
  143. >
  144. {isInstalling && <RiLoader2Line className='h-4 w-4 animate-spin-slow' />}
  145. <span>{t(`${i18nPrefix}.${isInstalling ? 'installing' : 'install'}`)}</span>
  146. </Button>
  147. </div>
  148. </>
  149. )
  150. }
  151. export default React.memo(Installed)