11import  {  addEdge ,  Connection ,  useReactFlow  }  from  '@xyflow/react' 
22import  {  useCallback  }  from  'react' 
33import  {  Graph  }  from  '../types' 
4+ import  {  useGraphApi  }  from  '../context/GraphContext.tsx' 
45
56export  function  useSocketConnect ( )  { 
67  const  {  setEdges,  getEdges,  getNodes }  =  useReactFlow < 
78    Graph . Node , 
89    Graph . Edge 
910  > ( ) 
11+   const  api  =  useGraphApi ( ) 
1012  return  useCallback ( 
1113    ( params : Connection )  =>  { 
1214      if  ( params . target  ===  null  ||  params . source  ===  null )  { 
1315        return 
1416      } 
1517
18+       let  isTargetArray  =  false 
19+ 
1620      const  targetNode  =  getNodes ( ) . find ( ( node )  =>  node . id  ===  params . target ) ! 
1721      const  targetInput  =  targetNode . data . internal . inputs . find ( 
1822        ( input )  =>  input . id  ===  params . targetHandle , 
1923      ) 
2024
25+       // Check if the target input is an array type 
26+       if  ( targetNode . type  &&  targetInput ?. name )  { 
27+         const  nodeConfig  =  api . getState ( ) . config . getNodeConfig ( targetNode . type ) 
28+         const  inputConfig  =  nodeConfig . inputs ?. find ( 
29+           ( input )  =>  input . name  ===  targetInput . name , 
30+         ) 
31+         isTargetArray  =  inputConfig ?. isArray  ||  false 
32+       } 
33+ 
2134      // We remove all edges that have the same target and targetHandle 
2235      // if the target handle is not an array type 
23-       const  edgesToRemove  =  targetInput 
24-         ? getEdges ( ) . filter ( 
36+       const  edgesToRemove  =  isTargetArray 
37+         ? [ ] 
38+         : getEdges ( ) . filter ( 
2539            ( e )  => 
2640              e . target  ===  params . target  && 
2741              e . targetHandle  ===  params . targetHandle , 
2842          ) 
29-         : [ ] 
3043
3144      setEdges ( ( edges )  => 
3245        addEdge < Graph . Edge > ( 
@@ -47,6 +60,6 @@ export function useSocketConnect() {
4760        ) . filter ( ( e )  =>  ! edgesToRemove . some ( ( r )  =>  r . id  ===  e . id ) ) , 
4861      ) 
4962    } , 
50-     [ getEdges ,  setEdges ] , 
63+     [ getEdges ,  setEdges ,   api ] , 
5164  ) 
5265} 
0 commit comments