multinode editor
This commit is contained in:
1 parent
fde0f49688
commit
8dfbb9faa9
2 files changed
+177
No files matched your search
@@ -0,0 +1,65 @@
|
|||||||
|
import React, { useState } from "react";
|
||||||
|
import { ComponentMeta, ComponentStory } from '@storybook/react';
|
||||||
|
import { MultiNodeEditor } from ".";
|
||||||
|
import { usePageTheme } from 'styles/page';
|
||||||
|
import { Button } from "components/Inputs/Button";
|
||||||
|
import { Modal } from "components/Modal";
|
||||||
|
|
||||||
|
|
||||||
|
export default {
|
||||||
|
title: 'MultiNodeEditor',
|
||||||
|
component: MultiNodeEditor,
|
||||||
|
parameters: { actions: { argTypesRegex: '^on.*' } },
|
||||||
|
args: {
|
||||||
|
sequence: [
|
||||||
|
{
|
||||||
|
"$smType": "condition",
|
||||||
|
"condition": "numeric_state",
|
||||||
|
"condition_data": {
|
||||||
|
"entity_id": [
|
||||||
|
"sensor.kitchen_humidity"
|
||||||
|
],
|
||||||
|
"conditions": [],
|
||||||
|
"above": "40"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"$smType": "condition",
|
||||||
|
"condition": "template",
|
||||||
|
"condition_data": {
|
||||||
|
"value_template": "states('switch.kitchen') == 'on'"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
} as ComponentMeta<typeof MultiNodeEditor>
|
||||||
|
|
||||||
|
export const Simple: ComponentStory<typeof MultiNodeEditor> = args => {
|
||||||
|
const { classes } = usePageTheme({});
|
||||||
|
return <div className={classes.page}>
|
||||||
|
<MultiNodeEditor {...args} />
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
export const InAModal: ComponentStory<typeof MultiNodeEditor> = props => {
|
||||||
|
const { classes } = usePageTheme({});
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
const [state, setState] = useState(props.sequence);
|
||||||
|
return <div className={classes.page}>
|
||||||
|
<Button onClick={() => setOpen(!open)}>Open Editor</Button>
|
||||||
|
<Modal open={open}>
|
||||||
|
<MultiNodeEditor
|
||||||
|
{...props}
|
||||||
|
sequence={state}
|
||||||
|
onClose={() => setOpen(!open)}
|
||||||
|
onAdd={(n, slide) => { setState([...state, n]); slide(state.length) }}
|
||||||
|
onRemove={(n, slide) => {
|
||||||
|
setState([
|
||||||
|
...state.slice(0, n),
|
||||||
|
...state.slice(n + 1)
|
||||||
|
]);
|
||||||
|
slide(state.length - 2)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</Modal>
|
||||||
|
</div>
|
||||||
|
};
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
import { FC, 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';
|
||||||
|
|
||||||
|
|
||||||
|
const makeDefault = (nodeType: keyof AutomationNodeMapping): AutomationNode => {
|
||||||
|
switch (nodeType) {
|
||||||
|
case 'action':
|
||||||
|
return {
|
||||||
|
$smType: 'action',
|
||||||
|
action: "service",
|
||||||
|
action_data: {
|
||||||
|
'data': {},
|
||||||
|
'service': '',
|
||||||
|
'target': {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case 'condition':
|
||||||
|
return {
|
||||||
|
$smType: 'condition',
|
||||||
|
condition: 'and',
|
||||||
|
condition_data: {
|
||||||
|
conditions: []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return {
|
||||||
|
platform: 'device',
|
||||||
|
device_id: '',
|
||||||
|
domain: '',
|
||||||
|
type: '',
|
||||||
|
subtype: '',
|
||||||
|
} as AutomationTriggerDevice
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Props {
|
||||||
|
sequence: AutomationNode[];
|
||||||
|
allowedTypes?: Array<keyof AutomationNodeMapping>;
|
||||||
|
onAdd?: (n: AutomationNode, slide: (i: number) => void) => void;
|
||||||
|
onClose?: () => void;
|
||||||
|
onSave?: (i: number, n: AutomationNode) => void;
|
||||||
|
onRemove?: (i: number, slide: (i: number) => void) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const MultiNodeEditor: FC<Props> = ({
|
||||||
|
sequence,
|
||||||
|
allowedTypes = ['action', 'condition'],
|
||||||
|
onClose,
|
||||||
|
onAdd = () => { },
|
||||||
|
onSave = () => { },
|
||||||
|
onRemove = () => { },
|
||||||
|
}) => {
|
||||||
|
const [slide, setSlide] = useState(0);
|
||||||
|
// components
|
||||||
|
const nextOrAddBtn = <Button
|
||||||
|
onClick={() => {
|
||||||
|
if (slide >= sequence.length - 1) {
|
||||||
|
onAdd(makeDefault(allowedTypes[0]), setSlide)
|
||||||
|
} else {
|
||||||
|
setSlide(slide + 1)
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{(slide < sequence.length - 1) ? ">>" : "+"}
|
||||||
|
</Button>;
|
||||||
|
// branch
|
||||||
|
if (sequence.length === 0) {
|
||||||
|
return <div style={{
|
||||||
|
display: 'flex',
|
||||||
|
flexDirection: 'column',
|
||||||
|
flex: 1,
|
||||||
|
justifyContent: 'center',
|
||||||
|
}}>
|
||||||
|
<div style={{
|
||||||
|
flex: 1,
|
||||||
|
display: 'flex',
|
||||||
|
justifyContent: 'center',
|
||||||
|
alignItems: 'flex-start',
|
||||||
|
}}>
|
||||||
|
{nextOrAddBtn}
|
||||||
|
</div>
|
||||||
|
<Button onClick={onClose}>Close</Button>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
return <>
|
||||||
|
<div style={{
|
||||||
|
display: 'flex',
|
||||||
|
flexDirection: 'row',
|
||||||
|
justifyContent: 'center',
|
||||||
|
alignItems: 'center',
|
||||||
|
}}>
|
||||||
|
<Button onClick={() => setSlide(slide - 1)} disabled={slide === 0}>{"<<"}</Button>
|
||||||
|
<span>{slide + 1}/{sequence.length}</span>
|
||||||
|
<Button
|
||||||
|
onClick={() => onRemove(slide, setSlide)}
|
||||||
|
>
|
||||||
|
Delete
|
||||||
|
</Button>
|
||||||
|
{nextOrAddBtn}
|
||||||
|
</div>
|
||||||
|
<NodeEditor
|
||||||
|
key={slide}
|
||||||
|
node={sequence[slide]}
|
||||||
|
allowedTypes={allowedTypes}
|
||||||
|
onClose={onClose}
|
||||||
|
onSave={n => onSave(slide, n)}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
}
|
||||||
Reference in new issue
Block a user