latest work

This commit is contained in:
ari committed 2022-06-12 16:42:44 -04:00
1 parent 651ba865b1
commit 240f8faef7
13 files changed
+323 -57

No files matched your search

@@ -0,0 +1,33 @@
.node-editor--action-wait {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.node-editor--action-wait .for {
flex: 2;
}
.node-editor--action-wait .for .input-time--inner {
justify-content: center;
}
.node-editor--action-wait .template {
margin: 0;
flex: 2;
}
.node-editor--action-wait .triggers {
width: 100%;
}
.node-editor--action-wait .wait-type {
flex: 1;
}
.node-editor--action-wait .wait-options {
width: 100%;
display: flex;
justify-content: space-evenly;
align-items: center;
}
@@ -1,41 +1,86 @@
import "./ActionWait.css";
import InputBoolean from "components/Inputs/InputBoolean";
import { InputList } from "components/Inputs/InputList";
import InputNumber from "components/Inputs/InputNumber";
import InputText from "components/Inputs/InputText";
import { WaitAction } from "types/automations/actions";
import { InputTime } from "components/Inputs/InputTime";
import { useState } from "react";
import { DelayAction, WaitAction } from "types/automations/actions";
import { OptionManager, updateActionData } from './OptionManager';
import { getNodeSubType } from 'utils/automations';
import InputYaml from "components/Inputs/InputYaml";
type ThisAction = WaitAction | DelayAction;
type Option = 'delay' | 'template' | 'trigger'
const getCurrentOption = (node: ThisAction): Option => {
if (getNodeSubType(node) === 'delay') {
return 'delay'
} else {
return 'template'
}
}
export const ActionWaitState: OptionManager<WaitAction> = {
export const ActionWaitState: OptionManager<ThisAction> = {
defaultState: () => ({
alias: "Wait",
wait_template: "",
// timeout: 0,
continue_on_timeout: false,
delay: "",
}),
isReady: ({
alias, wait_template
}) => (
alias !== '' &&
wait_template !== ""
),
renderOptionList: (state, setState) => {
isReady: () => true,
Component: ({ state, setState }) => {
// state
const update = updateActionData(state, setState);
return <>
<InputText
textBoxFor="template"
label="Template"
value={state.wait_template}
onChange={wait_template => update({ wait_template })}
/>
{/* <InputNumber
label="Timeout"
value={state.timeout ?? 0}
onChange={(timeout = 0) => update({ timeout })}
/> */}
<InputBoolean
label="Continue on Timeout"
value={state.continue_on_timeout ?? false}
onChange={continue_on_timeout => update({ continue_on_timeout })}
/>
</>
const [current, _setCurrent] = useState(getCurrentOption(state));
const setCurrent = (opt: Option) => {
_setCurrent(opt)
}
// determine options
let option: JSX.Element = <></>;
if (current === "delay") {
option = <>
<InputTime className="for" label="for" value={(state as any).delay} onChange={delay => update({ delay })} />
</>
} else if (current === 'template') {
state = state as WaitAction;
option = <>
<InputText
label="Template"
className="template"
value={state.wait_template ?? ""}
onChange={wait_template => update({ wait_template })}
/>
</>
} else if (current === 'trigger') {
state = state as WaitAction;
option = <>
<InputYaml
className="triggers"
label="Triggers"
value={state.wait_for_trigger ?? []}
onChange={wait_for_trigger => update({ wait_for_trigger })}
/>
</>
}
// render
return <div className="node-editor--action-wait">
<InputList
className="wait-type"
label="Wait Type" prettyOptionLabels
options={["template", 'trigger', "delay"]}
current={current} onChange={setCurrent} />
{option}
{current !== 'delay' ? <div className="wait-options">
<InputNumber
label="Timeout"
value={(state as any).timeout ? Number((state as any).timeout) : 0}
onChange={(timeout = 0) => update({ timeout: String(timeout) })}
/>
<InputBoolean
label="Continue on Timeout"
value={(state as any).continue_on_timeout ?? false}
onChange={continue_on_timeout => update({ continue_on_timeout })}
/>
</div> : <></>}
</div>
}
}
@@ -1,11 +1,19 @@
import { HAEntitiesState } from 'haService';
import { FC } from 'react';
import { AutomationAction } from 'types/automations/actions';
import { AutomationCondition } from 'types/automations/conditions';
export interface OptionManager<T> {
export type OptionManager<T> = {
defaultState(): T;
isReady(s: T): boolean;
} & ({
renderOptionList(s: T, set: (s: T) => void, entities: HAEntitiesState): JSX.Element;
}
} | {
Component: FC<{
state: T,
setState: (s: T) => void,
entities: HAEntitiesState,
}>
})
export const updateConditionData = <T extends AutomationCondition>(
@@ -13,17 +21,17 @@ export const updateConditionData = <T extends AutomationCondition>(
) => (
state: T,
data: Partial<T>,
) => setState({
...state,
...data,
})
) => setState({
...state,
...data,
})
export const updateActionData = <T extends AutomationAction>(
state: T,
setState: (s: T) => void
) => (
data: Partial<T>,
) => setState({
...state,
...data,
})
) => setState({
...state,
...data,
})
@@ -1,4 +1,4 @@
import { Dispatch, SetStateAction, useEffect, useState } from 'react';
import { Dispatch, SetStateAction, useEffect, useState, Component } from 'react';
import { AutomationNode, AutomationNodeTypes } from 'types/automations';
import { AutomationNodeSubtype } from 'types/automations';
import { getOptionManager } from './getOptionManager';
@@ -108,7 +108,9 @@ export const useEditorNodeState = (originalNode: AutomationNode, isErrored: bool
}
return <>
{aliasEditor}
{optionManager.renderOptionList(currentNode, setState, entities)}
{('renderOptionList' in optionManager) ?
optionManager.renderOptionList(currentNode, setState, entities) :
<optionManager.Component state={currentNode} setState={setState} entities={entities} />}
</>;
},
isReady() {
@@ -95,3 +95,12 @@ ServiceExample.args = {
"service": "",
},
}
export const DelayExample = Action.bind({})
DelayExample.args = {
...DelayExample.args,
node: {
"wait_template": "",
},
}