diff --git a/webapp/src/components/MultiNodeEditors/index.stories.tsx b/webapp/src/components/MultiNodeEditors/index.stories.tsx new file mode 100644 index 0000000..65f9cc4 --- /dev/null +++ b/webapp/src/components/MultiNodeEditors/index.stories.tsx @@ -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 + +export const Simple: ComponentStory = args => { + const { classes } = usePageTheme({}); + return
+ +
+} +export const InAModal: ComponentStory = props => { + const { classes } = usePageTheme({}); + const [open, setOpen] = useState(false); + const [state, setState] = useState(props.sequence); + return
+ + + 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) + }} + /> + +
+}; diff --git a/webapp/src/components/MultiNodeEditors/index.tsx b/webapp/src/components/MultiNodeEditors/index.tsx new file mode 100644 index 0000000..39482ae --- /dev/null +++ b/webapp/src/components/MultiNodeEditors/index.tsx @@ -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; + 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 = ({ + sequence, + allowedTypes = ['action', 'condition'], + onClose, + onAdd = () => { }, + onSave = () => { }, + onRemove = () => { }, +}) => { + const [slide, setSlide] = useState(0); + // components + const nextOrAddBtn = ; + // branch + if (sequence.length === 0) { + return
+
+ {nextOrAddBtn} +
+ +
+ } + return <> +
+ + {slide + 1}/{sequence.length} + + {nextOrAddBtn} +
+ onSave(slide, n)} + /> + +}