import { memo, useCallback, useMemo, } from 'react' import { useTranslation } from 'react-i18next' import { groupBy } from 'lodash-es' import BlockIcon from '../block-icon' import { BlockEnum } from '../types' import { useIsChatMode, useNodesExtraData, } from '../hooks' import { BLOCK_CLASSIFICATIONS } from './constants' import { useBlocks } from './hooks' import type { ToolDefaultValue } from './types' import Tooltip from '@/app/components/base/tooltip' type BlocksProps = { searchText: string onSelect: (type: BlockEnum, tool?: ToolDefaultValue) => void availableBlocksTypes?: BlockEnum[] } const Blocks = ({ searchText, onSelect, availableBlocksTypes = [], }: BlocksProps) => { const { t } = useTranslation() const isChatMode = useIsChatMode() const nodesExtraData = useNodesExtraData() const blocks = useBlocks() const groups = useMemo(() => { return BLOCK_CLASSIFICATIONS.reduce((acc, classification) => { const list = groupBy(blocks, 'classification')[classification].filter((block) => { if (block.type === BlockEnum.Answer && !isChatMode) return false return block.title.toLowerCase().includes(searchText.toLowerCase()) && availableBlocksTypes.includes(block.type) }) return { ...acc, [classification]: list, } }, {} as Record) }, [blocks, isChatMode, searchText, availableBlocksTypes]) const isEmpty = Object.values(groups).every(list => !list.length) const renderGroup = useCallback((classification: string) => { const list = groups[classification] return (
{ classification !== '-' && !!list.length && (
{t(`workflow.tabs.${classification}`)}
) } { list.map(block => (
{block.title}
{nodesExtraData[block.type].about}
)} >
onSelect(block.type)} >
{block.title}
)) } ) }, [groups, nodesExtraData, onSelect, t]) return (
{ isEmpty && (
{t('workflow.tabs.noResult')}
) } { !isEmpty && BLOCK_CLASSIFICATIONS.map(renderGroup) }
) } export default memo(Blocks)