diff --git a/web/src/automation/StateManager/getBaseState.ts b/web/src/automation/StateManager/getBaseState.ts index 88982db..4b95cee 100644 --- a/web/src/automation/StateManager/getBaseState.ts +++ b/web/src/automation/StateManager/getBaseState.ts @@ -8,7 +8,6 @@ export const getBaseState = ( node_type: T, node_subtype: NodeSubType ) : any => { - console.log(node_subtype) if ( node_type === 'action') { switch (node_subtype) { case 'Service': diff --git a/web/src/automation/StateManager/index.ts b/web/src/automation/StateManager/index.ts index 2b58f02..fe8639b 100644 --- a/web/src/automation/StateManager/index.ts +++ b/web/src/automation/StateManager/index.ts @@ -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() { diff --git a/web/src/automation/types/condition.ts b/web/src/automation/types/condition.ts index 37c6f9d..0876b2f 100644 --- a/web/src/automation/types/condition.ts +++ b/web/src/automation/types/condition.ts @@ -66,5 +66,6 @@ export const conditionTypes = [ 'Time', 'Trigger', 'Zone', + 'Generic', ] as const; export type ConditionType = typeof conditionTypes[number]; \ No newline at end of file diff --git a/web/src/automation/types/triggers.ts b/web/src/automation/types/triggers.ts index 3e23df3..5f8b832 100644 --- a/web/src/automation/types/triggers.ts +++ b/web/src/automation/types/triggers.ts @@ -101,5 +101,6 @@ export const triggerTypes = [ 'Webhook', 'Zone', 'Device', + 'Generic', ] as const; export type TriggerType = typeof triggerTypes[number]; \ No newline at end of file diff --git a/web/src/automation/utils.ts b/web/src/automation/utils.ts index 4f962d4..c579a75 100644 --- a/web/src/automation/utils.ts +++ b/web/src/automation/utils.ts @@ -1,4 +1,4 @@ -import { AutomationNode } from "./types"; +import { AutomationNode, AutomationNodeType, NodeSubType } from "./types"; export const getNameFromAction = (action: AutomationNode): string => { @@ -27,4 +27,78 @@ export const getNameFromAction = (action: AutomationNode): string => { return 'Choose' } return 'n/a' +} + +export const determineNodeTypes = (action: AutomationNode) : [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)}`) } \ No newline at end of file diff --git a/web/src/components/AutomationEditor/NewNodeModal.tsx b/web/src/components/AutomationEditor/NewNodeModal.tsx index 030765b..b6db8d0 100644 --- a/web/src/components/AutomationEditor/NewNodeModal.tsx +++ b/web/src/components/AutomationEditor/NewNodeModal.tsx @@ -36,7 +36,7 @@ export default function NewNodeModal({
- diff --git a/web/src/components/AutomationEditor/ScriptNode.tsx b/web/src/components/AutomationEditor/ScriptNode.tsx new file mode 100644 index 0000000..d3f2b72 --- /dev/null +++ b/web/src/components/AutomationEditor/ScriptNode.tsx @@ -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 + {stateManager.renderOptionList()} + +} \ No newline at end of file diff --git a/web/src/components/AutomationEditor/index.tsx b/web/src/components/AutomationEditor/index.tsx index 86c1d31..1fd7f55 100644 --- a/web/src/components/AutomationEditor/index.tsx +++ b/web/src/components/AutomationEditor/index.tsx @@ -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 = (node_type: T) => { - const nodes: types.AutomationNode[] = state[node_type]; return [ -
+
{state[node_type].map((n, i) => ( - removeNode(node_type, i)} + onDelete={() => removeNode(node_type, i)} + data={n as any} /> ))}
, @@ -63,16 +61,19 @@ export default function AutomationEditor({ automation }: Props) {
-

Trigger

- {makeChild("trigger")} -

Condition

- {makeChild("condition")} -

Actions

- {makeChild("action")} +
+

Trigger

+ {makeChild("trigger")} +

Condition

+ {makeChild("condition")} +
+
+

Actions

+ {makeChild("action")} +
+
{modalState && void; } export default function AutomationNode({ children, title, - showX=false, - showChildren=false, initialOpenState=false, onXClick = () => {}, }: PropsWithChildren) { @@ -22,20 +18,18 @@ export default function AutomationNode({ className="automation-node-inner-edge left" onClick={() => onXClick()} > - {showX && ( -
X
- )} +
X
{title}
setOpen(!isOpen)} > - {showChildren &&
{isOpen ? "▲" : "▼"}
} +
{isOpen ? "▲" : "▼"}
- {showChildren&&
+
{children} -
} +
} \ No newline at end of file diff --git a/web/src/router.tsx b/web/src/router.tsx index 48da619..3d4becf 100644 --- a/web/src/router.tsx +++ b/web/src/router.tsx @@ -18,12 +18,12 @@ export default function App() {
- {/* */} - + {/* {}} onCreate={()=>{}} - /> + /> */}
diff --git a/web/src/styles/common.scss b/web/src/styles/common.scss index d873451..86ce89d 100644 --- a/web/src/styles/common.scss +++ b/web/src/styles/common.scss @@ -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; } diff --git a/web/src/styles/components/AutomationEditor.scss b/web/src/styles/components/AutomationEditor.scss index 29bbc1d..f7bc99e 100644 --- a/web/src/styles/components/AutomationEditor.scss +++ b/web/src/styles/components/AutomationEditor.scss @@ -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%; } } diff --git a/web/src/styles/components/AutomationNode.scss b/web/src/styles/components/AutomationNode.scss index 3f9db7e..4d23363 100644 --- a/web/src/styles/components/AutomationNode.scss +++ b/web/src/styles/components/AutomationNode.scss @@ -50,7 +50,7 @@ } &-children { min-width: 250px; - padding: 2.5px; + padding: 20px; &.closed { height: 0; diff --git a/web/src/styles/components/InputWrapper.scss b/web/src/styles/components/InputWrapper.scss index 2af5646..7fb01d6 100644 --- a/web/src/styles/components/InputWrapper.scss +++ b/web/src/styles/components/InputWrapper.scss @@ -1,6 +1,7 @@ .input-wrapper { display: flex; flex-direction: column; + margin-bottom: 8px; label { font-size: 12px; } diff --git a/web/src/styles/components/Statemanger.scss b/web/src/styles/components/Statemanger.scss new file mode 100644 index 0000000..bc4e91e --- /dev/null +++ b/web/src/styles/components/Statemanger.scss @@ -0,0 +1,2 @@ +.state-manager-options { +} diff --git a/web/src/styles/components/index.scss b/web/src/styles/components/index.scss index f3c1967..4bf61ca 100644 --- a/web/src/styles/components/index.scss +++ b/web/src/styles/components/index.scss @@ -2,3 +2,7 @@ @import "./AutomationEditor.scss"; @import "./NewNodeModal.scss"; @import "./InputWrapper.scss"; + +* { + transition: all 200ms ease-in; +}