tools.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. position='right'
  44. 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'
  45. popupContent={(
  46. <div>
  47. <BlockIcon
  48. size='md'
  49. className='mb-2'
  50. type={BlockEnum.Tool}
  51. toolIcon={toolWithProvider.icon}
  52. />
  53. <div className='mb-1 text-sm leading-5 text-gray-900'>{tool.label[language]}</div>
  54. <div className='text-xs text-gray-700 leading-[18px]'>{tool.description[language]}</div>
  55. </div>
  56. )}
  57. >
  58. <div
  59. className='flex items-center px-3 w-full h-8 rounded-lg hover:bg-gray-50 cursor-pointer'
  60. onClick={() => onSelect(BlockEnum.Tool, {
  61. provider_id: toolWithProvider.id,
  62. provider_type: toolWithProvider.type,
  63. provider_name: toolWithProvider.name,
  64. tool_name: tool.name,
  65. tool_label: tool.label[language],
  66. title: tool.label[language],
  67. })}
  68. >
  69. <BlockIcon
  70. className='mr-2 shrink-0'
  71. type={BlockEnum.Tool}
  72. toolIcon={toolWithProvider.icon}
  73. />
  74. <div className='text-sm text-gray-900 flex-1 min-w-0 truncate'>{tool.label[language]}</div>
  75. </div>
  76. </Tooltip>
  77. ))
  78. }
  79. </div>
  80. )
  81. }, [onSelect, language])
  82. const renderLetterGroup = (letter) => {
  83. const tools = groupedTools[letter]
  84. return (
  85. <div
  86. key={letter}
  87. ref={el => (toolRefs.current[letter] = el)}
  88. >
  89. {tools.map(renderGroup)}
  90. </div>
  91. )
  92. }
  93. return (
  94. <div className='p-1 max-w-[320px] max-h-[464px] overflow-y-auto'>
  95. {
  96. !tools.length && !showWorkflowEmpty && (
  97. <div className='flex items-center px-3 h-[22px] text-xs font-medium text-gray-500'>{t('workflow.tabs.noResult')}</div>
  98. )
  99. }
  100. {!tools.length && showWorkflowEmpty && (
  101. <div className='py-10'>
  102. <Empty />
  103. </div>
  104. )}
  105. {!!tools.length && letters.map(renderLetterGroup)}
  106. {tools.length > 10 && <IndexBar letters={letters} itemRefs={toolRefs} />}
  107. </div>
  108. )
  109. }
  110. export default memo(Blocks)