some fixes for invalid nodes
This commit is contained in:
1 parent
afb765024d
commit
55bb941399
14 files changed
+192
-107
No files matched your search
@@ -2,42 +2,44 @@ import { Dispatch, SetStateAction, useEffect, useState } from 'react';
|
||||
import { AutomationNode, AutomationNodeTypes } from 'types/automations';
|
||||
import { AutomationNodeSubtype } from 'types/automations';
|
||||
import { getOptionManager } from './getOptionManager';
|
||||
import { getNodeType, getNodeSubType, getSubTypeList, validateNode } from 'utils/automations';
|
||||
import { getNodeSubType, getSubTypeList, validateNode } from 'utils/automations';
|
||||
import InputYaml from 'components/Inputs/InputYaml';
|
||||
import InputText from 'components/Inputs/InputText';
|
||||
import { getDescriptionFromAutomationNode } from 'utils/formatting';
|
||||
import { useHAEntities } from 'haService';
|
||||
import { Generic } from './Generic';
|
||||
import { getNodeTypeAndValidate } from '../../../utils/automations';
|
||||
|
||||
type UseState = <S>(s: S) => [S, Dispatch<SetStateAction<S>>]
|
||||
|
||||
export const useEditorNodeState = (originalNode: AutomationNode, isErrored: boolean, uState: UseState = useState) => {
|
||||
export const useEditorNodeState = (originalNode: AutomationNode, isErrored: boolean, allowedTypes: AutomationNodeTypes[], uState: UseState = useState) => {
|
||||
// state
|
||||
const [allState, setAllState] = uState({
|
||||
yamlMode: false,
|
||||
isModified: false,
|
||||
...computeInvalidYaml(originalNode)
|
||||
...computeInvalidYaml(originalNode, allowedTypes)
|
||||
});
|
||||
const entities = useHAEntities();
|
||||
// aliases
|
||||
const { node: currentNode, isModified, yamlMode, invalidManualYaml } = allState;
|
||||
// effects
|
||||
useEffect(() => {
|
||||
setAllState({ ...allState, node: originalNode, isModified: false, })
|
||||
// eslint-disable-next-line
|
||||
}, [originalNode])
|
||||
// function aliases
|
||||
const setState = (node: AutomationNode) => setAllState(({
|
||||
...allState, node,
|
||||
isModified: JSON.stringify(node) !== JSON.stringify(originalNode),
|
||||
}));
|
||||
const setYamlMode = (yamlMode: boolean) => setAllState({ ...allState, yamlMode, });
|
||||
const setStateForManualYaml = (yaml: any) => {
|
||||
try {
|
||||
createData(yaml, yaml)
|
||||
setAllState({
|
||||
...allState,
|
||||
...computeInvalidYaml(yaml)
|
||||
})
|
||||
} catch (err) {
|
||||
setAllState({ ...allState, invalidManualYaml: <>{String(err)}</>, isErrored: true, })
|
||||
}
|
||||
setAllState({
|
||||
...allState,
|
||||
...computeInvalidYaml(yaml, allowedTypes)
|
||||
})
|
||||
}
|
||||
// value aliases
|
||||
const {
|
||||
originalNodeType,
|
||||
originalNodeSubType,
|
||||
@@ -45,7 +47,8 @@ export const useEditorNodeState = (originalNode: AutomationNode, isErrored: bool
|
||||
subType,
|
||||
subTypes,
|
||||
optionManager,
|
||||
} = createData(originalNode, currentNode);
|
||||
} = createData(originalNode, currentNode, allowedTypes)
|
||||
// publuc
|
||||
return {
|
||||
nodeType,
|
||||
subType,
|
||||
@@ -54,6 +57,13 @@ export const useEditorNodeState = (originalNode: AutomationNode, isErrored: bool
|
||||
invalidManualYaml,
|
||||
isModified,
|
||||
isErrored: allState.isErrored,
|
||||
get prettyNodeType() {
|
||||
if (nodeType) {
|
||||
return `${nodeType.slice(0, 1).toUpperCase()}${nodeType.slice(1)} Type`
|
||||
} else {
|
||||
return 'n/a'
|
||||
}
|
||||
},
|
||||
get yamlMode() {
|
||||
return allState.isErrored || yamlMode
|
||||
},
|
||||
@@ -61,9 +71,11 @@ export const useEditorNodeState = (originalNode: AutomationNode, isErrored: bool
|
||||
return currentNode
|
||||
},
|
||||
renderOptionList() {
|
||||
console.log('rendering option list')
|
||||
let aliasEditor = <></>
|
||||
if (nodeType === 'trigger') {
|
||||
aliasEditor = <InputText
|
||||
key="alias"
|
||||
label="Trigger ID"
|
||||
value={(allState.node as any).id}
|
||||
onChange={id => setState({
|
||||
@@ -74,6 +86,7 @@ export const useEditorNodeState = (originalNode: AutomationNode, isErrored: bool
|
||||
}
|
||||
if (nodeType === 'action') {
|
||||
aliasEditor = <InputText
|
||||
key="alias"
|
||||
label="Alias"
|
||||
value={getDescriptionFromAutomationNode(allState.node)}
|
||||
onChange={alias => setState({
|
||||
@@ -89,8 +102,8 @@ export const useEditorNodeState = (originalNode: AutomationNode, isErrored: bool
|
||||
label=''
|
||||
value={currentNode}
|
||||
onChange={setStateForManualYaml}
|
||||
error={invalidManualYaml}
|
||||
/>
|
||||
<div className="node-editor--error">{invalidManualYaml}</div>
|
||||
</>
|
||||
}
|
||||
return <>
|
||||
@@ -108,25 +121,43 @@ export const useEditorNodeState = (originalNode: AutomationNode, isErrored: bool
|
||||
setState(getOptionManager(newType, getSubTypeList(newType)[0]).defaultState())
|
||||
}
|
||||
},
|
||||
setSubType(newSubType: AutomationNodeSubtype<typeof nodeType>) {
|
||||
setSubType(newSubType: AutomationNodeSubtype) {
|
||||
if (newSubType === originalNodeSubType) {
|
||||
setState(originalNode)
|
||||
} else {
|
||||
setState(getOptionManager(nodeType, newSubType as any).defaultState())
|
||||
if (nodeType) {
|
||||
setState(getOptionManager(nodeType, newSubType as any).defaultState())
|
||||
} else {
|
||||
setState(getOptionManager(allowedTypes[0], newSubType as any).defaultState())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const createData = (originalNode: AutomationNode, currentNode: AutomationNode) => {
|
||||
const originalNodeType = getNodeType(originalNode);
|
||||
const originalNodeSubType = getNodeSubType(originalNode);
|
||||
const nodeType = getNodeType(currentNode);
|
||||
const subType = getNodeSubType(currentNode);
|
||||
const subTypes = getSubTypeList(nodeType)
|
||||
const optionManager = getOptionManager(nodeType, subType as any);
|
||||
const createData = (originalNode: AutomationNode, currentNode: AutomationNode, allowedTypes: AutomationNodeTypes[]) => {
|
||||
let originalNodeType: AutomationNodeTypes = allowedTypes[0];
|
||||
let originalNodeSubType: AutomationNodeSubtype | null = null;
|
||||
let nodeType: AutomationNodeTypes = originalNodeType;
|
||||
let subType: AutomationNodeSubtype | null = originalNodeSubType;
|
||||
let subTypes: AutomationNodeSubtype[] = getSubTypeList(allowedTypes[0]);
|
||||
let optionManager = Generic;
|
||||
|
||||
try {
|
||||
originalNodeType = getNodeTypeAndValidate(originalNode, allowedTypes);
|
||||
originalNodeSubType = getNodeSubType(originalNode);
|
||||
} catch (err) {
|
||||
console.error(`ORIGINAL NODE ERROR ${JSON.stringify(originalNode)}`)
|
||||
}
|
||||
try {
|
||||
nodeType = getNodeTypeAndValidate(currentNode, allowedTypes);
|
||||
subType = getNodeSubType(currentNode);
|
||||
subTypes = getSubTypeList(nodeType)
|
||||
optionManager = getOptionManager(nodeType, subType as any);
|
||||
} catch (err) {
|
||||
console.error(`CURRENT NODE ERROR ${JSON.stringify(currentNode)}`)
|
||||
}
|
||||
return {
|
||||
originalNodeType,
|
||||
originalNodeSubType,
|
||||
@@ -137,13 +168,14 @@ const createData = (originalNode: AutomationNode, currentNode: AutomationNode) =
|
||||
}
|
||||
}
|
||||
|
||||
const computeInvalidYaml = (node: AutomationNode) => {
|
||||
const errors = validateNode(node);
|
||||
const computeInvalidYaml = (node: AutomationNode, allowedNodeTypes: AutomationNodeTypes[]) => {
|
||||
const errors = validateNode(node, allowedNodeTypes);
|
||||
if (errors) {
|
||||
console.log({ errors })
|
||||
return {
|
||||
node,
|
||||
isErrored: true,
|
||||
invalidManualYaml: <ul key={'error'}>
|
||||
invalidManualYaml: <ul key={'error'} className="node-editor--error">
|
||||
{errors.map(({ message, path }, i) => <li key={i}>
|
||||
<b>{path}</b>: {message}
|
||||
</li>)}
|
||||
|
||||
@@ -58,4 +58,9 @@
|
||||
max-width: 30em;
|
||||
overflow: auto;
|
||||
max-height: 10em;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
padding: 0 0.5em 0 1.5em;
|
||||
margin: 0;
|
||||
}
|
||||
@@ -29,7 +29,7 @@ export const NodeEditor: FC<NodeEditorProps> = ({
|
||||
isErrored = false,
|
||||
}) => {
|
||||
// state
|
||||
const state = useEditorNodeState(node, isErrored);
|
||||
const state = useEditorNodeState(node, isErrored, allowedTypes);
|
||||
|
||||
// alias
|
||||
const isModified = state.isModified;
|
||||
@@ -53,7 +53,7 @@ export const NodeEditor: FC<NodeEditorProps> = ({
|
||||
onChange={state.setNodeType}
|
||||
/> : <></>}
|
||||
{state.nodeType !== 'condition' ? <InputList
|
||||
label={`${state.nodeType.slice(0, 1).toUpperCase()}${state.nodeType.slice(1)} Type`}
|
||||
label={state.prettyNodeType}
|
||||
current={state.subType as any}
|
||||
options={state.subTypes}
|
||||
onChange={state.setSubType}
|
||||
|
||||
Reference in new issue
Block a user