cleaned up styling for editor

This commit is contained in:
ari committed 2022-05-13 17:26:44 -04:00
1 parent bfc95db6f6
commit 4cfc429980
25 files changed
+532 -230

No files matched your search

@@ -1,57 +0,0 @@
import { useEffect, useState } from 'react';
import { AutomationNode, AutomationNodeTypes } from 'types/automations';
import { AutomationNodeSubtype } from 'types/automations';
import { getOptionManager } from './getOptionManager';
import { getNodeType, getSubTypeList } from 'utils/automations';
import { getNodeSubType } from '../constants';
export const useEditorNodeState = (node: AutomationNode) => {
const [{ node: state, isModified }, setAllState] = useState({
node,
isModified: false,
});
useEffect(() => {
setAllState({ node, isModified: false })
}, [node])
const setState = (node: AutomationNode) => setAllState(({ node, isModified: true }));
const originalNodeType = getNodeType(node);
const originalNodeSubType = getNodeSubType(node);
const nodeType = getNodeType(state);
const subType = getNodeSubType(state);
const subTypes = getSubTypeList(nodeType)
const optionManager = getOptionManager(nodeType, subType as any);
return {
nodeType,
subType,
subTypes,
get isModified() {
return isModified
},
get data() {
return state
},
renderOptionList() {
return optionManager.renderOptionList(state, setState);
},
isReady() {
return optionManager.isReady(state);
},
setNodeType(newType: AutomationNodeTypes) {
if (newType === originalNodeType) {
setState(node)
} else {
setState(getOptionManager(newType, getSubTypeList(newType)[0]).defaultState())
}
},
setSubType(newSubType: AutomationNodeSubtype<typeof nodeType>) {
if (newSubType === originalNodeSubType) {
setState(node)
} else {
setState(getOptionManager(nodeType, newSubType as any).defaultState())
}
}
}
}
@@ -0,0 +1,106 @@
import { Dispatch, SetStateAction, useEffect, useState } from 'react';
import { AutomationNode, AutomationNodeTypes } from 'types/automations';
import { AutomationNodeSubtype } from 'types/automations';
import { getOptionManager } from './getOptionManager';
import { getNodeType, getSubTypeList } from 'utils/automations';
import { getNodeSubType } from '../constants';
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,
yamlMode: false,
isModified: false,
invalidManualYaml: "",
});
const { node: currentNode, isModified, yamlMode, invalidManualYaml } = allState;
useEffect(() => {
setAllState({ ...allState, node: originalNode, isModified: false, })
}, [originalNode])
const setState = (node: AutomationNode) => setAllState(({ ...allState, node, isModified: true, }));
const setYamlMode = (yamlMode: boolean) => setAllState({ ...allState, yamlMode, });
const setStateForManualYaml = (yaml: any) => {
try {
createData(yaml, yaml)
setAllState({ ...allState, node: yaml, invalidManualYaml: '' })
} catch (err) {
setAllState({ ...allState, invalidManualYaml: String(err) })
}
}
const {
originalNodeType,
originalNodeSubType,
nodeType,
subType,
subTypes,
optionManager,
} = createData(originalNode, currentNode);
return {
nodeType,
subType,
subTypes,
yamlMode,
setYamlMode,
invalidManualYaml,
isModified,
get data() {
return currentNode
},
renderOptionList() {
if (yamlMode) {
return <>
<InputYaml
label=''
value={currentNode}
onChange={setStateForManualYaml}
resizable
/>
<span className="node-editor--error">{invalidManualYaml}</span>
</>
}
return optionManager.renderOptionList(currentNode, setState);
},
isReady() {
return optionManager.isReady(currentNode);
},
setNodeType(newType: AutomationNodeTypes) {
if (newType === originalNodeType) {
setState(originalNode)
} else {
setState(getOptionManager(newType, getSubTypeList(newType)[0]).defaultState())
}
},
setSubType(newSubType: AutomationNodeSubtype<typeof nodeType>) {
if (newSubType === originalNodeSubType) {
setState(originalNode)
} else {
setState(getOptionManager(nodeType, 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);
return {
originalNodeType,
originalNodeSubType,
nodeType,
subType,
subTypes,
optionManager,
}
}
+18 -11
View File
@@ -1,12 +1,13 @@
import "./index.css";
import { FC } from "react";
import { FC } from 'react';
import { AutomationNode, AutomationNodeMapping } from 'types/automations/index';
import { useEditorNodeState } from "./OptionManager";
import { InputList } from "components/Inputs/InputList";
import { Button } from "components/Inputs/Button";
import InputBoolean from "components/Inputs/InputBoolean";
export interface Props {
export interface NodeEditorProps {
node: AutomationNode
allowedTypes?: Array<keyof AutomationNodeMapping>;
onClose?: () => void;
@@ -15,7 +16,7 @@ export interface Props {
saveBtnCreateText?: boolean;
}
export const NodeEditor: FC<Props> = ({
export const NodeEditor: FC<NodeEditorProps> = ({
node,
allowedTypes = ["action", "condition", "trigger"],
onClose = () => { },
@@ -30,15 +31,14 @@ export const NodeEditor: FC<Props> = ({
// alias
const isModified = state.isModified;
const isReady = state.isReady();
const areYouSureNotReady = () => !isReady ?
window.confirm("This node is missing some values, are you sure you want to save?") :
const areYouSureNotReady = (what: string) => !isReady ?
window.confirm(`This node is missing some values, are you sure you want to ${what}?`) :
true;
// events
onFlags(isReady, isModified);
// render
return <div className={["node-editor--root", isModified ? "modded" : "", !isReady ? 'not-ready' : ''].join(" ")}>
return <div className={["node-editor--root", isModified ? "modded" : "", (!isReady && isModified) ? 'not-ready' : ''].join(" ")}>
<div className="node-editor--body">
<div className="node-editor--body-title">
{allowedTypes.length > 1 ? <InputList
@@ -53,14 +53,21 @@ export const NodeEditor: FC<Props> = ({
options={state.subTypes}
onChange={state.setSubType}
/> : <></>}
<InputBoolean label="Yaml" onChange={state.setYamlMode} value={state.yamlMode} />
</div>
{state.renderOptionList()}
</div>
<div className="node-editor--footer">
<Button className="node-editor--footer--close" onClick={onClose}>Close</Button>
<Button className="node-editor--footer--save" onClick={() => {
areYouSureNotReady() && onSave(state.data)
}} title={!isReady ? "Some fields have not been properly filled up" : ""}>
<Button className="node-editor--footer--close" onClick={() => areYouSureNotReady('close') && onClose()}>Close</Button>
<Button
className="node-editor--footer--save"
onClick={() => {
areYouSureNotReady('save') && onSave(state.data)
}}
title={
!isReady ? "Some fields have not been properly filled up" : ""
}
>
{saveBtnCreateText ? "Create" : "Save"}
</Button>
{children}