| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import type { | 
					
						
							|  |  |  |   FC, | 
					
						
							|  |  |  |   ReactElement, | 
					
						
							|  |  |  | } from 'react' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   cloneElement, | 
					
						
							|  |  |  |   memo, | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |   useEffect, | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |   useMemo, | 
					
						
							| 
									
										
										
										
											2024-05-09 17:18:51 +08:00
										 |  |  |   useRef, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | } from 'react' | 
					
						
							| 
									
										
										
										
											2024-06-20 11:05:08 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   RiCheckboxCircleLine, | 
					
						
							|  |  |  |   RiErrorWarningLine, | 
					
						
							|  |  |  |   RiLoader2Line, | 
					
						
							|  |  |  | } from '@remixicon/react' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import type { NodeProps } from '../../types' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   BlockEnum, | 
					
						
							|  |  |  |   NodeRunningStatus, | 
					
						
							|  |  |  | } from '../../types' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   useNodesReadOnly, | 
					
						
							|  |  |  |   useToolIcon, | 
					
						
							|  |  |  | } from '../../hooks' | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  | import { useNodeIterationInteractions } from '../iteration/use-interactions' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   NodeSourceHandle, | 
					
						
							|  |  |  |   NodeTargetHandle, | 
					
						
							|  |  |  | } from './components/node-handle' | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  | import NodeResizer from './components/node-resizer' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import NodeControl from './components/node-control' | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  | import AddVariablePopupWithPosition from './components/add-variable-popup-with-position' | 
					
						
							| 
									
										
										
										
											2024-07-09 15:05:40 +08:00
										 |  |  | import cn from '@/utils/classnames' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import BlockIcon from '@/app/components/workflow/block-icon' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type BaseNodeProps = { | 
					
						
							|  |  |  |   children: ReactElement | 
					
						
							|  |  |  | } & NodeProps | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const BaseNode: FC<BaseNodeProps> = ({ | 
					
						
							|  |  |  |   id, | 
					
						
							|  |  |  |   data, | 
					
						
							|  |  |  |   children, | 
					
						
							|  |  |  | }) => { | 
					
						
							| 
									
										
										
										
											2024-05-09 17:18:51 +08:00
										 |  |  |   const nodeRef = useRef<HTMLDivElement>(null) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   const { nodesReadOnly } = useNodesReadOnly() | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |   const { handleNodeIterationChildSizeChange } = useNodeIterationInteractions() | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   const toolIcon = useToolIcon(data) | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     if (nodeRef.current && data.selected && data.isInIteration) { | 
					
						
							|  |  |  |       const resizeObserver = new ResizeObserver(() => { | 
					
						
							|  |  |  |         handleNodeIterationChildSizeChange(id) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       resizeObserver.observe(nodeRef.current) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return () => { | 
					
						
							|  |  |  |         resizeObserver.disconnect() | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, [data.isInIteration, data.selected, id, handleNodeIterationChildSizeChange]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const showSelectedBorder = data.selected || data._isBundled || data._isEntering | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |   const { | 
					
						
							|  |  |  |     showRunningBorder, | 
					
						
							|  |  |  |     showSuccessBorder, | 
					
						
							|  |  |  |     showFailedBorder, | 
					
						
							|  |  |  |   } = useMemo(() => { | 
					
						
							|  |  |  |     return { | 
					
						
							| 
									
										
										
										
											2024-05-09 17:18:51 +08:00
										 |  |  |       showRunningBorder: data._runningStatus === NodeRunningStatus.Running && !showSelectedBorder, | 
					
						
							|  |  |  |       showSuccessBorder: data._runningStatus === NodeRunningStatus.Succeeded && !showSelectedBorder, | 
					
						
							|  |  |  |       showFailedBorder: data._runningStatus === NodeRunningStatus.Failed && !showSelectedBorder, | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-05-09 17:18:51 +08:00
										 |  |  |   }, [data._runningStatus, showSelectedBorder]) | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   return ( | 
					
						
							|  |  |  |     <div | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |       className={cn( | 
					
						
							|  |  |  |         'flex border-[2px] rounded-2xl', | 
					
						
							| 
									
										
										
										
											2024-07-19 16:39:49 +08:00
										 |  |  |         showSelectedBorder ? 'border-components-option-card-option-selected-border' : 'border-transparent', | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |       )} | 
					
						
							| 
									
										
										
										
											2024-05-09 17:18:51 +08:00
										 |  |  |       ref={nodeRef} | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |       style={{ | 
					
						
							|  |  |  |         width: data.type === BlockEnum.Iteration ? data.width : 'auto', | 
					
						
							|  |  |  |         height: data.type === BlockEnum.Iteration ? data.height : 'auto', | 
					
						
							|  |  |  |       }} | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     > | 
					
						
							|  |  |  |       <div | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |         className={cn( | 
					
						
							|  |  |  |           'group relative pb-1 shadow-xs', | 
					
						
							|  |  |  |           'border border-transparent rounded-[15px]', | 
					
						
							| 
									
										
										
										
											2024-07-19 16:39:49 +08:00
										 |  |  |           data.type !== BlockEnum.Iteration && 'w-[240px] bg-workflow-block-bg', | 
					
						
							| 
									
										
										
										
											2024-07-22 15:43:24 +08:00
										 |  |  |           data.type === BlockEnum.Iteration && 'flex flex-col w-full h-full bg-[#fcfdff]/80', | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |           !data._runningStatus && 'hover:shadow-lg', | 
					
						
							|  |  |  |           showRunningBorder && '!border-primary-500', | 
					
						
							|  |  |  |           showSuccessBorder && '!border-[#12B76A]', | 
					
						
							|  |  |  |           showFailedBorder && '!border-[#F04438]', | 
					
						
							|  |  |  |           data._isBundled && '!shadow-lg', | 
					
						
							|  |  |  |         )} | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |       > | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |           data._showAddVariablePopup && ( | 
					
						
							|  |  |  |             <AddVariablePopupWithPosition | 
					
						
							|  |  |  |               nodeId={id} | 
					
						
							|  |  |  |               nodeData={data} | 
					
						
							|  |  |  |             /> | 
					
						
							|  |  |  |           ) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           data.type === BlockEnum.Iteration && ( | 
					
						
							|  |  |  |             <NodeResizer | 
					
						
							|  |  |  |               nodeId={id} | 
					
						
							|  |  |  |               nodeData={data} | 
					
						
							|  |  |  |             /> | 
					
						
							|  |  |  |           ) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2024-05-30 18:54:58 +08:00
										 |  |  |           !data._isCandidate && ( | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             <NodeTargetHandle | 
					
						
							|  |  |  |               id={id} | 
					
						
							|  |  |  |               data={data} | 
					
						
							|  |  |  |               handleClassName='!top-4 !-left-[9px] !translate-y-0' | 
					
						
							|  |  |  |               handleId='target' | 
					
						
							|  |  |  |             /> | 
					
						
							|  |  |  |           ) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2024-05-09 17:18:51 +08:00
										 |  |  |           data.type !== BlockEnum.IfElse && data.type !== BlockEnum.QuestionClassifier && !data._isCandidate && ( | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             <NodeSourceHandle | 
					
						
							|  |  |  |               id={id} | 
					
						
							|  |  |  |               data={data} | 
					
						
							|  |  |  |               handleClassName='!top-4 !-right-[9px] !translate-y-0' | 
					
						
							|  |  |  |               handleId='source' | 
					
						
							|  |  |  |             /> | 
					
						
							|  |  |  |           ) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2024-05-09 17:18:51 +08:00
										 |  |  |           !data._runningStatus && !nodesReadOnly && !data._isCandidate && ( | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             <NodeControl | 
					
						
							|  |  |  |               id={id} | 
					
						
							|  |  |  |               data={data} | 
					
						
							|  |  |  |             /> | 
					
						
							|  |  |  |           ) | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |         <div className={cn( | 
					
						
							|  |  |  |           'flex items-center px-3 pt-3 pb-2 rounded-t-2xl', | 
					
						
							|  |  |  |           data.type === BlockEnum.Iteration && 'bg-[rgba(250,252,255,0.9)]', | 
					
						
							|  |  |  |         )}> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |           <BlockIcon | 
					
						
							|  |  |  |             className='shrink-0 mr-2' | 
					
						
							|  |  |  |             type={data.type} | 
					
						
							|  |  |  |             size='md' | 
					
						
							|  |  |  |             toolIcon={toolIcon} | 
					
						
							|  |  |  |           /> | 
					
						
							|  |  |  |           <div | 
					
						
							|  |  |  |             title={data.title} | 
					
						
							| 
									
										
										
										
											2024-07-19 16:39:49 +08:00
										 |  |  |             className='grow mr-1 system-sm-semibold-uppercase text-text-primary truncate' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |           > | 
					
						
							|  |  |  |             {data.title} | 
					
						
							|  |  |  |           </div> | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |           { | 
					
						
							|  |  |  |             data._iterationLength && data._iterationIndex && data._runningStatus === NodeRunningStatus.Running && ( | 
					
						
							|  |  |  |               <div className='mr-1.5 text-xs font-medium text-primary-600'> | 
					
						
							|  |  |  |                 {data._iterationIndex}/{data._iterationLength} | 
					
						
							|  |  |  |               </div> | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |           { | 
					
						
							|  |  |  |             (data._runningStatus === NodeRunningStatus.Running || data._singleRunningStatus === NodeRunningStatus.Running) && ( | 
					
						
							| 
									
										
										
										
											2024-06-20 11:05:08 +08:00
										 |  |  |               <RiLoader2Line className='w-3.5 h-3.5 text-primary-600 animate-spin' /> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             data._runningStatus === NodeRunningStatus.Succeeded && ( | 
					
						
							| 
									
										
										
										
											2024-06-20 11:05:08 +08:00
										 |  |  |               <RiCheckboxCircleLine className='w-3.5 h-3.5 text-[#12B76A]' /> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             data._runningStatus === NodeRunningStatus.Failed && ( | 
					
						
							| 
									
										
										
										
											2024-06-20 11:05:08 +08:00
										 |  |  |               <RiErrorWarningLine className='w-3.5 h-3.5 text-[#F04438]' /> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |           data.type !== BlockEnum.Iteration && ( | 
					
						
							|  |  |  |             cloneElement(children, { id, data }) | 
					
						
							|  |  |  |           ) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           data.type === BlockEnum.Iteration && ( | 
					
						
							|  |  |  |             <div className='grow pl-1 pr-1 pb-1'> | 
					
						
							|  |  |  |               {cloneElement(children, { id, data })} | 
					
						
							|  |  |  |             </div> | 
					
						
							|  |  |  |           ) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           data.desc && data.type !== BlockEnum.Iteration && ( | 
					
						
							| 
									
										
										
										
											2024-07-19 16:39:49 +08:00
										 |  |  |             <div className='px-3 pt-1 pb-2 system-xs-regular text-text-tertiary whitespace-pre-line break-words'> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |               {data.desc} | 
					
						
							|  |  |  |             </div> | 
					
						
							|  |  |  |           ) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |     </div> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default memo(BaseNode) |