initial attempt at visual

This commit is contained in:
Ari Sosnovsky committed 2021-11-02 22:54:38 -04:00
1 parent 126f1df657
commit 08a50051d5
16 files changed
+170 -42

No files matched your search

@@ -8,7 +8,6 @@ export const getBaseState = <T extends AutomationNodeType>(
node_type: T,
node_subtype: NodeSubType<T>
) : any => {
console.log(node_subtype)
if ( node_type === 'action') {
switch (node_subtype) {
case 'Service':
+8 -5
View File
@@ -11,10 +11,16 @@ export default class StateManager {
constructor(
public nodeType: AutomationNodeType,
public nodeSubtype: NodeSubType
public nodeSubtype: NodeSubType,
initialState: any | undefined = undefined,
){
[this.state, this.setState] = useState({});
[this.state, this.setState] = useState(initialState ?? {});
this.baseState = this.swapBaseState(this.nodeType, this.nodeSubtype);
if (!initialState) {
useEffect(() => {
this.setState(this.baseState.defaultState());
}, [this.nodeType, this.nodeSubtype]);
}
}
swapBaseState(nodeType: AutomationNodeType, nodeSubtype: NodeSubType) {
try {
@@ -24,9 +30,6 @@ export default class StateManager {
}
this.nodeType = nodeType;
this.nodeSubtype = nodeSubtype;
useEffect(() => {
this.setState(this.baseState.defaultState());
}, [this.nodeType, this.nodeSubtype]);
return this.baseState;
}
renderOptionList() {
+1
View File
@@ -66,5 +66,6 @@ export const conditionTypes = [
'Time',
'Trigger',
'Zone',
'Generic',
] as const;
export type ConditionType = typeof conditionTypes[number];
+1
View File
@@ -101,5 +101,6 @@ export const triggerTypes = [
'Webhook',
'Zone',
'Device',
'Generic',
] as const;
export type TriggerType = typeof triggerTypes[number];
+75 -1
View File
@@ -1,4 +1,4 @@
import { AutomationNode } from "./types";
import { AutomationNode, AutomationNodeType, NodeSubType } from "./types";
export const getNameFromAction = (action: AutomationNode<any>): string => {
@@ -27,4 +27,78 @@ export const getNameFromAction = (action: AutomationNode<any>): string => {
return 'Choose'
}
return 'n/a'
}
export const determineNodeTypes = (action: AutomationNode<any>) : [AutomationNodeType, NodeSubType] => {
if ('condition' in action) {
switch (action.condition) {
case 'and':
return ['condition', 'And']
case 'or':
return ['condition', 'Or']
case 'not':
return ['condition', 'Not']
case 'numeric_state':
return ['condition', 'Numeric State']
case 'state':
return ['condition', 'State']
case 'template':
return ['condition', 'Template']
case 'trigger':
return ['condition', 'Trigger']
case 'zone':
return ['condition', 'Zone']
default:
return ['condition', 'Generic']
}
}
if ('platform' in action) {
switch (action.platform) {
case 'event':
return ['trigger', 'Event']
case 'homeassistant':
return ['trigger', 'Home Assistant']
case 'mqtt':
return ['trigger', 'MQTT']
case 'numeric_state':
return ['trigger', 'Numeric State']
case 'state':
return ['trigger', 'State']
case 'tag':
return ['trigger', 'Tag']
case 'template':
return ['trigger', 'Template']
case 'time':
return ['trigger', 'Time']
case 'time_pattern':
return ['trigger', 'Time Pattern']
case 'webhook':
return ['trigger', 'Webhook']
case 'zone':
return ['trigger', 'Zone']
default:
return ['trigger', 'Generic']
}
}
if ('service' in action) {
return ["action", "Service"]
}
if ('repeat' in action) {
return ["action", "Repeat"]
}
if ('wait_template' in action) {
return ["action", "Wait"]
}
if ('event' in action) {
return ["action", "Event"]
}
if (('type' in action) && ('device_id' in action)) {
return ["action", "Device"]
}
if ('choose' in action) {
return ["action", "Choose"]
}
throw Error(`Could not determine node type ${JSON.stringify(action)}`)
}
@@ -36,7 +36,7 @@ export default function NewNodeModal<T extends AutomationNodeType>({
</div>
<div className="new-node-modal--footer">
<button onClick={() => onClose()}>Close</button>
<button onClick={() => onCreate({} as any)}
<button onClick={() => onCreate(stateManager.state)}
disabled={!stateManager.isReady}>
Create
</button>
@@ -0,0 +1,21 @@
import StateManager from "../../automation/StateManager";
import { AutomationAction } from "../../automation/types";
import { determineNodeTypes, getNameFromAction } from "../../automation/utils";
import AutomationNode from "../Node";
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 <AutomationNode title={getNameFromAction(data)} onXClick={onDelete}>
{stateManager.renderOptionList()}
</AutomationNode>
}
+16 -15
View File
@@ -3,6 +3,7 @@ import * as types from "../../automation/types";
import { getNameFromAction } from "../../automation/utils";
import AutomationNode from "../Node";
import NewNodeModal from "./NewNodeModal";
import ScriptNode from "./ScriptNode";
export interface Props {
automation: types.Automation;
@@ -36,16 +37,13 @@ export default function AutomationEditor({ automation }: Props) {
}
// helper element functions
const makeChild = <T extends types.AutomationNodeType>(node_type: T) => {
const nodes: types.AutomationNode<T>[] = state[node_type];
return [
<div className="automation-box--elm-list" key="list">
<div className="automation-editor--elm-list" key="list">
{state[node_type].map((n, i) => (
<AutomationNode
<ScriptNode
key={i}
title={getNameFromAction(n)}
showX={false}
initialOpenState={false}
onXClick={() => removeNode(node_type, i)}
onDelete={() => removeNode(node_type, i)}
data={n as any}
/>
))}
</div>,
@@ -63,16 +61,19 @@ export default function AutomationEditor({ automation }: Props) {
<AutomationNode
title={`${automation.alias} (${automation.mode})`}
initialOpenState={true}
showChildren={true}
showX={false}
>
<div className="automation-editor--root">
<h4>Trigger</h4>
{makeChild("trigger")}
<h4>Condition</h4>
{makeChild("condition")}
<h4>Actions</h4>
{makeChild("action")}
<div className="automation-editor--triggers">
<h4>Trigger</h4>
{makeChild("trigger")}
<h4>Condition</h4>
{makeChild("condition")}
</div>
<div className="automation-editor--playground">
<h4>Actions</h4>
{makeChild("action")}
</div>
</div>
</AutomationNode>
{modalState && <NewNodeModal
+4 -10
View File
@@ -2,16 +2,12 @@ import { PropsWithChildren, useState } from "react";
export interface Props {
title: string;
showX?: boolean;
showChildren?: boolean;
initialOpenState?: boolean;
onXClick?: () => void;
}
export default function AutomationNode({
children,
title,
showX=false,
showChildren=false,
initialOpenState=false,
onXClick = () => {},
}: PropsWithChildren<Props>) {
@@ -22,20 +18,18 @@ export default function AutomationNode({
className="automation-node-inner-edge left"
onClick={() => onXClick()}
>
{showX && (
<div className="automation-node-side-button">X</div>
)}
<div className="automation-node-side-button">X</div>
</div>
<div className="automation-node-inner-text">{title}</div>
<div
className="automation-node-inner-edge right"
onClick={() => setOpen(!isOpen)}
>
{showChildren && <div className="automation-node-side-button">{isOpen ? "▲" : "▼"}</div>}
<div className="automation-node-side-button">{isOpen ? "▲" : "▼"}</div>
</div>
</div>
{showChildren&&<div className={`automation-node-children ${isOpen ? "open" : "closed"}`}>
<div className={`automation-node-children ${isOpen ? "open" : "closed"}`}>
{children}
</div>}
</div>
</div>
}
+3 -3
View File
@@ -18,12 +18,12 @@ export default function App() {
<div>
<Switch>
<Route path="/">
{/* <Home /> */}
<NewNodeModal
<Home />
{/* <NewNodeModal
node_type="action"
onClose={()=>{}}
onCreate={()=>{}}
/>
/> */}
</Route>
</Switch>
</div>
+3 -3
View File
@@ -79,9 +79,9 @@ button {
border-style: solid;
border-width: 1px;
padding: 2.5%;
margin-left: 5%;
margin-right: 5%;
padding: 2.5px;
margin-left: 5px;
margin-right: 5px;
border-radius: 5px;
cursor: pointer;
}
@@ -1,17 +1,44 @@
.automation-editor {
width: 100%;
height: 100%;
overflow: hidden;
.automation-node {
flex-direction: column;
justify-content: center;
> &-inner {
min-width: 100%;
}
}
&--root {
display: flex;
flex-direction: row;
width: 100%;
// justify-content: center;
// align-items: center;
}
&--triggers {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
&--playground {
width: 100%;
> .automation-editor--elm-list {
display: flex;
flex-direction: row;
align-items: start;
}
}
button.elm-list {
width: 90%;
}
&--elm-list {
max-height: 300px;
max-height: 20vh;
overflow-y: auto;
overflow-x: hidden;
overflow-x: auto;
padding: 5%;
width: 100%;
}
}
@@ -50,7 +50,7 @@
}
&-children {
min-width: 250px;
padding: 2.5px;
padding: 20px;
&.closed {
height: 0;
@@ -1,6 +1,7 @@
.input-wrapper {
display: flex;
flex-direction: column;
margin-bottom: 8px;
label {
font-size: 12px;
}
@@ -0,0 +1,2 @@
.state-manager-options {
}
+4
View File
@@ -2,3 +2,7 @@
@import "./AutomationEditor.scss";
@import "./NewNodeModal.scss";
@import "./InputWrapper.scss";
* {
transition: all 200ms ease-in;
}