From 5b14210f95eb2df804526c92b7924ee399ecdb93 Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Wed, 17 Nov 2021 11:45:33 -0500 Subject: [PATCH] styling for auto editor --- .../AutomationEditor/AutoInfoBox.tsx | 4 +- .../components/AutomationEditor/DAGEdge.tsx | 4 +- .../components/AutomationEditor/DAGNode.tsx | 62 ++++---- .../components/AutomationEditor/constants.ts | 6 +- .../AutomationEditor/index.stories.tsx | 90 ++++++++++++ web/src/components/AutomationEditor/index.tsx | 42 +++--- web/src/components/AutomationEditor/styles.ts | 135 ++++++++++++++++++ .../SequenceEditor/index.stories.tsx | 38 +++++ web/src/components/SequenceEditor/index.tsx | 12 +- web/src/icons/IconBase.tsx | 33 +++-- web/src/icons/icons.stories.tsx | 16 ++- web/src/icons/icons.tsx | 8 +- 12 files changed, 357 insertions(+), 93 deletions(-) create mode 100644 web/src/components/AutomationEditor/index.stories.tsx create mode 100644 web/src/components/AutomationEditor/styles.ts create mode 100644 web/src/components/SequenceEditor/index.stories.tsx diff --git a/web/src/components/AutomationEditor/AutoInfoBox.tsx b/web/src/components/AutomationEditor/AutoInfoBox.tsx index 5663b7e..2a7d702 100644 --- a/web/src/components/AutomationEditor/AutoInfoBox.tsx +++ b/web/src/components/AutomationEditor/AutoInfoBox.tsx @@ -4,12 +4,14 @@ import InputText from "../Inputs/InputText"; import InputTextArea from "../Inputs/InputTextArea"; interface Props { + className: string; metadata: AutomationMetadata; onUpdate: (m: AutomationMetadata) => void; } export default function AutoInfoBox({ metadata, onUpdate, + className, }: Props) { // aliases @@ -19,7 +21,7 @@ export default function AutoInfoBox({ }) // render - return
+ return
diff --git a/web/src/components/AutomationEditor/DAGEdge.tsx b/web/src/components/AutomationEditor/DAGEdge.tsx index ffb2175..d4e376f 100644 --- a/web/src/components/AutomationEditor/DAGEdge.tsx +++ b/web/src/components/AutomationEditor/DAGEdge.tsx @@ -6,11 +6,13 @@ export interface EdgeProp { p1: Point, p2: Point, direction: EdgeDirection, + className: string, } export default ({ p1: [x1,y1], p2: [x2,y2], direction, + className, }: EdgeProp) => { const [_,my] = [(x1+x2)/2, (y1+y2)/2] const theta = Math.atan2(y2 -y1, x2-x1) - Math.PI / 2 @@ -19,7 +21,7 @@ export default ({ const cx2 = x2 + 45 * Math.sin(theta) const cy2 = my + 30 * Math.cos(theta) - return {}, isOpen=false, color, -}: NodeProp) => -
-
-
- X -
-
- {text} -
-
- {isOpen ? "▲" : "▼"} +}: NodeProp) => { + const {classes, theme} = useNodeStyles({ + color, + nodeHeight: height, + nodeWidth: width, + }); + return +
+
+
+ +
+
+ {text} +
+
+ +
-
- - -export interface ConditionNodeProps extends NodeProp { - conditionType: string; + } -export const ConditionNode: FC = ({ - loc: [x,y], - text, - height=NODE_HEIGHT, - width=NODE_WIDTH, - onXClick=() => {}, - onOpenClick=() => {}, - isOpen=false, - color, - conditionType, - children, -}) => -
-
- {conditionType} -
-
- {children} -
-
-
\ No newline at end of file diff --git a/web/src/components/AutomationEditor/constants.ts b/web/src/components/AutomationEditor/constants.ts index 11de4a5..272853f 100644 --- a/web/src/components/AutomationEditor/constants.ts +++ b/web/src/components/AutomationEditor/constants.ts @@ -1,7 +1,5 @@ -export const NODE_WIDTH = 50 as const; -export const NODE_HEIGHT = 25 as const; -export const GRAPH_HEIGHT = 300 as const; -export const GRAPH_WIDTH = 300 as const; \ No newline at end of file +export const NODE_WIDTH = 90 as const; +export const NODE_HEIGHT = 40 as const; \ No newline at end of file diff --git a/web/src/components/AutomationEditor/index.stories.tsx b/web/src/components/AutomationEditor/index.stories.tsx new file mode 100644 index 0000000..6dea0aa --- /dev/null +++ b/web/src/components/AutomationEditor/index.stories.tsx @@ -0,0 +1,90 @@ +import { ComponentMeta, ComponentStory } from "@storybook/react"; +import { useState } from "react"; +import { AutomationData } from "~/automations/types"; +import { usePageTheme } from "~/styles/page"; +import AutomationEditor from "."; + +export default { + title: 'AutomationEditor', + component: AutomationEditor, + parameters: { actions: { argTypesRegex: '^on.*' } }, + } as ComponentMeta; + + +const Template: ComponentStory = ({automation, ...args}) => { + const {classes} = usePageTheme({}); + const [data, setData] = useState(automation); + return
+ { + setData(data) + }} + /> +
+} + +export const ServiceAction = Template.bind({}) +ServiceAction.args = { + automation: { + metadata: { + id: "X12314akx", + alias: "root", + description: 'Really Long Description', + mode: 'single', + }, + trigger: [ + { + platform: 'time', + at: '10:00:00' + }, + { + platform: "device", + device_id: "34749", + domain: "zha", + type: "remote_button_double_press", + subtype: "remote_button_double_press" + } + ], + sequence: [ + { + $smType: "condition", + condition: 'and', + condition_data: { + conditions: [ + { + $smType: 'condition', + condition: 'numeric_state', + condition_data: { + entity_id: 'sensor.temperature_kitchen', + below: '15', + } + }, + { + $smType: "condition", + condition: 'template', + condition_data: { + value_template: 'states(switch.kitchen_light) == "on"' + } + } + ] + } + }, + { + $smType: "action", + action: "service", + action_data: { + alias: "Start Music In Kitchen", + service: 'media_player.play_media', + target: { + entity_id: "media_player.kitchen_dot" + }, + data: { + media_content_id: "Good Morning", + media_content_type: "SPOTIFY", + } + } + } + ], + } as AutomationData +} \ No newline at end of file diff --git a/web/src/components/AutomationEditor/index.tsx b/web/src/components/AutomationEditor/index.tsx index fb6a14f..3a2ed90 100644 --- a/web/src/components/AutomationEditor/index.tsx +++ b/web/src/components/AutomationEditor/index.tsx @@ -1,4 +1,4 @@ -import { AutomationData } from "~/automations/types"; +import { AutomationData, AutomationSequenceNode } from "~/automations/types"; import { AutomationCondition } from "~/automations/types/conditions"; import { AutomationTrigger } from "~/automations/types/triggers"; import { getDescriptionFromAutomationNode } from "~/automations/utils"; @@ -6,7 +6,8 @@ import useWindowSize from "~/utils/useWindowSize"; import AutoInfoBox from "./AutoInfoBox"; import { NODE_HEIGHT, NODE_WIDTH } from "./constants"; import DAGEdge from "./DAGEdge"; -import DAGNode, { ConditionNode } from "./DAGNode"; +import DAGNode from "./DAGNode"; +import { useEditorStyles } from "./styles"; import { Point } from "./types"; interface Props { @@ -20,6 +21,7 @@ export default function AutomationEditor({ // state const {ratioWbh} = useWindowSize(); + const {classes, theme} = useEditorStyles({}); // alias const nHDF = 1.5 // node horizontal distance factor @@ -36,70 +38,64 @@ export default function AutomationEditor({ key={`edge.${i}`} p1={[NODE_WIDTH , nodeLoc[1] + 0.5 * NODE_HEIGHT]} p2={pointTrigToCond} + className={classes.dagEdge} direction="1->2" /> } - const renderMidCondition = (condition: AutomationCondition, i: number) => { + const renderSequence = (node: AutomationSequenceNode, i: number) => { const nodeLoc: Point = [pointTrigToCond[0] + NODE_WIDTH/2 , 0]; let lastNodeLoc = pointTrigToCond; if (i > 0) { lastNodeLoc = [nodeLoc[0] + NODE_WIDTH*((i - 1)*nHDF+1), nodeLoc[1] + NODE_HEIGHT/2]; nodeLoc[0] += NODE_WIDTH*i*nHDF } - let children = <>; - if ( - (condition.condition === 'and') || - (condition.condition === 'or') || - (condition.condition === 'not') - ) { - // children = condition.condition_data.conditions.map((c,i) => renderMidCondition(c,i)) - } return <> - - {children} - + text={getDescriptionFromAutomationNode(node)} + color={node.$smType === 'action' ? 'green' : 'blue'} + /> } // render - return
= 0.75 ? 'row' : 'column' }}> onUpdate({...automation, metadata})} /> -
+
- - + {automation.trigger.map(renderTrigger)} - {automation.sequence.map(renderMidCondition)} + {automation.sequence.map(renderSequence)} - + diff --git a/web/src/components/AutomationEditor/styles.ts b/web/src/components/AutomationEditor/styles.ts new file mode 100644 index 0000000..566caa8 --- /dev/null +++ b/web/src/components/AutomationEditor/styles.ts @@ -0,0 +1,135 @@ +import { Styles } from "jss"; +import Color from 'chroma-js'; +import { createAppUseStyles } from "~/styles/theme"; +import { Node } from "./types"; + + +export const useEditorStyles = createAppUseStyles(theme => ({ + root: { + height: '99%', + width: '99%', + color: theme.primaryAccent, + display: 'flex', + flexDirection: 'row', + }, + dag: { + borderWidth: 1, + borderStyle: 'solid', + borderColor: theme.secondaryAccent, + width: '100%', + }, + svg: { + height: '99%', + width: '99%', + padding: 0, + margin: 0, + }, + circle: { + fill: Color(theme.secondary).set('rgb.b', 125).hex(), + }, + infoBox: { + paddingLeft: '10px', + paddingRight: '10px', + display: 'flex', + flexDirection: 'column', + justifyContent: 'flex-start', + overflow: 'auto', + }, + dagEdge: { + stroke: theme.secondaryAccent, + }, +})) + +export const useNodeStyles = createAppUseStyles<{ + color: Node['color'], + nodeHeight: number, + nodeWidth: number, +}>(theme => { + const edgeStyle: Styles = { + flex: '1', + display: 'flex', + flexDirection: 'column', + flexWrap: 'nowrap', + borderRadius: '1em', + justifyContent: 'space-around', + alignItems: 'center', + '&:hover': { + backgroundColor: theme.primaryAccent, + cursor: 'pointer', + } + } + const buttonStyle: Styles = { + fontSize: '8px', + backgroundColor: "rgba(0,0,0,0)", + color: theme.primaryAccent, + borderStyle: 'none', + height: '15px', + width: '15px', + cursor: 'pointer', + transition: "500ms east-out", + } + return { + root: { + display: 'flex', + flexDirection: 'column', + justifyContent: 'flex-end', + height: `100%`, + maxHeight: `100%`, + maxWidth: '100%', + width: '100%', + fontSize: 10, + }, + inner: ({color}) => ({ + position: 'relative', + display: 'flex', + flexDirection: 'row', + backgroundColor: theme.secondary, + borderRadius: '1em', + flex: 1, + height: `calc(100% - 10px)`, + maxHeight: `calc(100% - 10px)`, + maxWidth: '100%', + borderTopWidth: '1px', + borderTopStyle: 'solid', + borderTopLeftRadius: '10px', + borderTopRightRadius: '10px', + borderTopColor: color === 'red' ? Color(theme.secondary).set('rgb.r', 125).hex() : + color === 'blue' ? Color(theme.secondary).set('rgb.b', 125).hex() : + color === 'green' ? Color(theme.secondary).set('rgb.g', 125).hex() : + 'none' + }), + text: { + flex: 4, + display: 'flex', + textAlign: 'center', + justifyContent: 'center', + alignItems: 'center', + height: '100%', + maxHeight: '100%', + }, + buttonDelete: { + ...buttonStyle, + "&:hover": { + color: theme.primary, + } + }, + buttonEdit: { + ...buttonStyle, + "&:hover": { + transform: "rotate(45deg)" + } + }, + leftEdge: { + ...edgeStyle, + borderLeft: `1px ${theme.secondaryAccent} solid`, + borderTopRightRadius: 0, + borderBottomRightRadius: 0, + }, + rightEdge: { + ...edgeStyle, + borderRight: `1px ${theme.secondaryAccent} solid`, + borderTopLeftRadius: 0, + borderBottomLeftRadius: 0, + }, + } +}) \ No newline at end of file diff --git a/web/src/components/SequenceEditor/index.stories.tsx b/web/src/components/SequenceEditor/index.stories.tsx new file mode 100644 index 0000000..9845d15 --- /dev/null +++ b/web/src/components/SequenceEditor/index.stories.tsx @@ -0,0 +1,38 @@ +import { ComponentMeta, ComponentStory } from "@storybook/react"; +import { useState } from "react"; +import { AutomationAction } from "~/automations/types/actions"; +import { usePageTheme } from "~/styles/page"; +import { SequenceEditor } from "."; + +export default { + title: 'SequenceEditor', + component: SequenceEditor, + parameters: { actions: { argTypesRegex: '^on.*' } }, + } as ComponentMeta; + + +const Template: ComponentStory = ({node, ...args}) => { + const {classes} = usePageTheme({}); + const [data, setData] = useState(node); + return
+ { + setData(data) + }} + /> +
+} + +export const ServiceAction = Template.bind({}) +ServiceAction.args = { + node: { + $smType: 'action', + action: 'service', + action_data: { + service: 'switch.toggle', + data: {}, + target: {}, + } + } as AutomationAction +} \ No newline at end of file diff --git a/web/src/components/SequenceEditor/index.tsx b/web/src/components/SequenceEditor/index.tsx index 753a7b5..d4c1741 100644 --- a/web/src/components/SequenceEditor/index.tsx +++ b/web/src/components/SequenceEditor/index.tsx @@ -1,14 +1,18 @@ import { FC } from "react"; -import { AutomationSequenceNode } from "~/automations/types"; +import { AutomationAction } from "~/automations/types/actions"; export const SequenceEditor: FC<{ - node: AutomationSequenceNode -}> = ({}) => { + node: AutomationAction, + onUpdate: (u: AutomationAction) => void, +}> = ({ + node, + onUpdate, +}) => { return
- + {node.action}
} \ No newline at end of file diff --git a/web/src/icons/IconBase.tsx b/web/src/icons/IconBase.tsx index c6fc132..b875913 100644 --- a/web/src/icons/IconBase.tsx +++ b/web/src/icons/IconBase.tsx @@ -3,21 +3,30 @@ import { createAppUseStyles } from "~/styles/theme"; -export const useIconStyles = createAppUseStyles(theme => ({ - icon: { - height: '1em', - width: '1em', +export const useIconStyles = createAppUseStyles<{size: number, color?: string}>(theme => ({ + icon: ({size}) => ({ + height: `${size}em`, + width: `${size}em`, display: 'flex', - }, - svg: { - fill: theme.primaryAccent - } + }), + svg: ({color=theme.primaryAccent}) => ({ + fill: color + }) })) -export const IconBase: FC & { - viewBox: string; -}> = ({children, viewBox, className="", ...props}) => { - const {classes} = useIconStyles({}); +export type IconBaseProps = AnchorHTMLAttributes & { + size?: number; + color?: string; +} +export const IconBase: FC = ({ + children, + viewBox, + size=1, + color, + className="", + ...props +}) => { + const {classes} = useIconStyles({ size, color }); return diff --git a/web/src/icons/icons.stories.tsx b/web/src/icons/icons.stories.tsx index b89710a..9b96498 100644 --- a/web/src/icons/icons.stories.tsx +++ b/web/src/icons/icons.stories.tsx @@ -1,18 +1,22 @@ +import { ComponentStory } from "@storybook/react"; import { usePageTheme } from "~/styles/page"; +import { IconBase } from "./IconBase"; import {PencilIcon, CheckMarkIcon} from "./icons"; export default { title: 'Icons', + component: IconBase, }; -const Template = () => { +export const IconList: ComponentStory = (args) => { const {classes} = usePageTheme({}); return
- - + +
} - -export const IconList = Template.bind({}) -// SimpleText.args = {} \ No newline at end of file +IconList.args = { + size: 2, + color: 'red' +} \ No newline at end of file diff --git a/web/src/icons/icons.tsx b/web/src/icons/icons.tsx index 2baa3b5..7708142 100644 --- a/web/src/icons/icons.tsx +++ b/web/src/icons/icons.tsx @@ -1,8 +1,8 @@ -import { AnchorHTMLAttributes, FC } from "react"; -import { IconBase } from "./IconBase"; +import { FC } from "react"; +import { IconBase, IconBaseProps } from "./IconBase"; -export const PencilIcon: FC> = props => +export const PencilIcon: FC = props => ; -export const CheckMarkIcon: FC> = props => +export const CheckMarkIcon: FC = props => ; \ No newline at end of file