added state modifier
This commit is contained in:
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 { ServiceScript } from "../types/script";
|
||||||
import { BaseStateManager } from "./BaseManager";
|
import { BaseState } from "./BaseState";
|
||||||
|
|
||||||
|
|
||||||
export class ScriptCallService extends BaseStateManager<ServiceScript> {
|
export default class ScriptCallServiceState extends BaseState<ServiceScript> {
|
||||||
defaultState(): ServiceScript {
|
defaultState = () => ({
|
||||||
return {
|
service: "",
|
||||||
service: "",
|
alias: "Call Service",
|
||||||
alias: "Call Service",
|
target: "",
|
||||||
target: "",
|
data: {},
|
||||||
data: {},
|
})
|
||||||
}
|
isReady(state: ServiceScript): boolean {
|
||||||
|
return state.service !== '' &&
|
||||||
|
state.alias !== '' &&
|
||||||
|
state.target !== ''
|
||||||
}
|
}
|
||||||
isReady(): boolean {
|
renderOptionList(state: ServiceScript): JSX.Element {
|
||||||
return this.state.service !== '' &&
|
|
||||||
this.state.alias !== '' &&
|
|
||||||
this.state.target !== ''
|
|
||||||
}
|
|
||||||
renderOptionList(): JSX.Element {
|
|
||||||
return <div className="state-manager-options">
|
return <div className="state-manager-options">
|
||||||
<label htmlFor="#alias">Description</label>
|
<InputWrapper label="Description">
|
||||||
<input id="alias" value={this.state.alias} onChange={e => {
|
<input value={state.alias} onChange={e => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
this.setState({
|
this.setState({
|
||||||
...this.state,
|
...state,
|
||||||
alias: e.target.value,
|
alias: e.target.value,
|
||||||
})
|
})
|
||||||
}}/>
|
}}/>
|
||||||
<label htmlFor="#service">Service</label>
|
</InputWrapper>
|
||||||
<input id="service" value={this.state.service} onChange={e => {
|
<InputWrapper label="Service">
|
||||||
e.preventDefault();
|
<input value={state.service} onChange={e => {
|
||||||
this.setState({
|
e.preventDefault();
|
||||||
...this.state,
|
this.setState({
|
||||||
service: e.target.value,
|
...state,
|
||||||
})
|
service: e.target.value,
|
||||||
}}/>
|
})
|
||||||
<label htmlFor="#target">Target</label>
|
}}/>
|
||||||
<input id="target" value={this.state.target} onChange={e => {
|
</InputWrapper>
|
||||||
e.preventDefault();
|
<InputWrapper label="Target">
|
||||||
this.setState({
|
<input value={state.target} onChange={e => {
|
||||||
...this.state,
|
e.preventDefault();
|
||||||
target: e.target.value,
|
this.setState({
|
||||||
})
|
...state,
|
||||||
}}/>
|
target: e.target.value,
|
||||||
|
})
|
||||||
|
}}/>
|
||||||
|
</InputWrapper>
|
||||||
</div>
|
</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}`)
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -56,3 +56,15 @@ export type AutomationCondition =
|
|||||||
| ZoneCondition
|
| ZoneCondition
|
||||||
| GenericCondition
|
| GenericCondition
|
||||||
;
|
;
|
||||||
|
export const conditionTypes = [
|
||||||
|
'And' ,
|
||||||
|
'Or' ,
|
||||||
|
'Not',
|
||||||
|
'Numeric State',
|
||||||
|
'State',
|
||||||
|
'Template',
|
||||||
|
'Time',
|
||||||
|
'Trigger',
|
||||||
|
'Zone',
|
||||||
|
] as const;
|
||||||
|
export type ConditionType = typeof conditionTypes[number];
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { AutomationCondition } from "./condition";
|
import { AutomationCondition, ConditionType, conditionTypes } from "./condition";
|
||||||
import { AutomationScript } from "./script";
|
import { AutomationScript, ScriptType, scriptTypes } from "./script";
|
||||||
import { AutomationTrigger } from "./triggers";
|
import { AutomationTrigger, TriggerType, triggerTypes } from "./triggers";
|
||||||
|
|
||||||
export interface Automation {
|
export interface Automation {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -19,3 +19,14 @@ export type AutomationAction = AutomationScript | AutomationCondition;
|
|||||||
export type AutomationNode<T extends AutomationNodeType> =
|
export type AutomationNode<T extends AutomationNodeType> =
|
||||||
AutomationNodeTypes[T][number];
|
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;
|
||||||
@@ -46,3 +46,12 @@ export type AutomationScript =
|
|||||||
| DeviceScript
|
| DeviceScript
|
||||||
| ChooseScript
|
| ChooseScript
|
||||||
;
|
;
|
||||||
|
export const scriptTypes = [
|
||||||
|
'Service',
|
||||||
|
'Repeat',
|
||||||
|
'Wait',
|
||||||
|
'Event',
|
||||||
|
'Device',
|
||||||
|
'Choose'
|
||||||
|
] as const;
|
||||||
|
export type ScriptType = typeof scriptTypes[number];
|
||||||
@@ -87,4 +87,19 @@ export type AutomationTrigger =
|
|||||||
| AutomationTriggerZone
|
| AutomationTriggerZone
|
||||||
| AutomationTriggerDevice
|
| AutomationTriggerDevice
|
||||||
| AutomationTriggerGeneric
|
| 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];
|
||||||
@@ -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,7 +1,7 @@
|
|||||||
import { AutomationNode } from "./types";
|
import { AutomationNode } from "./types";
|
||||||
|
|
||||||
|
|
||||||
export const getNameFromAction = (action: AutomationNode): string => {
|
export const getNameFromAction = (action: AutomationNode<any>): string => {
|
||||||
if(typeof action === 'string') {
|
if(typeof action === 'string') {
|
||||||
return action;
|
return action;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { useState } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import { AutomationNodeType, AutomationNodeTypes } from "../../automation/types";
|
import StateManager from "../../automation/StateManager";
|
||||||
import { typeList } from "../../automation/useNodeState";
|
import { AutomationNodeType, AutomationNodeTypes, NodeSubType, typeList } from "../../automation/types";
|
||||||
import InputList from "../InputList";
|
import InputList from "../InputList";
|
||||||
import InputWrapper from "../InputWrapper";
|
import InputWrapper from "../InputWrapper";
|
||||||
|
|
||||||
@@ -14,7 +14,14 @@ export default function NewNodeModal<T extends AutomationNodeType>({
|
|||||||
onClose,
|
onClose,
|
||||||
onCreate,
|
onCreate,
|
||||||
}: Props<T>) {
|
}: 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">
|
return <div className="new-node-modal">
|
||||||
<div className="new-node-modal--background" onClick={() => onClose()}></div>
|
<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--title">Create {node_type[0].toUpperCase()}{node_type.slice(1)}</div>
|
||||||
<div className="new-node-modal--body">
|
<div className="new-node-modal--body">
|
||||||
<InputWrapper label="Type:">
|
<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>
|
</InputWrapper>
|
||||||
|
{stateManager.renderOptionList()}
|
||||||
</div>
|
</div>
|
||||||
<div className="new-node-modal--footer">
|
<div className="new-node-modal--footer">
|
||||||
<button className="secondary" onClick={() => onClose()}>Close</button>
|
<button className="secondary" onClick={() => onClose()}>Close</button>
|
||||||
|
|||||||
@@ -14,14 +14,14 @@ export default function AutomationEditor({ automation }: Props) {
|
|||||||
|
|
||||||
// helper state functions
|
// helper state functions
|
||||||
const addNode = <T extends types.AutomationNodeType>(node_type: T, node: types.AutomationNodeTypes[T]) => {
|
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({
|
setState({
|
||||||
...state,
|
...state,
|
||||||
[node_type]: nodes.concat([node as any])
|
[node_type]: nodes.concat([node as any])
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
const removeNode = <T extends types.AutomationNodeType>(node_type: T, node_id: number) => {
|
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({
|
setState({
|
||||||
...state,
|
...state,
|
||||||
[node_type]: nodes
|
[node_type]: nodes
|
||||||
@@ -35,8 +35,8 @@ export default function AutomationEditor({ automation }: Props) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
// helper element functions
|
// helper element functions
|
||||||
const makeChild = (node_type: types.AutomationNodeType) => {
|
const makeChild = <T extends types.AutomationNodeType>(node_type: T) => {
|
||||||
const nodes: types.AutomationNode[] = state[node_type];
|
const nodes: types.AutomationNode<T>[] = state[node_type];
|
||||||
return [
|
return [
|
||||||
<div className="automation-box--elm-list" key="list">
|
<div className="automation-box--elm-list" key="list">
|
||||||
{state[node_type].map((n, i) => (
|
{state[node_type].map((n, i) => (
|
||||||
|
|||||||
Reference in new issue
Block a user