blocks.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import {
  2. memo,
  3. useCallback,
  4. useMemo,
  5. } from 'react'
  6. import { useTranslation } from 'react-i18next'
  7. import { groupBy } from 'lodash-es'
  8. import BlockIcon from '../block-icon'
  9. import { BlockEnum } from '../types'
  10. import {
  11. useIsChatMode,
  12. useNodesExtraData,
  13. } from '../hooks'
  14. import { BLOCK_CLASSIFICATIONS } from './constants'
  15. import { useBlocks } from './hooks'
  16. import type { ToolDefaultValue } from './types'
  17. import Tooltip from '@/app/components/base/tooltip'
  18. type BlocksProps = {
  19. searchText: string
  20. onSelect: (type: BlockEnum, tool?: ToolDefaultValue) => void
  21. availableBlocksTypes?: BlockEnum[]
  22. }
  23. const Blocks = ({
  24. searchText,
  25. onSelect,
  26. availableBlocksTypes = [],
  27. }: BlocksProps) => {
  28. const { t } = useTranslation()
  29. const isChatMode = useIsChatMode()
  30. const nodesExtraData = useNodesExtraData()
  31. const blocks = useBlocks()
  32. const groups = useMemo(() => {
  33. return BLOCK_CLASSIFICATIONS.reduce((acc, classification) => {
  34. const list = groupBy(blocks, 'classification')[classification].filter((block) => {
  35. if (block.type === BlockEnum.Answer && !isChatMode)
  36. return false
  37. return block.title.toLowerCase().includes(searchText.toLowerCase()) && availableBlocksTypes.includes(block.type)
  38. })
  39. return {
  40. ...acc,
  41. [classification]: list,
  42. }
  43. }, {} as Record<string, typeof blocks>)
  44. }, [blocks, isChatMode, searchText, availableBlocksTypes])
  45. const isEmpty = Object.values(groups).every(list => !list.length)
  46. const renderGroup = useCallback((classification: string) => {
  47. const list = groups[classification]
  48. return (
  49. <div
  50. key={classification}
  51. className='mb-1 last-of-type:mb-0'
  52. >
  53. {
  54. classification !== '-' && !!list.length && (
  55. <div className='flex items-start px-3 h-[22px] text-xs font-medium text-gray-500'>
  56. {t(`workflow.tabs.${classification}`)}
  57. </div>
  58. )
  59. }
  60. {
  61. list.map(block => (
  62. <Tooltip
  63. key={block.type}
  64. position='right'
  65. popupClassName='!p-0 !px-3 !py-2.5 !w-[200px] !leading-[18px] !text-xs !text-gray-700 !border-[0.5px] !border-black/5 !rounded-xl !shadow-lg'
  66. popupContent={(
  67. <div>
  68. <BlockIcon
  69. size='md'
  70. className='mb-2'
  71. type={block.type}
  72. />
  73. <div className='mb-1 text-sm leading-5 text-gray-900'>{block.title}</div>
  74. <div className='text-xs text-gray-700 leading-[18px]'>{nodesExtraData[block.type].about}</div>
  75. </div>
  76. )}
  77. >
  78. <div
  79. key={block.type}
  80. className='flex items-center px-3 w-full h-8 rounded-lg hover:bg-gray-50 cursor-pointer'
  81. onClick={() => onSelect(block.type)}
  82. >
  83. <BlockIcon
  84. className='mr-2 shrink-0'
  85. type={block.type}
  86. />
  87. <div className='text-sm text-gray-900'>{block.title}</div>
  88. </div>
  89. </Tooltip>
  90. ))
  91. }
  92. </div>
  93. )
  94. }, [groups, nodesExtraData, onSelect, t])
  95. return (
  96. <div className='p-1'>
  97. {
  98. isEmpty && (
  99. <div className='flex items-center px-3 h-[22px] text-xs font-medium text-gray-500'>{t('workflow.tabs.noResult')}</div>
  100. )
  101. }
  102. {
  103. !isEmpty && BLOCK_CLASSIFICATIONS.map(renderGroup)
  104. }
  105. </div>
  106. )
  107. }
  108. export default memo(Blocks)