@@ -156,7 +156,6 @@ function createNode(id, existingNodes, child, addInformationsToNode) {
156156
157157const buildGraph = ( workflows , addInformationsToNode , targetId , handleId ) => {
158158
159-
160159 if ( workflows . length === 0 ) {
161160 return { edges : [ ] , nodes : [ ] }
162161 }
@@ -331,7 +330,7 @@ const buildGraph = (workflows, addInformationsToNode, targetId, handleId) => {
331330 // sub path of switch group
332331
333332 const predicate = buildGraph ( [ {
334- kind : 'predicate'
333+ kind : 'predicate' ,
335334 } ] , addInformationsToNode , me , 'node' )
336335 const predicateNode = predicate . nodes [ 0 ]
337336
@@ -573,61 +572,119 @@ function WorkflowsDesigner(props) {
573572 } , [ ] ) ;
574573
575574 const graphToJson = ( ) => {
576- const nodesWithConnections = nodes
577- . filter ( node => node . id !== 'returned-node' )
578- . reduce ( ( acc , node ) => {
579- const connections = edges
580- . filter ( edge => edge . source === node . id )
581- return [
582- ...acc ,
583- {
584- node,
585- connections : connections . map ( connection => ( {
586- node : nodes . find ( n => n . id === connection . target ) ,
587- handle : connection . sourceHandle . replace ( `-${ connection . source } ` , '' )
588- } ) )
589- }
590- ]
591- } , [ ] )
575+ const start = {
576+ kind : 'workflow' ,
577+ steps : [ ] ,
578+ returned : nodes . find ( node => node . id === 'returned-node' ) . data . workflow . returned
579+ }
592580
593- const [ first , ...rest ] = nodesWithConnections
594- return nodeToJson ( first , rest , nodesWithConnections )
581+ const startOutput = edges . find ( edge => edge . source === 'start' )
582+ const firstNode = nodes . find ( node => node . id === startOutput . target )
583+
584+ const graph = nodeToJson ( firstNode , start )
585+
586+ return graph
595587 }
596588
597- const nodeToJson = ( current , rest , all ) => {
598- const { node, connections } = current
589+ const emptyWorkflow = ( {
590+ kind : 'workflow' ,
591+ steps : [ ] ,
592+ } )
593+
594+ const nodeToJson = ( node , currentWorkflow , disableRecursion ) => {
595+ const connections = edges . filter ( edge => edge . source === node . id )
599596
600597 const { kind } = node . data
601598
602- if ( kind === 'workflow' ) {
603- const steps = connections . filter ( conn => conn . handle !== 'output' ) . map ( conn => all . find ( n => n . node . id === conn . node . id ) )
599+ let subflow = undefined
604600
601+ if ( node . id . endsWith ( 'returned-node' ) ) {
605602 return {
606- kind : 'workflow' ,
607- steps : steps . map ( step => nodeToJson ( step , rest , all ) ) ,
608- returned : connections . find ( conn => conn . handle === 'output' ) ?. node . data . workflow . returned
603+ ...currentWorkflow ,
604+ returned : node . data . workflow
609605 }
610- } else if ( kind === "if" ) {
611- return {
606+ }
607+
608+ if ( kind === "if" ) {
609+ // CHECK THIS
610+ subflow = {
612611 kind : 'if' ,
613612 predicate : connections . find ( conn => conn . handle === 'predicate' ) ?. node . data . workflow ,
614613 then : connections . find ( conn => conn . handle === 'then' ) ?. node . data . workflow ,
615614 else : connections . find ( conn => conn . handle === 'else' ) ?. node . data . workflow
616615 }
617- } else if ( kind === 'flatmap' ) {
618-
619616 } else if ( kind === 'foreach' ) {
620- return {
621- ...node . data . workflow ,
622- node : connections . length > 0 ? nodeToJson ( connections [ 0 ] , rest , all ) : undefined
617+ const foreachFlow = node . data . workflow
618+ const foreachLoop = connections . find ( conn => conn . sourceHandle . startsWith ( 'ForEachLoop' ) )
619+
620+ if ( foreachLoop ) {
621+ const node = nodeToJson ( nodes . find ( n => n . id === foreachLoop . target ) , emptyWorkflow )
622+ subflow = {
623+ ...foreachFlow ,
624+ node
625+ }
626+ } else {
627+ subflow = foreachFlow
628+ }
629+ } else if ( kind === 'map' || kind === 'flatmap' || kind === 'foreach' ) {
630+ const flow = node . data . workflow
631+ const nodeLoop = connections . find ( conn => conn . sourceHandle . startsWith ( 'Item' ) )
632+
633+ if ( nodeLoop ) {
634+ const node = nodeToJson ( nodes . find ( n => n . id === nodeLoop . target ) , emptyWorkflow )
635+ subflow = {
636+ ...flow ,
637+ node
638+ }
639+ } else {
640+ subflow = flow
641+ }
642+ } else if ( kind === 'filter' ) {
643+ const targets = edges . filter ( edge => edge . target === node . id )
644+ const predicateFlow = node . data . workflow
645+ const predicate = targets . find ( conn => conn . targetHandle . startsWith ( 'predicate' ) )
646+
647+ if ( predicate ) {
648+ subflow = {
649+ ...predicateFlow ,
650+ predicate : nodeToJson ( nodes . find ( n => n . id === predicate . source ) , undefined , true )
651+ }
652+ } else {
653+ subflow = predicateFlow
623654 }
624- } else if ( kind === 'map' ) {
655+ } else if ( kind === 'parallel' || kind === 'switch' ) {
656+ const paths = connections . map ( conn => nodes . find ( n => n . id === conn . target ) )
625657
626- } else if ( kind === 'switch' ) {
658+ subflow = paths . reduce ( ( acc , path ) => {
659+ return {
660+ ...acc ,
661+ paths : [ ...acc . paths , nodeToJson ( path , emptyWorkflow ) ]
662+ }
663+ } , {
664+ ...node . data . workflow ,
665+ paths : [ ]
666+ } )
627667
628668 } else {
629- return node . data . workflow
669+ subflow = node . data . workflow
630670 }
671+
672+ let outputWorkflow = subflow
673+
674+ if ( currentWorkflow && currentWorkflow . kind === 'workflow' ) {
675+ outputWorkflow = {
676+ ...currentWorkflow ,
677+ steps : [ ...currentWorkflow . steps , subflow ]
678+ }
679+ }
680+
681+ const output = connections . find ( conn => conn . sourceHandle . startsWith ( 'output' ) )
682+
683+ if ( output && ! disableRecursion )
684+ return nodeToJson ( nodes . find ( n => n . id === output . target ) , outputWorkflow )
685+
686+ console . log ( outputWorkflow , currentWorkflow )
687+ return outputWorkflow
631688 }
632689
633690 const handleSave = ( ) => {
0 commit comments