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

@@ -12,4 +12,8 @@
.multinode-editor.modded>.multinode-editor--navbar * {
border-color: var(--green-light);
}
.multinode-editor.modded .multinode-editor--total {
color: var(--green-light)
}
@@ -10,6 +10,7 @@ export default {
component: MultiNodeEditor,
parameters: { actions: { argTypesRegex: '^on.*' } },
args: {
allowedTypes: ['action', 'condition', 'trigger'],
sequence: [
{
"condition": "numeric_state",
@@ -28,10 +29,14 @@ export default {
} as ComponentMeta<typeof MultiNodeEditor>
export const Simple: ComponentStory<typeof MultiNodeEditor> = args => {
const [state, setState] = useState(args.sequence);
return <div className="page">
<div className="center column" style={{ paddingTop: "1em" }}>
<MultiNodeEditor {...args} />
<MultiNodeEditor {...args} sequence={state} onSave={s => {
args.onSave && args.onSave(s)
setState(s)
}} />
</div>
</div>
}
@@ -39,6 +44,7 @@ export const InAModal: ComponentStory<typeof MultiNodeEditor> = props => {
const [open, setOpen] = useState(false);
const [state, setState] = useState(props.sequence);
console.log({ state })
return <div className="page">
<Button onClick={() => setOpen(!open)}>Open Editor</Button>
<Modal open={open}>
@@ -46,8 +52,19 @@ export const InAModal: ComponentStory<typeof MultiNodeEditor> = props => {
{...props}
sequence={state}
onClose={() => setOpen(!open)}
onSave={setState}
onSave={s => {
props.onSave && props.onSave(s)
setState(s)
}}
/>
</Modal>
</div>
};
export const EmptyConditionEditor = Simple.bind({})
EmptyConditionEditor.args = {
...EmptyConditionEditor.args,
allowedTypes: ['condition'],
sequence: [],
}
@@ -1,100 +1,24 @@
import "./index.css";
import { FC, useState } from 'react';
import { FC, useEffect, useState } from 'react';
import { AutomationNode, AutomationNodeMapping } from 'types/automations';
import { NodeEditor } from 'components/NodeEditor';
import { Button } from 'components/Inputs/Button';
import { AutomationTriggerDevice } from 'types/automations/triggers';
import { useMultiNodeEditorState } from "./state";
import { MultiNodeEditorProps } from "./types";
const makeDefault = (nodeType: keyof AutomationNodeMapping): AutomationNode => {
switch (nodeType) {
case 'action':
return {
'data': {},
'service': '',
'target': {},
}
case 'condition':
return {
condition: 'and',
conditions: []
}
default:
return {
platform: 'device',
device_id: '',
domain: '',
type: '',
subtype: '',
} as AutomationTriggerDevice
}
}
export interface MultiNodeEditorProps {
sequence: AutomationNode[];
allowedTypes?: Array<keyof AutomationNodeMapping>;
onClose?: () => void;
onSave?: (n: AutomationNode[]) => void;
}
export const MultiNodeEditor: FC<MultiNodeEditorProps> = ({
sequence: initialState,
allowedTypes = ['action', 'condition'],
onClose: _onClose = () => { },
onSave = () => { },
}) => {
export const MultiNodeEditor: FC<MultiNodeEditorProps> = props => {
// state
const [{ slide, state, isModified, isReady }, setState] = useState({
slide: 0,
state: initialState,
isModified: false,
isReady: true,
})
// alias
const areYouSureItsNotReady = () => !isReady ?
window.confirm("The node is not ready, are you sure you want to move?") :
true
const areYouSureItsModified = () => !isModified ?
window.confirm("If you close this without saving, you will lose all progress!") :
true
const onClose = () => areYouSureItsModified() ?
_onClose() :
null
const onAdd = () => setState({
slide: state.length,
state: [...state, makeDefault(allowedTypes[0])],
isReady,
isModified: true,
});
const onRemove = () => setState({
slide: slide > 0 ? slide - 1 : 0,
state: [...state.slice(0, slide), ...state.slice(slide + 1)],
isReady,
isModified: true,
});
const onAllSave = (n: AutomationNode) => onSave([
...state.slice(0, slide), n, ...state.slice(slide + 1)
]);
const onNextOrAdd = () => {
if (!areYouSureItsNotReady()) {
return
}
if (slide >= state.length - 1) {
onAdd()
} else {
setState({ slide: slide + 1, state, isReady, isModified, })
}
}
const onBack = () => areYouSureItsNotReady() &&
setState({ slide: slide - 1, state, isReady, isModified })
const state = useMultiNodeEditorState(props)
// components
const nextOrAddBtn = <Button
onClick={onNextOrAdd}
onClick={state.moveForward}
>
{(slide < state.length - 1) ? ">>" : "+"}
{!state.isLast ? ">>" : "+"}
</Button>;
// branch
if (state.length === 0) {
if (state.isEmpty) {
return <div className='column center' style={{
minWidth: '20vw',
}}>
@@ -107,30 +31,26 @@ export const MultiNodeEditor: FC<MultiNodeEditorProps> = ({
{nextOrAddBtn}
</div>
<div className='column center' style={{ marginTop: '1em' }}>
<Button onClick={onClose}>Close</Button>
<Button onClick={() => onSave(state)}>Save</Button>
<Button onClick={state.onClose}>Close</Button>
</div>
</div>
}
return <div className={["multinode-editor", isModified ? "modded" : ''].join(' ')}>
return <div className={["multinode-editor", state.isModified ? "modded" : ''].join(' ')}>
<div className="multinode-editor--navbar">
<Button onClick={onBack} disabled={slide === 0}>{"<<"}</Button>
<Button onClick={onRemove}> Delete</Button>
<Button onClick={state.moveBack} disabled={state.currentSlideNumber === 0}>{"<<"}</Button>
<Button onClick={state.onRemove}> Delete</Button>
{nextOrAddBtn}
</div>
<NodeEditor
key={slide}
node={state[slide]}
allowedTypes={allowedTypes}
onClose={onClose}
onSave={onAllSave}
onFlags={
(ir) => (ir !== isReady) &&
setState({ slide, state, isReady: ir, isModified })
}
key={state.currentSlideNumber}
node={state.currentSlide}
allowedTypes={props.allowedTypes}
onClose={state.onClose}
onSave={state.onSave}
onFlags={state.updateInternalNode}
/>
<div className='column center'>
<span>{slide + 1}/{state.length}</span>
<span>{state.currentSlideNumber + 1}/<u className="multinode-editor--total">{state.totalSlides}</u></span>
</div>
</div>
}
@@ -0,0 +1,154 @@
import { MultiNodeEditorProps } from "./types";
import { useState, useRef } from 'react';
import { AutomationNode } from '../../types/automations/index';
import { makeDefault } from "utils/automations";
export type MultiNodeEditorInternalState = {
slides: AutomationNode[];
slide: number;
multiNode: {
isModified: boolean,
isSaved: boolean,
},
internalNodeModified: boolean,
};
export const useMultiNodeEditorState = (props: MultiNodeEditorProps) => {
const [state, _setState] = useState(makeInitialState({
slides: props.sequence
}))
const justSaved = useRef(false);
const {
slide,
slides,
multiNode,
internalNodeModified,
} = state;
const setState = (a: MultiNodeEditorInternalState) => {
console.log('setting state', a)
_setState(a)
}
return {
get totalSlides() {
return slides.length
},
get currentSlideNumber() {
return slide
},
get currentSlide() {
return slides[slide]
},
get isLast() {
return slide >= slides.length - 1
},
get isEmpty() {
return slides.length === 0
},
get isModified() {
return (internalNodeModified || multiNode.isModified) && !multiNode.isSaved
},
moveBack() {
if (slide <= 0) {
return
}
if (internalNodeModified && !multiNode.isSaved) {
if (!window.confirm("Moving back without saving will cause you to lose the work, are you sure you want to do that?")) {
return
}
}
setState({ ...state, slide: slide - 1, internalNodeModified: false, })
},
moveForward() {
const createNew = slide >= slides.length - 1;
if (internalNodeModified && !multiNode.isSaved) {
const action = createNew ? 'Creating a new new node' : 'Moving forward'
if (!window.confirm(`${action} without saving will cause you to lose the work, are you sure you want to do that?`)) {
return
}
}
if (createNew) {
setState({
...state,
slide: slides.length,
slides: slides.concat([makeDefault(props.allowedTypes[0])]),
internalNodeModified: false,
multiNode: {
isModified: true,
isSaved: false,
}
})
} else {
setState({ ...state, slide: slide + 1, internalNodeModified: false, })
}
},
updateInternalNode(isReady: boolean, isModified: boolean) {
if (!isReady || isModified) {
if (!state.multiNode.isModified || state.multiNode.isSaved) {
if (justSaved.current) {
justSaved.current = false
} else {
setState({
...state,
multiNode: { isModified: true, isSaved: false, },
internalNodeModified: true,
})
}
}
}
},
onRemove() {
setState({
...state,
slide: Math.max(slide - 1, 0),
slides: slides
.slice(0, slide)
.concat(
slides.slice(slide + 1)
),
multiNode: {
isModified: multiNode.isModified,
isSaved: false,
}
})
},
onSave(node: AutomationNode) {
justSaved.current = true
setState({
...state,
slides: slides
.slice(0, slide)
.concat([node])
.concat(
slides.slice(slide + 1)
),
multiNode: {
isModified: multiNode.isModified,
isSaved: true,
},
internalNodeModified: false,
})
},
onClose() {
if (!multiNode.isSaved) {
if (!window.confirm("You haven't saved your work, are you ok with losing it?")) {
return
}
}
props.onSave && props.onSave(slides)
props.onClose && props.onClose()
}
}
}
const makeInitialState = (props: Partial<MultiNodeEditorInternalState>): MultiNodeEditorInternalState => ({
slides: [],
slide: 0,
multiNode: {
isModified: false,
isSaved: false,
},
internalNodeModified: false,
...props,
})
@@ -0,0 +1,8 @@
import { AutomationNode, AutomationNodeMapping } from "types/automations";
export interface MultiNodeEditorProps {
sequence: AutomationNode[];
allowedTypes: Array<keyof AutomationNodeMapping>;
onClose?: () => void;
onSave?: (n: AutomationNode[]) => void;
}