2024-04-08 18:51:46 +08:00
|
|
|
import {
|
|
|
|
memo,
|
2024-10-31 11:37:47 +08:00
|
|
|
useMemo,
|
2024-07-09 09:43:34 +08:00
|
|
|
useRef,
|
2024-04-08 18:51:46 +08:00
|
|
|
} from 'react'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
2024-10-12 16:04:16 +08:00
|
|
|
import type { BlockEnum, ToolWithProvider } from '../types'
|
2024-07-09 09:43:34 +08:00
|
|
|
import IndexBar, { groupItems } from './index-bar'
|
2024-04-08 18:51:46 +08:00
|
|
|
import type { ToolDefaultValue } from './types'
|
2024-10-12 18:15:11 +08:00
|
|
|
import { ViewType } from './view-type-select'
|
2024-05-27 21:57:08 +08:00
|
|
|
import Empty from '@/app/components/tools/add-tool-modal/empty'
|
2024-04-08 18:51:46 +08:00
|
|
|
import { useGetLanguage } from '@/context/i18n'
|
2024-10-31 11:37:47 +08:00
|
|
|
import ToolListTreeView from './tool/tool-list-tree-view/list'
|
|
|
|
import ToolListFlatView from './tool/tool-list-flat-view/list'
|
2024-04-08 18:51:46 +08:00
|
|
|
|
|
|
|
type ToolsProps = {
|
2024-05-27 21:57:08 +08:00
|
|
|
showWorkflowEmpty: boolean
|
2024-04-08 18:51:46 +08:00
|
|
|
onSelect: (type: BlockEnum, tool?: ToolDefaultValue) => void
|
2024-05-27 21:57:08 +08:00
|
|
|
tools: ToolWithProvider[]
|
2024-10-12 18:15:11 +08:00
|
|
|
viewType: ViewType
|
2024-04-08 18:51:46 +08:00
|
|
|
}
|
|
|
|
const Blocks = ({
|
2024-05-27 21:57:08 +08:00
|
|
|
showWorkflowEmpty,
|
2024-04-08 18:51:46 +08:00
|
|
|
onSelect,
|
2024-05-27 21:57:08 +08:00
|
|
|
tools,
|
2024-10-12 18:15:11 +08:00
|
|
|
viewType,
|
2024-04-08 18:51:46 +08:00
|
|
|
}: ToolsProps) => {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const language = useGetLanguage()
|
2024-10-31 11:37:47 +08:00
|
|
|
const isFlatView = viewType === ViewType.flat
|
|
|
|
const isShowLetterIndex = isFlatView && tools.length > 10
|
2024-04-08 18:51:46 +08:00
|
|
|
|
2024-10-31 11:37:47 +08:00
|
|
|
/*
|
|
|
|
treeViewToolsData:
|
|
|
|
{
|
|
|
|
A: {
|
|
|
|
'google': [ // plugin organize name
|
|
|
|
...tools
|
|
|
|
],
|
|
|
|
'custom': [ // custom tools
|
|
|
|
...tools
|
|
|
|
],
|
|
|
|
'workflow': [ // workflow as tools
|
|
|
|
...tools
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
const { letters, groups: withLetterAndGroupViewToolsData } = groupItems(tools, tool => (tool as any).label[language][0])
|
|
|
|
const treeViewToolsData = useMemo(() => {
|
|
|
|
const result: Record<string, ToolWithProvider[]> = {}
|
|
|
|
Object.keys(withLetterAndGroupViewToolsData).forEach((letter) => {
|
|
|
|
Object.keys(withLetterAndGroupViewToolsData[letter]).forEach((groupName) => {
|
|
|
|
if (!result[groupName])
|
|
|
|
result[groupName] = []
|
2024-04-08 18:51:46 +08:00
|
|
|
|
2024-10-31 11:37:47 +08:00
|
|
|
result[groupName].push(...withLetterAndGroupViewToolsData[letter][groupName])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return result
|
|
|
|
}, [withLetterAndGroupViewToolsData])
|
2024-04-08 18:51:46 +08:00
|
|
|
|
2024-10-31 15:59:30 +08:00
|
|
|
const listViewToolData = useMemo(() => {
|
|
|
|
const result: ToolWithProvider[] = []
|
|
|
|
Object.keys(withLetterAndGroupViewToolsData).forEach((letter) => {
|
|
|
|
Object.keys(withLetterAndGroupViewToolsData[letter]).forEach((groupName) => {
|
|
|
|
result.push(...withLetterAndGroupViewToolsData[letter][groupName])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return result
|
|
|
|
}, [withLetterAndGroupViewToolsData])
|
|
|
|
|
2024-10-31 11:37:47 +08:00
|
|
|
const toolRefs = useRef({})
|
2024-07-09 09:43:34 +08:00
|
|
|
|
2024-04-08 18:51:46 +08:00
|
|
|
return (
|
2024-10-18 18:18:42 +08:00
|
|
|
<div className='p-1 max-w-[320px]'>
|
2024-04-08 18:51:46 +08:00
|
|
|
{
|
2024-05-27 21:57:08 +08:00
|
|
|
!tools.length && !showWorkflowEmpty && (
|
2024-04-08 18:51:46 +08:00
|
|
|
<div className='flex items-center px-3 h-[22px] text-xs font-medium text-gray-500'>{t('workflow.tabs.noResult')}</div>
|
|
|
|
)
|
|
|
|
}
|
2024-05-27 21:57:08 +08:00
|
|
|
{!tools.length && showWorkflowEmpty && (
|
|
|
|
<div className='py-10'>
|
2024-07-09 09:43:34 +08:00
|
|
|
<Empty />
|
2024-05-27 21:57:08 +08:00
|
|
|
</div>
|
|
|
|
)}
|
2024-10-31 11:37:47 +08:00
|
|
|
{!!tools.length && (
|
|
|
|
isFlatView ? (
|
|
|
|
<ToolListFlatView
|
2024-10-31 15:59:30 +08:00
|
|
|
payload={listViewToolData}
|
2024-10-31 16:10:48 +08:00
|
|
|
isShowLetterIndex={isShowLetterIndex}
|
2024-10-31 15:59:30 +08:00
|
|
|
onSelect={onSelect}
|
2024-10-31 11:37:47 +08:00
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<ToolListTreeView
|
|
|
|
payload={treeViewToolsData}
|
|
|
|
onSelect={onSelect}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
)}
|
|
|
|
|
|
|
|
{isShowLetterIndex && <IndexBar letters={letters} itemRefs={toolRefs} />}
|
2024-04-08 18:51:46 +08:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default memo(Blocks)
|