From ccca0edb65d002e459972bd4717d7cd443d93ab7 Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Fri, 12 Nov 2021 14:39:55 -0500 Subject: [PATCH] updated automation metadata --- web/src/automations/types/triggers.ts | 12 +- .../components/AutomationDAG/dagFuncs.spec.ts | 264 ------------------ web/src/components/AutomationDAG/dagFuncs.ts | 89 ------ web/src/components/AutomationDAG/index.tsx | 53 ---- .../AutomationEditor/AutoInfoBox.tsx | 42 +++ .../AutomationEditor/AutomationNode/index.tsx | 35 --- .../AutomationEditor/ConditionNode.tsx | 20 -- .../DAGEdge.tsx | 2 +- .../DAGNode.tsx | 5 +- .../AutomationEditor/NewNodeModal.tsx | 46 --- .../AutomationEditor/ScriptNode.tsx | 21 -- .../constants.ts | 0 web/src/components/AutomationEditor/index.tsx | 118 +++----- .../types.ts | 6 +- web/src/components/Inputs/InputTextArea.tsx | 37 +++ web/src/components/Inputs/InputViewEdit.tsx | 16 +- web/src/pages/Home.tsx | 90 +++--- web/src/router.tsx | 5 +- web/src/styles/components/AutomationDAG.scss | 7 +- web/src/styles/components/InputWrapper.scss | 93 +++--- 20 files changed, 238 insertions(+), 723 deletions(-) delete mode 100644 web/src/components/AutomationDAG/dagFuncs.spec.ts delete mode 100644 web/src/components/AutomationDAG/dagFuncs.ts delete mode 100644 web/src/components/AutomationDAG/index.tsx create mode 100644 web/src/components/AutomationEditor/AutoInfoBox.tsx delete mode 100644 web/src/components/AutomationEditor/AutomationNode/index.tsx delete mode 100644 web/src/components/AutomationEditor/ConditionNode.tsx rename web/src/components/{AutomationDAG => AutomationEditor}/DAGEdge.tsx (95%) rename web/src/components/{AutomationDAG => AutomationEditor}/DAGNode.tsx (87%) delete mode 100644 web/src/components/AutomationEditor/NewNodeModal.tsx delete mode 100644 web/src/components/AutomationEditor/ScriptNode.tsx rename web/src/components/{AutomationDAG => AutomationEditor}/constants.ts (100%) rename web/src/components/{AutomationDAG => AutomationEditor}/types.ts (74%) create mode 100644 web/src/components/Inputs/InputTextArea.tsx diff --git a/web/src/automations/types/triggers.ts b/web/src/automations/types/triggers.ts index 5f8b832..8293dfd 100644 --- a/web/src/automations/types/triggers.ts +++ b/web/src/automations/types/triggers.ts @@ -2,7 +2,7 @@ import { AutomationTime } from "./common"; export interface AutomationTriggerEvent { platform: 'event'; - event_type: string; + event_type: string | string[]; event_data?: any; context?: any; } @@ -23,15 +23,15 @@ export interface AutomationTriggerNumericState { value_template?: string; above?: string; below?: string; - for?: AutomationTime | string; + for?: AutomationTime; } export interface AutomationTriggerState { platform: 'state'; entity_id: string | string[]; attribute?: string; - from?: string; - to?: string; - for?: AutomationTime | string; + from?: string | string[]; + to?: string | string[]; + for?: AutomationTime; } export interface AutomationTriggerTag { platform: 'tag'; @@ -41,7 +41,7 @@ export interface AutomationTriggerTag { export interface AutomationTriggerTemplate { platform: 'template'; value_template: string; - for?: AutomationTime | string; + for?: AutomationTime; } export interface AutomationTriggerTime { platform: 'time'; diff --git a/web/src/components/AutomationDAG/dagFuncs.spec.ts b/web/src/components/AutomationDAG/dagFuncs.spec.ts deleted file mode 100644 index 753a865..0000000 --- a/web/src/components/AutomationDAG/dagFuncs.spec.ts +++ /dev/null @@ -1,264 +0,0 @@ - -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({ - metadata: { - id: "root", - alias: "root", - description: 'root', - mode: 'single', - }, - trigger: [], - condition: [], - action: [ - ], - })).toEqual({ - nodes: {}, - edges: [ - ], - }) -}) - -test('simple single node type input dag', () => { - expect(computeDAG({ - metadata: { - id: "root", - alias: "root", - description: 'root', - mode: 'single', - }, - trigger: [ - { - platform: 'time', - at: '10:00:00' - } - ], - condition: [ - { - $smType: 'condition', - condition: 'template', - condition_data: { - value_template: 'states(switch.kitchen_light) == "on"' - } - } - ], - action: [ - { - $smType: 'action', - action: "service", - action_data: { - 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({ - metadata: { - id: "root", - alias: "root", - description: 'root', - mode: 'single', - }, - trigger: [ - ], - condition: [ - { - $smType: "condition", - condition: 'template', - condition_data: { - value_template: 'states(switch.kitchen_light) == "on"' - } - } - ], - action: [ - { - $smType: "action", - action: "service", - action_data: { - 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({ - metadata: { - id: "root", - alias: "root", - description: 'root', - mode: 'single', - }, - trigger: [ - { - platform: 'time', - at: '10:00:00' - } - ], - condition: [], - action: [ - { - $smType: 'action', - action: 'service', - action_data: { - 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({ - metadata: { - id: "root", - alias: "root", - description: 'root', - mode: 'single', - }, - trigger: [ - { - platform: 'time', - at: '10:00:00' - } - ], - condition: [ - { - $smType: "condition", - condition: 'template', - condition_data: { - 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) -}) - diff --git a/web/src/components/AutomationDAG/dagFuncs.ts b/web/src/components/AutomationDAG/dagFuncs.ts deleted file mode 100644 index b8871c7..0000000 --- a/web/src/components/AutomationDAG/dagFuncs.ts +++ /dev/null @@ -1,89 +0,0 @@ - -import { AutomationData } from "../../automations/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: AutomationData) : 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 - } -} \ No newline at end of file diff --git a/web/src/components/AutomationDAG/index.tsx b/web/src/components/AutomationDAG/index.tsx deleted file mode 100644 index 7f9a3e2..0000000 --- a/web/src/components/AutomationDAG/index.tsx +++ /dev/null @@ -1,53 +0,0 @@ -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, Node } from "./types"; -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} = normalizeDAG(dag); - // render - return
- - - - - - - {/* */} - - {edges.map(({from, to, direction}, i) => - )} - - - {Object.keys(nodes).map((nodeId) => - onDelete(nodeId)} - onOpenClick={() => onOpenNode(nodeId)} - /> - )} - - -
-} \ No newline at end of file diff --git a/web/src/components/AutomationEditor/AutoInfoBox.tsx b/web/src/components/AutomationEditor/AutoInfoBox.tsx new file mode 100644 index 0000000..b41392f --- /dev/null +++ b/web/src/components/AutomationEditor/AutoInfoBox.tsx @@ -0,0 +1,42 @@ +import { AutomationMetadata } from "~/automations/types"; +import InputList from "../Inputs/InputList"; +import InputText from "../Inputs/InputText"; +import InputTextArea from "../Inputs/InputTextArea"; + +interface Props { + metadata: AutomationMetadata; + onUpdate: (m: AutomationMetadata) => void; +} +export default function AutoInfoBox({ + metadata, + onUpdate, +}: Props) { + + // aliases + const onUpdateMetadata = (k: K) =>(update: AutomationMetadata[K]) => onUpdate({ + ...metadata, + [k]: update + }) + + // render + return + +
+ + + + +
+
+
+} \ No newline at end of file diff --git a/web/src/components/AutomationEditor/AutomationNode/index.tsx b/web/src/components/AutomationEditor/AutomationNode/index.tsx deleted file mode 100644 index 84a2e3b..0000000 --- a/web/src/components/AutomationEditor/AutomationNode/index.tsx +++ /dev/null @@ -1,35 +0,0 @@ -import { PropsWithChildren, useState } from "react"; - -export interface Props { - title: string; - initialOpenState?: boolean; - onXClick?: () => void; -} -export default function AutomationNode({ - children, - title, - initialOpenState=false, - onXClick = () => {}, -}: PropsWithChildren) { - const [isOpen, setOpen] = useState(initialOpenState); - return
-
-
onXClick()} - > -
X
-
-
{title}
-
setOpen(!isOpen)} - > -
{isOpen ? "▲" : "▼"}
-
-
-
- {children} -
-
-} \ No newline at end of file diff --git a/web/src/components/AutomationEditor/ConditionNode.tsx b/web/src/components/AutomationEditor/ConditionNode.tsx deleted file mode 100644 index 36f6293..0000000 --- a/web/src/components/AutomationEditor/ConditionNode.tsx +++ /dev/null @@ -1,20 +0,0 @@ -import { useState } from "react"; -import { AutomationCondition } from "../../automation/types/condition"; -import InputList from "../InputList"; - - - -export interface Props { - condition: AutomationCondition -} -export default function ConditionNode({ - condition -}: Props) { - - return
- {/* */} -
-} \ No newline at end of file diff --git a/web/src/components/AutomationDAG/DAGEdge.tsx b/web/src/components/AutomationEditor/DAGEdge.tsx similarity index 95% rename from web/src/components/AutomationDAG/DAGEdge.tsx rename to web/src/components/AutomationEditor/DAGEdge.tsx index 8b5d4fa..f9b881a 100644 --- a/web/src/components/AutomationDAG/DAGEdge.tsx +++ b/web/src/components/AutomationEditor/DAGEdge.tsx @@ -11,7 +11,7 @@ export interface EdgeProp { export default ({p1, p2, direction}: EdgeProp) => { const [x1,y1] = [p1[0] + NODE_WIDTH, p1[1]+NODE_HEIGHT/2] const [x2,y2] = [p2[0], p2[1]+NODE_HEIGHT/2] - const [mx,my] = [(x1+x2)/2, (y1+y2)/2] + const [_,my] = [(x1+x2)/2, (y1+y2)/2] const theta = Math.atan2(y2 -y1, x2-x1) -Math.PI / 2 const cx1 = x1 + NODE_WIDTH const cy1 = my - 30 * Math.cos(theta) diff --git a/web/src/components/AutomationDAG/DAGNode.tsx b/web/src/components/AutomationEditor/DAGNode.tsx similarity index 87% rename from web/src/components/AutomationDAG/DAGNode.tsx rename to web/src/components/AutomationEditor/DAGNode.tsx index 5c89b61..494d9e7 100644 --- a/web/src/components/AutomationDAG/DAGNode.tsx +++ b/web/src/components/AutomationEditor/DAGNode.tsx @@ -9,7 +9,6 @@ export interface NodeProp extends Node { isOpen?: boolean; onXClick?: () => void; onOpenClick?: () => void; - color?: Node['color']; } export default ({ loc: [x,y], @@ -19,10 +18,10 @@ export default ({ onXClick=() => {}, onOpenClick=() => {}, isOpen=false, - color='none', + nodeType, }: NodeProp) =>
-
+
X
diff --git a/web/src/components/AutomationEditor/NewNodeModal.tsx b/web/src/components/AutomationEditor/NewNodeModal.tsx deleted file mode 100644 index b6db8d0..0000000 --- a/web/src/components/AutomationEditor/NewNodeModal.tsx +++ /dev/null @@ -1,46 +0,0 @@ -import { useState, useEffect } from "react"; -import StateManager from "../../automation/StateManager"; -import { AutomationNodeType, AutomationNodeTypes, NodeSubType, typeList } from "../../automation/types"; -import InputList from "../InputList"; - -export interface Props { - node_type: T, - onClose: () => void, - onCreate: (newnode: AutomationNodeTypes[T]) => void, -} -export default function NewNodeModal({ - node_type, - onClose, - onCreate, -}: Props) { - const [selectedSubType, setSubType] = useState(typeList[node_type][0]); - const stateManager = new StateManager(node_type, selectedSubType); - - useEffect(() => { - if (stateManager.nodeType !== node_type || stateManager.nodeSubtype !== selectedSubType) { - stateManager.swapBaseState(node_type, selectedSubType) - } - }, [node_type, selectedSubType]); - - return
-
onClose()}>
-
e.preventDefault()}> -
Create {node_type[0].toUpperCase()}{node_type.slice(1)}
-
- setSubType(n as NodeSubType)} - /> - {stateManager.renderOptionList()} -
-
- - -
-
-
-} \ No newline at end of file diff --git a/web/src/components/AutomationEditor/ScriptNode.tsx b/web/src/components/AutomationEditor/ScriptNode.tsx deleted file mode 100644 index c71002e..0000000 --- a/web/src/components/AutomationEditor/ScriptNode.tsx +++ /dev/null @@ -1,21 +0,0 @@ -import StateManager from "../../automation/StateManager"; -import { AutomationAction } from "../../automation/types"; -import { determineNodeTypes, getDescriptionFromAutomationNode } from "../../automation/utils"; -import AutomationNode from "./AutomationNode"; - - -export interface Props { - data: AutomationAction; - onDelete: () => void; -} -export default function ScriptNode({ - data, - onDelete, -}: Props) { - const [nodeType, nodeSubType] = determineNodeTypes(data); - const stateManager = new StateManager(nodeType, nodeSubType, data); - - return - {stateManager.renderOptionList()} - -} \ No newline at end of file diff --git a/web/src/components/AutomationDAG/constants.ts b/web/src/components/AutomationEditor/constants.ts similarity index 100% rename from web/src/components/AutomationDAG/constants.ts rename to web/src/components/AutomationEditor/constants.ts diff --git a/web/src/components/AutomationEditor/index.tsx b/web/src/components/AutomationEditor/index.tsx index 841d0c3..a21a372 100644 --- a/web/src/components/AutomationEditor/index.tsx +++ b/web/src/components/AutomationEditor/index.tsx @@ -1,87 +1,41 @@ -import { useState } from "react"; -import * as types from "../../automation/types"; -import AutomationNode from "./AutomationNode"; -import NewNodeModal from "./NewNodeModal"; -import ScriptNode from "./ScriptNode"; +import { AutomationData } from "~/automations/types"; +import AutoInfoBox from "./AutoInfoBox"; -export interface Props { - automation: types.Automation; +interface Props { + automation: AutomationData; + onUpdate: (auto: AutomationData) => void; } -export default function AutomationEditor({ automation }: Props) { - // state - const [modalState, setModalState] = useState(null); - const [state, setState] = useState(automation); +export default function AutomationEditor({ + automation, + onUpdate, +}: Props) { - // helper state functions - const addNode = (node_type: T, node: types.AutomationNodeTypes[T]) => { - const nodes: types.AutomationNode[] = state[node_type]; - setState({ - ...state, - [node_type]: nodes.concat([node as any]) - }); - } - const removeNode = (node_type: T, node_id: number) => { - const nodes: types.AutomationNode[] = state[node_type]; - setState({ - ...state, - [node_type]: nodes - .slice(0, node_id) - .concat( - nodes.slice( - node_id + 1, - nodes.length - ) - ) - }); - } - // helper element functions - const makeChild = (node_type: T) => { - return [ -
- {state[node_type].map((n, i) => ( - removeNode(node_type, i)} - data={n as any} - /> - ))} -
, - - ]; - } + const graphWidth = 300; + const graphHeight = 300; - return
- -
-
-

Trigger

- {makeChild("trigger")} -

Condition

- {makeChild("condition")} -
-
-

Actions

- {makeChild("action")} -
- -
-
- {modalState && setModalState(null)} - onCreate={node => { - addNode(modalState, node); - setModalState(null); - }} - />} -
+ return
+ + + + + + + onUpdate({...automation, metadata})} + /> + {/* */} + {/* + + + + + */} + +
} \ No newline at end of file diff --git a/web/src/components/AutomationDAG/types.ts b/web/src/components/AutomationEditor/types.ts similarity index 74% rename from web/src/components/AutomationDAG/types.ts rename to web/src/components/AutomationEditor/types.ts index ff61f9a..8f66ff9 100644 --- a/web/src/components/AutomationDAG/types.ts +++ b/web/src/components/AutomationEditor/types.ts @@ -1,10 +1,11 @@ +import { AutomationNodeTypes } from "../../automations/types"; export type Point = [number, number]; export interface Node { loc: Point; text: JSX.Element | string; - color?: 'red' | 'green' | 'blue' | 'none'; + nodeType: AutomationNodeTypes; } export interface Edge { from: string; @@ -16,6 +17,3 @@ export interface DAG { nodes: Record; edges: Array; } -export interface NormalizedDag extends DAG { - // overflow: boolean; -} \ No newline at end of file diff --git a/web/src/components/Inputs/InputTextArea.tsx b/web/src/components/Inputs/InputTextArea.tsx new file mode 100644 index 0000000..38a9f66 --- /dev/null +++ b/web/src/components/Inputs/InputTextArea.tsx @@ -0,0 +1,37 @@ +import { useToolTip } from "~/tooltip/context"; +import InputWrapper from "./InputWrapper"; + +export interface Props { + textBoxFor?: string; + label: string; + value: string; + onChange: (v: string) => void; + additionalTooltipFilters?: Record; +} +export default function InputTextArea({ + label, + textBoxFor, + value="", + onChange, + additionalTooltipFilters={}, +}: Props) { + const tooltip = useToolTip(); + return +