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

install-multi.tsx 9.1KB

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