install.tsx 4.5 KB

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