'use client' import { useMemo, useRef, useState } from 'react' import { useTranslation } from 'react-i18next' import type { Collection } from './types' import Marketplace from './marketplace' import cn from '@/utils/classnames' import { useTabSearchParams } from '@/hooks/use-tab-searchparams' import TabSliderNew from '@/app/components/base/tab-slider-new' import LabelFilter from '@/app/components/tools/labels/filter' import Input from '@/app/components/base/input' import ProviderDetail from '@/app/components/tools/provider/detail' import Empty from '@/app/components/plugins/marketplace/empty' import CustomCreateCard from '@/app/components/tools/provider/custom-create-card' import WorkflowToolEmpty from '@/app/components/tools/add-tool-modal/empty' import Card from '@/app/components/plugins/card' import CardMoreInfo from '@/app/components/plugins/card/card-more-info' import PluginDetailPanel from '@/app/components/plugins/plugin-detail-panel' import { useSelector as useAppContextSelector } from '@/context/app-context' import { useAllToolProviders } from '@/service/use-tools' import { useInstalledPluginList, useInvalidateInstalledPluginList } from '@/service/use-plugins' const ProviderList = () => { const { t } = useTranslation() const containerRef = useRef(null) const { enable_marketplace } = useAppContextSelector(s => s.systemFeatures) const [activeTab, setActiveTab] = useTabSearchParams({ defaultTab: 'builtin', }) const options = [ { value: 'builtin', text: t('tools.type.builtIn') }, { value: 'api', text: t('tools.type.custom') }, { value: 'workflow', text: t('tools.type.workflow') }, ] const [tagFilterValue, setTagFilterValue] = useState([]) const handleTagsChange = (value: string[]) => { setTagFilterValue(value) } const [keywords, setKeywords] = useState('') const handleKeywordsChange = (value: string) => { setKeywords(value) } const { data: collectionList = [], refetch } = useAllToolProviders() const filteredCollectionList = useMemo(() => { return collectionList.filter((collection) => { if (collection.type !== activeTab) return false if (tagFilterValue.length > 0 && (!collection.labels || collection.labels.every(label => !tagFilterValue.includes(label)))) return false if (keywords) return Object.values(collection.label).some(value => value.toLowerCase().includes(keywords.toLowerCase())) return true }) }, [activeTab, tagFilterValue, keywords, collectionList]) const [currentProvider, setCurrentProvider] = useState() const { data: pluginList } = useInstalledPluginList() const invalidateInstalledPluginList = useInvalidateInstalledPluginList() const currentPluginDetail = useMemo(() => { const detail = pluginList?.plugins.find(plugin => plugin.plugin_id === currentProvider?.plugin_id) return detail }, [currentProvider?.plugin_id, pluginList?.plugins]) return ( <>
{ setActiveTab(state) if (state !== activeTab) setCurrentProvider(undefined) }} options={options} />
handleKeywordsChange(e.target.value)} onClear={() => handleKeywordsChange('')} />
{(filteredCollectionList.length > 0 || activeTab !== 'builtin') && (
{activeTab === 'api' && } {filteredCollectionList.map(collection => (
setCurrentProvider(collection)} > } />
))} {!filteredCollectionList.length && activeTab === 'workflow' &&
}
)} {!filteredCollectionList.length && activeTab === 'builtin' && ( )} { enable_marketplace && activeTab === 'builtin' && ( { containerRef.current?.scrollTo({ top: containerRef.current.scrollHeight, behavior: 'smooth' }) }} searchPluginText={keywords} filterPluginTags={tagFilterValue} /> ) }
{currentProvider && !currentProvider.plugin_id && ( setCurrentProvider(undefined)} onRefreshData={refetch} /> )} invalidateInstalledPluginList()} onHide={() => setCurrentProvider(undefined)} /> ) } ProviderList.displayName = 'ToolProviderList' export default ProviderList