provider-list.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 { useSelector as useAppContextSelector } from '@/context/app-context'
  19. import { useAllToolProviders } from '@/service/use-tools'
  20. import { useInstalledPluginList, useInvalidateInstalledPluginList } from '@/service/use-plugins'
  21. const ProviderList = () => {
  22. const { t } = useTranslation()
  23. const containerRef = useRef<HTMLDivElement>(null)
  24. const { enable_marketplace } = useAppContextSelector(s => s.systemFeatures)
  25. const [activeTab, setActiveTab] = useTabSearchParams({
  26. defaultTab: 'builtin',
  27. })
  28. const options = [
  29. { value: 'builtin', text: t('tools.type.builtIn') },
  30. { value: 'api', text: t('tools.type.custom') },
  31. { value: 'workflow', text: t('tools.type.workflow') },
  32. ]
  33. const [tagFilterValue, setTagFilterValue] = useState<string[]>([])
  34. const handleTagsChange = (value: string[]) => {
  35. setTagFilterValue(value)
  36. }
  37. const [keywords, setKeywords] = useState<string>('')
  38. const handleKeywordsChange = (value: string) => {
  39. setKeywords(value)
  40. }
  41. const { data: collectionList = [], refetch } = useAllToolProviders()
  42. const filteredCollectionList = useMemo(() => {
  43. return collectionList.filter((collection) => {
  44. if (collection.type !== activeTab)
  45. return false
  46. if (tagFilterValue.length > 0 && (!collection.labels || collection.labels.every(label => !tagFilterValue.includes(label))))
  47. return false
  48. if (keywords)
  49. return Object.values(collection.label).some(value => value.toLowerCase().includes(keywords.toLowerCase()))
  50. return true
  51. })
  52. }, [activeTab, tagFilterValue, keywords, collectionList])
  53. const [currentProvider, setCurrentProvider] = useState<Collection | undefined>()
  54. const { data: pluginList } = useInstalledPluginList()
  55. const invalidateInstalledPluginList = useInvalidateInstalledPluginList()
  56. const currentPluginDetail = useMemo(() => {
  57. const detail = pluginList?.plugins.find(plugin => plugin.plugin_id === currentProvider?.plugin_id)
  58. return detail
  59. }, [currentProvider?.plugin_id, pluginList?.plugins])
  60. return (
  61. <>
  62. <div className='relative flex overflow-hidden shrink-0 h-0 grow'>
  63. <div
  64. ref={containerRef}
  65. className='relative flex flex-col overflow-y-auto bg-background-body grow'
  66. >
  67. <div className={cn(
  68. 'sticky top-0 flex justify-between items-center pt-4 px-12 pb-2 leading-[56px] bg-background-body z-20 flex-wrap gap-y-2',
  69. currentProvider && 'pr-6',
  70. )}>
  71. <TabSliderNew
  72. value={activeTab}
  73. onChange={(state) => {
  74. setActiveTab(state)
  75. if (state !== activeTab)
  76. setCurrentProvider(undefined)
  77. }}
  78. options={options}
  79. />
  80. <div className='flex items-center gap-2'>
  81. <LabelFilter value={tagFilterValue} onChange={handleTagsChange} />
  82. <Input
  83. showLeftIcon
  84. showClearIcon
  85. wrapperClassName='w-[200px]'
  86. value={keywords}
  87. onChange={e => handleKeywordsChange(e.target.value)}
  88. onClear={() => handleKeywordsChange('')}
  89. />
  90. </div>
  91. </div>
  92. {(filteredCollectionList.length > 0 || activeTab !== 'builtin') && (
  93. <div className={cn(
  94. 'relative grid content-start grid-cols-1 gap-4 px-12 pt-2 pb-4 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 shrink-0',
  95. !filteredCollectionList.length && activeTab === 'workflow' && 'grow',
  96. )}>
  97. {activeTab === 'api' && <CustomCreateCard onRefreshData={refetch} />}
  98. {filteredCollectionList.map(collection => (
  99. <div
  100. key={collection.id}
  101. onClick={() => setCurrentProvider(collection)}
  102. >
  103. <Card
  104. className={cn(
  105. 'border-[1.5px] border-transparent cursor-pointer',
  106. currentProvider?.id === collection.id && 'border-components-option-card-option-selected-border',
  107. )}
  108. hideCornerMark
  109. payload={{
  110. ...collection,
  111. brief: collection.description,
  112. org: collection.plugin_id ? collection.plugin_id.split('/')[0] : '',
  113. name: collection.plugin_id ? collection.plugin_id.split('/')[1] : collection.name,
  114. } as any}
  115. footer={
  116. <CardMoreInfo
  117. tags={collection.labels}
  118. />
  119. }
  120. />
  121. </div>
  122. ))}
  123. {!filteredCollectionList.length && activeTab === 'workflow' && <div className='absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2'><WorkflowToolEmpty /></div>}
  124. </div>
  125. )}
  126. {!filteredCollectionList.length && activeTab === 'builtin' && (
  127. <Empty lightCard text={t('tools.noTools')} className='px-12 h-[224px]' />
  128. )}
  129. {
  130. enable_marketplace && activeTab === 'builtin' && (
  131. <Marketplace
  132. onMarketplaceScroll={() => {
  133. containerRef.current?.scrollTo({ top: containerRef.current.scrollHeight, behavior: 'smooth' })
  134. }}
  135. searchPluginText={keywords}
  136. filterPluginTags={tagFilterValue}
  137. />
  138. )
  139. }
  140. </div>
  141. </div>
  142. {currentProvider && !currentProvider.plugin_id && (
  143. <ProviderDetail
  144. collection={currentProvider}
  145. onHide={() => setCurrentProvider(undefined)}
  146. onRefreshData={refetch}
  147. />
  148. )}
  149. <PluginDetailPanel
  150. detail={currentPluginDetail}
  151. onUpdate={() => invalidateInstalledPluginList()}
  152. onHide={() => setCurrentProvider(undefined)}
  153. />
  154. </>
  155. )
  156. }
  157. ProviderList.displayName = 'ToolProviderList'
  158. export default ProviderList