From d210c3924bd4c6ff0fd56ca659d4d1f069bc5303 Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Tue, 16 Nov 2021 11:51:42 -0500 Subject: [PATCH] fnctional display mode for nested conditions --- .../ConditionEditor/ConditionNode.tsx | 66 +++++++++++++++++++ .../ConditionEditor/index.stories.tsx | 57 +++++++++++++++- web/src/components/ConditionEditor/index.tsx | 52 ++++----------- web/src/components/ConditionEditor/style.ts | 47 ++++++++----- .../components/ConditionEditor/viewRender.tsx | 39 +++++++++++ web/src/styles/theme.ts | 11 +++- 6 files changed, 211 insertions(+), 61 deletions(-) create mode 100644 web/src/components/ConditionEditor/ConditionNode.tsx create mode 100644 web/src/components/ConditionEditor/viewRender.tsx diff --git a/web/src/components/ConditionEditor/ConditionNode.tsx b/web/src/components/ConditionEditor/ConditionNode.tsx new file mode 100644 index 0000000..dd35d67 --- /dev/null +++ b/web/src/components/ConditionEditor/ConditionNode.tsx @@ -0,0 +1,66 @@ +import { Classes } from "jss"; +import { FC } from "react"; +import { AutomationCondition } from "~/automations/types/conditions"; +import { getEditor } from "./editorRender"; +import { useStyles } from "./style"; +import { getViewer } from "./viewRender"; + + + +export const ConditionNode: FC<{ + condition: AutomationCondition; + displayMode: boolean; + showDelete?: boolean; + onDelete?: (which: 'root' | number) => void; + onAddChild?: () => void; + onUpdate?: (data: AutomationCondition) => void; + children?: (props: { + classes: Classes + }) => JSX.Element, +}> = ({ + condition, + showDelete=false, + onDelete=() => {}, + onUpdate=() => {}, + displayMode=() => {}, + onAddChild=() => {}, + children=() => {}, +}) => { + // children + let childrenConditions: JSX.Element; + if (displayMode) { + const Viewer = getViewer(condition); + childrenConditions = + } else { + const Editor = getEditor(condition); + childrenConditions = { + console.log({data}) + onUpdate(data) + }}/> + } + // styles + let hasChildren = false; + if (condition.condition === "not") { + hasChildren = true + } else if (condition.condition ==='and') { + hasChildren = true + } else if (condition.condition ==='or') { + hasChildren = true + } + const {classes} = useStyles({ hasChildren }); + + // components + const DeleteButton = (p: {onClick?: () => void, isRoot?: boolean}) => + + + return
+ {(showDelete || !displayMode) && onDelete('root')}/>} +
+ {condition.condition.replace('_', ' ')} +
+
+ {childrenConditions} +
+ {children({classes})} +
+} \ No newline at end of file diff --git a/web/src/components/ConditionEditor/index.stories.tsx b/web/src/components/ConditionEditor/index.stories.tsx index f715d9e..948c6cf 100644 --- a/web/src/components/ConditionEditor/index.stories.tsx +++ b/web/src/components/ConditionEditor/index.stories.tsx @@ -1,5 +1,6 @@ import { ComponentMeta, ComponentStory } from "@storybook/react"; import { useState } from "react"; +import { AutomationCondition } from "~/automations/types/conditions"; import { usePageTheme } from "~/styles/page"; import { ConditionEditor } from "."; @@ -18,8 +19,8 @@ const Template: ComponentStory = ({condition, ...args}) } -export const SimpleGraph = Template.bind({}) -SimpleGraph.args = { +export const TemplateViewer = Template.bind({}) +TemplateViewer.args = { condition: { $smType: 'condition', condition: 'template', @@ -27,4 +28,56 @@ SimpleGraph.args = { value_template: "states('switch.light_kitchen') == 'on'" } }, +} + +export const LogicCondition = Template.bind({}) +LogicCondition.args = { + condition: { + $smType: 'condition', + condition: 'or', + condition_data: { + conditions: [ + { + $smType: 'condition', + condition: 'numeric_state', + condition_data: { + entity_id: 'sensor.humidity_kitchen', + above: '60' + } + }, + { + $smType: 'condition', + condition: 'numeric_state', + condition_data: { + entity_id: 'sensor.humidity_living_room', + above: '60' + } + }, + { + $smType: 'condition', + condition: 'and', + condition_data: { + conditions: [ + { + $smType: 'condition', + condition: 'numeric_state', + condition_data: { + entity_id: 'sensor.humidity_bedroom', + above: '60' + } + }, + { + $smType: 'condition', + condition: 'numeric_state', + condition_data: { + entity_id: 'sensor.humidity_bathroom', + above: '60' + } + } + ] + } + }, + ] + } + } as AutomationCondition, } \ No newline at end of file diff --git a/web/src/components/ConditionEditor/index.tsx b/web/src/components/ConditionEditor/index.tsx index cb92ee4..530d086 100644 --- a/web/src/components/ConditionEditor/index.tsx +++ b/web/src/components/ConditionEditor/index.tsx @@ -2,8 +2,10 @@ import { FC, useState } from "react"; import { AutomationCondition } from "~/automations/types/conditions"; import { getDescriptionFromAutomationNode } from "~/automations/utils"; import { CheckMarkIcon, PencilIcon } from "~/icons/icons"; +import { ConditionNode } from "./ConditionNode"; import { getEditor } from "./editorRender"; import { useStyles } from "./style"; +import { getViewer } from "./viewRender"; @@ -16,47 +18,21 @@ export const ConditionEditor: FC<{ condition, onDelete, onUpdate, - // onAddChild, + onAddChild, }) => { // state const [displayMode, setDisplayMode] = useState(true); - // children - let children: any = 'n/a' - if (displayMode) { - children = getDescriptionFromAutomationNode(condition); - } else { - const Editor = getEditor(condition); - children = { - console.log({data}) - onUpdate(data) - }}/> - } - // styles - let hasChildren = false; - if (condition.condition === "not") { - hasChildren = true - } else if (condition.condition ==='and') { - hasChildren = true - } else if (condition.condition ==='or') { - hasChildren = true - } - const {classes} = useStyles({ hasChildren }); - - // components - const DeleteButton = (p: {onClick?: () => void, isRoot?: boolean}) => - - - return
- onDelete('root')}/> -
- {condition.condition.replace('_', '')} -
-
- {children} -
- -
+ } + } \ No newline at end of file diff --git a/web/src/components/ConditionEditor/style.ts b/web/src/components/ConditionEditor/style.ts index a9d7a7f..bd90245 100644 --- a/web/src/components/ConditionEditor/style.ts +++ b/web/src/components/ConditionEditor/style.ts @@ -27,33 +27,47 @@ export const useStyles = createAppUseStyles<{ position: 'relative', display: 'flex', flexDirection: hasChildren? 'column' : 'row', - width: hasChildren? '100%' : undefined, - height: hasChildren? '100%' : undefined, + // width: hasChildren? '100%' : undefined, + // height: hasChildren? '100%' : undefined, }), - title: { + title: ({hasChildren}) => ({ + display: 'flex', + border: hasChildren ? `1px solid ${theme.primaryAccent}` : 'none', + borderRadius: 10, + borderBottomRightRadius: 0, + borderBottomLeftRadius: 0, + }), + titleText: ({hasChildren}) => ({ background: theme.condition.primaryColor, border: `1px solid ${theme.primaryAccent}`, borderRadius: 10, - borderTopRightRadius: 0, - borderBottomRightRadius: 0, + borderTopRightRadius: hasChildren ? 10 : 0, + borderBottomRightRadius: hasChildren ? 10 : 0, padding: 10, maxWidth: 80, + minWidth: 30, textOverflow: 'ellipsis', - }, + textAlign: 'center', + }), deleteBtnRoot: { ...deleteBtn, position: 'absolute', left: -35, top: 10, }, - modifyBtn: { + modifyBtn: ({hasChildren}) => ({ backgroundColor: theme.primary, fill: theme.primaryAccent, border: `1px solid ${theme.primaryAccent}`, - borderRadius: 10, + borderRadius: hasChildren ? 0 : 10, borderTopLeftRadius: 0, - borderBottomLeftRadius: 0, + borderBottomLeftRadius: hasChildren ? 10 : 0, + borderBottomRightRadius: 10, cursor: 'pointer', + display: 'flex', + justifyContent: 'center', + alignItems: 'center', + padding: 5, "&:hover": { backgroundColor: Color(theme.primary).brighten().hex(), fill: Color(theme.primaryAccent).brighten().hex(), @@ -61,22 +75,19 @@ export const useStyles = createAppUseStyles<{ "&:hover $icon": { transform: "rotate(90deg)" } - }, + }), icon: { transition: "200ms ease-out" }, deleteBtn, - children: ({}) => ({ + children: ({hasChildren}) => ({ flex: 1, - paddingLeft: 10, - paddingRight: 10, + paddingLeft: hasChildren ? 50 : 10, + paddingRight: hasChildren ? 0 : 10, display: 'flex', - justifyContent: 'center', - alignItems: 'center', + flexDirection: 'column', + justifyContent: 'center', border: `1px solid ${theme.primaryAccent}`, - // borderRadius: 10, - // borderTopLeftRadius: 0, - // borderBottomLeftRadius: 0, }) } }) \ No newline at end of file diff --git a/web/src/components/ConditionEditor/viewRender.tsx b/web/src/components/ConditionEditor/viewRender.tsx new file mode 100644 index 0000000..e34043c --- /dev/null +++ b/web/src/components/ConditionEditor/viewRender.tsx @@ -0,0 +1,39 @@ +import { FC } from "react"; +import { AutomationCondition, LogicCondition, TemplateCondition } from "~/automations/types/conditions"; +import { getDescriptionFromAutomationNode } from "~/automations/utils"; +import { ConditionNode } from "./ConditionNode"; + + +interface Viewer extends FC<{ + condition: C, +}> {} + +export const getViewer = (condition: AutomationCondition): Viewer => { + if( + (condition.condition === 'and') || + (condition.condition === 'or') || + (condition.condition === 'not') + ) + { + return LogicViewer + } + return GenericViewer +} + +export const GenericViewer: Viewer = ({ + condition, +}) => { + return
+ {getDescriptionFromAutomationNode(condition)} +
+} + +export const LogicViewer: Viewer = ({ + condition, +}) => { + return <> + {condition.condition_data.conditions.map( (condition, i) => { + return + } )} + +} \ No newline at end of file diff --git a/web/src/styles/theme.ts b/web/src/styles/theme.ts index 28976a8..402f2b5 100644 --- a/web/src/styles/theme.ts +++ b/web/src/styles/theme.ts @@ -1,3 +1,4 @@ +import { Classes } from "jss"; import { Context, createContext } from "react" import { createUseStyles, Styles } from "react-jss"; import { createTheming } from "theming" @@ -19,14 +20,18 @@ export const { redAccent: '#9f3a3a' })); -type GetThemeType> = C extends Context ? T : unknown; +export type GetThemeType> = C extends Context ? T : unknown; +export type ClassNamesOfStyle = Record, string>; export type AppTheme = GetThemeType; export const createAppUseStyles = < Props extends {} = {}, C extends string = string, ->(f: (theme: AppTheme) => Styles) => (props: Props) => { +>(f: (theme: AppTheme) => Styles) => (props: Props): { + classes: Classes>, + theme: AppTheme, +} => { const theme = useTheme(); const useStyles = createUseStyles(f) - const classes = useStyles({...props, theme}); + const classes = useStyles({...props, theme}) as Classes>; return {classes, theme}; } \ No newline at end of file