support for malformed automations
This commit is contained in:
1 parent
6c57408f27
commit
a19db93c37
35 files changed
+683
-161
No files matched your search
@@ -5,6 +5,12 @@
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.automation-editor-failures {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
padding: 1em;
|
||||
}
|
||||
|
||||
.automation-editor--flow-wrapper {
|
||||
flex: 1;
|
||||
padding: 0 0.5em 0 0.5em;
|
||||
|
||||
@@ -111,3 +111,63 @@ EmptyStart.args = {
|
||||
sequence: []
|
||||
}
|
||||
}
|
||||
|
||||
export const BadAutomationInvalidMetadata = Template.bind({})
|
||||
BadAutomationInvalidMetadata.args = {
|
||||
...BadAutomationInvalidMetadata.args,
|
||||
automation: {
|
||||
condition: [],
|
||||
tags: {},
|
||||
metadata: {
|
||||
} as any,
|
||||
trigger: [
|
||||
],
|
||||
sequence: []
|
||||
}
|
||||
}
|
||||
|
||||
export const BadAutomationInvalidTriggers = Template.bind({})
|
||||
BadAutomationInvalidTriggers.args = {
|
||||
...BadAutomationInvalidTriggers.args,
|
||||
automation: {
|
||||
condition: [],
|
||||
tags: {},
|
||||
metadata: {
|
||||
id: "random",
|
||||
alias: "Random",
|
||||
description: "Example Metadata",
|
||||
trigger_variables: {
|
||||
'wowo': '!'
|
||||
},
|
||||
mode: 'single',
|
||||
},
|
||||
trigger: [
|
||||
"haha I am a string"
|
||||
] as any,
|
||||
sequence: []
|
||||
}
|
||||
}
|
||||
|
||||
export const BadAutomationInvalidSequence = Template.bind({})
|
||||
BadAutomationInvalidSequence.args = {
|
||||
...BadAutomationInvalidSequence.args,
|
||||
automation: {
|
||||
condition: [],
|
||||
tags: {},
|
||||
metadata: {
|
||||
id: "random",
|
||||
alias: "Bad Choose Sequence",
|
||||
description: "Example Metadata",
|
||||
trigger_variables: {
|
||||
'wowo': '!'
|
||||
},
|
||||
mode: 'single',
|
||||
},
|
||||
trigger: [],
|
||||
sequence: [
|
||||
{
|
||||
choose: {}
|
||||
}
|
||||
] as any
|
||||
}
|
||||
}
|
||||
@@ -5,9 +5,11 @@ import { AutomationData } from "types/automations";
|
||||
import { AutoInfoBox } from "./AutoInfoBox";
|
||||
import { ButtonIcon } from "components/Icons/ButtonIcons";
|
||||
import { Button } from "components/Inputs/Button";
|
||||
import { useAutomatioEditorState } from "./state";
|
||||
import { DAGAutomationFlow } from "components/DAGFlow";
|
||||
import { useAutomatioEditorState, EditorData } from './state';
|
||||
import { SafeDAGAutomationFlow } from "components/DAGFlow/safe";
|
||||
import { DAGAutomationFlowDims } from "components/DAGFlow/types";
|
||||
import { MiniFailure } from 'types/validators/helper';
|
||||
import InputYaml from "components/Inputs/InputYaml";
|
||||
|
||||
interface Props {
|
||||
automation?: AutomationData;
|
||||
@@ -26,7 +28,9 @@ export const AutomationEditor: FC<Props> = ({
|
||||
updateTrigger,
|
||||
updateMetadata,
|
||||
updateCondition,
|
||||
validate,
|
||||
save,
|
||||
saveAndUpdate,
|
||||
} = useAutomatioEditorState(propsAutos, propsOnUpdate);
|
||||
|
||||
const [closeInfo, setCloseInfo] = useState(false);
|
||||
@@ -37,6 +41,14 @@ export const AutomationEditor: FC<Props> = ({
|
||||
Loading...
|
||||
</div>
|
||||
}
|
||||
if (state.status === 'invalid') {
|
||||
return <ValidationBox
|
||||
failures={state.failures}
|
||||
data={state.data}
|
||||
validate={validate}
|
||||
onSave={saveAndUpdate}
|
||||
/>
|
||||
}
|
||||
return <div className="automation-editor">
|
||||
<AutoInfoBox
|
||||
className={closeInfo ? "hide" : "show"}
|
||||
@@ -61,7 +73,7 @@ export const AutomationEditor: FC<Props> = ({
|
||||
Save <CheckMarkIcon color="#bf4" />
|
||||
</Button>
|
||||
</div>
|
||||
<DAGAutomationFlow
|
||||
<SafeDAGAutomationFlow
|
||||
className="automation-editor--flow-wrapper--flow"
|
||||
sequence={state.data.sequence}
|
||||
trigger={state.data.trigger}
|
||||
@@ -74,3 +86,58 @@ export const AutomationEditor: FC<Props> = ({
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
|
||||
export const ValidationBox: FC<{
|
||||
failures: MiniFailure[]
|
||||
validate: (d: any) => MiniFailure[] | null,
|
||||
data: EditorData,
|
||||
onSave: (d: EditorData) => void,
|
||||
}> = props => {
|
||||
|
||||
const [{ failures, data }, setState] = useState({
|
||||
failures: props.failures,
|
||||
data: props.data,
|
||||
});
|
||||
|
||||
const makeSave = <K extends keyof EditorData>(k: K) => (d: EditorData[K]) => {
|
||||
const newData = {
|
||||
...data,
|
||||
[k]: d,
|
||||
}
|
||||
const newFails = props.validate(newData);
|
||||
if (newFails) {
|
||||
setState({
|
||||
failures: newFails,
|
||||
data: newData,
|
||||
})
|
||||
} else {
|
||||
setState({
|
||||
failures: [],
|
||||
data: newData,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return <div className="automation-editor-failures">
|
||||
<ul>
|
||||
{failures.map((f, i) => <>
|
||||
<li key={i}>
|
||||
<b>{f.path}</b>:
|
||||
<ul>
|
||||
{f.message.map((m, j) => <li key={j}>
|
||||
{m}
|
||||
</li>)}
|
||||
</ul>
|
||||
</li>
|
||||
</>)}
|
||||
</ul>
|
||||
<span>Please correct the automation file manually and then continue!</span>
|
||||
<InputYaml label="Metadata" value={data.metadata} onChange={makeSave('metadata')} resizable />
|
||||
<InputYaml label="Tags" value={data.tags} onChange={makeSave('tags')} resizable />
|
||||
<InputYaml label="Trigger" value={data.trigger} onChange={makeSave('trigger')} resizable />
|
||||
<InputYaml label="Condition" value={data.condition} onChange={makeSave('condition')} resizable />
|
||||
<InputYaml label="Actions" value={data.sequence} onChange={makeSave('sequence')} resizable />
|
||||
<Button disabled={failures.length > 0} onClick={() => props.onSave(data)}>Save</Button>
|
||||
</div>
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { AutomationData, AutomationMetadata } from "types/automations";
|
||||
|
||||
import * as v from "types/validators/autmation";
|
||||
import { getFailures } from "types/validators/helper";
|
||||
import { MiniFailure } from '../../types/validators/helper';
|
||||
|
||||
export type EditorData = ReturnType<typeof genEditorData>;
|
||||
export type EditorState = {
|
||||
@@ -8,6 +10,10 @@ export type EditorState = {
|
||||
data: EditorData
|
||||
} | {
|
||||
status: 'loading'
|
||||
} | {
|
||||
status: 'invalid',
|
||||
failures: MiniFailure[],
|
||||
data: EditorData,
|
||||
}
|
||||
export type EditorStatus = EditorState['status'];
|
||||
|
||||
@@ -20,7 +26,7 @@ export const useAutomatioEditorState = (
|
||||
});
|
||||
const makeUpdate = <K extends keyof EditorData, D extends EditorData[K]>(name: K) => (
|
||||
data: D
|
||||
) => state.status !== 'loading' && update({
|
||||
) => ((state.status === 'changed') || (state.status === 'unchanged')) && update({
|
||||
status: 'changed',
|
||||
data: {
|
||||
...state.data,
|
||||
@@ -28,21 +34,46 @@ export const useAutomatioEditorState = (
|
||||
}
|
||||
})
|
||||
|
||||
const saveAndUpdate = (data: EditorData) => {
|
||||
const cleanData = {
|
||||
...data,
|
||||
tags: data.tags.reduce((all, [tagName, tagValue]) => ({
|
||||
...all,
|
||||
[tagName.trim()]: tagValue.trim(),
|
||||
}), {})
|
||||
};
|
||||
update({
|
||||
status: "loading",
|
||||
})
|
||||
onSave(cleanData);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (automation) {
|
||||
update({
|
||||
status: 'unchanged',
|
||||
data: genEditorData(automation),
|
||||
})
|
||||
const failures = getFailures(automation, v.AutomationData);
|
||||
if (failures) {
|
||||
update({
|
||||
status: 'invalid',
|
||||
failures,
|
||||
data: genEditorData(automation),
|
||||
})
|
||||
} else {
|
||||
update({
|
||||
status: 'unchanged',
|
||||
data: genEditorData(automation),
|
||||
})
|
||||
}
|
||||
}
|
||||
}, [automation]);
|
||||
|
||||
return {
|
||||
validate: (data: any) => getFailures(data, v.AutomationData),
|
||||
updateTrigger: makeUpdate('trigger'),
|
||||
updateSequence: makeUpdate('sequence'),
|
||||
updateCondition: makeUpdate('condition'),
|
||||
saveAndUpdate,
|
||||
updateMetadata: (metadata: AutomationMetadata, tags: Array<[string, string]>) => {
|
||||
if (state.status !== 'loading') {
|
||||
if ((state.status === 'changed') || (state.status === 'unchanged')) {
|
||||
update({
|
||||
status: 'changed',
|
||||
data: {
|
||||
@@ -55,17 +86,7 @@ export const useAutomatioEditorState = (
|
||||
},
|
||||
save: () => {
|
||||
if (state.status !== 'loading') {
|
||||
const data = {
|
||||
...state.data,
|
||||
tags: state.data.tags.reduce((all, [tagName, tagValue]) => ({
|
||||
...all,
|
||||
[tagName]: tagValue,
|
||||
}), {})
|
||||
};
|
||||
update({
|
||||
status: "loading",
|
||||
})
|
||||
onSave(data);
|
||||
saveAndUpdate(state.data)
|
||||
}
|
||||
},
|
||||
get state() {
|
||||
|
||||
@@ -64,3 +64,57 @@ FewAutos.args = {
|
||||
createMockAuto(),
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
|
||||
export const BadAutmations = Template.bind({})
|
||||
BadAutmations.args = {
|
||||
...BadAutmations.args,
|
||||
automations: [
|
||||
{
|
||||
condition: [],
|
||||
tags: {},
|
||||
metadata: {
|
||||
id: "Bad Choose Sequence",
|
||||
alias: "Bad Choose",
|
||||
description: "Example Metadata",
|
||||
trigger_variables: {
|
||||
'wowo': '!'
|
||||
},
|
||||
mode: 'single',
|
||||
},
|
||||
trigger: [],
|
||||
sequence: [
|
||||
{
|
||||
choose: {}
|
||||
}
|
||||
] as any
|
||||
},
|
||||
{
|
||||
condition: [],
|
||||
tags: {},
|
||||
metadata: {
|
||||
id: "random",
|
||||
alias: "Bad Trigger",
|
||||
description: "Example Metadata",
|
||||
trigger_variables: {
|
||||
'wowo': '!'
|
||||
},
|
||||
mode: 'single',
|
||||
},
|
||||
trigger: [
|
||||
"haha I am a string"
|
||||
] as any,
|
||||
sequence: []
|
||||
},
|
||||
{
|
||||
condition: [],
|
||||
tags: {},
|
||||
metadata: {
|
||||
} as any,
|
||||
trigger: [
|
||||
],
|
||||
sequence: []
|
||||
}
|
||||
],
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import "./AutomationListBox.css";
|
||||
import { FC, useState } from 'react';
|
||||
import { AutomationData } from "types/automations";
|
||||
import { AutomationData, AutomationMetadata } from "types/automations";
|
||||
import InputMultiSelect from "components/Inputs/InputMultiSelect";
|
||||
import InputText from "components/Inputs/InputText";
|
||||
import { Button } from "components/Inputs/Button";
|
||||
@@ -65,6 +65,9 @@ export const getTagList = (autos: AutomationData[]): string[] => Object.keys(aut
|
||||
}, {}))
|
||||
|
||||
const filterAutomations = (autos: AutomationData[], searchText: string, selected: number[], tags: string[]): Array<[AutomationData, number]> => autos.map<[AutomationData, number]>((a, i) => [a, i]).filter(([a, i]) => {
|
||||
if (!a.metadata) {
|
||||
return true
|
||||
}
|
||||
if (searchText.length > 0) {
|
||||
if (
|
||||
!(
|
||||
@@ -130,25 +133,17 @@ const convertGroupsToItems = (
|
||||
if (Array.isArray(groups)) {
|
||||
return groups.map(i => {
|
||||
const [auto, autoIndex] = autos[i];
|
||||
let title = "BadAuto<<Missing Metadata>>"
|
||||
if (auto.metadata) {
|
||||
title = `Name = ${auto.metadata.alias}\nID = ${auto.metadata.id}\nDescription = ${auto.metadata.description}`
|
||||
}
|
||||
return <div
|
||||
key={i}
|
||||
className={["automation-list-box--body--item", selected === autoIndex ? 'selected' : ''].join(' ')}
|
||||
title={`Name = ${auto.metadata.alias}\nID = ${auto.metadata.id}\nDescription = ${auto.metadata.description}`}
|
||||
title={title}
|
||||
onClick={() => onSelectAutomation(autoIndex)}
|
||||
>
|
||||
<div
|
||||
className="automation-list-box--body--item--title"
|
||||
>
|
||||
<b>{auto.metadata.alias.slice(0, 15)} <span>({auto.metadata.id.slice(0, 5)})</span></b>
|
||||
<span>{auto.metadata.description.slice(0, 25)}</span>
|
||||
<div
|
||||
className="automation-list-box--body--item--tags"
|
||||
>
|
||||
{Object.keys(auto.tags).map(tagName => <span key={tagName}>
|
||||
<b>{tagName}:</b> {auto.tags[tagName]}
|
||||
</span>)}
|
||||
</div>
|
||||
</div>
|
||||
<MetadataBox metadata={auto.metadata} tags={auto.tags} />
|
||||
<Button onClick={() => onRemove(autoIndex)}><TrashIcon /></Button>
|
||||
</div>
|
||||
})
|
||||
@@ -167,6 +162,31 @@ const convertGroupsToItems = (
|
||||
}
|
||||
}
|
||||
|
||||
export const MetadataBox: FC<{
|
||||
metadata: AutomationMetadata,
|
||||
tags: Record<string, string>,
|
||||
}> = (auto) => {
|
||||
let title = <span>{"BadAuto<<Missing Metadata>>"}</span>
|
||||
if (auto.metadata) {
|
||||
title = <>
|
||||
<b>{String(auto.metadata.alias).slice(0, 15)} <span>({String(auto.metadata.id).slice(0, 5)})</span></b>
|
||||
<span>{String(auto.metadata.description).slice(0, 25)}</span>
|
||||
</>
|
||||
}
|
||||
return <div
|
||||
className="automation-list-box--body--item--title"
|
||||
>
|
||||
{title}
|
||||
<div
|
||||
className="automation-list-box--body--item--tags"
|
||||
>
|
||||
{Object.keys(auto.tags).map(tagName => <span key={tagName}>
|
||||
<b>{tagName}:</b> {auto.tags[tagName]}
|
||||
</span>)}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
|
||||
const AutomationListBoxGroup: FC<{
|
||||
groupName: string,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { FC } from "react";
|
||||
import { AddIcon, PencilIcon, TrashIcon } from '../Icons/index';
|
||||
import { AddIcon, TrashIcon } from '../Icons/index';
|
||||
import { useCircleStyles } from './styles';
|
||||
import { Handle, Position, XYPosition } from 'react-flow-renderer';
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { makeFlowCircle } from "./DAGCircle"
|
||||
import { DAGAutomationFlowDims, FlowData } from "./types"
|
||||
import { DAGAutomationFlowDims } from "./types"
|
||||
import { XYPosition } from 'react-flow-renderer';
|
||||
|
||||
export const makeAddButton = (
|
||||
|
||||
@@ -20,7 +20,18 @@ const nodeTypes = {
|
||||
'dagcircle': DAGCircle,
|
||||
}
|
||||
|
||||
interface Props {
|
||||
const makeOnEditAutomationConditions = (
|
||||
conditions: AutomationCondition[],
|
||||
updateConditions: (c: AutomationCondition[]) => void,
|
||||
openModal: UpdateModalState,
|
||||
) => () => openModal({
|
||||
single: false,
|
||||
node: conditions,
|
||||
allowedTypes: ['condition'],
|
||||
update: c => updateConditions(c as any),
|
||||
})
|
||||
|
||||
export interface DAGAutomationFlowProps {
|
||||
className?: string;
|
||||
trigger: AutomationTrigger[];
|
||||
condition: AutomationCondition[];
|
||||
@@ -31,7 +42,7 @@ interface Props {
|
||||
onConditionUpdate: (s: AutomationCondition[]) => void;
|
||||
}
|
||||
|
||||
export const DAGAutomationFlow: FC<Props> = ({
|
||||
export const DAGAutomationFlow: FC<DAGAutomationFlowProps> = ({
|
||||
className,
|
||||
trigger,
|
||||
condition,
|
||||
@@ -131,14 +142,3 @@ export const DAGAutomationFlow: FC<Props> = ({
|
||||
</ReactFlow>
|
||||
</>;
|
||||
}
|
||||
|
||||
const makeOnEditAutomationConditions = (
|
||||
conditions: AutomationCondition[],
|
||||
updateConditions: (c: AutomationCondition[]) => void,
|
||||
openModal: UpdateModalState,
|
||||
) => () => openModal({
|
||||
single: false,
|
||||
node: conditions,
|
||||
allowedTypes: ['condition'],
|
||||
update: c => updateConditions(c as any),
|
||||
})
|
||||
@@ -0,0 +1,4 @@
|
||||
.dag-flow-safe {
|
||||
height: 100%;
|
||||
overflow: scroll;
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
import "./safe.css";
|
||||
import { Component, ErrorInfo } from "react";
|
||||
import { DAGAutomationFlow, DAGAutomationFlowProps } from ".";
|
||||
import { AutomationSequenceNode } from "types/automations";
|
||||
import { AutomationCondition } from "types/automations/conditions";
|
||||
import { AutomationTrigger } from 'types/automations/triggers';
|
||||
import InputYaml from "components/Inputs/InputYaml";
|
||||
import { Button } from "components/Inputs/Button";
|
||||
|
||||
type GoodState = {
|
||||
hasError: false;
|
||||
}
|
||||
type FailedStateData = {
|
||||
sequence: AutomationSequenceNode[],
|
||||
condition: AutomationCondition[],
|
||||
trigger: AutomationTrigger[],
|
||||
}
|
||||
type FailedState = {
|
||||
hasError: true,
|
||||
error: any,
|
||||
errorInfo?: ErrorInfo,
|
||||
} & FailedStateData
|
||||
type State = GoodState | FailedState
|
||||
export class SafeDAGAutomationFlow extends Component<DAGAutomationFlowProps, State> {
|
||||
|
||||
state: State = {
|
||||
hasError: false
|
||||
}
|
||||
|
||||
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
||||
this.setState({
|
||||
hasError: true,
|
||||
error,
|
||||
errorInfo,
|
||||
sequence: this.props.sequence,
|
||||
condition: this.props.condition,
|
||||
trigger: this.props.trigger,
|
||||
})
|
||||
}
|
||||
|
||||
render() {
|
||||
if (this.state.hasError) {
|
||||
const makeSet = <K extends keyof FailedStateData>(k: K) => (d: FailedStateData[K]) => this.setState({
|
||||
...this.state,
|
||||
[k]: d,
|
||||
});
|
||||
const makeSave = <K extends keyof FailedStateData>(k: K) => () => {
|
||||
if (!this.state.hasError) {
|
||||
return
|
||||
}
|
||||
if (k === 'condition') {
|
||||
this.props.onConditionUpdate(this.state.condition)
|
||||
} else if (k === 'sequence') {
|
||||
this.props.onSequenceUpdate(this.state.sequence)
|
||||
} else if (k === 'trigger') {
|
||||
this.props.onTriggerUpdate(this.state.trigger)
|
||||
}
|
||||
}
|
||||
return <div className="dag-flow-safe">
|
||||
<h1>An Error has occured.</h1>
|
||||
|
||||
<h1>Failed to Open Autmation!</h1>
|
||||
Something is likely not configured in your automation properly.
|
||||
<hr />
|
||||
<h2>Error:</h2>
|
||||
<h2>{String(this.state.error)}</h2>
|
||||
<br />
|
||||
{JSON.stringify(this.state.error, null, 4)}
|
||||
{this.state.errorInfo && this.state.errorInfo.componentStack.split('\n').map((s, i) => <>
|
||||
<span key={i}>{s}</span><br key={`br-${i}`} />
|
||||
</>)}
|
||||
<h2>Automation YAML</h2>
|
||||
<InputYaml resizable label="Trigger" value={this.state.trigger} onChange={makeSet('trigger')} />
|
||||
<Button onClick={makeSave('trigger')}>Save Triggers</Button>
|
||||
<InputYaml resizable label="Condition" value={this.state.condition} onChange={makeSet('condition')} />
|
||||
<Button onClick={makeSave('condition')}>Save Conditions</Button>
|
||||
<InputYaml resizable label="Action" value={this.state.sequence} onChange={makeSet('sequence')} />
|
||||
<Button onClick={makeSave('sequence')}>Save Actions</Button>
|
||||
</div>
|
||||
}
|
||||
|
||||
return <DAGAutomationFlow {...this.props} />;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
import { AutomationTrigger } from 'types/automations/triggers';
|
||||
import { DAGAutomationFlowDims, FlowData, TriggerMakerOptions } from './types';
|
||||
import { makeFlowCircle } from './DAGCircle';
|
||||
import { makeAddButton } from './flowDataMods';
|
||||
|
||||
export const triggerToFlow = (
|
||||
@@ -14,7 +13,6 @@ export const triggerToFlow = (
|
||||
distanceFactor,
|
||||
nodeHeight,
|
||||
nodeWidth,
|
||||
circleSize,
|
||||
padding,
|
||||
conditionHeight,
|
||||
} = dims;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import "./InputBoolean.css";
|
||||
import InputWrapper from "./InputWrapper";
|
||||
|
||||
export interface Props {
|
||||
label: string;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import * as yaml from "js-yaml";
|
||||
import { useState } from "react";
|
||||
import InputTextArea from "./InputTextArea";
|
||||
import { useDelayedFunction, useDelayEffect } from 'utils/useDelay';
|
||||
import { useDelayEffect } from 'utils/useDelay';
|
||||
|
||||
export interface Props<T extends {}> {
|
||||
label: string;
|
||||
|
||||
@@ -8,10 +8,6 @@ import InputYaml from 'components/Inputs/InputYaml';
|
||||
|
||||
type UseState = <S>(s: S) => [S, Dispatch<SetStateAction<S>>]
|
||||
|
||||
const mockUseState: UseState = s => {
|
||||
return [s, () => { }]
|
||||
}
|
||||
|
||||
export const useEditorNodeState = (originalNode: AutomationNode, uState: UseState = useState) => {
|
||||
const [allState, setAllState] = uState({
|
||||
node: originalNode,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { AutomationNodeSubtype, AutomationNodeTypes } from "types/automations";
|
||||
import { ActionType } from "types/automations/actions";
|
||||
import { AutomationNode } from '../../types/automations/index';
|
||||
|
||||
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
import * as st from 'superstruct'
|
||||
|
||||
import { ActionType, AutomationAction } from "./actions";
|
||||
import { AutomationCondition, ConditionType } from "./conditions";
|
||||
import { AutomationTrigger, TriggerType } from "./triggers";
|
||||
import * as mv from "types/validators/metadata";
|
||||
|
||||
|
||||
export interface AutomationMetadata {
|
||||
id: string;
|
||||
alias: string;
|
||||
description: string;
|
||||
trigger_variables?: Record<string, string>;
|
||||
mode: "single" | "restart" | "queued" | "parallel";
|
||||
}
|
||||
export type AutomationMetadata = st.Infer<typeof mv.AutomationMetadata>
|
||||
export interface AutomationData {
|
||||
metadata: AutomationMetadata;
|
||||
trigger: AutomationTrigger[];
|
||||
|
||||
@@ -1,79 +1,24 @@
|
||||
import * as st from 'superstruct'
|
||||
import { AutomationTime } from "./common";
|
||||
import * as v from "types/validators/triggers";
|
||||
|
||||
interface AutomationTriggerBase {
|
||||
alias?: string;
|
||||
}
|
||||
|
||||
export interface AutomationTriggerEvent extends AutomationTriggerBase {
|
||||
platform: 'event';
|
||||
event_type: string | string[];
|
||||
event_data?: any;
|
||||
context?: any;
|
||||
}
|
||||
export interface AutomationTriggerHA extends AutomationTriggerBase {
|
||||
platform: 'homeassistant';
|
||||
event: 'start' | 'shutdown';
|
||||
}
|
||||
export interface AutomationTriggerMQTT extends AutomationTriggerBase {
|
||||
platform: 'mqtt';
|
||||
topic: string;
|
||||
payload: string;
|
||||
value_template: string;
|
||||
}
|
||||
export interface AutomationTriggerNumericState extends AutomationTriggerBase {
|
||||
platform: 'numeric_state';
|
||||
entity_id: string | string[];
|
||||
attribute?: string;
|
||||
value_template?: string;
|
||||
above?: string;
|
||||
below?: string;
|
||||
for?: AutomationTime;
|
||||
}
|
||||
export interface AutomationTriggerState extends AutomationTriggerBase {
|
||||
platform: 'state';
|
||||
entity_id: string | string[];
|
||||
attribute?: string;
|
||||
from?: string;
|
||||
to?: string;
|
||||
for?: AutomationTime;
|
||||
}
|
||||
export interface AutomationTriggerTag extends AutomationTriggerBase {
|
||||
platform: 'tag';
|
||||
tag_id: string;
|
||||
device_id: string | string[];
|
||||
}
|
||||
export interface AutomationTriggerTemplate extends AutomationTriggerBase {
|
||||
platform: 'template';
|
||||
value_template: string;
|
||||
for?: AutomationTime;
|
||||
}
|
||||
export interface AutomationTriggerTime extends AutomationTriggerBase {
|
||||
platform: 'time';
|
||||
at: AutomationTime;
|
||||
}
|
||||
export interface AutomationTriggerTimePattern extends AutomationTriggerBase {
|
||||
platform: 'time_pattern';
|
||||
hours?: string;
|
||||
minutes?: string;
|
||||
seconds?: string;
|
||||
}
|
||||
export interface AutomationTriggerWebhook extends AutomationTriggerBase {
|
||||
platform: 'webhook';
|
||||
webhook_id: string;
|
||||
}
|
||||
export interface AutomationTriggerZone extends AutomationTriggerBase {
|
||||
platform: 'zone';
|
||||
entity_id: string | string[];
|
||||
zone: string;
|
||||
event: 'enter' | 'leave';
|
||||
}
|
||||
export interface AutomationTriggerDevice extends AutomationTriggerBase {
|
||||
platform: 'device';
|
||||
device_id: string;
|
||||
domain: string;
|
||||
type: string;
|
||||
subtype: string;
|
||||
}
|
||||
export type AutomationTriggerEvent = st.Infer<typeof v.AutomationTriggerEvent>;
|
||||
export type AutomationTriggerHA = st.Infer<typeof v.AutomationTriggerHA>;
|
||||
export type AutomationTriggerMQTT = st.Infer<typeof v.AutomationTriggerMQTT>;
|
||||
export type AutomationTriggerNumericState = st.Infer<typeof v.AutomationTriggerNumericState>;
|
||||
export type AutomationTriggerState = st.Infer<typeof v.AutomationTriggerState>;
|
||||
export type AutomationTriggerTag = st.Infer<typeof v.AutomationTriggerTag>;
|
||||
export type AutomationTriggerTemplate = st.Infer<typeof v.AutomationTriggerTemplate>;
|
||||
export type AutomationTriggerTime = st.Infer<typeof v.AutomationTriggerTime>;
|
||||
export type AutomationTriggerTimePattern = st.Infer<typeof v.AutomationTriggerTimePattern>;
|
||||
export type AutomationTriggerWebhook = st.Infer<typeof v.AutomationTriggerWebhook>;
|
||||
export type AutomationTriggerZone = st.Infer<typeof v.AutomationTriggerZone>;
|
||||
export type AutomationTriggerDevice = st.Infer<typeof v.AutomationTriggerDevice>;
|
||||
|
||||
export type AutomationTrigger =
|
||||
| AutomationTriggerEvent
|
||||
| AutomationTriggerHA
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import * as st from 'superstruct'
|
||||
|
||||
|
||||
const AutomationActionNodeBase = {
|
||||
alias: st.optional(st.string()),
|
||||
variables: st.optional(st.record(st.string(), st.any())),
|
||||
}
|
||||
|
||||
export const ServiceAction = st.object({
|
||||
...AutomationActionNodeBase,
|
||||
data: st.any(),
|
||||
target: st.any(),
|
||||
service: st.string(),
|
||||
})
|
||||
|
||||
export const RepeatAction = st.object({
|
||||
...AutomationActionNodeBase,
|
||||
repeat: st.object({
|
||||
count: st.number(),
|
||||
sequence: st.array(),
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import * as st from 'superstruct'
|
||||
import { AutomationMetadata } from './metadata'
|
||||
import { AutomationTrigger } from './triggers';
|
||||
|
||||
|
||||
export const AutomationData = st.object({
|
||||
metadata: AutomationMetadata,
|
||||
trigger: st.array(AutomationTrigger),
|
||||
condition: st.array(st.object()),
|
||||
sequence: st.array(st.object()),
|
||||
tags: st.record(st.string(), st.string()),
|
||||
})
|
||||
@@ -0,0 +1,9 @@
|
||||
import * as st from 'superstruct'
|
||||
|
||||
|
||||
export const AutomationTime = st.object({
|
||||
hours: st.optional(st.number()),
|
||||
minutes: st.optional(st.number()),
|
||||
seconds: st.optional(st.number()),
|
||||
milliseconds: st.optional(st.number()),
|
||||
})
|
||||
@@ -0,0 +1,31 @@
|
||||
import * as st from 'superstruct';
|
||||
|
||||
export type MiniFailure = {
|
||||
path: string,
|
||||
message: string[],
|
||||
}
|
||||
|
||||
export const getFailures = <T, S>(value: unknown, struct: st.Struct<T, S>): MiniFailure[] | null => {
|
||||
try {
|
||||
st.assert(value, struct)
|
||||
} catch (err) {
|
||||
if (err instanceof st.StructError) {
|
||||
const failureMap = err.failures().reduce((all: Record<string, Set<string>> = {}, next) => {
|
||||
const path = next.path.join('.');
|
||||
if (all[path]) {
|
||||
all[path].add(next.message)
|
||||
} else {
|
||||
all[path] = new Set([next.message])
|
||||
}
|
||||
return all
|
||||
}, {})
|
||||
return Object.keys(failureMap).map((path) => ({
|
||||
path,
|
||||
message: Array.from(failureMap[path].values())
|
||||
}))
|
||||
}
|
||||
throw err
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import * as st from "superstruct";
|
||||
import { AutomationMetadata } from "./metadata";
|
||||
import { AutomationData } from './autmation';
|
||||
import { getFailures } from './helper';
|
||||
|
||||
test('superstructure is', () => {
|
||||
const badCheck = st.is({}, AutomationMetadata);
|
||||
expect(badCheck).toBeFalsy()
|
||||
})
|
||||
|
||||
test('getFailures returns failures', () => {
|
||||
const failures = getFailures({ metadata: {} }, AutomationData);
|
||||
expect(failures).toHaveLength(8)
|
||||
})
|
||||
|
||||
test('getFailures returns null', () => {
|
||||
const failures = getFailures({
|
||||
id: "test",
|
||||
alias: "hey",
|
||||
description: "wow",
|
||||
mode: "single",
|
||||
}, AutomationMetadata);
|
||||
expect(failures).toBeNull()
|
||||
})
|
||||
@@ -0,0 +1,10 @@
|
||||
import * as st from 'superstruct'
|
||||
|
||||
|
||||
export const AutomationMetadata = st.object({
|
||||
id: st.string(),
|
||||
alias: st.string(),
|
||||
description: st.string(),
|
||||
trigger_variables: st.optional(st.nullable(st.record(st.string(), st.string()))),
|
||||
mode: st.enums(["single", "restart", "queued", "parallel"]),
|
||||
})
|
||||
@@ -0,0 +1,117 @@
|
||||
import * as st from 'superstruct'
|
||||
import { AutomationTime } from './common'
|
||||
|
||||
|
||||
const AutomationTriggerBase = {
|
||||
alias: st.optional(st.string())
|
||||
}
|
||||
|
||||
export const AutomationTriggerEvent = st.object({
|
||||
...AutomationTriggerBase,
|
||||
platform: st.literal('event'),
|
||||
event_type: st.union([st.string(), st.array(st.string())]),
|
||||
event_data: st.any(),
|
||||
context: st.any(),
|
||||
})
|
||||
|
||||
export const AutomationTriggerHA = st.object({
|
||||
...AutomationTriggerBase,
|
||||
platform: st.literal('homeassistant'),
|
||||
event: st.enums(['start', 'shutdown']),
|
||||
})
|
||||
|
||||
export const AutomationTriggerMQTT = st.object({
|
||||
...AutomationTriggerBase,
|
||||
platform: st.literal('mqtt'),
|
||||
topic: st.string(),
|
||||
payload: st.string(),
|
||||
value_template: st.string(),
|
||||
})
|
||||
|
||||
export const AutomationTriggerNumericState = st.object({
|
||||
...AutomationTriggerBase,
|
||||
platform: st.literal('numeric_state'),
|
||||
entity_id: st.union([st.string(), st.array(st.string())]),
|
||||
attribute: st.optional(st.string()),
|
||||
value_template: st.optional(st.string()),
|
||||
above: st.optional(st.string()),
|
||||
below: st.optional(st.string()),
|
||||
for: st.optional(AutomationTime),
|
||||
})
|
||||
|
||||
export const AutomationTriggerState = st.object({
|
||||
...AutomationTriggerBase,
|
||||
platform: st.literal('state'),
|
||||
entity_id: st.union([st.string(), st.array(st.string())]),
|
||||
attribute: st.optional(st.string()),
|
||||
from: st.optional(st.string()),
|
||||
to: st.optional(st.string()),
|
||||
for: st.optional(AutomationTime),
|
||||
})
|
||||
|
||||
export const AutomationTriggerTag = st.object({
|
||||
...AutomationTriggerBase,
|
||||
platform: st.literal('tag'),
|
||||
tag_id: st.string(),
|
||||
device_id: st.union([st.string(), st.array(st.string())]),
|
||||
})
|
||||
|
||||
export const AutomationTriggerTemplate = st.object({
|
||||
...AutomationTriggerBase,
|
||||
platform: st.literal('template'),
|
||||
value_template: st.string(),
|
||||
for: st.optional(AutomationTime),
|
||||
})
|
||||
|
||||
export const AutomationTriggerTime = st.object({
|
||||
...AutomationTriggerBase,
|
||||
platform: st.literal('time'),
|
||||
at: AutomationTime,
|
||||
})
|
||||
|
||||
export const AutomationTriggerTimePattern = st.object({
|
||||
...AutomationTriggerBase,
|
||||
platform: st.literal('time_pattern'),
|
||||
hours: st.optional(st.string()),
|
||||
minutes: st.optional(st.string()),
|
||||
seconds: st.optional(st.string()),
|
||||
})
|
||||
|
||||
export const AutomationTriggerWebhook = st.object({
|
||||
...AutomationTriggerBase,
|
||||
platform: st.literal('webhook'),
|
||||
webhook_id: st.string(),
|
||||
})
|
||||
|
||||
export const AutomationTriggerZone = st.object({
|
||||
...AutomationTriggerBase,
|
||||
platform: st.literal('zone'),
|
||||
entity_id: st.union([st.string(), st.array(st.string())]),
|
||||
zone: st.string(),
|
||||
event: st.enums(['enter', 'leave'])
|
||||
})
|
||||
|
||||
export const AutomationTriggerDevice = st.object({
|
||||
...AutomationTriggerBase,
|
||||
platform: st.literal('device'),
|
||||
device_id: st.string(),
|
||||
domain: st.string(),
|
||||
type: st.string(),
|
||||
subtype: st.string(),
|
||||
})
|
||||
|
||||
|
||||
export const AutomationTrigger = st.union([
|
||||
AutomationTriggerEvent,
|
||||
AutomationTriggerHA,
|
||||
AutomationTriggerMQTT,
|
||||
AutomationTriggerNumericState,
|
||||
AutomationTriggerState,
|
||||
AutomationTriggerTag,
|
||||
AutomationTriggerTemplate,
|
||||
AutomationTriggerTime,
|
||||
AutomationTriggerTimePattern,
|
||||
AutomationTriggerWebhook,
|
||||
AutomationTriggerZone,
|
||||
AutomationTriggerDevice
|
||||
])
|
||||
@@ -5,7 +5,7 @@ export const useDelayEffect = (cb: () => void, deps: any[], timeout: number) =>
|
||||
const fcn = useDelayedFunction(cb, timeout);
|
||||
useEffect(() => {
|
||||
fcn()
|
||||
}, [deps])
|
||||
}, deps)
|
||||
}
|
||||
|
||||
export const useDelayedFunction = <FcnParams extends any[]>(
|
||||
|
||||
@@ -16,8 +16,6 @@ export default function useWindowSize(mobileRatioInd: number = 0.8): Size {
|
||||
const [windowSize, setWindowSize] = useState<Size>(makeUpdate(window, mobileRatioInd));
|
||||
|
||||
// alias
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
// Handler to call on window resize
|
||||
function handleResize() {
|
||||
@@ -33,7 +31,7 @@ export default function useWindowSize(mobileRatioInd: number = 0.8): Size {
|
||||
|
||||
// Remove event listener on cleanup
|
||||
return () => window.removeEventListener("resize", handleResize);
|
||||
}, []); // Empty array ensures that effect is only run on mount
|
||||
}, [mobileRatioInd]); // Empty array ensures that effect is only run on mount
|
||||
|
||||
return windowSize;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user