ported parts of node editor

This commit is contained in:
ari committed 2021-12-01 17:07:43 -05:00
1 parent 0764feb432
commit 59542abad7
11 files changed
+227 -75

No files matched your search

-19
View File
@@ -1,25 +1,6 @@
import { ButtonHTMLAttributes, FC } from "react";
import { createAppUseStyles } from "styles/theme";
import { useButtonStyles } from "./styles";
// const useButtonStyles = createAppUseStyles(theme => ({
// button: {
// backgroundColor: theme.secondary,
// color: theme.primaryAccent,
// borderColor: theme.primaryAccent,
// borderStyle: 'solid',
// borderWidth: 1,
// padding: '2.5%',
// marginLeft: '5%',
// marginRight: '5%',
// borderRadius: '5px',
// cursor: 'pointer',
// '&:hover': {
// backgroundColor: theme.primary,
// borderColor: theme.secondaryAccent,
// }
// }
// }))
export const Button: FC<ButtonHTMLAttributes<HTMLButtonElement>> = props => {
const { classes } = useButtonStyles({});
return <button {...props} className={`${classes.input} ${props.className ?? ''}`} />
+22 -22
View File
@@ -3,28 +3,28 @@ import { useInputListStyles } from "./styles";
export interface Props<T extends string> {
current: T;
options: readonly T[];
onChange: (n: T) => void;
id?: string;
className?: string;
label: string;
current: T;
options: readonly T[];
onChange: (n: T) => void;
id?: string;
className?: string;
label: string;
}
export default function InputList<T extends string>({
current, options, onChange, label,
...selattr
current, options, onChange, label,
...selattr
}: Props<T>) {
const {classes} = useInputListStyles({});
return <InputWrapper label={label} labelSize={'small'} noMargin>
<select
className={classes.input}
{...selattr} value={current}
onChange={e => {
e.preventDefault();
onChange(e.currentTarget.value);
}}
>
{options.map((t, i) => <option id={String(i)} key={i}>{t}</option>)}
</select>
</InputWrapper>
}
const { classes } = useInputListStyles({});
return <InputWrapper label={label} labelSize={'small'} noMargin>
<select
className={classes.input}
{...selattr} value={current}
onChange={e => {
e.preventDefault();
onChange(e.currentTarget.value as any);
}}
>
{options.map((t, i) => <option id={String(i)} key={i}>{t}</option>)}
</select>
</InputWrapper>
}
+6 -1
View File
@@ -28,7 +28,11 @@ const commonInputStyle = (theme: AppTheme) => ({
})
export const useButtonStyles = createAppUseStyles<{}>(theme => ({
...commonInputStyle(theme),
input: {
...commonInputStyle(theme).input,
paddingTop: '2%',
paddingBottom: '2%',
},
}));
export const useInputWrapperStyles = createAppUseStyles<{
@@ -50,6 +54,7 @@ export const useInputWrapperStyles = createAppUseStyles<{
top: labelSize === 'normal' ? '0.5em' : '0.25em',
fontWeight: 'bold',
transition: 'all 200ms',
color: theme.primaryAccent,
})
}
})
@@ -0,0 +1,53 @@
import { AutomationNodeSubtype, AutomationNodeTypes } from "types/automations";
const actionTypes = [
'choose',
'device',
'event',
'repeat',
'service',
'wait',
] as AutomationNodeSubtype<'action'>[];
const conditionTypes = [
'and',
'not',
'numeric_state',
'or',
'state',
'template',
'time',
'trigger',
'zone',
] as AutomationNodeSubtype<'condition'>[];
const triggerTypes = [
'device',
'event',
'homeassistant',
'mqtt',
'numeric_state',
'state',
'tag',
'template',
'time',
'time_pattern',
'webhook',
'zone',
] as AutomationNodeSubtype<'trigger'>[];
export const getSubTypeList = <T extends AutomationNodeTypes>(
nodeType: T
): AutomationNodeSubtype<T>[] => {
if (nodeType === 'action') {
return actionTypes as any;
} else if (nodeType === 'condition') {
return conditionTypes as any;
} else if (nodeType === 'trigger') {
return triggerTypes as any;
} else if (nodeType === 'sequence') {
return actionTypes.concat(conditionTypes) as any;
}
return [];
}
@@ -0,0 +1,22 @@
import React from "react";
import { ComponentMeta, ComponentStory } from '@storybook/react';
import { NodeEditor } from ".";
import { usePageTheme } from "styles/page";
export default {
title: 'NodeEditor',
component: NodeEditor,
parameters: { actions: { argTypesRegex: '^on.*' } },
args: {
nodeType: 'sequence',
subType: 'and',
}
} as ComponentMeta<typeof NodeEditor>
export const Basic: ComponentStory<typeof NodeEditor> = props => {
const { classes } = usePageTheme({});
return <div className={classes.page}>
<NodeEditor {...props} />
</div>
};
@@ -0,0 +1,57 @@
import { Button } from "components/Inputs/Button";
import { useState, useEffect } from "react";
import { AutomationNodeTypes, AutomationNode, AutomationNodeSubtype } from "types/automations";
import { useNodeEditorStyles } from './styles';
import { getSubTypeList } from './constants';
import InputList from "components/Inputs/InputList";
export interface Props<T extends AutomationNodeTypes> {
nodeType: T,
subType: AutomationNodeSubtype<T>,
onClose: () => void,
onUpdate: (node: AutomationNode[T]) => void,
}
export function NodeEditor<T extends AutomationNodeTypes>({
nodeType,
subType,
onClose,
onUpdate,
}: Props<T>) {
const { classes } = useNodeEditorStyles({});
const currentSubTypeList = getSubTypeList(nodeType);
const [selectedSubType, setSubType] = useState<AutomationNodeSubtype>(subType);
// const stateManager = new StateManager(node_type, selectedSubType);
// useEffect(() => {
// if (stateManager.nodeType !== node_type || stateManager.nodeSubtype !== selectedSubType) {
// stateManager.swapBaseState(node_type, selectedSubType)
// }
// }, [node_type, selectedSubType]);
return <div className={classes.root}>
<div className={classes.background} onClick={() => onClose()}></div>
<div className={classes.inner} onClick={e => e.preventDefault()}>
<div className={classes.title}>
Create {nodeType[0].toUpperCase()}{nodeType.slice(1)}
</div>
<div className={classes.body}>
<InputList
label="Type:"
options={currentSubTypeList}
current={selectedSubType}
onChange={setSubType}
/>
{/*{stateManager.renderOptionList()} */}
</div>
<div className={classes.footer}>
<Button onClick={() => onClose()}>Close</Button>
<Button
// onClick={() => onUpdate(stateManager.state)}
// disabled={!stateManager.isReady}
>
Save
</Button>
</div>
</div>
</div>
}
@@ -0,0 +1,54 @@
import Color from "chroma-js";
import { createAppUseStyles } from "styles/theme";
export const useNodeEditorStyles = createAppUseStyles<{
}>(theme => ({
root: {
position: 'absolute',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
width: '100vw',
height: '100vh',
left: '0',
top: '0',
},
background: {
position: 'absolute',
zIndex: 0,
width: '100vw',
height: '100vw',
backgroundColor: Color(theme.secondary).alpha(0.5).hex(),
},
inner: {
zIndex: 1,
maxWidth: '80%',
maxHeight: '60%',
minHeight: '200px',
minWidth: '200px',
borderRadius: '20px',
padding: '15px',
display: 'flex',
flexDirection: 'column',
backgroundColor: theme.primary,
borderSize: '1px',
borderStyle: 'solid',
borderColor: theme.primaryAccent,
},
title: {
textAlign: 'center',
padding: 10,
fontWeight: 'bold',
color: theme.secondaryAccent,
},
body: {
flexGrow: 1
},
footer: {
display: 'flex',
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
}
}))
+1 -9
View File
@@ -52,12 +52,4 @@ export type AutomationAction =
| DeviceAction
| ChooseAction
;
export const ActionTypes = [
'Service',
'Repeat',
'Wait',
'Event',
'Device',
'Choose'
] as const;
export type ActionType = typeof ActionTypes[number];
export type ActionType = AutomationAction['action'];
@@ -48,3 +48,4 @@ export type AutomationCondition =
| TriggerCondition
| ZoneCondition
;
export type ConditionType = AutomationCondition['condition'];
+10 -4
View File
@@ -1,6 +1,6 @@
import { AutomationAction } from "./actions";
import { AutomationCondition } from "./conditions";
import { AutomationTrigger } from "./triggers";
import { ActionType, AutomationAction } from "./actions";
import { AutomationCondition, ConditionType } from "./conditions";
import { AutomationTrigger, TriggerType } from "./triggers";
export interface AutomationMetadata {
@@ -23,4 +23,10 @@ type AutomationNodeMapping = {
"sequence": AutomationSequenceNode;
}
export type AutomationNodeTypes = keyof AutomationNodeMapping;
export type AutomationNode<N extends AutomationNodeTypes = any> = AutomationNodeMapping[N];
export type AutomationNode<N extends AutomationNodeTypes = any> = AutomationNodeMapping[N];
export type AutomationNodeSubtype<T extends AutomationNodeTypes = any> =
T extends 'trigger' ? TriggerType :
T extends 'action' ? ActionType :
T extends 'condtion' ? ConditionType :
T extends 'sequence' ? ConditionType | ActionType :
never;
+1 -20
View File
@@ -74,9 +74,6 @@ export interface AutomationTriggerDevice extends AutomationTriggerBase {
type: string;
subtype: string;
}
export interface AutomationTriggerGeneric extends AutomationTriggerBase {
platform: string;
}
export type AutomationTrigger =
| AutomationTriggerEvent
| AutomationTriggerHA
@@ -90,21 +87,5 @@ export type AutomationTrigger =
| AutomationTriggerWebhook
| AutomationTriggerZone
| AutomationTriggerDevice
| AutomationTriggerGeneric
;
export const triggerTypes = [
'Event',
'Home Assistant',
'MQTT',
'Numeric State',
'State',
'Tag',
'Template',
'Time',
'Time Pattern',
'Webhook',
'Zone',
'Device',
'Generic',
] as const;
export type TriggerType = typeof triggerTypes[number];
export type TriggerType = AutomationTrigger['platform'];