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.

provider-list.tsx 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. 'use client'
  2. import { useMemo, useRef, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import type { Collection } from './types'
  5. import Marketplace from './marketplace'
  6. import cn from '@/utils/classnames'
  7. import { useTabSearchParams } from '@/hooks/use-tab-searchparams'
  8. import TabSliderNew from '@/app/components/base/tab-slider-new'
  9. import LabelFilter from '@/app/components/tools/labels/filter'
  10. import Input from '@/app/components/base/input'
  11. import ProviderDetail from '@/app/components/tools/provider/detail'
  12. import Empty from '@/app/components/plugins/marketplace/empty'
  13. import CustomCreateCard from '@/app/components/tools/provider/custom-create-card'
  14. import WorkflowToolEmpty from '@/app/components/tools/add-tool-modal/empty'
  15. import Card from '@/app/components/plugins/card'
  16. import CardMoreInfo from '@/app/components/plugins/card/card-more-info'
  17. import PluginDetailPanel from '@/app/components/plugins/plugin-detail-panel'
  18. import MCPList from './mcp'
  19. import { useAllToolProviders } from '@/service/use-tools'
  20. import { useInstalledPluginList, useInvalidateInstalledPluginList } from '@/service/use-plugins'
  21. import { useGlobalPublicStore } from '@/context/global-public-context'
  22. import { ToolTypeEnum } from '../workflow/block-selector/types'
  23. const getToolType = (type: string) => {
  24. switch (type) {
  25. case 'builtin':
  26. return ToolTypeEnum.BuiltIn
  27. case 'api':
  28. return ToolTypeEnum.Custom
  29. case 'workflow':
  30. return ToolTypeEnum.Workflow
  31. case 'mcp':
  32. return ToolTypeEnum.MCP
  33. default:
  34. return ToolTypeEnum.BuiltIn
  35. }
  36. }
  37. const ProviderList = () => {
  38. // const searchParams = useSearchParams()
  39. // searchParams.get('category') === 'workflow'
  40. const { t } = useTranslation()
  41. const { enable_marketplace } = useGlobalPublicStore(s => s.systemFeatures)
  42. const containerRef = useRef<HTMLDivElement>(null)
  43. const [activeTab, setActiveTab] = useTabSearchParams({
  44. defaultTab: 'builtin',
  45. })
  46. const options = [
  47. { value: 'builtin', text: t('tools.type.builtIn') },
  48. { value: 'api', text: t('tools.type.custom') },
  49. { value: 'workflow', text: t('tools.type.workflow') },
  50. { value: 'mcp', text: 'MCP' },
  51. ]
  52. const [tagFilterValue, setTagFilterValue] = useState<string[]>([])
  53. const handleTagsChange = (value: string[]) => {
  54. setTagFilterValue(value)
  55. }
  56. const [keywords, setKeywords] = useState<string>('')
  57. const handleKeywordsChange = (value: string) => {
  58. setKeywords(value)
  59. }
  60. const { data: collectionList = [], refetch } = useAllToolProviders()
  61. const filteredCollectionList = useMemo(() => {
  62. return collectionList.filter((collection) => {
  63. if (collection.type !== activeTab)
  64. return false
  65. if (tagFilterValue.length > 0 && (!collection.labels || collection.labels.every(label => !tagFilterValue.includes(label))))
  66. return false
  67. if (keywords)
  68. return Object.values(collection.label).some(value => value.toLowerCase().includes(keywords.toLowerCase()))
  69. return true
  70. })
  71. }, [activeTab, tagFilterValue, keywords, collectionList])
  72. const [currentProviderId, setCurrentProviderId] = useState<string | undefined>()
  73. const currentProvider = useMemo<Collection | undefined>(() => {
  74. return filteredCollectionList.find(collection => collection.id === currentProviderId)
  75. }, [currentProviderId, filteredCollectionList])
  76. const { data: pluginList } = useInstalledPluginList()
  77. const invalidateInstalledPluginList = useInvalidateInstalledPluginList()
  78. const currentPluginDetail = useMemo(() => {
  79. const detail = pluginList?.plugins.find(plugin => plugin.plugin_id === currentProvider?.plugin_id)
  80. return detail
  81. }, [currentProvider?.plugin_id, pluginList?.plugins])
  82. return (
  83. <>
  84. <div className='relative flex h-0 shrink-0 grow overflow-hidden'>
  85. <div
  86. ref={containerRef}
  87. className='relative flex grow flex-col overflow-y-auto bg-background-body'
  88. >
  89. <div className={cn(
  90. 'sticky top-0 z-10 flex flex-wrap items-center justify-between gap-y-2 bg-background-body px-12 pb-2 pt-4 leading-[56px]',
  91. currentProviderId && 'pr-6',
  92. )}>
  93. <TabSliderNew
  94. value={activeTab}
  95. onChange={(state) => {
  96. setActiveTab(state)
  97. if (state !== activeTab)
  98. setCurrentProviderId(undefined)
  99. }}
  100. options={options}
  101. />
  102. <div className='flex items-center gap-2'>
  103. {activeTab !== 'mcp' && (
  104. <LabelFilter value={tagFilterValue} onChange={handleTagsChange} />
  105. )}
  106. <Input
  107. showLeftIcon
  108. showClearIcon
  109. wrapperClassName='w-[200px]'
  110. value={keywords}
  111. onChange={e => handleKeywordsChange(e.target.value)}
  112. onClear={() => handleKeywordsChange('')}
  113. />
  114. </div>
  115. </div>
  116. {activeTab !== 'mcp' && (
  117. <div className={cn(
  118. 'relative grid shrink-0 grid-cols-1 content-start gap-4 px-12 pb-4 pt-2 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4',
  119. !filteredCollectionList.length && activeTab === 'workflow' && 'grow',
  120. )}>
  121. {activeTab === 'api' && <CustomCreateCard onRefreshData={refetch} />}
  122. {filteredCollectionList.map(collection => (
  123. <div
  124. key={collection.id}
  125. onClick={() => setCurrentProviderId(collection.id)}
  126. >
  127. <Card
  128. className={cn(
  129. 'cursor-pointer border-[1.5px] border-transparent',
  130. currentProviderId === collection.id && 'border-components-option-card-option-selected-border',
  131. )}
  132. hideCornerMark
  133. payload={{
  134. ...collection,
  135. brief: collection.description,
  136. org: collection.plugin_id ? collection.plugin_id.split('/')[0] : '',
  137. name: collection.plugin_id ? collection.plugin_id.split('/')[1] : collection.name,
  138. } as any}
  139. footer={
  140. <CardMoreInfo
  141. tags={collection.labels}
  142. />
  143. }
  144. />
  145. </div>
  146. ))}
  147. {!filteredCollectionList.length && activeTab === 'workflow' && <div className='absolute left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2'><WorkflowToolEmpty type={getToolType(activeTab)} /></div>}
  148. </div>
  149. )}
  150. {!filteredCollectionList.length && activeTab === 'builtin' && (
  151. <Empty lightCard text={t('tools.noTools')} className='h-[224px] px-12' />
  152. )}
  153. {enable_marketplace && activeTab === 'builtin' && (
  154. <Marketplace
  155. onMarketplaceScroll={() => {
  156. containerRef.current?.scrollTo({ top: containerRef.current.scrollHeight, behavior: 'smooth' })
  157. }}
  158. searchPluginText={keywords}
  159. filterPluginTags={tagFilterValue}
  160. />
  161. )}
  162. {activeTab === 'mcp' && (
  163. <MCPList searchText={keywords} />
  164. )}
  165. </div>
  166. </div>
  167. {currentProvider && !currentProvider.plugin_id && (
  168. <ProviderDetail
  169. collection={currentProvider}
  170. onHide={() => setCurrentProviderId(undefined)}
  171. onRefreshData={refetch}
  172. />
  173. )}
  174. <PluginDetailPanel
  175. detail={currentPluginDetail}
  176. onUpdate={() => invalidateInstalledPluginList()}
  177. onHide={() => setCurrentProviderId(undefined)}
  178. />
  179. </>
  180. )
  181. }
  182. ProviderList.displayName = 'ToolProviderList'
  183. export default ProviderList