import { useEffect, useMemo, useRef, useState, } from 'react' import type { OnSelectBlock, ToolWithProvider, } from '../types' import { ToolTypeEnum } from './types' import Tools from './tools' import { useToolTabs } from './hooks' import ViewTypeSelect, { ViewType } from './view-type-select' import cn from '@/utils/classnames' import { useGetLanguage } from '@/context/i18n' import PluginList from '@/app/components/workflow/block-selector/market-place-plugin/list' import ActionButton from '../../base/action-button' import { RiAddLine } from '@remixicon/react' import { PluginType } from '../../plugins/types' import { useMarketplacePlugins } from '../../plugins/marketplace/hooks' type AllToolsProps = { className?: string searchText: string buildInTools: ToolWithProvider[] customTools: ToolWithProvider[] workflowTools: ToolWithProvider[] onSelect: OnSelectBlock supportAddCustomTool?: boolean onAddedCustomTool?: () => void onShowAddCustomCollectionModal?: () => void } const AllTools = ({ className, searchText, onSelect, buildInTools, workflowTools, customTools, supportAddCustomTool, onShowAddCustomCollectionModal, }: AllToolsProps) => { const tabs = useToolTabs() const language = useGetLanguage() const [activeTab, setActiveTab] = useState(ToolTypeEnum.All) const [activeView, setActiveView] = useState(ViewType.flat) const isMatchingKeywords = (text: string, keywords: string) => { return text.toLowerCase().includes(keywords.toLowerCase()) } const tools = useMemo(() => { let mergedTools: ToolWithProvider[] = [] if (activeTab === ToolTypeEnum.All) mergedTools = [...buildInTools, ...customTools, ...workflowTools] if (activeTab === ToolTypeEnum.BuiltIn) mergedTools = buildInTools if (activeTab === ToolTypeEnum.Custom) mergedTools = customTools if (activeTab === ToolTypeEnum.Workflow) mergedTools = workflowTools if (!searchText) return mergedTools.filter(toolWithProvider => toolWithProvider.tools.length > 0) return mergedTools.filter((toolWithProvider) => { return isMatchingKeywords(toolWithProvider.name, searchText) || toolWithProvider.tools.some((tool) => { return Object.values(tool.label).some((label) => { return isMatchingKeywords(label, searchText) }) }) }) }, [activeTab, buildInTools, customTools, workflowTools, searchText, language]) const { queryPluginsWithDebounced: fetchPlugins, plugins: notInstalledPlugins = [], } = useMarketplacePlugins() useEffect(() => { if (searchText) { fetchPlugins({ query: searchText, category: PluginType.tool, }) } // eslint-disable-next-line react-hooks/exhaustive-deps }, [searchText]) const pluginRef = useRef(null) const wrapElemRef = useRef(null) return (
{ tabs.map(tab => (
setActiveTab(tab.key)} > {tab.name}
)) }
{supportAddCustomTool && (
)}
{/* Plugins from marketplace */}
) } export default AllTools