>([
- // [10+1.25*NODE_WIDTH, 25],
- // [10+2*NODE_WIDTH, 100],
- // [25+1.5*NODE_WIDTH, 170],
- // [25+2*NODE_WIDTH, 170],
- // ])
+ // state
+ const [{nodes, edges}, _setDAG] = useState(normalizeDAG(dag));
- // useEffect(()=>{
- // window.setTimeout(() => {
- // setPoints(points.map(randomWalk))
- // },1000)
- // });
-
- const {nodes, edges} = normalizeDAG(dag);
+ // set state
+ const setDAG = (dag: DAG) => _setDAG(normalizeDAG(dag));
+ const removeNode = (nodeId: string) => () => {
+ const newNodes = {...nodes}
+ delete newNodes[nodeId];
+ _setDAG({
+ nodes: newNodes,
+ edges: edges.filter(e => e.from !== nodeId && e.to !== nodeId),
+ })
+ }
+ // render
return
-
- {nodes.map((node, i) => <>
-
- >)}
- {edges.map(({from, to, direction}) => <>
-
- >)}
+
+
+ {edges.map(({from, to, direction}, i) =>
+ )}
+
+
+ {Object.keys(nodes).map((nodeId) =>
+
+ )}
+
}
\ No newline at end of file
diff --git a/web/src/components/AutomationDAG/types.ts b/web/src/components/AutomationDAG/types.ts
index 56087da..6254147 100644
--- a/web/src/components/AutomationDAG/types.ts
+++ b/web/src/components/AutomationDAG/types.ts
@@ -6,12 +6,12 @@ export interface Node {
text: string;
}
export interface Edge {
- from: number;
- to: number;
+ from: string;
+ to: string;
direction: '1->2' | '1<->2' | '1<-2';
}
export type EdgeDirection = Edge['direction'];
export interface DAG {
- nodes: Array;
+ nodes: Record;
edges: Array;
}
\ No newline at end of file
diff --git a/web/src/components/AutomationDAG/utils.ts b/web/src/components/AutomationDAG/utils.ts
index 4e4d8ea..2846e7f 100644
--- a/web/src/components/AutomationDAG/utils.ts
+++ b/web/src/components/AutomationDAG/utils.ts
@@ -1,6 +1,6 @@
import { GRAPH_HEIGHT, GRAPH_WIDTH, NODE_HEIGHT, NODE_WIDTH } from "./constants";
-import {DAG, Point} from "./types";
+import {DAG, Node, Point} from "./types";
export const clip = (p: Point) : Point => {
@@ -12,10 +12,13 @@ export interface NormalizedDag extends DAG {
}
export const normalizeDAG = (dag: DAG): NormalizedDag => {
// let overflow: bo
- const nodes = dag.nodes.map(n => ({
- ...n,
- loc: clip(n.loc),
- }));
+ const nodes = Object.keys(dag.nodes).map<[string, Node]>(nodeId => [nodeId, {
+ ...dag.nodes[nodeId],
+ loc: clip(dag.nodes[nodeId].loc),
+ }]).reduce((all, [nodeId, next]) => ({
+ [nodeId]: next,
+ ...all,
+ }),{});
// dag.edges.forEach(edge => {
// const n1 = nodes[edge.from];
// const n2 = nodes[edge.to];
diff --git a/web/src/pages/Home.tsx b/web/src/pages/Home.tsx
index ba031c6..1fd34ed 100644
--- a/web/src/pages/Home.tsx
+++ b/web/src/pages/Home.tsx
@@ -1,23 +1,37 @@
import AutomationDAG from "../components/AutomationDAG";
-import AutomationEditor from "../components/AutomationEditor";
-import sample from "../sample/auto_01.yaml";
+// import AutomationEditor from "../components/AutomationEditor";
+// import sample from "../sample/auto_01.yaml";
export function Home() {
return (
2',
},
{
- from: 0, to: 2,
+ from: "0", to: "2",
+ direction: '1->2',
+ },
+ {
+ from: "2", to: "3",
+ direction: '1->2',
+ },
+ {
+ from: "1", to: "4",
+ direction: '1->2',
+ },
+ {
+ from: "0", to: "3",
direction: '1->2',
},
]}
diff --git a/web/src/utils/useWindowSize.ts b/web/src/utils/useWindowSize.ts
new file mode 100644
index 0000000..a1f44fe
--- /dev/null
+++ b/web/src/utils/useWindowSize.ts
@@ -0,0 +1,39 @@
+import { useState, useEffect } from "react";
+
+// Define general type for useWindowSize hook, which includes width and height
+interface Size {
+ width: number;
+ height: number;
+}
+
+// Hook
+export default function useWindowSize(): Size {
+ // Initialize state with undefined width/height so server and client renders match
+ // Learn more here: https://joshwcomeau.com/react/the-perils-of-rehydration/
+ const [windowSize, setWindowSize] = useState({
+ width: window.innerWidth,
+ height: window.innerHeight,
+ });
+
+ useEffect(() => {
+ // Handler to call on window resize
+ function handleResize() {
+ // Set window width/height to state
+ setWindowSize({
+ width: window.innerWidth,
+ height: window.innerHeight,
+ });
+ }
+
+ // Add event listener
+ window.addEventListener("resize", handleResize);
+
+ // Call handler right away so state gets updated with initial window size
+ handleResize();
+
+ // Remove event listener on cleanup
+ return () => window.removeEventListener("resize", handleResize);
+ }, []); // Empty array ensures that effect is only run on mount
+
+ return windowSize;
+}
\ No newline at end of file