install-multi.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. 'use client'
  2. import type { FC } 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 { useFetchPluginsInMarketPlaceByIds, 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. type Props = {
  13. allPlugins: Dependency[]
  14. selectedPlugins: Plugin[]
  15. onSelect: (plugin: Plugin, selectedIndex: number) => void
  16. onLoadedAllPlugin: (installedInfo: Record<string, VersionInfo>) => void
  17. isFromMarketPlace?: boolean
  18. }
  19. const InstallByDSLList: FC<Props> = ({
  20. allPlugins,
  21. selectedPlugins,
  22. onSelect,
  23. onLoadedAllPlugin,
  24. isFromMarketPlace,
  25. }) => {
  26. // DSL has id, to get plugin info to show more info
  27. const { isLoading: isFetchingMarketplaceDataById, data: infoGetById, error: infoByIdError } = useFetchPluginsInMarketPlaceByIds(allPlugins.filter(d => d.type === 'marketplace').map(d => (d as GitHubItemAndMarketPlaceDependency).value.marketplace_plugin_unique_identifier!))
  28. // has meta(org,name,version), to get id
  29. const { isLoading: isFetchingDataByMeta, data: infoByMeta, error: infoByMetaError } = useFetchPluginsInMarketPlaceByInfo(allPlugins.filter(d => d.type === 'marketplace').map(d => (d as GitHubItemAndMarketPlaceDependency).value!))
  30. const [plugins, doSetPlugins] = useState<(Plugin | undefined)[]>((() => {
  31. const hasLocalPackage = allPlugins.some(d => d.type === 'package')
  32. if (!hasLocalPackage)
  33. return []
  34. const _plugins = allPlugins.map((d) => {
  35. if (d.type === 'package') {
  36. return {
  37. ...(d as any).value.manifest,
  38. plugin_id: (d as any).value.unique_identifier,
  39. }
  40. }
  41. return undefined
  42. })
  43. return _plugins
  44. })())
  45. const pluginsRef = React.useRef<(Plugin | undefined)[]>(plugins)
  46. const setPlugins = useCallback((p: (Plugin | undefined)[]) => {
  47. doSetPlugins(p)
  48. pluginsRef.current = p
  49. }, [])
  50. const [errorIndexes, setErrorIndexes] = useState<number[]>([])
  51. const handleGitHubPluginFetched = useCallback((index: number) => {
  52. return (p: Plugin) => {
  53. const nextPlugins = produce(pluginsRef.current, (draft) => {
  54. draft[index] = p
  55. })
  56. setPlugins(nextPlugins)
  57. }
  58. }, [setPlugins])
  59. const handleGitHubPluginFetchError = useCallback((index: number) => {
  60. return () => {
  61. setErrorIndexes([...errorIndexes, index])
  62. }
  63. }, [errorIndexes])
  64. const marketPlaceInDSLIndex = useMemo(() => {
  65. const res: number[] = []
  66. allPlugins.forEach((d, index) => {
  67. if (d.type === 'marketplace')
  68. res.push(index)
  69. })
  70. return res
  71. }, [allPlugins])
  72. useEffect(() => {
  73. if (!isFetchingMarketplaceDataById && infoGetById?.data.plugins) {
  74. const payloads = infoGetById?.data.plugins
  75. const failedIndex: number[] = []
  76. const nextPlugins = produce(pluginsRef.current, (draft) => {
  77. marketPlaceInDSLIndex.forEach((index, i) => {
  78. if (payloads[i]) {
  79. draft[index] = {
  80. ...payloads[i],
  81. version: payloads[i].version || payloads[i].latest_version,
  82. }
  83. }
  84. else { failedIndex.push(index) }
  85. })
  86. })
  87. setPlugins(nextPlugins)
  88. if (failedIndex.length > 0)
  89. setErrorIndexes([...errorIndexes, ...failedIndex])
  90. }
  91. // eslint-disable-next-line react-hooks/exhaustive-deps
  92. }, [isFetchingMarketplaceDataById])
  93. useEffect(() => {
  94. if (!isFetchingDataByMeta && infoByMeta?.data.list) {
  95. const payloads = infoByMeta?.data.list
  96. const failedIndex: number[] = []
  97. const nextPlugins = produce(pluginsRef.current, (draft) => {
  98. marketPlaceInDSLIndex.forEach((index, i) => {
  99. if (payloads[i]) {
  100. const item = payloads[i]
  101. draft[index] = {
  102. ...item.plugin,
  103. plugin_id: item.version.unique_identifier,
  104. }
  105. }
  106. else {
  107. failedIndex.push(index)
  108. }
  109. })
  110. })
  111. setPlugins(nextPlugins)
  112. if (failedIndex.length > 0)
  113. setErrorIndexes([...errorIndexes, ...failedIndex])
  114. }
  115. // eslint-disable-next-line react-hooks/exhaustive-deps
  116. }, [isFetchingDataByMeta])
  117. useEffect(() => {
  118. // get info all failed
  119. if (infoByMetaError || infoByIdError)
  120. setErrorIndexes([...errorIndexes, ...marketPlaceInDSLIndex])
  121. // eslint-disable-next-line react-hooks/exhaustive-deps
  122. }, [infoByMetaError, infoByIdError])
  123. const isLoadedAllData = (plugins.filter(p => !!p).length + errorIndexes.length) === allPlugins.length
  124. const { installedInfo } = useCheckInstalled({
  125. pluginIds: plugins?.filter(p => !!p).map((d) => {
  126. return `${d?.org || d?.author}/${d?.name}`
  127. }) || [],
  128. enabled: isLoadedAllData,
  129. })
  130. const getVersionInfo = useCallback((pluginId: string) => {
  131. const pluginDetail = installedInfo?.[pluginId]
  132. const hasInstalled = !!pluginDetail
  133. return {
  134. hasInstalled,
  135. installedVersion: pluginDetail?.installedVersion,
  136. toInstallVersion: '',
  137. }
  138. }, [installedInfo])
  139. useEffect(() => {
  140. if (isLoadedAllData && installedInfo)
  141. onLoadedAllPlugin(installedInfo!)
  142. // eslint-disable-next-line react-hooks/exhaustive-deps
  143. }, [isLoadedAllData, installedInfo])
  144. const handleSelect = useCallback((index: number) => {
  145. return () => {
  146. onSelect(plugins[index]!, index)
  147. }
  148. }, [onSelect, plugins])
  149. return (
  150. <>
  151. {allPlugins.map((d, index) => {
  152. if (errorIndexes.includes(index)) {
  153. return (
  154. <LoadingError key={index} />
  155. )
  156. }
  157. const plugin = plugins[index]
  158. if (d.type === 'github') {
  159. return (<GithubItem
  160. key={index}
  161. checked={!!selectedPlugins.find(p => p.plugin_id === plugins[index]?.plugin_id)}
  162. onCheckedChange={handleSelect(index)}
  163. dependency={d as GitHubItemAndMarketPlaceDependency}
  164. onFetchedPayload={handleGitHubPluginFetched(index)}
  165. onFetchError={handleGitHubPluginFetchError(index)}
  166. versionInfo={getVersionInfo(`${plugin?.org || plugin?.author}/${plugin?.name}`)}
  167. />)
  168. }
  169. if (d.type === 'marketplace') {
  170. return (
  171. <MarketplaceItem
  172. key={index}
  173. checked={!!selectedPlugins.find(p => p.plugin_id === plugins[index]?.plugin_id)}
  174. onCheckedChange={handleSelect(index)}
  175. payload={plugin}
  176. version={(d as GitHubItemAndMarketPlaceDependency).value.version! || plugin?.version || ''}
  177. versionInfo={getVersionInfo(`${plugin?.org || plugin?.author}/${plugin?.name}`)}
  178. />
  179. )
  180. }
  181. // Local package
  182. return (
  183. <PackageItem
  184. key={index}
  185. checked={!!selectedPlugins.find(p => p.plugin_id === plugins[index]?.plugin_id)}
  186. onCheckedChange={handleSelect(index)}
  187. payload={d as PackageDependency}
  188. isFromMarketPlace={isFromMarketPlace}
  189. versionInfo={getVersionInfo(`${plugin?.org || plugin?.author}/${plugin?.name}`)}
  190. />
  191. )
  192. })
  193. }
  194. </>
  195. )
  196. }
  197. export default React.memo(InstallByDSLList)