tools.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import {
  2. memo,
  3. useMemo,
  4. useRef,
  5. } from 'react'
  6. import { useTranslation } from 'react-i18next'
  7. import type { BlockEnum, ToolWithProvider } from '../types'
  8. import IndexBar, { groupItems } from './index-bar'
  9. import type { ToolDefaultValue, ToolValue } from './types'
  10. import { ViewType } from './view-type-select'
  11. import Empty from '@/app/components/tools/add-tool-modal/empty'
  12. import { useGetLanguage } from '@/context/i18n'
  13. import ToolListTreeView from './tool/tool-list-tree-view/list'
  14. import ToolListFlatView from './tool/tool-list-flat-view/list'
  15. import classNames from '@/utils/classnames'
  16. type ToolsProps = {
  17. showWorkflowEmpty: boolean
  18. onSelect: (type: BlockEnum, tool?: ToolDefaultValue) => void
  19. tools: ToolWithProvider[]
  20. viewType: ViewType
  21. hasSearchText: boolean
  22. className?: string
  23. indexBarClassName?: string
  24. selectedTools?: ToolValue[]
  25. }
  26. const Blocks = ({
  27. showWorkflowEmpty,
  28. onSelect,
  29. tools,
  30. viewType,
  31. hasSearchText,
  32. className,
  33. indexBarClassName,
  34. selectedTools,
  35. }: ToolsProps) => {
  36. const { t } = useTranslation()
  37. const language = useGetLanguage()
  38. const isFlatView = viewType === ViewType.flat
  39. const isShowLetterIndex = isFlatView && tools.length > 10
  40. /*
  41. treeViewToolsData:
  42. {
  43. A: {
  44. 'google': [ // plugin organize name
  45. ...tools
  46. ],
  47. 'custom': [ // custom tools
  48. ...tools
  49. ],
  50. 'workflow': [ // workflow as tools
  51. ...tools
  52. ]
  53. }
  54. }
  55. */
  56. const { letters, groups: withLetterAndGroupViewToolsData } = groupItems(tools, tool => (tool as any).label[language][0])
  57. const treeViewToolsData = useMemo(() => {
  58. const result: Record<string, ToolWithProvider[]> = {}
  59. Object.keys(withLetterAndGroupViewToolsData).forEach((letter) => {
  60. Object.keys(withLetterAndGroupViewToolsData[letter]).forEach((groupName) => {
  61. if (!result[groupName])
  62. result[groupName] = []
  63. result[groupName].push(...withLetterAndGroupViewToolsData[letter][groupName])
  64. })
  65. })
  66. return result
  67. }, [withLetterAndGroupViewToolsData])
  68. const listViewToolData = useMemo(() => {
  69. const result: ToolWithProvider[] = []
  70. letters.forEach((letter) => {
  71. Object.keys(withLetterAndGroupViewToolsData[letter]).forEach((groupName) => {
  72. result.push(...withLetterAndGroupViewToolsData[letter][groupName].map((item) => {
  73. return {
  74. ...item,
  75. letter,
  76. }
  77. }))
  78. })
  79. })
  80. return result
  81. }, [withLetterAndGroupViewToolsData, letters])
  82. const toolRefs = useRef({})
  83. return (
  84. <div className={classNames('p-1 max-w-[320px]', className)}>
  85. {
  86. !tools.length && !showWorkflowEmpty && (
  87. <div className='flex h-[22px] items-center px-3 text-xs font-medium text-text-tertiary'>{t('workflow.tabs.noResult')}</div>
  88. )
  89. }
  90. {!tools.length && showWorkflowEmpty && (
  91. <div className='py-10'>
  92. <Empty />
  93. </div>
  94. )}
  95. {!!tools.length && (
  96. isFlatView ? (
  97. <ToolListFlatView
  98. toolRefs={toolRefs}
  99. letters={letters}
  100. payload={listViewToolData}
  101. isShowLetterIndex={isShowLetterIndex}
  102. hasSearchText={hasSearchText}
  103. onSelect={onSelect}
  104. selectedTools={selectedTools}
  105. />
  106. ) : (
  107. <ToolListTreeView
  108. payload={treeViewToolsData}
  109. hasSearchText={hasSearchText}
  110. onSelect={onSelect}
  111. selectedTools={selectedTools}
  112. />
  113. )
  114. )}
  115. {isShowLetterIndex && <IndexBar letters={letters} itemRefs={toolRefs} className={indexBarClassName} />}
  116. </div>
  117. )
  118. }
  119. export default memo(Blocks)