tools.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import {
  2. memo,
  3. useCallback,
  4. useRef,
  5. } from 'react'
  6. import { useTranslation } from 'react-i18next'
  7. import BlockIcon from '../block-icon'
  8. import { BlockEnum } from '../types'
  9. import type { ToolWithProvider } from '../types'
  10. import IndexBar, { groupItems } from './index-bar'
  11. import type { ToolDefaultValue } from './types'
  12. import Tooltip from '@/app/components/base/tooltip'
  13. import Empty from '@/app/components/tools/add-tool-modal/empty'
  14. import { useGetLanguage } from '@/context/i18n'
  15. type ToolsProps = {
  16. showWorkflowEmpty: boolean
  17. onSelect: (type: BlockEnum, tool?: ToolDefaultValue) => void
  18. tools: ToolWithProvider[]
  19. }
  20. const Blocks = ({
  21. showWorkflowEmpty,
  22. onSelect,
  23. tools,
  24. }: ToolsProps) => {
  25. const { t } = useTranslation()
  26. const language = useGetLanguage()
  27. const { letters, groups: groupedTools } = groupItems(tools, tool => tool.label[language][0])
  28. const toolRefs = useRef({})
  29. const renderGroup = useCallback((toolWithProvider: ToolWithProvider) => {
  30. const list = toolWithProvider.tools
  31. return (
  32. <div
  33. key={toolWithProvider.id}
  34. className='mb-1 last-of-type:mb-0'
  35. >
  36. <div className='flex items-start px-3 h-[22px] text-xs font-medium text-gray-500'>
  37. {toolWithProvider.label[language]}
  38. </div>
  39. {
  40. list.map(tool => (
  41. <Tooltip
  42. key={tool.name}
  43. selector={`workflow-block-tool-${tool.name}`}
  44. position='right'
  45. className='!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'
  46. htmlContent={(
  47. <div>
  48. <BlockIcon
  49. size='md'
  50. className='mb-2'
  51. type={BlockEnum.Tool}
  52. toolIcon={toolWithProvider.icon}
  53. />
  54. <div className='mb-1 text-sm leading-5 text-gray-900'>{tool.label[language]}</div>
  55. <div className='text-xs text-gray-700 leading-[18px]'>{tool.description[language]}</div>
  56. </div>
  57. )}
  58. noArrow
  59. >
  60. <div
  61. className='flex items-center px-3 w-full h-8 rounded-lg hover:bg-gray-50 cursor-pointer'
  62. onClick={() => onSelect(BlockEnum.Tool, {
  63. provider_id: toolWithProvider.id,
  64. provider_type: toolWithProvider.type,
  65. provider_name: toolWithProvider.name,
  66. tool_name: tool.name,
  67. tool_label: tool.label[language],
  68. title: tool.label[language],
  69. })}
  70. >
  71. <BlockIcon
  72. className='mr-2 shrink-0'
  73. type={BlockEnum.Tool}
  74. toolIcon={toolWithProvider.icon}
  75. />
  76. <div className='text-sm text-gray-900 truncate'>{tool.label[language]}</div>
  77. </div>
  78. </Tooltip>
  79. ))
  80. }
  81. </div>
  82. )
  83. }, [onSelect, language])
  84. const renderLetterGroup = (letter) => {
  85. const tools = groupedTools[letter]
  86. return (
  87. <div
  88. key={letter}
  89. ref={el => (toolRefs.current[letter] = el)}
  90. >
  91. {tools.map(renderGroup)}
  92. </div>
  93. )
  94. }
  95. return (
  96. <div className='p-1 max-w-[320px] max-h-[464px] overflow-y-auto'>
  97. {
  98. !tools.length && !showWorkflowEmpty && (
  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. {!tools.length && showWorkflowEmpty && (
  103. <div className='py-10'>
  104. <Empty />
  105. </div>
  106. )}
  107. {!!tools.length && letters.map(renderLetterGroup)}
  108. {tools.length > 10 && <IndexBar letters={letters} itemRefs={toolRefs} />}
  109. </div>
  110. )
  111. }
  112. export default memo(Blocks)