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 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. 'use client'
  2. import type { FC } from 'react'
  3. import { useRef } from 'react'
  4. import React, { useCallback, useState } from 'react'
  5. import type { Dependency, InstallStatusResponse, Plugin, VersionInfo } from '../../../types'
  6. import Button from '@/app/components/base/button'
  7. import { RiLoader2Line } from '@remixicon/react'
  8. import { useTranslation } from 'react-i18next'
  9. import type { ExposeRefs } from './install-multi'
  10. import InstallMulti from './install-multi'
  11. import { useInstallOrUpdate } from '@/service/use-plugins'
  12. import useRefreshPluginList from '../../hooks/use-refresh-plugin-list'
  13. import { useCanInstallPluginFromMarketplace } from '@/app/components/plugins/plugin-page/use-permission'
  14. import { useMittContextSelector } from '@/context/mitt-context'
  15. import Checkbox from '@/app/components/base/checkbox'
  16. const i18nPrefix = 'plugin.installModal'
  17. type Props = {
  18. allPlugins: Dependency[]
  19. onStartToInstall?: () => void
  20. onInstalled: (plugins: Plugin[], installStatus: InstallStatusResponse[]) => void
  21. onCancel: () => void
  22. isFromMarketPlace?: boolean
  23. isHideButton?: boolean
  24. }
  25. const Install: FC<Props> = ({
  26. allPlugins,
  27. onStartToInstall,
  28. onInstalled,
  29. onCancel,
  30. isFromMarketPlace,
  31. isHideButton,
  32. }) => {
  33. const { t } = useTranslation()
  34. const emit = useMittContextSelector(s => s.emit)
  35. const [selectedPlugins, setSelectedPlugins] = React.useState<Plugin[]>([])
  36. const [selectedIndexes, setSelectedIndexes] = React.useState<number[]>([])
  37. const selectedPluginsNum = selectedPlugins.length
  38. const installMultiRef = useRef<ExposeRefs>(null)
  39. const { refreshPluginList } = useRefreshPluginList()
  40. const [canInstall, setCanInstall] = React.useState(false)
  41. const [installedInfo, setInstalledInfo] = useState<Record<string, VersionInfo> | undefined>(undefined)
  42. const handleLoadedAllPlugin = useCallback((installedInfo: Record<string, VersionInfo> | undefined) => {
  43. setInstalledInfo(installedInfo)
  44. setCanInstall(true)
  45. }, [])
  46. // Install from marketplace and github
  47. const { mutate: installOrUpdate, isPending: isInstalling } = useInstallOrUpdate({
  48. onSuccess: (res: InstallStatusResponse[]) => {
  49. onInstalled(selectedPlugins, res.map((r, i) => {
  50. return ({
  51. ...r,
  52. isFromMarketPlace: allPlugins[selectedIndexes[i]].type === 'marketplace',
  53. })
  54. }))
  55. const hasInstallSuccess = res.some(r => r.success)
  56. if (hasInstallSuccess) {
  57. refreshPluginList(undefined, true)
  58. emit('plugin:install:success', selectedPlugins.map((p) => {
  59. return `${p.plugin_id}/${p.name}`
  60. }))
  61. }
  62. },
  63. })
  64. const handleInstall = () => {
  65. onStartToInstall?.()
  66. installOrUpdate({
  67. payload: allPlugins.filter((_d, index) => selectedIndexes.includes(index)),
  68. plugin: selectedPlugins,
  69. installedInfo: installedInfo!,
  70. })
  71. }
  72. const [isSelectAll, setIsSelectAll] = useState(false)
  73. const [isIndeterminate, setIsIndeterminate] = useState(false)
  74. const handleClickSelectAll = useCallback(() => {
  75. if (isSelectAll)
  76. installMultiRef.current?.deSelectAllPlugins()
  77. else
  78. installMultiRef.current?.selectAllPlugins()
  79. }, [isSelectAll])
  80. const handleSelectAll = useCallback((plugins: Plugin[], selectedIndexes: number[]) => {
  81. setSelectedPlugins(plugins)
  82. setSelectedIndexes(selectedIndexes)
  83. setIsSelectAll(true)
  84. setIsIndeterminate(false)
  85. }, [])
  86. const handleDeSelectAll = useCallback(() => {
  87. setSelectedPlugins([])
  88. setSelectedIndexes([])
  89. setIsSelectAll(false)
  90. setIsIndeterminate(false)
  91. }, [])
  92. const handleSelect = useCallback((plugin: Plugin, selectedIndex: number, allPluginsLength: number) => {
  93. const isSelected = !!selectedPlugins.find(p => p.plugin_id === plugin.plugin_id)
  94. let nextSelectedPlugins
  95. if (isSelected)
  96. nextSelectedPlugins = selectedPlugins.filter(p => p.plugin_id !== plugin.plugin_id)
  97. else
  98. nextSelectedPlugins = [...selectedPlugins, plugin]
  99. setSelectedPlugins(nextSelectedPlugins)
  100. const nextSelectedIndexes = isSelected ? selectedIndexes.filter(i => i !== selectedIndex) : [...selectedIndexes, selectedIndex]
  101. setSelectedIndexes(nextSelectedIndexes)
  102. if (nextSelectedPlugins.length === 0) {
  103. setIsSelectAll(false)
  104. setIsIndeterminate(false)
  105. }
  106. else if (nextSelectedPlugins.length === allPluginsLength) {
  107. setIsSelectAll(true)
  108. setIsIndeterminate(false)
  109. }
  110. else {
  111. setIsIndeterminate(true)
  112. setIsSelectAll(false)
  113. }
  114. }, [selectedPlugins, selectedIndexes])
  115. const { canInstallPluginFromMarketplace } = useCanInstallPluginFromMarketplace()
  116. return (
  117. <>
  118. <div className='flex flex-col items-start justify-center gap-4 self-stretch px-6 py-3'>
  119. <div className='system-md-regular text-text-secondary'>
  120. <p>{t(`${i18nPrefix}.${selectedPluginsNum > 1 ? 'readyToInstallPackages' : 'readyToInstallPackage'}`, { num: selectedPluginsNum })}</p>
  121. </div>
  122. <div className='w-full space-y-1 rounded-2xl bg-background-section-burn p-2'>
  123. <InstallMulti
  124. ref={installMultiRef}
  125. allPlugins={allPlugins}
  126. selectedPlugins={selectedPlugins}
  127. onSelect={handleSelect}
  128. onSelectAll={handleSelectAll}
  129. onDeSelectAll={handleDeSelectAll}
  130. onLoadedAllPlugin={handleLoadedAllPlugin}
  131. isFromMarketPlace={isFromMarketPlace}
  132. />
  133. </div>
  134. </div>
  135. {/* Action Buttons */}
  136. {!isHideButton && (
  137. <div className='flex items-center justify-between gap-2 self-stretch p-6 pt-5'>
  138. <div className='px-2'>
  139. {canInstall && <div className='flex items-center gap-x-2' onClick={handleClickSelectAll}>
  140. <Checkbox checked={isSelectAll} indeterminate={isIndeterminate} />
  141. <p className='system-sm-medium cursor-pointer text-text-secondary'>{isSelectAll ? t('common.operation.deSelectAll') : t('common.operation.selectAll')}</p>
  142. </div>}
  143. </div>
  144. <div className='flex items-center justify-end gap-2 self-stretch'>
  145. {!canInstall && (
  146. <Button variant='secondary' className='min-w-[72px]' onClick={onCancel}>
  147. {t('common.operation.cancel')}
  148. </Button>
  149. )}
  150. <Button
  151. variant='primary'
  152. className='flex min-w-[72px] space-x-0.5'
  153. disabled={!canInstall || isInstalling || selectedPlugins.length === 0 || !canInstallPluginFromMarketplace}
  154. onClick={handleInstall}
  155. >
  156. {isInstalling && <RiLoader2Line className='h-4 w-4 animate-spin-slow' />}
  157. <span>{t(`${i18nPrefix}.${isInstalling ? 'installing' : 'install'}`)}</span>
  158. </Button>
  159. </div>
  160. </div>
  161. )}
  162. </>
  163. )
  164. }
  165. export default React.memo(Install)