moved old state handler code
This commit is contained in:
1 parent
d0bc01d2f1
commit
54e628481c
19 files changed
+642
-55
No files matched your search
@@ -21,7 +21,6 @@ const commonInputStyle = (theme: AppTheme) => ({
|
|||||||
"&:disabled": {
|
"&:disabled": {
|
||||||
backgroundColor: '#403f3f',
|
backgroundColor: '#403f3f',
|
||||||
borderColor: '#403f3f',
|
borderColor: '#403f3f',
|
||||||
color: '#403f3faa',
|
|
||||||
cursor: 'not-allowed',
|
cursor: 'not-allowed',
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
import InputYaml from "components/Inputs/InputYaml";
|
||||||
|
import InputText from "components/Inputs/InputText";
|
||||||
|
import { ChooseAction } from "types/automations/actions";
|
||||||
|
import { BaseOptionManager, updateActionData } from './BaseOptionManager';
|
||||||
|
|
||||||
|
|
||||||
|
export default class ActionChooseState extends BaseOptionManager<ChooseAction> {
|
||||||
|
defaultState = () => ({
|
||||||
|
$smType: 'action',
|
||||||
|
action: 'choose',
|
||||||
|
action_data: {
|
||||||
|
alias: "Choose",
|
||||||
|
default: [],
|
||||||
|
choose: [],
|
||||||
|
}
|
||||||
|
} as ChooseAction)
|
||||||
|
isReady(state: ChooseAction): boolean {
|
||||||
|
return state.action_data.alias !== ''
|
||||||
|
}
|
||||||
|
renderOptionList(state: ChooseAction): JSX.Element {
|
||||||
|
const update = updateActionData(state, this.setState);
|
||||||
|
return <div className="state-manager-options">
|
||||||
|
<InputText
|
||||||
|
label="Description"
|
||||||
|
value={state.action_data.alias ?? ""}
|
||||||
|
onChange={alias => update({ alias })}
|
||||||
|
/>
|
||||||
|
<InputYaml
|
||||||
|
label="Choose Sequence"
|
||||||
|
value={state.action_data.choose}
|
||||||
|
onChange={choose => update({ choose })}
|
||||||
|
/>
|
||||||
|
<InputYaml
|
||||||
|
label="Default Sequence"
|
||||||
|
value={state.action_data.default}
|
||||||
|
onChange={d => update({ default: d })}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import InputText from "components/Inputs/InputText";
|
||||||
|
import { DeviceAction } from "types/automations/actions";
|
||||||
|
import { BaseOptionManager, updateActionData } from './BaseOptionManager';
|
||||||
|
|
||||||
|
|
||||||
|
export default class ActionDeviceState extends BaseOptionManager<DeviceAction> {
|
||||||
|
defaultState = () => ({
|
||||||
|
$smType: 'action',
|
||||||
|
action: 'device',
|
||||||
|
action_data: {
|
||||||
|
alias: "Device",
|
||||||
|
type: "",
|
||||||
|
entity_id: "",
|
||||||
|
device_id: "",
|
||||||
|
domain: "",
|
||||||
|
}
|
||||||
|
} as DeviceAction)
|
||||||
|
isReady(state: DeviceAction): boolean {
|
||||||
|
return state.action_data.alias !== '' &&
|
||||||
|
state.action_data.type !== "" &&
|
||||||
|
state.action_data.entity_id !== "" &&
|
||||||
|
state.action_data.device_id !== "" &&
|
||||||
|
state.action_data.domain !== ""
|
||||||
|
}
|
||||||
|
renderOptionList(state: DeviceAction): JSX.Element {
|
||||||
|
const update = updateActionData(state, this.setState);
|
||||||
|
return <div className="state-manager-options">
|
||||||
|
<InputText
|
||||||
|
label="Description"
|
||||||
|
value={state.action_data.alias ?? ""}
|
||||||
|
onChange={alias => update({ alias })}
|
||||||
|
/>
|
||||||
|
<InputText
|
||||||
|
textBoxFor="device_id"
|
||||||
|
label="Device"
|
||||||
|
value={state.action_data.device_id}
|
||||||
|
onChange={device_id => update({ device_id })}
|
||||||
|
/>
|
||||||
|
<InputText
|
||||||
|
textBoxFor="type"
|
||||||
|
label="Action"
|
||||||
|
value={state.action_data.type}
|
||||||
|
onChange={type => update({ type })}
|
||||||
|
/>
|
||||||
|
<InputText
|
||||||
|
textBoxFor="entity_id"
|
||||||
|
label="Entity ID"
|
||||||
|
value={state.action_data.entity_id ?? ""}
|
||||||
|
onChange={entity_id => update({ entity_id, domain: entity_id.split('.')[0] })}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
import InputText from "components/Inputs/InputText";
|
||||||
|
import { ServiceAction } from "types/automations/actions";
|
||||||
|
import { BaseOptionManager, updateActionData } from './BaseOptionManager';
|
||||||
|
|
||||||
|
|
||||||
|
export default class ActionCallServiceState extends BaseOptionManager<ServiceAction> {
|
||||||
|
defaultState = () => ({
|
||||||
|
$smType: 'action',
|
||||||
|
action: 'service',
|
||||||
|
action_data: {
|
||||||
|
service: "",
|
||||||
|
target: "",
|
||||||
|
data: {},
|
||||||
|
}
|
||||||
|
} as ServiceAction)
|
||||||
|
isReady(state: ServiceAction): boolean {
|
||||||
|
const { service, alias, target } = state.action_data;
|
||||||
|
return service !== '' &&
|
||||||
|
alias !== '' &&
|
||||||
|
target !== ''
|
||||||
|
}
|
||||||
|
renderOptionList(state: ServiceAction): JSX.Element {
|
||||||
|
const { service, alias = "", target } = state.action_data;
|
||||||
|
const update = updateActionData(state, this.setState);
|
||||||
|
return <div className="state-manager-options">
|
||||||
|
<InputText
|
||||||
|
label="Description"
|
||||||
|
value={alias}
|
||||||
|
onChange={alias => update({ alias })}
|
||||||
|
/>
|
||||||
|
<InputText
|
||||||
|
textBoxFor="service"
|
||||||
|
label="Service"
|
||||||
|
value={service}
|
||||||
|
onChange={service => update({ service })}
|
||||||
|
/>
|
||||||
|
<InputText
|
||||||
|
label="Target"
|
||||||
|
value={target}
|
||||||
|
onChange={target => update({ target })}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
import { AutomationAction } from 'types/automations/actions';
|
||||||
|
import { AutomationCondition } from 'types/automations/conditions';
|
||||||
|
export abstract class BaseOptionManager<T> {
|
||||||
|
abstract defaultState(): T ;
|
||||||
|
abstract isReady(s: T): boolean;
|
||||||
|
abstract renderOptionList(s: T): JSX.Element;
|
||||||
|
|
||||||
|
constructor(public setState: (s: T) => void) { }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export const updateConditionData = <T extends AutomationCondition>(
|
||||||
|
setState: (s: T) => void
|
||||||
|
) => (
|
||||||
|
state: T,
|
||||||
|
data: Partial<T['condition_data']>,
|
||||||
|
) => setState({
|
||||||
|
...state,
|
||||||
|
condition_data: {
|
||||||
|
...state.condition_data,
|
||||||
|
...data,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
export const updateActionData = <T extends AutomationAction>(
|
||||||
|
state: T,
|
||||||
|
setState: (s: T) => void
|
||||||
|
) => (
|
||||||
|
data: Partial<T['action_data']>,
|
||||||
|
) => setState({
|
||||||
|
...state,
|
||||||
|
action_data: {
|
||||||
|
...state.action_data,
|
||||||
|
...data,
|
||||||
|
},
|
||||||
|
})
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import InputYaml from "components/Inputs/InputYaml";
|
||||||
|
import InputText from "components/Inputs/InputText";
|
||||||
|
import { BaseOptionManager } from "./BaseOptionManager";
|
||||||
|
import { LogicCondition } from "types/automations/conditions";
|
||||||
|
import InputList from "components/Inputs/InputList";
|
||||||
|
|
||||||
|
|
||||||
|
export default class ConditionLogicState extends BaseOptionManager<LogicCondition> {
|
||||||
|
defaultState = () => ({
|
||||||
|
$smType: 'condition',
|
||||||
|
condition: 'and',
|
||||||
|
condition_data: {
|
||||||
|
alias: "",
|
||||||
|
conditions: [],
|
||||||
|
}
|
||||||
|
} as LogicCondition)
|
||||||
|
isReady(state: LogicCondition): boolean {
|
||||||
|
const { alias, conditions } = state.condition_data;
|
||||||
|
return alias !== '' &&
|
||||||
|
conditions?.length > 0;
|
||||||
|
}
|
||||||
|
renderOptionList(state: LogicCondition): JSX.Element {
|
||||||
|
const { alias = "", conditions } = state.condition_data;
|
||||||
|
return <div className="state-manager-options">
|
||||||
|
<InputText
|
||||||
|
label="Description"
|
||||||
|
value={alias}
|
||||||
|
onChange={a => this.setState({
|
||||||
|
...state, condition_data: {
|
||||||
|
alias: a, conditions
|
||||||
|
}
|
||||||
|
})}
|
||||||
|
/>
|
||||||
|
<InputList
|
||||||
|
label="Condition"
|
||||||
|
current={state.condition}
|
||||||
|
options={['and', 'or', 'not']}
|
||||||
|
onChange={(condition: any) => this.setState({ ...state, condition })}
|
||||||
|
/>
|
||||||
|
<InputYaml
|
||||||
|
label="Sequence"
|
||||||
|
value={conditions}
|
||||||
|
onChange={c => this.setState({
|
||||||
|
...state, condition_data: {
|
||||||
|
alias, conditions: c
|
||||||
|
}
|
||||||
|
})}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
import InputText from "components/Inputs/InputText";
|
||||||
|
import { BaseOptionManager, updateConditionData } from './BaseOptionManager';
|
||||||
|
import { NumericCondition } from "types/automations/conditions";
|
||||||
|
|
||||||
|
|
||||||
|
export default class ConditionNumStateState extends BaseOptionManager<NumericCondition> {
|
||||||
|
defaultState = () => ({
|
||||||
|
$smType: 'condition',
|
||||||
|
condition_data: {
|
||||||
|
alias: "",
|
||||||
|
entity_id: '',
|
||||||
|
// above: '',
|
||||||
|
// below: '',
|
||||||
|
// attribute: '',
|
||||||
|
value_template: '',
|
||||||
|
},
|
||||||
|
condition: 'numeric_state',
|
||||||
|
} as NumericCondition)
|
||||||
|
isReady(state: NumericCondition): boolean {
|
||||||
|
const { alias, entity_id, value_template } = state.condition_data;
|
||||||
|
return alias !== '' &&
|
||||||
|
entity_id !== '' &&
|
||||||
|
value_template !== '';
|
||||||
|
}
|
||||||
|
renderOptionList(state: NumericCondition): JSX.Element {
|
||||||
|
const { alias = "", entity_id, value_template } = state.condition_data;
|
||||||
|
return <div className="state-manager-options">
|
||||||
|
<InputText
|
||||||
|
label="Description"
|
||||||
|
value={alias}
|
||||||
|
onChange={alias => updateConditionData(this.setState)(state, {
|
||||||
|
alias
|
||||||
|
})}
|
||||||
|
/>
|
||||||
|
<InputText
|
||||||
|
textBoxFor="entity_id"
|
||||||
|
label="Entity ID"
|
||||||
|
value={entity_id}
|
||||||
|
onChange={entity_id => updateConditionData(this.setState)(state, {
|
||||||
|
entity_id
|
||||||
|
})}
|
||||||
|
/>
|
||||||
|
<InputText
|
||||||
|
label="Template"
|
||||||
|
value={value_template ?? ""}
|
||||||
|
onChange={value_template => updateConditionData(this.setState)(state, {
|
||||||
|
value_template
|
||||||
|
})}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import { BaseOptionManager } from "./BaseOptionManager";
|
||||||
|
|
||||||
|
|
||||||
|
export default class Generic extends BaseOptionManager<any> {
|
||||||
|
defaultState = () => ({})
|
||||||
|
isReady(state: any): boolean {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
renderOptionList(state: any): JSX.Element {
|
||||||
|
return <div className="state-manager-options">
|
||||||
|
<b>THIS IS NOT SUPPORTED YET!</b>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
import InputBoolean from "../../components/InputBoolean";
|
||||||
|
import InputYaml from "../../components/InputYaml";
|
||||||
|
import InputNumber from "../../components/InputNumber";
|
||||||
|
import InputText from "../../components/InputText";
|
||||||
|
import { FireEventScript } from "../types/script";
|
||||||
|
import { BaseOptionManager } from "./BaseOptionManager";
|
||||||
|
|
||||||
|
|
||||||
|
export default class ScriptEventState extends BaseOptionManager<FireEventScript> {
|
||||||
|
defaultState = () => ({
|
||||||
|
alias: "Fire Event",
|
||||||
|
event: "",
|
||||||
|
event_data: {},
|
||||||
|
})
|
||||||
|
isReady(state: FireEventScript): boolean {
|
||||||
|
return state.alias !== '' &&
|
||||||
|
state.event !== ""
|
||||||
|
}
|
||||||
|
renderOptionList(state: FireEventScript): JSX.Element {
|
||||||
|
return <div className="state-manager-options">
|
||||||
|
<InputText
|
||||||
|
label="Description"
|
||||||
|
value={state.alias}
|
||||||
|
onChange={alias => this.setState({ ...state, alias })}
|
||||||
|
/>
|
||||||
|
<InputText
|
||||||
|
textBoxFor="event"
|
||||||
|
label="Event"
|
||||||
|
value={state.event}
|
||||||
|
onChange={event => this.setState({ ...state, event })}
|
||||||
|
/>
|
||||||
|
<InputYaml
|
||||||
|
label="Event Data"
|
||||||
|
value={state.event_data}
|
||||||
|
onChange={event_data => this.setState({ ...state, event_data })}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
import InputNumber from "../../components/InputNumber";
|
||||||
|
import InputText from "../../components/InputText";
|
||||||
|
import InputYaml from "../../components/InputYaml";
|
||||||
|
import { RepeatScript } from "../types/script";
|
||||||
|
import { BaseOptionManager } from "./BaseOptionManager";
|
||||||
|
|
||||||
|
|
||||||
|
export default class ScriptRepeatState extends BaseOptionManager<RepeatScript> {
|
||||||
|
defaultState = () => ({
|
||||||
|
alias: "Repeat",
|
||||||
|
repeat: {
|
||||||
|
count: 1,
|
||||||
|
sequence: [],
|
||||||
|
}
|
||||||
|
})
|
||||||
|
isReady(state: RepeatScript): boolean {
|
||||||
|
return state.alias !== '' &&
|
||||||
|
state?.repeat?.sequence.length > 0
|
||||||
|
}
|
||||||
|
renderOptionList(state: RepeatScript): JSX.Element {
|
||||||
|
return <div className="state-manager-options">
|
||||||
|
<InputText
|
||||||
|
label="Description"
|
||||||
|
value={state.alias}
|
||||||
|
onChange={alias => this.setState({ ...state, alias })}
|
||||||
|
/>
|
||||||
|
<InputNumber
|
||||||
|
label="How many times?"
|
||||||
|
value={state?.repeat?.count}
|
||||||
|
onChange={count => this.setState({ ...state, repeat: { ...state.repeat, count } })}
|
||||||
|
/>
|
||||||
|
<InputYaml
|
||||||
|
label="Sequence"
|
||||||
|
value={state?.repeat?.sequence ?? {}}
|
||||||
|
onChange={sequence => this.setState({ ...state, repeat: { ...state.repeat, sequence } })}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
import InputBoolean from "../../components/InputBoolean";
|
||||||
|
import InputNumber from "../../components/InputNumber";
|
||||||
|
import InputText from "../../components/InputText";
|
||||||
|
import { WaitScript } from "../types/script";
|
||||||
|
import { BaseOptionManager } from "./BaseOptionManager";
|
||||||
|
|
||||||
|
|
||||||
|
export default class ScriptWaitState extends BaseOptionManager<WaitScript> {
|
||||||
|
defaultState = () => ({
|
||||||
|
alias: "Wait",
|
||||||
|
wait_template: "",
|
||||||
|
timeout: 0,
|
||||||
|
continue_on_timeout: false,
|
||||||
|
})
|
||||||
|
isReady(state: WaitScript): boolean {
|
||||||
|
return state.alias !== '' &&
|
||||||
|
state.wait_template !== ""
|
||||||
|
}
|
||||||
|
renderOptionList(state: WaitScript): JSX.Element {
|
||||||
|
return <div className="state-manager-options">
|
||||||
|
<InputText
|
||||||
|
label="Description"
|
||||||
|
value={state.alias}
|
||||||
|
onChange={alias => this.setState({ ...state, alias })}
|
||||||
|
/>
|
||||||
|
<InputText
|
||||||
|
textBoxFor="template"
|
||||||
|
label="Template"
|
||||||
|
value={state.wait_template}
|
||||||
|
onChange={wait_template => this.setState({ ...state, wait_template })}
|
||||||
|
/>
|
||||||
|
<InputNumber
|
||||||
|
label="Timeout"
|
||||||
|
value={state.timeout ?? 0}
|
||||||
|
onChange={timeout => this.setState({ ...state, timeout })}
|
||||||
|
/>
|
||||||
|
<InputBoolean
|
||||||
|
label="Continue on Timeout"
|
||||||
|
value={state.continue_on_timeout ?? false}
|
||||||
|
onChange={continue_on_timeout => this.setState({ ...state, continue_on_timeout })}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
import { AutomationNodeTypes, AutomationNodeSubtype, AutomationNode } from "types/automations"
|
||||||
|
import ActionChooseState from "./ActionChoose";
|
||||||
|
import ActionDeviceState from "./ActionDevice";
|
||||||
|
import ActionCallServiceState from "./ActionService"
|
||||||
|
import { BaseOptionManager } from "./BaseOptionManager";
|
||||||
|
import Generic from "./Generic";
|
||||||
|
|
||||||
|
|
||||||
|
export const getOptionManager = <T extends AutomationNodeTypes>(
|
||||||
|
nodeType: T,
|
||||||
|
nodeSubtype: AutomationNodeSubtype<T>,
|
||||||
|
) : any => {
|
||||||
|
if ( nodeType === 'action') {
|
||||||
|
switch (nodeSubtype) {
|
||||||
|
case 'service':
|
||||||
|
return ActionCallServiceState;
|
||||||
|
// case "Repeat":
|
||||||
|
// return ScriptRepeatState;
|
||||||
|
// case "Wait":
|
||||||
|
// return ScriptWaitState;
|
||||||
|
// case "Event":
|
||||||
|
// return ScriptEventState;
|
||||||
|
case "device":
|
||||||
|
return ActionDeviceState;
|
||||||
|
case "choose":
|
||||||
|
return ActionChooseState;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// if (nodeType === 'condition') {
|
||||||
|
// switch (nodeSubtype) {
|
||||||
|
// case 'Logic':
|
||||||
|
// return ConditionLogicState
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
return Generic
|
||||||
|
}
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
import { useState } from 'react';
|
||||||
|
import { AutomationNode, AutomationNodeTypes } from 'types/automations';
|
||||||
|
import { AutomationNodeSubtype } from 'types/automations';
|
||||||
|
import { BaseOptionManager } from './BaseOptionManager';
|
||||||
|
import { getOptionManager } from './getOptionManager';
|
||||||
|
|
||||||
|
|
||||||
|
export const useEditorNodeState = (node: AutomationNode) => {
|
||||||
|
const [state, setState] = useState(node);
|
||||||
|
const nodeType: AutomationNodeTypes = state.$smType ?? 'trigger';
|
||||||
|
const subType: AutomationNodeSubtype =
|
||||||
|
state.$smType === 'action' ? state.action :
|
||||||
|
state.$smType === 'condition' ? state.condition :
|
||||||
|
state.platform;
|
||||||
|
const optionManager: BaseOptionManager<any> = new (getOptionManager(nodeType, subType as any))(setState);
|
||||||
|
return {
|
||||||
|
nodeType,
|
||||||
|
subType,
|
||||||
|
renderOptionList() {
|
||||||
|
return optionManager.renderOptionList(state);
|
||||||
|
},
|
||||||
|
isReady() {
|
||||||
|
return optionManager.isReady(state);
|
||||||
|
},
|
||||||
|
get subTypes(): AutomationNodeSubtype<typeof nodeType>[] {
|
||||||
|
switch (nodeType) {
|
||||||
|
case 'action':
|
||||||
|
return [
|
||||||
|
"service",
|
||||||
|
"repeat",
|
||||||
|
"wait",
|
||||||
|
"device",
|
||||||
|
"choose",
|
||||||
|
]
|
||||||
|
case "condition":
|
||||||
|
return [
|
||||||
|
'or',
|
||||||
|
'and',
|
||||||
|
'not',
|
||||||
|
'numeric_state',
|
||||||
|
'state',
|
||||||
|
'template',
|
||||||
|
'time',
|
||||||
|
'trigger',
|
||||||
|
'zone',
|
||||||
|
]
|
||||||
|
case 'trigger':
|
||||||
|
return [
|
||||||
|
'event',
|
||||||
|
'homeassistant',
|
||||||
|
'mqtt',
|
||||||
|
'numeric_state',
|
||||||
|
'state',
|
||||||
|
'tag',
|
||||||
|
'template',
|
||||||
|
'time',
|
||||||
|
'time_pattern',
|
||||||
|
'webhook',
|
||||||
|
'zone',
|
||||||
|
'device',
|
||||||
|
]
|
||||||
|
default:
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
setNodeType(newType: AutomationNodeTypes) {
|
||||||
|
if (newType === (node.$smType ?? 'trigger')) {
|
||||||
|
if (node.$smType === 'action') {
|
||||||
|
setState({
|
||||||
|
...state,
|
||||||
|
$smType: 'action',
|
||||||
|
action: node.action
|
||||||
|
} as any)
|
||||||
|
} else if (node.$smType === 'condition') {
|
||||||
|
setState({
|
||||||
|
...state,
|
||||||
|
$smType: 'condition',
|
||||||
|
condition: node.condition
|
||||||
|
} as any)
|
||||||
|
} else {
|
||||||
|
setState({
|
||||||
|
...state,
|
||||||
|
$smType: undefined,
|
||||||
|
platform: node.platform
|
||||||
|
} as any)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
setState({
|
||||||
|
...state,
|
||||||
|
$smType: newType === 'trigger' ? undefined : newType,
|
||||||
|
} as any)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
setSubType(newSubType: AutomationNodeSubtype<typeof nodeType>) {
|
||||||
|
if (state.$smType === 'action') {
|
||||||
|
setState({
|
||||||
|
...state,
|
||||||
|
action: newSubType,
|
||||||
|
} as any)
|
||||||
|
} else if (state.$smType === 'condition') {
|
||||||
|
setState({
|
||||||
|
...state,
|
||||||
|
condition: newSubType,
|
||||||
|
} as any)
|
||||||
|
} else {
|
||||||
|
setState({
|
||||||
|
...state,
|
||||||
|
platform: newSubType,
|
||||||
|
} as any)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { AutomationNodeSubtype, AutomationNodeTypes } from "types/automations";
|
import { AutomationNodeSubtype, AutomationNodeTypes } from "types/automations";
|
||||||
|
import { AutomationNode } from '../../types/automations/index';
|
||||||
|
|
||||||
|
|
||||||
const actionTypes = [
|
const actionTypes = [
|
||||||
@@ -39,15 +40,25 @@ const triggerTypes = [
|
|||||||
|
|
||||||
export const getSubTypeList = <T extends AutomationNodeTypes>(
|
export const getSubTypeList = <T extends AutomationNodeTypes>(
|
||||||
nodeType: T
|
nodeType: T
|
||||||
): AutomationNodeSubtype<T>[] => {
|
): Array<AutomationNodeSubtype<T>| 'condition'> => {
|
||||||
if (nodeType === 'action') {
|
if (nodeType === 'action') {
|
||||||
return actionTypes as any;
|
return actionTypes as any;
|
||||||
} else if (nodeType === 'condition') {
|
} else if (nodeType === 'condition') {
|
||||||
return conditionTypes as any;
|
return conditionTypes as any;
|
||||||
} else if (nodeType === 'trigger') {
|
} else if (nodeType === 'trigger') {
|
||||||
return triggerTypes as any;
|
return triggerTypes as any;
|
||||||
} else if (nodeType === 'sequence') {
|
|
||||||
return actionTypes.concat(conditionTypes) as any;
|
|
||||||
}
|
}
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const getNodeSubType = <T extends AutomationNodeTypes>(
|
||||||
|
node: AutomationNode<T>
|
||||||
|
): AutomationNodeSubtype<T> => {
|
||||||
|
if (node.$smType === 'action') {
|
||||||
|
return node.action as any;
|
||||||
|
} else if(node.$smType === 'condition') {
|
||||||
|
return node.condition as any;
|
||||||
|
} else {
|
||||||
|
return node.platform as any;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,15 +8,30 @@ export default {
|
|||||||
title: 'NodeEditor',
|
title: 'NodeEditor',
|
||||||
component: NodeEditor,
|
component: NodeEditor,
|
||||||
parameters: { actions: { argTypesRegex: '^on.*' } },
|
parameters: { actions: { argTypesRegex: '^on.*' } },
|
||||||
args: {
|
args: {}
|
||||||
nodeType: 'sequence',
|
|
||||||
subType: 'and',
|
|
||||||
}
|
|
||||||
} as ComponentMeta<typeof NodeEditor>
|
} as ComponentMeta<typeof NodeEditor>
|
||||||
|
|
||||||
export const Basic: ComponentStory<typeof NodeEditor> = props => {
|
const Base: ComponentStory<typeof NodeEditor> = props => {
|
||||||
const { classes } = usePageTheme({});
|
const { classes } = usePageTheme({});
|
||||||
return <div className={classes.page}>
|
return <div className={classes.page}>
|
||||||
<NodeEditor {...props} />
|
<NodeEditor {...props} />
|
||||||
</div>
|
</div>
|
||||||
};
|
};
|
||||||
|
export const Action = Base.bind({})
|
||||||
|
Action.args = {
|
||||||
|
...Action.args,
|
||||||
|
node: {
|
||||||
|
$smType: 'action',
|
||||||
|
action: 'service',
|
||||||
|
action_data: {
|
||||||
|
'data': {},
|
||||||
|
'service': '',
|
||||||
|
'target': '',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export const SingleOption = Action.bind({})
|
||||||
|
SingleOption.args = {
|
||||||
|
...Action.args,
|
||||||
|
allowedTypes: ['action'],
|
||||||
|
}
|
||||||
@@ -1,56 +1,50 @@
|
|||||||
import { Button } from "components/Inputs/Button";
|
|
||||||
import { useState, useEffect } from "react";
|
import { FC } from "react";
|
||||||
import { AutomationNodeTypes, AutomationNode, AutomationNodeSubtype } from "types/automations";
|
import { useNodeEditorStyles } from "./styles";
|
||||||
import { useNodeEditorStyles } from './styles';
|
import { AutomationNode, AutomationNodeMapping } from 'types/automations/index';
|
||||||
import { getSubTypeList } from './constants';
|
import { useEditorNodeState } from "./OptionManager";
|
||||||
import InputList from "components/Inputs/InputList";
|
import InputList from "components/Inputs/InputList";
|
||||||
|
import { Button } from "components/Inputs/Button";
|
||||||
|
|
||||||
export interface Props<T extends AutomationNodeTypes> {
|
|
||||||
nodeType: T,
|
|
||||||
subType: AutomationNodeSubtype<T>,
|
|
||||||
onClose: () => void,
|
export interface Props {
|
||||||
onUpdate: (node: AutomationNode[T]) => void,
|
node: AutomationNode
|
||||||
|
allowedTypes?: Array<keyof AutomationNodeMapping>;
|
||||||
}
|
}
|
||||||
export function NodeEditor<T extends AutomationNodeTypes>({
|
|
||||||
nodeType,
|
|
||||||
subType,
|
|
||||||
onClose,
|
|
||||||
onUpdate,
|
|
||||||
}: Props<T>) {
|
|
||||||
const { classes } = useNodeEditorStyles({});
|
|
||||||
const currentSubTypeList = getSubTypeList(nodeType);
|
|
||||||
const [selectedSubType, setSubType] = useState<AutomationNodeSubtype>(subType);
|
|
||||||
// const stateManager = new StateManager(node_type, selectedSubType);
|
|
||||||
|
|
||||||
// useEffect(() => {
|
export const NodeEditor: FC<Props> = ({
|
||||||
// if (stateManager.nodeType !== node_type || stateManager.nodeSubtype !== selectedSubType) {
|
node,
|
||||||
// stateManager.swapBaseState(node_type, selectedSubType)
|
allowedTypes = ["action", "condition", "trigger"]
|
||||||
// }
|
}) => {
|
||||||
// }, [node_type, selectedSubType]);
|
const { classes } = useNodeEditorStyles({});
|
||||||
|
const state = useEditorNodeState(node);
|
||||||
|
|
||||||
return <div className={classes.root}>
|
return <div className={classes.root}>
|
||||||
<div className={classes.background} onClick={() => onClose()}></div>
|
<div className={classes.background} />
|
||||||
<div className={classes.inner} onClick={e => e.preventDefault()}>
|
<div className={classes.inner}>
|
||||||
<div className={classes.title}>
|
<div className={classes.title}>
|
||||||
Create {nodeType[0].toUpperCase()}{nodeType.slice(1)}
|
|
||||||
</div>
|
</div>
|
||||||
<div className={classes.body}>
|
<div className={classes.body}>
|
||||||
|
{allowedTypes.length > 1 ? <InputList
|
||||||
|
label="Type"
|
||||||
|
current={state.nodeType}
|
||||||
|
options={allowedTypes}
|
||||||
|
onChange={state.setNodeType}
|
||||||
|
/> : <span>{state.nodeType}</span>}
|
||||||
<InputList
|
<InputList
|
||||||
label="Type:"
|
label={`${state.nodeType} type`}
|
||||||
options={currentSubTypeList}
|
current={state.subType as any}
|
||||||
current={selectedSubType}
|
options={state.subTypes}
|
||||||
onChange={setSubType}
|
onChange={state.setSubType}
|
||||||
/>
|
/>
|
||||||
{/*{stateManager.renderOptionList()} */}
|
{state.renderOptionList()}
|
||||||
</div>
|
</div>
|
||||||
<div className={classes.footer}>
|
<div className={classes.footer}>
|
||||||
<Button onClick={() => onClose()}>Close</Button>
|
<Button>Close</Button>
|
||||||
<Button
|
<Button disabled={!state.isReady()}>Save</Button>
|
||||||
// onClick={() => onUpdate(stateManager.state)}
|
|
||||||
// disabled={!stateManager.isReady}
|
|
||||||
>
|
|
||||||
Save
|
|
||||||
</Button>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ export const useNodeEditorStyles = createAppUseStyles<{
|
|||||||
color: theme.secondaryAccent,
|
color: theme.secondaryAccent,
|
||||||
},
|
},
|
||||||
body: {
|
body: {
|
||||||
|
overflow: 'auto',
|
||||||
flexGrow: 1
|
flexGrow: 1
|
||||||
},
|
},
|
||||||
footer: {
|
footer: {
|
||||||
|
|||||||
@@ -16,17 +16,21 @@ export interface AutomationData {
|
|||||||
sequence: AutomationSequenceNode[];
|
sequence: AutomationSequenceNode[];
|
||||||
}
|
}
|
||||||
export type AutomationSequenceNode = AutomationAction | AutomationCondition;
|
export type AutomationSequenceNode = AutomationAction | AutomationCondition;
|
||||||
type AutomationNodeMapping = {
|
export type AutomationNodeMapping = {
|
||||||
"trigger": AutomationTrigger;
|
"trigger": AutomationTrigger;
|
||||||
"action": AutomationAction;
|
"action": AutomationAction;
|
||||||
"condition": AutomationCondition;
|
"condition": AutomationCondition;
|
||||||
"sequence": AutomationSequenceNode;
|
|
||||||
}
|
}
|
||||||
export type AutomationNodeTypes = keyof AutomationNodeMapping;
|
export type AutomationNodeTypes = keyof AutomationNodeMapping | undefined;
|
||||||
export type AutomationNode<N extends AutomationNodeTypes = any> = AutomationNodeMapping[N];
|
export type AutomationNode<N extends AutomationNodeTypes = any> =
|
||||||
|
N extends undefined ? AutomationTrigger :
|
||||||
|
N extends 'trigger' ? AutomationTrigger :
|
||||||
|
N extends 'action' ? AutomationAction :
|
||||||
|
N extends 'condition' ? AutomationCondition :
|
||||||
|
never;
|
||||||
export type AutomationNodeSubtype<T extends AutomationNodeTypes = any> =
|
export type AutomationNodeSubtype<T extends AutomationNodeTypes = any> =
|
||||||
|
T extends undefined ? TriggerType :
|
||||||
T extends 'trigger' ? TriggerType :
|
T extends 'trigger' ? TriggerType :
|
||||||
T extends 'action' ? ActionType :
|
T extends 'action' ? ActionType :
|
||||||
T extends 'condtion' ? ConditionType :
|
T extends 'condtion' ? ConditionType :
|
||||||
T extends 'sequence' ? ConditionType | ActionType :
|
|
||||||
never;
|
never;
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
import { AutomationTime } from "./common";
|
import { AutomationTime } from "./common";
|
||||||
|
|
||||||
interface AutomationTriggerBase {
|
interface AutomationTriggerBase {
|
||||||
alias?: string;
|
alias?: string;
|
||||||
|
$smType: undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AutomationTriggerEvent extends AutomationTriggerBase {
|
export interface AutomationTriggerEvent extends AutomationTriggerBase {
|
||||||
|
|||||||
Reference in new issue
Block a user