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.1KB

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