install.tsx 4.9 KB

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