import { CSSProperties } from 'react'; import { shallow } from 'zustand/shallow'; import cc from 'classcat'; import { ConnectionLineType, getBezierPath, getSmoothStepPath, getConnectionStatus, getStraightPath, } from '@xyflow/system'; import { useStore } from '../../hooks/useStore'; import { getSimpleBezierPath } from '../Edges/SimpleBezierEdge'; import type { ConnectionLineComponent, Node, ReactFlowState } from '../../types'; import { useConnection } from '../../hooks/useConnection'; type ConnectionLineWrapperProps = { type: ConnectionLineType; component?: ConnectionLineComponent; containerStyle?: CSSProperties; style?: CSSProperties; }; const selector = (s: ReactFlowState) => ({ nodesConnectable: s.nodesConnectable, isValid: s.connection.isValid, inProgress: s.connection.inProgress, width: s.width, height: s.height, }); export function ConnectionLineWrapper({ containerStyle, style, type, component, }: ConnectionLineWrapperProps) { const { nodesConnectable, width, height, isValid, inProgress } = useStore(selector, shallow); const renderConnection = !!(width && nodesConnectable && inProgress); if (!renderConnection) { return null; } return ( style={style} type={type} CustomComponent={component} isValid={isValid} /> ); } type ConnectionLineProps = { type: ConnectionLineType; style?: CSSProperties; CustomComponent?: ConnectionLineComponent; isValid: boolean | null; }; const ConnectionLine = ({ style, type = ConnectionLineType.Bezier, CustomComponent, isValid, }: ConnectionLineProps) => { const { inProgress, from, fromNode, fromHandle, fromPosition, to, toNode, toHandle, toPosition, pointer } = useConnection(); if (!inProgress) { return; } if (CustomComponent) { return ( ); } let path = ''; const pathParams = { sourceX: from.x, sourceY: from.y, sourcePosition: fromPosition, targetX: to.x, targetY: to.y, targetPosition: toPosition, }; switch (type) { case ConnectionLineType.Bezier: [path] = getBezierPath(pathParams); break; case ConnectionLineType.SimpleBezier: [path] = getSimpleBezierPath(pathParams); break; case ConnectionLineType.Step: [path] = getSmoothStepPath({ ...pathParams, borderRadius: 0, }); break; case ConnectionLineType.SmoothStep: [path] = getSmoothStepPath(pathParams); break; default: [path] = getStraightPath(pathParams); } return ; }; ConnectionLine.displayName = 'ConnectionLine';