tools.tsx 3.3 KB

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