added state modifier

This commit is contained in:
Ari Sosnovsky committed 2021-10-29 12:39:31 -04:00
1 parent 5f486a5f89
commit 32fd7f2dff
14 files changed
+199 -182

No files matched your search

@@ -1,17 +0,0 @@
import { useState } from "react";
export abstract class BaseStateManager<T> {
abstract defaultState(): T ;
abstract isReady(): boolean;
abstract renderOptionList(): JSX.Element;
public state: T;
public setState: (s:T) => void;
constructor(){
[this.state, this.setState] = useState(this.defaultState());
}
}
@@ -0,0 +1,12 @@
import { useState } from "react";
export abstract class BaseState<T> {
abstract defaultState(): T ;
abstract isReady(s: T): boolean;
abstract renderOptionList(s: T): JSX.Element;
constructor(public setState: (s: T) => void) {}
}
@@ -0,0 +1,28 @@
import InputWrapper from "../../components/InputWrapper";
import { ServiceScript } from "../types/script";
import { BaseState } from "./BaseState";
export default class Generic extends BaseState<{alias: string}> {
defaultState = () => ({
alias: "Unknown",
})
isReady(state: ServiceScript): boolean {
return true;
}
renderOptionList(state: ServiceScript): JSX.Element {
return <div className="state-manager-options">
<InputWrapper label="Description">
<input value={state.alias} onChange={e => {
e.preventDefault();
this.setState({
...state,
alias: e.target.value,
})
}}/>
</InputWrapper>
<br/>
<b>THIS IS NOT SUPPORTED YET!</b>
</div>
}
}
@@ -1,47 +1,49 @@
import InputWrapper from "../../components/InputWrapper";
import { ServiceScript } from "../types/script";
import { BaseStateManager } from "./BaseManager";
import { BaseState } from "./BaseState";
export class ScriptCallService extends BaseStateManager<ServiceScript> {
defaultState(): ServiceScript {
return {
service: "",
alias: "Call Service",
target: "",
data: {},
}
export default class ScriptCallServiceState extends BaseState<ServiceScript> {
defaultState = () => ({
service: "",
alias: "Call Service",
target: "",
data: {},
})
isReady(state: ServiceScript): boolean {
return state.service !== '' &&
state.alias !== '' &&
state.target !== ''
}
isReady(): boolean {
return this.state.service !== '' &&
this.state.alias !== '' &&
this.state.target !== ''
}
renderOptionList(): JSX.Element {
renderOptionList(state: ServiceScript): JSX.Element {
return <div className="state-manager-options">
<label htmlFor="#alias">Description</label>
<input id="alias" value={this.state.alias} onChange={e => {
e.preventDefault();
this.setState({
...this.state,
alias: e.target.value,
})
}}/>
<label htmlFor="#service">Service</label>
<input id="service" value={this.state.service} onChange={e => {
e.preventDefault();
this.setState({
...this.state,
service: e.target.value,
})
}}/>
<label htmlFor="#target">Target</label>
<input id="target" value={this.state.target} onChange={e => {
e.preventDefault();
this.setState({
...this.state,
target: e.target.value,
})
}}/>
<InputWrapper label="Description">
<input value={state.alias} onChange={e => {
e.preventDefault();
this.setState({
...state,
alias: e.target.value,
})
}}/>
</InputWrapper>
<InputWrapper label="Service">
<input value={state.service} onChange={e => {
e.preventDefault();
this.setState({
...state,
service: e.target.value,
})
}}/>
</InputWrapper>
<InputWrapper label="Target">
<input value={state.target} onChange={e => {
e.preventDefault();
this.setState({
...state,
target: e.target.value,
})
}}/>
</InputWrapper>
</div>
}
}
@@ -0,0 +1,17 @@
import { AutomationNode, AutomationNodeType, NodeSubType } from "../types"
import { BaseState } from "./BaseState"
import ScriptCallServiceState from "./ScriptService"
export const getBaseState = <T extends AutomationNodeType>(
node_type: T,
node_subtype: NodeSubType<T>
) : any => {
if ( node_type === 'action') {
switch (node_subtype) {
case 'Service':
return ScriptCallServiceState
}
}
throw new Error(`Invalid or Not implemented node type ${node_type} -> ${node_subtype}`)
}
+32
View File
@@ -0,0 +1,32 @@
import { useState } from "react";
import { AutomationNodeType, NodeSubType } from "../types";
import { BaseState } from "./BaseState";
import Generic from "./Generic";
import { getBaseState } from "./getBaseState";
export default class StateManager {
public state: any;
private setState: (s: any) => void;
private baseState: BaseState<any>;
constructor(
public nodeType: AutomationNodeType,
public nodeSubtype: NodeSubType
){
[this.state, this.setState] = useState({});
this.baseState = this.swapBaseState(this.nodeType, this.nodeSubtype);
}
swapBaseState(nodeType: AutomationNodeType, nodeSubtype: NodeSubType) {
try {
this.baseState = new (getBaseState(nodeType, nodeSubtype))(this.setState);
} catch (error) {
this.baseState = new Generic(this.setState);
}
this.nodeType = nodeType;
this.nodeSubtype = nodeSubtype;
return this.baseState;
}
renderOptionList() {
return this.baseState.renderOptionList(this.state)
}
}
+12
View File
@@ -56,3 +56,15 @@ export type AutomationCondition =
| ZoneCondition
| GenericCondition
;
export const conditionTypes = [
'And' ,
'Or' ,
'Not',
'Numeric State',
'State',
'Template',
'Time',
'Trigger',
'Zone',
] as const;
export type ConditionType = typeof conditionTypes[number];
+14 -3
View File
@@ -1,6 +1,6 @@
import { AutomationCondition } from "./condition";
import { AutomationScript } from "./script";
import { AutomationTrigger } from "./triggers";
import { AutomationCondition, ConditionType, conditionTypes } from "./condition";
import { AutomationScript, ScriptType, scriptTypes } from "./script";
import { AutomationTrigger, TriggerType, triggerTypes } from "./triggers";
export interface Automation {
id: string;
@@ -19,3 +19,14 @@ export type AutomationAction = AutomationScript | AutomationCondition;
export type AutomationNode<T extends AutomationNodeType> =
AutomationNodeTypes[T][number];
export type NodeSubType<T=any> =
T extends 'action' ? ScriptType :
T extends 'condition' ? ConditionType :
T extends 'trigger' ? TriggerType :
never;
export const typeList = {
'action': scriptTypes,
'condition': conditionTypes,
'trigger': triggerTypes,
} as const;
+9
View File
@@ -46,3 +46,12 @@ export type AutomationScript =
| DeviceScript
| ChooseScript
;
export const scriptTypes = [
'Service',
'Repeat',
'Wait',
'Event',
'Device',
'Choose'
] as const;
export type ScriptType = typeof scriptTypes[number];
+16 -1
View File
@@ -87,4 +87,19 @@ export type AutomationTrigger =
| AutomationTriggerZone
| AutomationTriggerDevice
| AutomationTriggerGeneric
;
;
export const triggerTypes = [
'Event',
'Home Assistant',
'MQTT',
'Numeric State',
'State',
'Tag',
'Template',
'Time',
'Time Pattern',
'Webhook',
'Zone',
'Device',
] as const;
export type TriggerType = typeof triggerTypes[number];
-112
View File
@@ -1,112 +0,0 @@
import { BaseStateManager } from "./StateManager/BaseManager";
import { ScriptCallService } from "./StateManager/ScriptService";
import { AutomationNode, AutomationNodeType } from "./types";
export const triggerTypes = [
'Event',
'Home Assistant',
'MQTT',
'Numeric State',
'State',
'Tag',
'Template',
'Time',
'Time Pattern',
'Webhook',
'Zone',
'Device',
] as const;
export type TriggerType = typeof triggerTypes[number];
export const conditionTypes = [
'And' ,
'Or' ,
'Not',
'Numeric State',
'State',
'Template',
'Time',
'Trigger',
'Zone',
] as const;
export type ConditionType = typeof conditionTypes[number];
export const scriptTypes = [
'Service',
'Repeat',
'Wait',
'Event',
'Device',
'Choose'
] as const;
export type ScriptType = typeof scriptTypes[number];
export type NodeStateType<T> =
T extends 'action' ? ScriptType :
T extends 'condition' ? ConditionType :
T extends 'trigger' ? TriggerType :
never;
export const typeList = {
'action': scriptTypes,
'condition': conditionTypes,
'trigger': triggerTypes,
} as const;
export const useNewNodeState = <T extends AutomationNodeType>(
node_type: T,
node_subtype: NodeStateType<T>
) : BaseStateManager<AutomationNode<T>> => {
if ( node_type === 'action') {
switch (node_subtype) {
case 'Service':
return new ScriptCallService() as any;
// case 'Repeat':
// return {
// repeat: {
// count: 0,
// sequence: [],
// }
// }
// case 'Wait':
// return {
// wait_template: ""
// }
// case 'Event':
// return {
// event: "",
// event_data: {}
// }
// case "Device":
// return {
// type: "",
// device_id: "",
// entity_id: "",
// domain: "",
// }
// case "Choose":
// return {
// choose: [],
// default: [],
// }
}
}
// else if (node_type === "condition") {
// if (node_subtype in ['And', 'Or', 'Noe']) {
// return {
// condition: node_subtype.toLowerCase(),
// conditions: [],
// }
// } else if (node_subtype === 'Numeric State') {
// return {
// condition: "numeric_state",
// entity_id: ""
// }
// } else if (node_subtype === 'State') {
// return {
// condition: "state",
// entity_id: "",
// state: "",
// }
// }
// }
throw new Error(`Invalid or Not implemented node type ${node_type} -> ${node_subtype}`)
}
+1 -1
View File
@@ -1,7 +1,7 @@
import { AutomationNode } from "./types";
export const getNameFromAction = (action: AutomationNode): string => {
export const getNameFromAction = (action: AutomationNode<any>): string => {
if(typeof action === 'string') {
return action;
}
@@ -1,6 +1,6 @@
import { useState } from "react";
import { AutomationNodeType, AutomationNodeTypes } from "../../automation/types";
import { typeList } from "../../automation/useNodeState";
import { useState, useEffect } from "react";
import StateManager from "../../automation/StateManager";
import { AutomationNodeType, AutomationNodeTypes, NodeSubType, typeList } from "../../automation/types";
import InputList from "../InputList";
import InputWrapper from "../InputWrapper";
@@ -14,7 +14,14 @@ export default function NewNodeModal<T extends AutomationNodeType>({
onClose,
onCreate,
}: Props<T>) {
const [selectedSubType, setSubType] = useState<string>(typeList[node_type][0]);
const [selectedSubType, setSubType] = useState<NodeSubType>(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 <div className="new-node-modal">
<div className="new-node-modal--background" onClick={() => onClose()}></div>
@@ -22,8 +29,9 @@ export default function NewNodeModal<T extends AutomationNodeType>({
<div className="new-node-modal--title">Create {node_type[0].toUpperCase()}{node_type.slice(1)}</div>
<div className="new-node-modal--body">
<InputWrapper label="Type:">
<InputList options={typeList[node_type]} current={selectedSubType} onChange={n => setSubType(n)}/>
<InputList options={typeList[node_type]} current={selectedSubType} onChange={n => setSubType(n as NodeSubType)}/>
</InputWrapper>
{stateManager.renderOptionList()}
</div>
<div className="new-node-modal--footer">
<button className="secondary" onClick={() => onClose()}>Close</button>
@@ -14,14 +14,14 @@ export default function AutomationEditor({ automation }: Props) {
// helper state functions
const addNode = <T extends types.AutomationNodeType>(node_type: T, node: types.AutomationNodeTypes[T]) => {
const nodes: types.AutomationNode[] = state[node_type];
const nodes: types.AutomationNode<T>[] = state[node_type];
setState({
...state,
[node_type]: nodes.concat([node as any])
});
}
const removeNode = <T extends types.AutomationNodeType>(node_type: T, node_id: number) => {
const nodes: types.AutomationNode[] = state[node_type];
const nodes: types.AutomationNode<T>[] = state[node_type];
setState({
...state,
[node_type]: nodes
@@ -35,8 +35,8 @@ export default function AutomationEditor({ automation }: Props) {
});
}
// helper element functions
const makeChild = (node_type: types.AutomationNodeType) => {
const nodes: types.AutomationNode[] = state[node_type];
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">
{state[node_type].map((n, i) => (