您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

install-multi.tsx 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. 'use client'
  2. import type { ForwardRefRenderFunction } from 'react'
  3. import { useImperativeHandle } from 'react'
  4. import React, { useCallback, useEffect, useMemo, useState } from 'react'
  5. import type { Dependency, GitHubItemAndMarketPlaceDependency, PackageDependency, Plugin, VersionInfo } from '../../../types'
  6. import MarketplaceItem from '../item/marketplace-item'
  7. import GithubItem from '../item/github-item'
  8. import { useFetchPluginsInMarketPlaceByInfo } from '@/service/use-plugins'
  9. import useCheckInstalled from '@/app/components/plugins/install-plugin/hooks/use-check-installed'
  10. import produce from 'immer'
  11. import PackageItem from '../item/package-item'
  12. import LoadingError from '../../base/loading-error'
  13. import { useGlobalPublicStore } from '@/context/global-public-context'
  14. import { pluginInstallLimit } from '../../hooks/use-install-plugin-limit'
  15. type Props = {
  16. allPlugins: Dependency[]
  17. selectedPlugins: Plugin[]
  18. onSelect: (plugin: Plugin, selectedIndex: number, allCanInstallPluginsLength: number) => void
  19. onSelectAll: (plugins: Plugin[], selectedIndexes: number[]) => void
  20. onDeSelectAll: () => void
  21. onLoadedAllPlugin: (installedInfo: Record<string, VersionInfo>) => void
  22. isFromMarketPlace?: boolean
  23. }
  24. export type ExposeRefs = {
  25. selectAllPlugins: () => void
  26. deSelectAllPlugins: () => void
  27. }
  28. const InstallByDSLList: ForwardRefRenderFunction<ExposeRefs, Props> = ({
  29. allPlugins,
  30. selectedPlugins,
  31. onSelect,
  32. onSelectAll,
  33. onDeSelectAll,
  34. onLoadedAllPlugin,
  35. isFromMarketPlace,
  36. }, ref) => {
  37. const systemFeatures = useGlobalPublicStore(s => s.systemFeatures)
  38. // DSL has id, to get plugin info to show more info
  39. const { isLoading: isFetchingMarketplaceDataById, data: infoGetById, error: infoByIdError } = useFetchPluginsInMarketPlaceByInfo(allPlugins.filter(d => d.type === 'marketplace').map((d) => {
  40. const dependecy = (d as GitHubItemAndMarketPlaceDependency).value
  41. // split org, name, version by / and :
  42. // and remove @ and its suffix
  43. const [orgPart, nameAndVersionPart] = dependecy.marketplace_plugin_unique_identifier!.split('@')[0].split('/')
  44. const [name, version] = nameAndVersionPart.split(':')
  45. return {
  46. organization: orgPart,
  47. plugin: name,
  48. version,
  49. }
  50. }))
  51. // has meta(org,name,version), to get id
  52. const { isLoading: isFetchingDataByMeta, data: infoByMeta, error: infoByMetaError } = useFetchPluginsInMarketPlaceByInfo(allPlugins.filter(d => d.type === 'marketplace').map(d => (d as GitHubItemAndMarketPlaceDependency).value!))
  53. const [plugins, doSetPlugins] = useState<(Plugin | undefined)[]>((() => {
  54. const hasLocalPackage = allPlugins.some(d => d.type === 'package')
  55. if (!hasLocalPackage)
  56. return []
  57. const _plugins = allPlugins.map((d) => {
  58. if (d.type === 'package') {
  59. return {
  60. ...(d as any).value.manifest,
  61. plugin_id: (d as any).value.unique_identifier,
  62. }
  63. }
  64. return undefined
  65. })
  66. return _plugins
  67. })())
  68. const pluginsRef = React.useRef<(Plugin | undefined)[]>(plugins)
  69. const setPlugins = useCallback((p: (Plugin | undefined)[]) => {
  70. doSetPlugins(p)
  71. pluginsRef.current = p
  72. }, [])
  73. const [errorIndexes, setErrorIndexes] = useState<number[]>([])
  74. const handleGitHubPluginFetched = useCallback((index: number) => {
  75. return (p: Plugin) => {
  76. const nextPlugins = produce(pluginsRef.current, (draft) => {
  77. draft[index] = p
  78. })
  79. setPlugins(nextPlugins)
  80. }
  81. }, [setPlugins])
  82. const handleGitHubPluginFetchError = useCallback((index: number) => {
  83. return () => {
  84. setErrorIndexes([...errorIndexes, index])
  85. }
  86. }, [errorIndexes])
  87. const marketPlaceInDSLIndex = useMemo(() => {
  88. const res: number[] = []
  89. allPlugins.forEach((d, index) => {
  90. if (d.type === 'marketplace')
  91. res.push(index)
  92. })
  93. return res
  94. }, [allPlugins])
  95. useEffect(() => {
  96. if (!isFetchingMarketplaceDataById && infoGetById?.data.list) {
  97. const sortedList = allPlugins.filter(d => d.type === 'marketplace').map((d) => {
  98. const p = d as GitHubItemAndMarketPlaceDependency
  99. const id = p.value.marketplace_plugin_unique_identifier?.split(':')[0]
  100. const retPluginInfo = infoGetById.data.list.find(item => item.plugin.plugin_id === id)?.plugin
  101. return { ...retPluginInfo, from: d.type } as Plugin
  102. })
  103. const payloads = sortedList
  104. const failedIndex: number[] = []
  105. const nextPlugins = produce(pluginsRef.current, (draft) => {
  106. marketPlaceInDSLIndex.forEach((index, i) => {
  107. if (payloads[i]) {
  108. draft[index] = {
  109. ...payloads[i],
  110. version: payloads[i]!.version || payloads[i]!.latest_version,
  111. }
  112. }
  113. else { failedIndex.push(index) }
  114. })
  115. })
  116. setPlugins(nextPlugins)
  117. if (failedIndex.length > 0)
  118. setErrorIndexes([...errorIndexes, ...failedIndex])
  119. }
  120. }, [isFetchingMarketplaceDataById])
  121. useEffect(() => {
  122. if (!isFetchingDataByMeta && infoByMeta?.data.list) {
  123. const payloads = infoByMeta?.data.list
  124. const failedIndex: number[] = []
  125. const nextPlugins = produce(pluginsRef.current, (draft) => {
  126. marketPlaceInDSLIndex.forEach((index, i) => {
  127. if (payloads[i]) {
  128. const item = payloads[i]
  129. draft[index] = {
  130. ...item.plugin,
  131. plugin_id: item.version.unique_identifier,
  132. }
  133. }
  134. else {
  135. failedIndex.push(index)
  136. }
  137. })
  138. })
  139. setPlugins(nextPlugins)
  140. if (failedIndex.length > 0)
  141. setErrorIndexes([...errorIndexes, ...failedIndex])
  142. }
  143. }, [isFetchingDataByMeta])
  144. useEffect(() => {
  145. // get info all failed
  146. if (infoByMetaError || infoByIdError)
  147. setErrorIndexes([...errorIndexes, ...marketPlaceInDSLIndex])
  148. }, [infoByMetaError, infoByIdError])
  149. const isLoadedAllData = (plugins.filter(p => !!p).length + errorIndexes.length) === allPlugins.length
  150. const { installedInfo } = useCheckInstalled({
  151. pluginIds: plugins?.filter(p => !!p).map((d) => {
  152. return `${d?.org || d?.author}/${d?.name}`
  153. }) || [],
  154. enabled: isLoadedAllData,
  155. })
  156. const getVersionInfo = useCallback((pluginId: string) => {
  157. const pluginDetail = installedInfo?.[pluginId]
  158. const hasInstalled = !!pluginDetail
  159. return {
  160. hasInstalled,
  161. installedVersion: pluginDetail?.installedVersion,
  162. toInstallVersion: '',
  163. }
  164. }, [installedInfo])
  165. useEffect(() => {
  166. if (isLoadedAllData && installedInfo)
  167. onLoadedAllPlugin(installedInfo!)
  168. }, [isLoadedAllData, installedInfo])
  169. const handleSelect = useCallback((index: number) => {
  170. return () => {
  171. const canSelectPlugins = plugins.filter((p) => {
  172. const { canInstall } = pluginInstallLimit(p!, systemFeatures)
  173. return canInstall
  174. })
  175. onSelect(plugins[index]!, index, canSelectPlugins.length)
  176. }
  177. }, [onSelect, plugins, systemFeatures])
  178. useImperativeHandle(ref, () => ({
  179. selectAllPlugins: () => {
  180. const selectedIndexes: number[] = []
  181. const selectedPlugins: Plugin[] = []
  182. allPlugins.forEach((d, index) => {
  183. const p = plugins[index]
  184. if (!p)
  185. return
  186. const { canInstall } = pluginInstallLimit(p, systemFeatures)
  187. if (canInstall) {
  188. selectedIndexes.push(index)
  189. selectedPlugins.push(p)
  190. }
  191. })
  192. onSelectAll(selectedPlugins, selectedIndexes)
  193. },
  194. deSelectAllPlugins: () => {
  195. onDeSelectAll()
  196. },
  197. }))
  198. return (
  199. <>
  200. {allPlugins.map((d, index) => {
  201. if (errorIndexes.includes(index)) {
  202. return (
  203. <LoadingError key={index} />
  204. )
  205. }
  206. const plugin = plugins[index]
  207. if (d.type === 'github') {
  208. return (<GithubItem
  209. key={index}
  210. checked={!!selectedPlugins.find(p => p.plugin_id === plugins[index]?.plugin_id)}
  211. onCheckedChange={handleSelect(index)}
  212. dependency={d as GitHubItemAndMarketPlaceDependency}
  213. onFetchedPayload={handleGitHubPluginFetched(index)}
  214. onFetchError={handleGitHubPluginFetchError(index)}
  215. versionInfo={getVersionInfo(`${plugin?.org || plugin?.author}/${plugin?.name}`)}
  216. />)
  217. }
  218. if (d.type === 'marketplace') {
  219. return (
  220. <MarketplaceItem
  221. key={index}
  222. checked={!!selectedPlugins.find(p => p.plugin_id === plugins[index]?.plugin_id)}
  223. onCheckedChange={handleSelect(index)}
  224. payload={{ ...plugin, from: d.type } as Plugin}
  225. version={(d as GitHubItemAndMarketPlaceDependency).value.version! || plugin?.version || ''}
  226. versionInfo={getVersionInfo(`${plugin?.org || plugin?.author}/${plugin?.name}`)}
  227. />
  228. )
  229. }
  230. // Local package
  231. return (
  232. <PackageItem
  233. key={index}
  234. checked={!!selectedPlugins.find(p => p.plugin_id === plugins[index]?.plugin_id)}
  235. onCheckedChange={handleSelect(index)}
  236. payload={d as PackageDependency}
  237. isFromMarketPlace={isFromMarketPlace}
  238. versionInfo={getVersionInfo(`${plugin?.org || plugin?.author}/${plugin?.name}`)}
  239. />
  240. )
  241. })
  242. }
  243. </>
  244. )
  245. }
  246. export default React.forwardRef(InstallByDSLList)