fnctional display mode for nested conditions
This commit is contained in:
1 parent
23ece34a50
commit
d210c3924b
6 files changed
+211
-61
No files matched your search
@@ -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<string>
|
||||
}) => JSX.Element,
|
||||
}> = ({
|
||||
condition,
|
||||
showDelete=false,
|
||||
onDelete=() => {},
|
||||
onUpdate=() => {},
|
||||
displayMode=() => {},
|
||||
onAddChild=() => {},
|
||||
children=() => {},
|
||||
}) => {
|
||||
// children
|
||||
let childrenConditions: JSX.Element;
|
||||
if (displayMode) {
|
||||
const Viewer = getViewer(condition);
|
||||
childrenConditions = <Viewer condition={condition}/>
|
||||
} else {
|
||||
const Editor = getEditor(condition);
|
||||
childrenConditions = <Editor condition={condition} onChange={data => {
|
||||
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}) =>
|
||||
<button className={p.isRoot ? classes.deleteBtnRoot : classes.deleteBtn} onClick={p?.onClick}>X</button>
|
||||
|
||||
return <div className={classes.root}>
|
||||
{(showDelete || !displayMode) && <DeleteButton isRoot onClick={ () => onDelete('root')}/>}
|
||||
<div className={classes.title}>
|
||||
<span className={classes.titleText}>{condition.condition.replace('_', ' ')}</span>
|
||||
</div>
|
||||
<div className={classes.children}>
|
||||
{childrenConditions}
|
||||
</div>
|
||||
{children({classes})}
|
||||
</div>
|
||||
}
|
||||
@@ -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<typeof ConditionEditor> = ({condition, ...args})
|
||||
</div>
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
@@ -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 = <Editor condition={condition} onChange={data => {
|
||||
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}) =>
|
||||
<button className={p.isRoot ? classes.deleteBtnRoot : classes.deleteBtn} onClick={p?.onClick}>X</button>
|
||||
|
||||
return <div className={classes.root}>
|
||||
<DeleteButton isRoot onClick={ () => onDelete('root')}/>
|
||||
<div className={classes.title}>
|
||||
{condition.condition.replace('_', '')}
|
||||
</div>
|
||||
<div className={classes.children}>
|
||||
{children}
|
||||
</div>
|
||||
<button className={classes.modifyBtn} onClick={() => setDisplayMode(!displayMode)}>
|
||||
return <ConditionNode
|
||||
displayMode={displayMode}
|
||||
condition={condition}
|
||||
onDelete={onDelete}
|
||||
onUpdate={onUpdate}
|
||||
onAddChild={onAddChild}
|
||||
showDelete
|
||||
>
|
||||
{({classes}) => <button className={classes.modifyBtn} onClick={() => setDisplayMode(!displayMode)}>
|
||||
{displayMode ? <PencilIcon className={classes.icon}/> : <CheckMarkIcon className={classes.icon}/>}
|
||||
</button>
|
||||
</div>
|
||||
</button>}
|
||||
</ConditionNode>
|
||||
}
|
||||
@@ -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,
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -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<C extends AutomationCondition> extends FC<{
|
||||
condition: C,
|
||||
}> {}
|
||||
|
||||
export const getViewer = (condition: AutomationCondition): Viewer<any> => {
|
||||
if(
|
||||
(condition.condition === 'and') ||
|
||||
(condition.condition === 'or') ||
|
||||
(condition.condition === 'not')
|
||||
)
|
||||
{
|
||||
return LogicViewer
|
||||
}
|
||||
return GenericViewer
|
||||
}
|
||||
|
||||
export const GenericViewer: Viewer<AutomationCondition> = ({
|
||||
condition,
|
||||
}) => {
|
||||
return <div>
|
||||
{getDescriptionFromAutomationNode(condition)}
|
||||
</div>
|
||||
}
|
||||
|
||||
export const LogicViewer: Viewer<LogicCondition> = ({
|
||||
condition,
|
||||
}) => {
|
||||
return <>
|
||||
{condition.condition_data.conditions.map( (condition, i) => {
|
||||
return <ConditionNode key={i} condition={condition} displayMode/>
|
||||
} )}
|
||||
</>
|
||||
}
|
||||
Reference in new issue
Block a user