tools.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import {
  2. memo,
  3. useCallback,
  4. } from 'react'
  5. import { useTranslation } from 'react-i18next'
  6. import {
  7. RiAddLine,
  8. } from '@remixicon/react'
  9. import cn from '@/utils/classnames'
  10. import { ArrowUpRight } from '@/app/components/base/icons/src/vender/line/arrows'
  11. import { Check } from '@/app/components/base/icons/src/vender/line/general'
  12. import { Tag01 } from '@/app/components/base/icons/src/vender/line/financeAndECommerce'
  13. import type { ToolWithProvider } from '@/app/components/workflow/types'
  14. import { BlockEnum } from '@/app/components/workflow/types'
  15. import BlockIcon from '@/app/components/workflow/block-icon'
  16. import Tooltip from '@/app/components/base/tooltip'
  17. import Button from '@/app/components/base/button'
  18. import { useGetLanguage } from '@/context/i18n'
  19. import { useStore as useLabelStore } from '@/app/components/tools/labels/store'
  20. import Empty from '@/app/components/tools/add-tool-modal/empty'
  21. import type { Tool } from '@/app/components/tools/types'
  22. import { CollectionType } from '@/app/components/tools/types'
  23. import type { AgentTool } from '@/types/app'
  24. import { MAX_TOOLS_NUM } from '@/config'
  25. type ToolsProps = {
  26. showWorkflowEmpty: boolean
  27. tools: ToolWithProvider[]
  28. addedTools: AgentTool[]
  29. onSelect: (provider: ToolWithProvider, tool: Tool) => void
  30. onAuthSetup: (provider: ToolWithProvider) => void
  31. }
  32. const Blocks = ({
  33. showWorkflowEmpty,
  34. tools,
  35. addedTools,
  36. onSelect,
  37. onAuthSetup,
  38. }: ToolsProps) => {
  39. const { t } = useTranslation()
  40. const language = useGetLanguage()
  41. const labelList = useLabelStore(s => s.labelList)
  42. const addable = addedTools.length < MAX_TOOLS_NUM
  43. const renderGroup = useCallback((toolWithProvider: ToolWithProvider) => {
  44. const list = toolWithProvider.tools
  45. const needAuth = toolWithProvider.allow_delete && !toolWithProvider.is_team_authorization && toolWithProvider.type === CollectionType.builtIn
  46. return (
  47. <div
  48. key={toolWithProvider.id}
  49. className='group mb-1 last-of-type:mb-0'
  50. >
  51. <div className='flex items-center justify-between w-full pl-3 pr-1 h-[22px] text-xs font-medium text-gray-500'>
  52. {toolWithProvider.label[language]}
  53. <a className='hidden cursor-pointer items-center group-hover:flex' href={`/tools?category=${toolWithProvider.type}`} target='_blank'>{t('tools.addToolModal.manageInTools')}<ArrowUpRight className='ml-0.5 w-3 h-3' /></a>
  54. </div>
  55. {list.map((tool) => {
  56. const labelContent = (() => {
  57. if (!tool.labels)
  58. return ''
  59. return tool.labels.map((name) => {
  60. const label = labelList.find(item => item.name === name)
  61. return label?.label[language]
  62. }).filter(Boolean).join(', ')
  63. })()
  64. const added = !!addedTools?.find(v => v.provider_id === toolWithProvider.id && v.provider_type === toolWithProvider.type && v.tool_name === tool.name)
  65. return (
  66. <Tooltip
  67. key={tool.name}
  68. position='bottom'
  69. popupClassName='!p-0 !px-3 !py-2.5 !w-[210px] !leading-[18px] !text-xs !text-gray-700 !border-[0.5px] !border-black/5 !bg-transparent !rounded-xl !shadow-lg translate-x-[108px]'
  70. popupContent={(
  71. <div>
  72. <BlockIcon
  73. size='md'
  74. className='mb-2'
  75. type={BlockEnum.Tool}
  76. toolIcon={toolWithProvider.icon}
  77. />
  78. <div className='mb-1 text-sm leading-5 text-gray-900'>{tool.label[language]}</div>
  79. <div className='text-xs text-gray-700 leading-[18px]'>{tool.description[language]}</div>
  80. {tool.labels?.length > 0 && (
  81. <div className='flex items-center shrink-0 mt-1'>
  82. <div className='relative w-full flex items-center gap-1 py-1 rounded-md text-gray-500' title={labelContent}>
  83. <Tag01 className='shrink-0 w-3 h-3 text-gray-500' />
  84. <div className='grow text-xs text-start leading-[18px] font-normal truncate'>{labelContent}</div>
  85. </div>
  86. </div>
  87. )}
  88. </div>
  89. )}
  90. >
  91. <div className='group/item flex items-center w-full pl-3 pr-1 h-8 rounded-lg hover:bg-gray-50 cursor-pointer'>
  92. <BlockIcon
  93. className={cn('mr-2 shrink-0', needAuth && 'opacity-30')}
  94. type={BlockEnum.Tool}
  95. toolIcon={toolWithProvider.icon}
  96. />
  97. <div className={cn('grow text-sm text-gray-900 truncate', needAuth && 'opacity-30')}>{tool.label[language]}</div>
  98. {!needAuth && added && (
  99. <div className='flex items-center gap-1 rounded-[6px] border border-gray-100 px-2 py-[3px] bg-white text-gray-300 text-xs font-medium leading-[18px]'>
  100. <Check className='w-3 h-3' />
  101. {t('tools.addToolModal.added').toLocaleUpperCase()}
  102. </div>
  103. )}
  104. {!needAuth && !added && addable && (
  105. <Button
  106. variant='secondary-accent'
  107. size='small'
  108. className={cn('hidden shrink-0 items-center group-hover/item:flex')}
  109. onClick={() => onSelect(toolWithProvider, tool)}
  110. >
  111. <RiAddLine className='w-3 h-3' />
  112. {t('tools.addToolModal.add').toLocaleUpperCase()}
  113. </Button>
  114. )}
  115. {needAuth && (
  116. <Button
  117. variant='secondary-accent'
  118. size='small'
  119. className={cn('hidden shrink-0 group-hover/item:flex')}
  120. onClick={() => onAuthSetup(toolWithProvider)}
  121. >{t('tools.auth.setup')}</Button>
  122. )}
  123. </div>
  124. </Tooltip>
  125. )
  126. })}
  127. </div>
  128. )
  129. }, [addable, language, t, labelList, addedTools, onAuthSetup, onSelect])
  130. return (
  131. <div className='p-1 pb-6 max-w-[440px]'>
  132. {!tools.length && !showWorkflowEmpty && (
  133. <div className='flex items-center px-3 h-[22px] text-xs font-medium text-gray-500'>{t('workflow.tabs.noResult')}</div>
  134. )}
  135. {!tools.length && showWorkflowEmpty && (
  136. <div className='pt-[280px]'>
  137. <Empty />
  138. </div>
  139. )}
  140. {!!tools.length && tools.map(renderGroup)}
  141. </div>
  142. )
  143. }
  144. export default memo(Blocks)