From 240f8faef770309a165409339835bbe48e5d7c2e Mon Sep 17 00:00:00 2001 From: Ari Date: Sun, 12 Jun 2022 16:42:44 -0400 Subject: [PATCH] latest work --- webapp/src/components/Inputs/InputDayTime.tsx | 148 ++++++++++++++++++ webapp/src/components/Inputs/InputList.tsx | 16 +- webapp/src/components/Inputs/InputText.tsx | 4 +- webapp/src/components/Inputs/InputTime.tsx | 5 +- webapp/src/components/Inputs/InputYaml.tsx | 6 +- .../NodeEditor/OptionManager/ActionWait.css | 33 ++++ .../NodeEditor/OptionManager/ActionWait.tsx | 107 +++++++++---- .../NodeEditor/OptionManager/OptionManager.ts | 28 ++-- .../NodeEditor/OptionManager/index.tsx | 6 +- .../components/NodeEditor/index.stories.tsx | 9 ++ webapp/src/types/automations/actions.ts | 3 +- webapp/src/types/validators/actions.ts | 11 +- webapp/src/utils/automations.ts | 4 + 13 files changed, 323 insertions(+), 57 deletions(-) create mode 100644 webapp/src/components/Inputs/InputDayTime.tsx create mode 100644 webapp/src/components/NodeEditor/OptionManager/ActionWait.css diff --git a/webapp/src/components/Inputs/InputDayTime.tsx b/webapp/src/components/Inputs/InputDayTime.tsx new file mode 100644 index 0000000..c4e1e9c --- /dev/null +++ b/webapp/src/components/Inputs/InputDayTime.tsx @@ -0,0 +1,148 @@ +import "./InputTime.css"; +import { useState, useEffect, FC } from "react"; +import TextField from '@mui/material/TextField'; +import EditIcon from '@mui/icons-material/Edit'; +import CloseIcon from "@mui/icons-material/Close"; +import IconButton from "@mui/material/IconButton" +import { AutomationTime, AutomationTimeObject } from "types/automations/common"; +import { convertAutomationTimeToTimeObject, convertObjectToAutomationTimeString, milisecondsToObject } from "utils/time"; +import { convertStringToAutomationTimeObject, convertAutomationTimeToTimeString } from 'utils/time'; +import { prettyName } from "utils/formatting"; +import FormControl from "@mui/material/FormControl"; +import InputLabel from '@mui/material/InputLabel'; +import Input from "@mui/material/Input"; +import FormHelperText from '@mui/material/FormHelperText'; + +// constants +const fields: Array = [ + 'hours', + 'minutes', + 'seconds', + 'milliseconds' +] +export interface Props { + label: string; + value?: AutomationTime; + onChange: (v?: AutomationTimeObject) => void; +} +export const InputTime: FC = (props) => { + + + // state + const [disabled, setDisabled] = useState(!props.value) + const [displayValue, setDisplayValue] = useState(convertAutomationTimeToTimeObject(props.value)) + + // process display input + const updateOne = (k: keyof AutomationTimeObject) => (v: string) => { + if (Number(v) < 0) { + setDisplayValue({ + ...displayValue, + [k]: 0 + }) + } else if (k === "hours") { + setDisplayValue({ + ...displayValue, + [k]: Number(v) + }) + } else if (k === "minutes") { + let minutes = Number(v); + let { hours = 0 } = displayValue + if (minutes > 59) { + const additonalHours = Math.floor(minutes / 60) + hours += additonalHours + minutes = minutes - additonalHours * 60 + } + setDisplayValue({ + ...displayValue, + minutes, + hours, + }) + } else if (k === "seconds") { + let seconds = Number(v); + let { hours = 0, minutes = 0 } = displayValue + if (seconds > 59) { + const additonalHours = Math.floor(seconds / (60 * 60)) + hours += additonalHours + seconds = seconds - additonalHours * 60 + } + if (seconds > 59) { + const additionalMinutes = Math.floor(seconds / 60) + minutes += additionalMinutes + seconds = seconds - additionalMinutes * 60 + } + setDisplayValue({ + ...displayValue, + minutes, + hours, + seconds, + }) + } else if (k === "milliseconds") { + let milliseconds = Number(v); + let { hours = 0, minutes = 0, seconds = 0 } = displayValue + if (milliseconds > 1000) { + const additonalHours = Math.floor(milliseconds / (60 * 60 * 1000)) + hours += additonalHours + milliseconds = milliseconds - additonalHours * 60 * 60 * 1000 + } + if (milliseconds > 1000) { + const additionalMinutes = Math.floor(milliseconds / (60 * 1000)) + minutes += additionalMinutes + milliseconds = milliseconds - additionalMinutes * 60 * 1000 + } + if (milliseconds > 1000) { + const additionalSeconds = Math.floor(milliseconds / 1000) + seconds += additionalSeconds + milliseconds = milliseconds - additionalSeconds * 1000 + } + setDisplayValue({ + ...displayValue, + minutes, + hours, + seconds, + milliseconds, + }) + } + } + + + // effect + useEffect(() => { + if (disabled && !props.value) { + return + } + if (disabled) { + props.onChange(undefined) + } else { + const pVal = convertAutomationTimeToTimeString(props.value); + const cVal = convertObjectToAutomationTimeString(displayValue); + if (pVal !== cVal) { + props.onChange(displayValue) + } + } + }, [disabled, displayValue]) + + + return
+ {props.label} +
+ {fields.map(name => + updateOne(name)(e.target.value)} + /> + )} + setDisabled(!disabled)}> + {disabled ? : } + +
+
+} diff --git a/webapp/src/components/Inputs/InputList.tsx b/webapp/src/components/Inputs/InputList.tsx index 6cf77b2..c0fa6d9 100644 --- a/webapp/src/components/Inputs/InputList.tsx +++ b/webapp/src/components/Inputs/InputList.tsx @@ -4,6 +4,7 @@ import InputLabel from '@mui/material/InputLabel'; import MenuItem from '@mui/material/MenuItem'; import FormControl from '@mui/material/FormControl'; import Select from '@mui/material/Select'; +import { prettyName } from "utils/formatting"; export interface Props { current: T; @@ -12,25 +13,26 @@ export interface Props { label: string; id?: string; className?: string; + prettyOptionLabels?: boolean; } -export const InputList: FC> = ({ +export function InputList({ current, options, onChange, label, id, className, -}) => { + prettyOptionLabels, +}: Props) { const invalidCurrent = current && !options.includes(current); - return - {label} + return + {label} diff --git a/webapp/src/components/Inputs/InputText.tsx b/webapp/src/components/Inputs/InputText.tsx index d94fbb8..c928037 100644 --- a/webapp/src/components/Inputs/InputText.tsx +++ b/webapp/src/components/Inputs/InputText.tsx @@ -12,6 +12,7 @@ export interface Props { additionalTooltipFilters?: Record; multiline?: boolean; maxRows?: number; + className?: string; } export default function InputText({ label, @@ -23,10 +24,11 @@ export default function InputText({ multiline = false, maxRows = 4, children, + className, }: PropsWithChildren) { const tooltip = useToolTip(); const [isFocused, setIsFocused] = useState(false) - return + return { if (e.key === 'Enter') { diff --git a/webapp/src/components/Inputs/InputTime.tsx b/webapp/src/components/Inputs/InputTime.tsx index c4e1e9c..c1f249f 100644 --- a/webapp/src/components/Inputs/InputTime.tsx +++ b/webapp/src/components/Inputs/InputTime.tsx @@ -24,6 +24,7 @@ export interface Props { label: string; value?: AutomationTime; onChange: (v?: AutomationTimeObject) => void; + className?: string; } export const InputTime: FC = (props) => { @@ -122,8 +123,8 @@ export const InputTime: FC = (props) => { }, [disabled, displayValue]) - return
- {props.label} + return
+ {/* {props.label} */}
{fields.map(name => { value: T; onChange: (v: T) => void; error?: JSX.Element | string, + className?: string; } -export default function InputYaml({ +export default function InputYaml({ label, error: incmError, value = {} as any, onChange, + className, }: Props) { const [{ text, error }, setState] = useState<{ text: string, @@ -50,7 +52,7 @@ export default function InputYaml({ } } }, [text], 1000) - return + return { + if (getNodeSubType(node) === 'delay') { + return 'delay' + } else { + return 'template' + } +} -export const ActionWaitState: OptionManager = { +export const ActionWaitState: OptionManager = { 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 <> - update({ wait_template })} - /> - {/* update({ 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 = <> + update({ delay })} /> + + } else if (current === 'template') { + state = state as WaitAction; + option = <> + update({ wait_template })} + /> + + } else if (current === 'trigger') { + state = state as WaitAction; + option = <> + update({ wait_for_trigger })} + /> + + } + + // render + return
+ + {option} + {current !== 'delay' ?
+ update({ timeout: String(timeout) })} + /> + update({ continue_on_timeout })} + /> +
: <>} +
} } diff --git a/webapp/src/components/NodeEditor/OptionManager/OptionManager.ts b/webapp/src/components/NodeEditor/OptionManager/OptionManager.ts index 4f40710..11c6fb7 100644 --- a/webapp/src/components/NodeEditor/OptionManager/OptionManager.ts +++ b/webapp/src/components/NodeEditor/OptionManager/OptionManager.ts @@ -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 { +export type OptionManager = { 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 = ( @@ -13,17 +21,17 @@ export const updateConditionData = ( ) => ( state: T, data: Partial, - ) => setState({ - ...state, - ...data, - }) +) => setState({ + ...state, + ...data, +}) export const updateActionData = ( state: T, setState: (s: T) => void ) => ( data: Partial, - ) => setState({ - ...state, - ...data, - }) +) => setState({ + ...state, + ...data, +}) diff --git a/webapp/src/components/NodeEditor/OptionManager/index.tsx b/webapp/src/components/NodeEditor/OptionManager/index.tsx index dd247b6..295f34a 100644 --- a/webapp/src/components/NodeEditor/OptionManager/index.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/index.tsx @@ -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) : + } ; }, isReady() { diff --git a/webapp/src/components/NodeEditor/index.stories.tsx b/webapp/src/components/NodeEditor/index.stories.tsx index 7b0aeb3..87752a8 100644 --- a/webapp/src/components/NodeEditor/index.stories.tsx +++ b/webapp/src/components/NodeEditor/index.stories.tsx @@ -95,3 +95,12 @@ ServiceExample.args = { "service": "", }, } + + +export const DelayExample = Action.bind({}) +DelayExample.args = { + ...DelayExample.args, + node: { + "wait_template": "", + }, +} diff --git a/webapp/src/types/automations/actions.ts b/webapp/src/types/automations/actions.ts index 0268c28..80761ee 100644 --- a/webapp/src/types/automations/actions.ts +++ b/webapp/src/types/automations/actions.ts @@ -1,5 +1,4 @@ import { AutomationSequenceNode } from "."; -import { AutomationTime } from "./common"; import { AutomationCondition } from "./conditions"; import * as v from "types/validators/actions"; import * as st from 'superstruct'; @@ -15,6 +14,7 @@ export type RepeatAction = AutomationActionNodeBase<{ } }> export type WaitAction = st.Infer; +export type DelayAction = st.Infer; export type FireEventAction = st.Infer; export type DeviceAction = st.Infer; export type ChooseAction = AutomationActionNodeBase<{ @@ -36,6 +36,7 @@ export type ActionType = | 'service' | 'repeat' | 'wait' + | 'delay' | 'event' | 'device' | 'choose' diff --git a/webapp/src/types/validators/actions.ts b/webapp/src/types/validators/actions.ts index ee51cbc..807ceea 100644 --- a/webapp/src/types/validators/actions.ts +++ b/webapp/src/types/validators/actions.ts @@ -1,5 +1,6 @@ import * as st from 'superstruct' import { AutomationTime } from './common'; +import { AutomationTrigger } from './triggers'; export const AutomationActionNodeBase = st.type({ @@ -29,9 +30,17 @@ export const RepeatAction = st.intersection([ export const WaitAction = st.intersection([ AutomationActionNodeBase, st.type({ - wait_template: st.string(), timeout: st.optional(AutomationTime), continue_on_timeout: st.optional(st.boolean()), + wait_template: st.optional(st.string()), + wait_for_trigger: st.optional(st.array(AutomationTrigger)), + }), +]); + +export const DelayAction = st.intersection([ + AutomationActionNodeBase, + st.type({ + delay: AutomationTime }) ]); diff --git a/webapp/src/utils/automations.ts b/webapp/src/utils/automations.ts index 193023c..7a24466 100644 --- a/webapp/src/utils/automations.ts +++ b/webapp/src/utils/automations.ts @@ -31,6 +31,7 @@ export const getSubTypeList = (nodeType: T): Auto "service", "repeat", "wait", + "delay", "device", "choose", ] as any @@ -111,6 +112,8 @@ export const getNodeSubType = ( return 'device' as any } else if ('choose' in node) { return 'choose' as any + } else if ('delay' in node) { + return 'delay' as any } else { throw new Error("Invalid node type!") } @@ -138,6 +141,7 @@ const ActionValidators: Record = { "repeat": v.actions.RepeatAction, "service": v.actions.ServiceAction, "wait": v.actions.WaitAction, + "delay": v.actions.DelayAction, } const ConditionValidators: Record = {