From 8b4c8aec228c138cf7121c6cbe7aa74c3990ada7 Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Mon, 8 Nov 2021 19:46:06 -0500 Subject: [PATCH] svg work --- web/src/components/AutomationDAG/DAGEdge.tsx | 6 +- web/src/components/AutomationDAG/index.tsx | 64 +++++++++----------- web/src/components/AutomationDAG/types.ts | 6 +- web/src/components/AutomationDAG/utils.ts | 13 ++-- web/src/pages/Home.tsx | 32 +++++++--- web/src/utils/useWindowSize.ts | 39 ++++++++++++ 6 files changed, 104 insertions(+), 56 deletions(-) create mode 100644 web/src/utils/useWindowSize.ts diff --git a/web/src/components/AutomationDAG/DAGEdge.tsx b/web/src/components/AutomationDAG/DAGEdge.tsx index 5ab2ca1..8b5d4fa 100644 --- a/web/src/components/AutomationDAG/DAGEdge.tsx +++ b/web/src/components/AutomationDAG/DAGEdge.tsx @@ -18,10 +18,7 @@ export default ({p1, p2, direction}: EdgeProp) => { const cx2 = x2 - NODE_WIDTH const cy2 = my + 30 * Math.cos(theta) - return <> - - - { "url(#arrow)" : '' } /> - } diff --git a/web/src/components/AutomationDAG/index.tsx b/web/src/components/AutomationDAG/index.tsx index 79901ff..807d905 100644 --- a/web/src/components/AutomationDAG/index.tsx +++ b/web/src/components/AutomationDAG/index.tsx @@ -1,36 +1,29 @@ -import { useEffect, useState } from "react" +import { useEffect, useRef, useState } from "react" import { GRAPH_HEIGHT, GRAPH_WIDTH, NODE_WIDTH } from "./constants"; import DAGEdge from "./DAGEdge"; import DAGNode from "./DAGNode"; -import { DAG, Point } from "./types"; +import { DAG, Node } from "./types"; import { normalizeDAG } from "./utils"; - -const randomWalk = ([x,y]:Point) : Point => [ - Math.round(Math.max(0, Math.min(x + (Math.random() < 0.5 ? -1 : 1)* 10*Math.random(), 300))), - Math.round(Math.max(0, Math.min(y + (Math.random() < 0.5 ? -1 : 1)* 10*Math.random(), 300))), -] - export interface Props extends DAG { } export default function AutomationDAG({ ...dag }: Props) { - // const [mx, my] = [10, 100]; - // const [points, setPoints] = useState>([ - // [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