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