diff --git a/webapp/src/components/SequenceNodes/index.stories.tsx b/webapp/src/components/SequenceNodes/index.stories.tsx index 6397855..f5bf1ee 100644 --- a/webapp/src/components/SequenceNodes/index.stories.tsx +++ b/webapp/src/components/SequenceNodes/index.stories.tsx @@ -3,6 +3,7 @@ import { ComponentMeta, ComponentStory } from '@storybook/react'; import { SequenceNodes } from './index'; import { SVGBoard } from '../DAGSvgs/Board'; import { NODE_HEIGHT, NODE_WIDTH } from '../DAGSvgs/constants'; +import { useState } from 'react'; export default { @@ -13,15 +14,20 @@ export default { nodeHeight: NODE_HEIGHT, nodeWidth: NODE_WIDTH, distanceFactor: 1.5, - startLoc: [50,50] + startPoint: [50, 50] } } as ComponentMeta -const Template: ComponentStory = args => - - +const Template: ComponentStory = args => { + const [state, setState] = useState(args.sequence) + return + + +} export const Simple = Template.bind({}) Simple.args = { @@ -71,7 +77,7 @@ Simple.args = { export const Multinode = Template.bind({}) Multinode.args = { ...Multinode.args, - startLoc: [0,0], + startPoint: [0, 0], sequence: [ { $smType: "condition", diff --git a/webapp/src/components/SequenceNodes/index.tsx b/webapp/src/components/SequenceNodes/index.tsx index 4a17b38..9b885c5 100644 --- a/webapp/src/components/SequenceNodes/index.tsx +++ b/webapp/src/components/SequenceNodes/index.tsx @@ -1,33 +1,17 @@ import { FC } from "react"; import { useTheme } from "styles/theme"; -import { Point } from "types/graphs"; -import { AutomationSequenceNode } from '../../types/automations/index'; import { computeNodesEdges } from './location'; +import { SequenceNodeSettings } from './types'; -export const SequenceNodes: FC<{ - startLoc: Point, - sequence: AutomationSequenceNode[], - nodeHeight: number, - nodeWidth: number, - distanceFactor: number, -}> = ({ - startLoc, - sequence, - nodeHeight, - nodeWidth, - distanceFactor, -}) => { +export const SequenceNodes: FC = (props) => { const theme = useTheme(); return <> - {Array.from(computeNodesEdges( - startLoc, - nodeWidth, - nodeHeight, - distanceFactor, - sequence, - theme.secondaryAccent, - ))} + {Array.from(computeNodesEdges({ + ...props, + keyPrefix: "", + edgeColor: theme.secondaryAccent, + }))} } diff --git a/webapp/src/components/SequenceNodes/location.tsx b/webapp/src/components/SequenceNodes/location.tsx index d07b887..0bae367 100644 --- a/webapp/src/components/SequenceNodes/location.tsx +++ b/webapp/src/components/SequenceNodes/location.tsx @@ -2,79 +2,101 @@ import { DAGEdge } from "components/DAGSvgs/DAGEdge"; import { AutomationSequenceNode } from "types/automations"; import { Point } from "types/graphs"; import { DAGNode } from '../DAGSvgs/DAGNode'; +import { SequenceNodeSettings } from './types'; export function* computeNodesEdges( - startPoint: Point, - nodeWidth: number, - nodeHeight: number, - distanceFactor: number, - sequence: AutomationSequenceNode[], - edgeColor: string, - keyPrefix: string = "", -): Generator { + { + sequence, + keyPrefix = "", + onChange = () => { }, + ...st + }: SequenceNodeSettings, +): Generator { let lastStartPoint: Point | null = null; let lastNodeLoc: Point | null = null; for (let i = 0; i < sequence.length; i++) { const node = sequence[i]; - const nodeLoc: Point = lastStartPoint = lastStartPoint === null ? [...startPoint] : [ - lastStartPoint[0] + nodeWidth * distanceFactor, + const nodeLoc: Point = lastStartPoint = lastStartPoint === null ? [...st.startPoint] : [ + lastStartPoint[0] + st.nodeWidth * st.distanceFactor, lastStartPoint[1] ]; - let nextNodeLoc: Point = [nodeLoc[0] + distanceFactor * nodeWidth, nodeLoc[1] + nodeHeight / 2]; + let nextNodeLoc: Point = [nodeLoc[0] + st.distanceFactor * st.nodeWidth, nodeLoc[1] + st.nodeHeight / 2]; yield onChange([ + ...sequence.slice(0, i), + ...sequence.slice(i + 1), + ])} /> if ((node.$smType === 'action') && (node.action === 'choose')) { for (let j = 0; j < node.action_data.choose.length; j++) { - const subSeq = node.action_data.choose[j].sequence; const subStartLoc: Point = [ - nodeLoc[0] + nodeWidth * distanceFactor, - nodeLoc[1] + nodeHeight * (j + 1) + nodeLoc[0] + st.nodeWidth * st.distanceFactor, + nodeLoc[1] + st.nodeHeight * (j + 1) ]; - const it = computeNodesEdges( - subStartLoc, - nodeWidth, - nodeHeight, - distanceFactor, - subSeq, - edgeColor, - keyPrefix=`.${i}.${j}[${nodeLoc}]` - ); - yield + const it = computeNodesEdges({ + ...st, + onChange: seq => onChange([ + ...sequence.slice(0, i), + { + ...node, + action_data: { + ...node.action_data, + choose: [ + ...node.action_data.choose.slice(0, j), + { + ...node.action_data.choose[j], + sequence: seq, + }, + ...node.action_data.choose.slice(j + 1), + ] + } + }, + ...sequence.slice(i + 1), + ]), + sequence: node.action_data.choose[j].sequence, + startPoint: subStartLoc, + keyPrefix: `.${i}.${j}` + }); let next = it.next(); while (!next.done) { yield next.value next = it.next() } - nextNodeLoc[0] = (next.value as any)[1][0] + nodeWidth * distanceFactor - lastStartPoint = [(next.value as any)[1][0], lastStartPoint[1]] + if (next.value) { + yield + nextNodeLoc[0] = next.value[0] + st.nodeWidth * st.distanceFactor + lastStartPoint = [next.value[0], lastStartPoint[1]] + } } } if (i === sequence.length - 1) { yield } lastNodeLoc = [...nodeLoc] } - return [startPoint, lastStartPoint] + return lastStartPoint } diff --git a/webapp/src/components/SequenceNodes/types.ts b/webapp/src/components/SequenceNodes/types.ts new file mode 100644 index 0000000..3bfd6b5 --- /dev/null +++ b/webapp/src/components/SequenceNodes/types.ts @@ -0,0 +1,13 @@ +import { AutomationSequenceNode } from "types/automations"; +import { Point } from "types/graphs"; + +export type SequenceNodeSettings = { + startPoint: Point, + sequence: AutomationSequenceNode[], + nodeHeight: number, + nodeWidth: number, + distanceFactor: number, + edgeColor: string, + keyPrefix?: string, + onChange?: (s: AutomationSequenceNode[]) => void; +}