| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   memo, | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +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' | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | import type { BlockEnum, ToolWithProvider } from '../types' | 
					
						
							| 
									
										
										
										
											2024-07-09 09:43:34 +08:00
										 |  |  | import IndexBar, { groupItems } from './index-bar' | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | import type { ToolDefaultValue, ToolValue } from './types' | 
					
						
							|  |  |  | 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' | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | import ToolListTreeView from './tool/tool-list-tree-view/list' | 
					
						
							|  |  |  | import ToolListFlatView from './tool/tool-list-flat-view/list' | 
					
						
							|  |  |  | import classNames from '@/utils/classnames' | 
					
						
							| 
									
										
										
										
											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[] | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |   viewType: ViewType | 
					
						
							|  |  |  |   hasSearchText: boolean | 
					
						
							|  |  |  |   className?: string | 
					
						
							|  |  |  |   indexBarClassName?: string | 
					
						
							|  |  |  |   selectedTools?: ToolValue[] | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |   viewType, | 
					
						
							|  |  |  |   hasSearchText, | 
					
						
							|  |  |  |   className, | 
					
						
							|  |  |  |   indexBarClassName, | 
					
						
							|  |  |  |   selectedTools, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | }: ToolsProps) => { | 
					
						
							|  |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  |   const language = useGetLanguage() | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |   const isFlatView = viewType === ViewType.flat | 
					
						
							|  |  |  |   const isShowLetterIndex = isFlatView && tools.length > 10 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |   /* | 
					
						
							|  |  |  |   treeViewToolsData: | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     A: { | 
					
						
							|  |  |  |       'google': [ // plugin organize name
 | 
					
						
							|  |  |  |         ...tools | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |       'custom': [ // custom tools
 | 
					
						
							|  |  |  |         ...tools | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |       'workflow': [ // workflow as tools
 | 
					
						
							|  |  |  |         ...tools | 
					
						
							|  |  |  |       ] | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   */ | 
					
						
							| 
									
										
										
										
											2025-04-14 16:06:10 +08:00
										 |  |  |   const { letters, groups: withLetterAndGroupViewToolsData } = groupItems(tools, tool => tool.label[language][0]) | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |   const treeViewToolsData = useMemo(() => { | 
					
						
							|  |  |  |     const result: Record<string, ToolWithProvider[]> = {} | 
					
						
							|  |  |  |     Object.keys(withLetterAndGroupViewToolsData).forEach((letter) => { | 
					
						
							|  |  |  |       Object.keys(withLetterAndGroupViewToolsData[letter]).forEach((groupName) => { | 
					
						
							|  |  |  |         if (!result[groupName]) | 
					
						
							|  |  |  |           result[groupName] = [] | 
					
						
							|  |  |  |         result[groupName].push(...withLetterAndGroupViewToolsData[letter][groupName]) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     return result | 
					
						
							|  |  |  |   }, [withLetterAndGroupViewToolsData]) | 
					
						
							| 
									
										
										
										
											2024-07-09 09:43:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |   const listViewToolData = useMemo(() => { | 
					
						
							|  |  |  |     const result: ToolWithProvider[] = [] | 
					
						
							|  |  |  |     letters.forEach((letter) => { | 
					
						
							|  |  |  |       Object.keys(withLetterAndGroupViewToolsData[letter]).forEach((groupName) => { | 
					
						
							|  |  |  |         result.push(...withLetterAndGroupViewToolsData[letter][groupName].map((item) => { | 
					
						
							|  |  |  |           return { | 
					
						
							|  |  |  |             ...item, | 
					
						
							|  |  |  |             letter, | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         })) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     return result | 
					
						
							|  |  |  |   }, [withLetterAndGroupViewToolsData, letters]) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |   const toolRefs = useRef({}) | 
					
						
							| 
									
										
										
										
											2024-07-09 09:43:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   return ( | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     <div className={classNames('p-1 max-w-[320px]', className)}> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |       { | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |         !tools.length && !showWorkflowEmpty && ( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |           <div className='flex h-[22px] items-center px-3 text-xs font-medium text-text-tertiary'>{t('workflow.tabs.noResult')}</div> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											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> | 
					
						
							|  |  |  |       )} | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |       {!!tools.length && ( | 
					
						
							|  |  |  |         isFlatView ? ( | 
					
						
							|  |  |  |           <ToolListFlatView | 
					
						
							|  |  |  |             toolRefs={toolRefs} | 
					
						
							|  |  |  |             letters={letters} | 
					
						
							|  |  |  |             payload={listViewToolData} | 
					
						
							|  |  |  |             isShowLetterIndex={isShowLetterIndex} | 
					
						
							|  |  |  |             hasSearchText={hasSearchText} | 
					
						
							|  |  |  |             onSelect={onSelect} | 
					
						
							|  |  |  |             selectedTools={selectedTools} | 
					
						
							|  |  |  |           /> | 
					
						
							|  |  |  |         ) : ( | 
					
						
							|  |  |  |           <ToolListTreeView | 
					
						
							|  |  |  |             payload={treeViewToolsData} | 
					
						
							|  |  |  |             hasSearchText={hasSearchText} | 
					
						
							|  |  |  |             onSelect={onSelect} | 
					
						
							|  |  |  |             selectedTools={selectedTools} | 
					
						
							|  |  |  |           /> | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |       )} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       {isShowLetterIndex && <IndexBar letters={letters} itemRefs={toolRefs} className={indexBarClassName} />} | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     </div> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default memo(Blocks) |