more name stuff + .gitignore

This commit is contained in:
Ari Sosnovsky committed 2021-11-09 20:43:08 -05:00
1 parent 76dca488ee
commit ea9dd2a8a3
26 files changed
+595 -162

No files matched your search

+3 -3
View File
@@ -9,7 +9,7 @@ export interface NodeProp extends Node {
isOpen?: boolean;
onXClick?: () => void;
onOpenClick?: () => void;
topColor?: 'red' | 'green' | 'blue' | 'none';
color?: Node['color'];
}
export default ({
loc: [x,y],
@@ -19,10 +19,10 @@ export default ({
onXClick=() => {},
onOpenClick=() => {},
isOpen=false,
topColor='none',
color='none',
}: NodeProp) => <foreignObject x={x} y={y} width={width} height={height}>
<div className="automation-dag--node">
<div className={`automation-dag--node-inner ${topColor !== 'none' ? `top-color ${topColor}`: ''}`}>
<div className={`automation-dag--node-inner ${color !== 'none' ? `top-color ${color}`: ''}`}>
<div className="automation-dag--node-inner-edge left" onClick={onXClick}>
<div className="automation-node-side-button">X</div>
</div>
@@ -0,0 +1,233 @@
import { GRAPH_HEIGHT, GRAPH_WIDTH, NODE_HEIGHT, NODE_WIDTH } from "./constants";
import { DAG } from "./types";
import {clip, computeDAG} from "./dagFuncs";
test('it clips overflowing values', () => {
expect(clip([GRAPH_WIDTH, GRAPH_HEIGHT])).toEqual([
GRAPH_WIDTH-NODE_WIDTH,
GRAPH_HEIGHT-NODE_HEIGHT
])
expect(clip([-10, -10])).toEqual([
0, 0
])
expect(clip([0, 0])).toEqual([
0, 0
])
})
test('simple empty input dag', () => {
expect(computeDAG({
id: "root",
alias: "root",
description: 'root',
mode: 'single',
trigger: [],
condition: [],
action: [
],
})).toEqual({
nodes: {},
edges: [
],
})
})
test('simple single node type input dag', () => {
expect(computeDAG({
id: "root",
alias: "root",
description: 'root',
mode: 'single',
trigger: [
{
platform: 'time',
at: '10:00:00'
}
],
condition: [
{
condition: 'template',
value_template: 'states(switch.kitchen_light) == "on"'
}
],
action: [
{
alias: "Start Music In Kitchen",
service: 'media_player.play_media',
target: {
entity_id: "media_player.kitchen_dot"
},
data: {
media_content_id: "Good Morning",
media_content_type: "SPOTIFY",
}
}
],
})).toEqual({
nodes: {
"trigger.0": {
text: '',
// text: "At 10:00:00",
loc: [0,5],
color: 'green',
},
"condition.0": {
loc: [NODE_WIDTH*1.5, 5],
text: '',
color: 'blue',
// text: 'states(switch.kitchen_light) == "on"'
},
"action.0": {
loc: [NODE_WIDTH*3, 5],
text: '',
color: 'red',
// text: 'Start Music In Kitchen',
}
},
edges: [
{ from: 'trigger.0', to: 'condition.0', direction: '1->2' },
{ from: 'condition.0', to: 'action.0', direction: '1->2' },
],
} as DAG)
})
test('simple dag with no triggers', () => {
expect(computeDAG({
id: "root",
alias: "root",
description: 'root',
mode: 'single',
trigger: [
],
condition: [
{
condition: 'template',
value_template: 'states(switch.kitchen_light) == "on"'
}
],
action: [
{
alias: "Start Music In Kitchen",
service: 'media_player.play_media',
target: {
entity_id: "media_player.kitchen_dot"
},
data: {
media_content_id: "Good Morning",
media_content_type: "SPOTIFY",
}
}
],
})).toEqual({
nodes: {
"condition.0": {
loc: [NODE_WIDTH*1.5, 5],
text: '',
color: 'blue',
// text: 'states(switch.kitchen_light) == "on"'
},
"action.0": {
loc: [NODE_WIDTH*3, 5],
text: '',
color: 'red',
// text: 'Start Music In Kitchen',
}
},
edges: [
{ from: 'condition.0', to: 'action.0', direction: '1->2' },
],
} as DAG)
})
test('simple dag with no conditions', () => {
expect(computeDAG({
id: "root",
alias: "root",
description: 'root',
mode: 'single',
trigger: [
{
platform: 'time',
at: '10:00:00'
}
],
condition: [],
action: [
{
alias: "Start Music In Kitchen",
service: 'media_player.play_media',
target: {
entity_id: "media_player.kitchen_dot"
},
data: {
media_content_id: "Good Morning",
media_content_type: "SPOTIFY",
}
}
],
})).toEqual({
nodes: {
"trigger.0": {
text: '',
// text: "At 10:00:00",
loc: [0,5],
color: 'green',
},
"action.0": {
loc: [NODE_WIDTH*3, 5],
text: '',
color: 'red',
// text: 'Start Music In Kitchen',
}
},
edges: [
{ from: 'trigger.0', to: 'action.0', direction: '1->2' },
],
} as DAG)
})
test('simple dag with no actions', () => {
expect(computeDAG({
id: "root",
alias: "root",
description: 'root',
mode: 'single',
trigger: [
{
platform: 'time',
at: '10:00:00'
}
],
condition: [
{
condition: 'template',
value_template: 'states(switch.kitchen_light) == "on"'
}
],
action: [
],
})).toEqual({
nodes: {
"trigger.0": {
text: '',
// text: "At 10:00:00",
loc: [0,5],
color: 'green',
},
"condition.0": {
loc: [NODE_WIDTH*1.5, 5],
text: '',
color: 'blue',
// text: 'states(switch.kitchen_light) == "on"'
},
},
edges: [
{ from: 'trigger.0', to: 'condition.0', direction: '1->2' }
],
} as DAG)
})
@@ -0,0 +1,89 @@
import { Automation } from "../../automation/types";
import { GRAPH_HEIGHT, GRAPH_WIDTH, NODE_HEIGHT, NODE_WIDTH } from "./constants";
import {DAG, Node, Point, NormalizedDag} from "./types";
export const clip = (p: Point) : Point => {
const [x,y] = p.map(c => Math.max(0, c));
return [Math.min(GRAPH_WIDTH-NODE_WIDTH, x), Math.min(GRAPH_HEIGHT-NODE_HEIGHT, y)]
}
export const normalizeDAG = (dag: DAG): NormalizedDag => {
// let overflow: bo
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,
}),{});
return {
nodes,
edges: dag.edges,
}
}
export const computeDAG = (automation: Automation) : DAG => {
const nodes: DAG['nodes'] = {};
const edges: DAG['edges'] = [];
const triggerIds: string[] = [];
automation.trigger.forEach((t,i) => {
nodes[`trigger.${i}`] = {
loc: [0, 5+NODE_HEIGHT*i*1.5],
text: "",
color: 'green',
}
triggerIds.push(`trigger.${i}`);
})
let lastConditionId: string | undefined = undefined;
automation.condition.forEach((c,i) => {
const cid = `condition.${i}`;
nodes[cid] = {
loc: [NODE_WIDTH*1.5, 5+NODE_HEIGHT*i*1.5],
text: "",
color: 'blue',
}
lastConditionId = cid;
if (i === 0) {
triggerIds.forEach(tid => edges.push({
from: tid, to: cid, direction: '1->2',
}));
} else {
edges.push({
from: lastConditionId, to: cid, direction: '1->2',
})
}
});
if (!lastConditionId) {
if(triggerIds.length > 0) {
lastConditionId = triggerIds[triggerIds.length-1]
}
}
const actionIds: string[] = [];
automation.action.forEach((c,i) => {
const aid = `action.${i}`;
nodes[aid] = {
loc: [NODE_WIDTH*3, 5+NODE_HEIGHT*i*1.5],
text: "",
color: 'red',
}
actionIds.push(aid);
if (i === 0) {
if (lastConditionId) {
edges.push({
from: lastConditionId, to: aid, direction: '1->2',
});
}
} else {
edges.push({
from: `action.${i-1}`, to: aid, direction: '1->2',
})
}
})
return {
nodes, edges
}
}
+12 -15
View File
@@ -3,26 +3,18 @@ import { GRAPH_HEIGHT, GRAPH_WIDTH, NODE_WIDTH } from "./constants";
import DAGEdge from "./DAGEdge";
import DAGNode from "./DAGNode";
import { DAG, Node } from "./types";
import { normalizeDAG } from "./utils";
import { normalizeDAG } from "./dagFuncs";
export interface Props extends DAG {
onDelete: (nodeId: string) => void;
onOpenNode: (nodeId: string) => void;
}
export default function AutomationDAG({
onDelete,
onOpenNode,
...dag
}: Props) {
// state
const [{nodes, edges}, _setDAG] = useState(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),
})
}
const {nodes, edges} = normalizeDAG(dag);
// render
return <div className="automation-dag" >
<svg
@@ -48,7 +40,12 @@ export default function AutomationDAG({
</g>
<g className="automation-dag-nodes">
{Object.keys(nodes).map((nodeId) =>
<DAGNode key={nodeId} {...nodes[nodeId]} onXClick={removeNode(nodeId)}/>
<DAGNode
key={nodeId}
{...nodes[nodeId]}
onXClick={() => onDelete(nodeId)}
onOpenClick={() => onOpenNode(nodeId)}
/>
)}
</g>
</svg>
@@ -4,6 +4,7 @@ export type Point = [number, number];
export interface Node {
loc: Point;
text: JSX.Element | string;
color?: 'red' | 'green' | 'blue' | 'none';
}
export interface Edge {
from: string;
@@ -1,33 +0,0 @@
import { GRAPH_HEIGHT, GRAPH_WIDTH, NODE_HEIGHT, NODE_WIDTH } from "./constants";
import {clip, computeDAG} from "./utils";
test('it clips overflowing values', () => {
expect(clip([GRAPH_WIDTH, GRAPH_HEIGHT])).toEqual([
GRAPH_WIDTH-NODE_WIDTH,
GRAPH_HEIGHT-NODE_HEIGHT
])
expect(clip([-10, -10])).toEqual([
0, 0
])
expect(clip([0, 0])).toEqual([
0, 0
])
})
test('simple empty input dag', () => {
expect(computeDAG({
id: "root",
alias: "root",
description: 'root',
mode: 'single',
trigger: [],
condition: [],
action: [
],
})).toEqual({
nodes: {},
edges: [
],
})
})
-58
View File
@@ -1,58 +0,0 @@
import { Automation } from "../../automation/types";
import { GRAPH_HEIGHT, GRAPH_WIDTH, NODE_HEIGHT, NODE_WIDTH } from "./constants";
import {DAG, Node, Point, NormalizedDag} from "./types";
export const clip = (p: Point) : Point => {
const [x,y] = p.map(c => Math.max(0, c));
return [Math.min(GRAPH_WIDTH-NODE_WIDTH, x), Math.min(GRAPH_HEIGHT-NODE_HEIGHT, y)]
}
export const normalizeDAG = (dag: DAG): NormalizedDag => {
// let overflow: bo
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];
// n2.loc[0] = Math.max(n2.loc[0], n1.loc[0] + 1.25*NODE_WIDTH)
// })
return {
nodes,
edges: dag.edges,
}
}
export const computeDAG = (automation: Automation) : DAG => {
const nodes: DAG['nodes'] = {};
const edges: DAG['edges'] = [];
automation.action
// let currentNode: InputDagNode | null = rootNode;
// let currLoc: Point = [0,0];
// let idPath = '';
// // let
// while (currentNode) {
// const n = {
// text: currentNode.display,
// loc: currLoc,
// }
// if(idPath) {
// nodes[idPath + "." + currentNode.name] = n
// edges
// } else {
// nodes[currentNode.name] = n
// }
// currentNode = currentNode.next[0]
// }
return {
nodes, edges
}
}
@@ -1,6 +1,6 @@
import StateManager from "../../automation/StateManager";
import { AutomationAction } from "../../automation/types";
import { determineNodeTypes, getNameFromAction } from "../../automation/utils";
import { determineNodeTypes, getDescriptionFromAutomationNode } from "../../automation/utils";
import AutomationNode from "../AutomationNode";
@@ -15,7 +15,7 @@ export default function ScriptNode({
const [nodeType, nodeSubType] = determineNodeTypes(data);
const stateManager = new StateManager(nodeType, nodeSubType, data);
return <AutomationNode title={getNameFromAction(data)} onXClick={onDelete}>
return <AutomationNode title={getDescriptionFromAutomationNode(data)} onXClick={onDelete}>
{stateManager.renderOptionList()}
</AutomationNode>
}
@@ -1,6 +1,6 @@
import { useState } from "react";
import * as types from "../../automation/types";
import { getNameFromAction } from "../../automation/utils";
import { getDescriptionFromAutomationNode } from "../../automation/utils";
import AutomationNode from "../AutomationNode";
import NewNodeModal from "./NewNodeModal";
import ScriptNode from "./ScriptNode";