import { IFlowNode } from '@/types/flow'; import Image from 'next/image'; import NodeParamHandler from './node-param-handler'; import classNames from 'classnames'; import { useState } from 'react'; import NodeHandler from './node-handler'; import { Popover, Tooltip } from 'antd'; import { CopyOutlined, DeleteOutlined, InfoCircleOutlined } from '@ant-design/icons'; import { useReactFlow } from 'reactflow'; import IconWrapper from '../common/icon-wrapper'; import { getUniqueNodeId } from '@/utils/flow'; import { cloneDeep } from 'lodash'; type CanvasNodeProps = { data: IFlowNode; }; const ICON_PATH_PREFIX = '/icons/node/'; function TypeLabel({ label }: { label: string }) { return
{label}
; } const CanvasNode: React.FC = ({ data }) => { const node = data; const { inputs, outputs, parameters, flow_type: flowType } = node; const [isHovered, setIsHovered] = useState(false); const reactFlow = useReactFlow(); function onHover() { setIsHovered(true); } function onLeave() { setIsHovered(false); } function copyNode(e: React.MouseEvent) { e.preventDefault(); e.stopPropagation(); const nodes = reactFlow.getNodes(); const originalNode = nodes.find((item) => item.id === node.id); if (originalNode) { const newNodeId = getUniqueNodeId(originalNode as IFlowNode, nodes); const cloneNode = cloneDeep(originalNode); const duplicatedNode = { ...cloneNode, id: newNodeId, position: { x: cloneNode.position.x + 400, y: cloneNode.position.y, }, positionAbsolute: { x: cloneNode.positionAbsolute!.x + 400, y: cloneNode.positionAbsolute!.y, }, data: { ...cloneNode.data, id: newNodeId, }, selected: false, }; reactFlow.setNodes((nodes) => [...nodes, duplicatedNode]); } } function deleteNode(e: React.MouseEvent) { e.preventDefault(); e.stopPropagation(); reactFlow.setNodes((nodes) => nodes.filter((item) => item.id !== node.id)); reactFlow.setEdges((edges) => edges.filter((edge) => edge.source !== node.id && edge.target !== node.id)); } function renderOutput(data: IFlowNode) { if (flowType === 'operator' && outputs?.length > 0) { return ( <> {(outputs || []).map((output, index) => ( ))} ); } else if (flowType === 'resource') { // resource nodes show output default return ( <> ); } } return (

{node.label}

{node.description}

} placement="right">
} >
{/* icon and label */}

{node.label}

{inputs && inputs.length > 0 && ( <> {(inputs || []).map((input, index) => ( ))} )} {parameters && parameters.length > 0 && ( <> {(parameters || []).map((parameter, index) => ( ))} )} {renderOutput(node)}
); }; export default CanvasNode;