import { memo, useCallback, useRef, } from 'react' import { useTranslation } from 'react-i18next' import BlockIcon from '../block-icon' import { BlockEnum } from '../types' import type { ToolWithProvider } from '../types' import IndexBar, { groupItems } from './index-bar' import type { ToolDefaultValue } from './types' import Tooltip from '@/app/components/base/tooltip' import Empty from '@/app/components/tools/add-tool-modal/empty' import { useGetLanguage } from '@/context/i18n' type ToolsProps = { showWorkflowEmpty: boolean onSelect: (type: BlockEnum, tool?: ToolDefaultValue) => void tools: ToolWithProvider[] } const Blocks = ({ showWorkflowEmpty, onSelect, tools, }: ToolsProps) => { const { t } = useTranslation() const language = useGetLanguage() const { letters, groups: groupedTools } = groupItems(tools, tool => tool.label[language][0]) const toolRefs = useRef({}) const renderGroup = useCallback((toolWithProvider: ToolWithProvider) => { const list = toolWithProvider.tools return (
{toolWithProvider.label[language]}
{ list.map(tool => (
{tool.label[language]}
{tool.description[language]}
)} >
onSelect(BlockEnum.Tool, { provider_id: toolWithProvider.id, provider_type: toolWithProvider.type, provider_name: toolWithProvider.name, tool_name: tool.name, tool_label: tool.label[language], title: tool.label[language], })} >
{tool.label[language]}
)) } ) }, [onSelect, language]) const renderLetterGroup = (letter) => { const tools = groupedTools[letter] return (
(toolRefs.current[letter] = el)} > {tools.map(renderGroup)}
) } return (
{ !tools.length && !showWorkflowEmpty && (
{t('workflow.tabs.noResult')}
) } {!tools.length && showWorkflowEmpty && (
)} {!!tools.length && letters.map(renderLetterGroup)} {tools.length > 10 && }
) } export default memo(Blocks)