You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. import Badge from '@/app/components/base/badge'
  19. type BlocksProps = {
  20. searchText: string
  21. onSelect: (type: BlockEnum, tool?: ToolDefaultValue) => void
  22. availableBlocksTypes?: BlockEnum[]
  23. }
  24. const Blocks = ({
  25. searchText,
  26. onSelect,
  27. availableBlocksTypes = [],
  28. }: BlocksProps) => {
  29. const { t } = useTranslation()
  30. const isChatMode = useIsChatMode()
  31. const nodesExtraData = useNodesExtraData()
  32. const blocks = useBlocks()
  33. const groups = useMemo(() => {
  34. return BLOCK_CLASSIFICATIONS.reduce((acc, classification) => {
  35. const list = groupBy(blocks, 'classification')[classification].filter((block) => {
  36. if (block.type === BlockEnum.Answer && !isChatMode)
  37. return false
  38. return block.title.toLowerCase().includes(searchText.toLowerCase()) && availableBlocksTypes.includes(block.type)
  39. })
  40. return {
  41. ...acc,
  42. [classification]: list,
  43. }
  44. }, {} as Record<string, typeof blocks>)
  45. }, [blocks, isChatMode, searchText, availableBlocksTypes])
  46. const isEmpty = Object.values(groups).every(list => !list.length)
  47. const renderGroup = useCallback((classification: string) => {
  48. const list = groups[classification]
  49. return (
  50. <div
  51. key={classification}
  52. className='mb-1 last-of-type:mb-0'
  53. >
  54. {
  55. classification !== '-' && !!list.length && (
  56. <div className='flex h-[22px] items-start px-3 text-xs font-medium text-text-tertiary'>
  57. {t(`workflow.tabs.${classification}`)}
  58. </div>
  59. )
  60. }
  61. {
  62. list.map(block => (
  63. <Tooltip
  64. key={block.type}
  65. position='right'
  66. popupClassName='w-[200px]'
  67. needsDelay={false}
  68. popupContent={(
  69. <div>
  70. <BlockIcon
  71. size='md'
  72. className='mb-2'
  73. type={block.type}
  74. />
  75. <div className='system-md-medium mb-1 text-text-primary'>{block.title}</div>
  76. <div className='system-xs-regular text-text-tertiary'>{nodesExtraData[block.type].about}</div>
  77. </div>
  78. )}
  79. >
  80. <div
  81. key={block.type}
  82. className='flex h-8 w-full cursor-pointer items-center rounded-lg px-3 hover:bg-state-base-hover'
  83. onClick={() => onSelect(block.type)}
  84. >
  85. <BlockIcon
  86. className='mr-2 shrink-0'
  87. type={block.type}
  88. />
  89. <div className='grow text-sm text-text-secondary'>{block.title}</div>
  90. {
  91. block.type === BlockEnum.LoopEnd && (
  92. <Badge
  93. text={t('workflow.nodes.loop.loopNode')}
  94. className='ml-2 shrink-0'
  95. />
  96. )
  97. }
  98. </div>
  99. </Tooltip>
  100. ))
  101. }
  102. </div>
  103. )
  104. }, [groups, nodesExtraData, onSelect, t])
  105. return (
  106. <div className='p-1'>
  107. {
  108. isEmpty && (
  109. <div className='flex h-[22px] items-center px-3 text-xs font-medium text-text-tertiary'>{t('workflow.tabs.noResult')}</div>
  110. )
  111. }
  112. {
  113. !isEmpty && BLOCK_CLASSIFICATIONS.map(renderGroup)
  114. }
  115. </div>
  116. )
  117. }
  118. export default memo(Blocks)