dagflow triggers
This commit is contained in:
1 parent
5dcd17ad17
commit
468a022582
12 files changed
+691
-3
No files matched your search
@@ -0,0 +1,55 @@
|
||||
import { FC } from "react";
|
||||
import { AddIcon, PencilIcon, TrashIcon } from '../Icons/index';
|
||||
import { useCircleStyles } from './styles';
|
||||
import { Handle, Position } from 'react-flow-renderer';
|
||||
|
||||
|
||||
|
||||
export interface DAGCircleProps {
|
||||
onEdit?: () => void;
|
||||
onAdd?: () => void;
|
||||
onRemove?: () => void;
|
||||
size: number,
|
||||
backgroundColor?: string;
|
||||
disableSource?: boolean;
|
||||
disableTarget?: boolean;
|
||||
}
|
||||
export const DAGCircle: FC<{ data: DAGCircleProps }> = ({
|
||||
data: {
|
||||
size,
|
||||
onEdit,
|
||||
onAdd,
|
||||
onRemove,
|
||||
backgroundColor,
|
||||
disableSource = false,
|
||||
disableTarget = false,
|
||||
}
|
||||
}) => {
|
||||
const { classes } = useCircleStyles({
|
||||
size: size * 0.9,
|
||||
hasOnFunction: !!onEdit || !!onAdd,
|
||||
hasRemoveFunction: !!onRemove,
|
||||
backgroundColor,
|
||||
});
|
||||
return <>
|
||||
{!disableSource && <Handle type="source" position={Position.Right} />}
|
||||
{!disableTarget && <Handle type="target" position={Position.Left} />}
|
||||
<div className={classes.root}>
|
||||
{
|
||||
onEdit ?
|
||||
<PencilIcon
|
||||
size={70} units="%"
|
||||
className={classes.icon}
|
||||
onClick={onEdit}
|
||||
/> : onAdd ?
|
||||
<AddIcon
|
||||
size={70} units="%"
|
||||
className={classes.icon}
|
||||
onClick={onAdd}
|
||||
/>
|
||||
: backgroundColor ? "" : "else"
|
||||
}
|
||||
{onRemove ? <TrashIcon className={classes.trashIcon} onClick={onRemove} /> : <></>}
|
||||
</div>
|
||||
</>
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import Color from "chroma-js";
|
||||
import { PencilIcon } from "components/Icons"
|
||||
import { NodeColor } from "types/graphs";
|
||||
import { useNodeStyles } from "./styles";
|
||||
import { Handle, Position } from 'react-flow-renderer';
|
||||
import { FC } from "react";
|
||||
|
||||
|
||||
export interface DAGNodeProps {
|
||||
height: number;
|
||||
width: number;
|
||||
onXClick?: () => void;
|
||||
onEditClick?: () => void;
|
||||
color: NodeColor;
|
||||
label: string;
|
||||
hasInput?: boolean;
|
||||
}
|
||||
export const DAGNode: FC<{ data: DAGNodeProps }> = ({
|
||||
data: {
|
||||
label,
|
||||
height,
|
||||
width,
|
||||
onXClick = () => { },
|
||||
onEditClick = () => { },
|
||||
color,
|
||||
hasInput = false,
|
||||
},
|
||||
}) => {
|
||||
const { classes, theme } = useNodeStyles({
|
||||
color,
|
||||
nodeHeight: height,
|
||||
nodeWidth: width,
|
||||
});
|
||||
return <>
|
||||
{hasInput && <Handle type="target" position={Position.Left} />}
|
||||
<Handle type="source" position={Position.Right} />
|
||||
<div className={classes.root}>
|
||||
<div className={classes.inner}>
|
||||
<div className={classes.leftEdge} onClick={onXClick}>
|
||||
<button className={classes.buttonDelete}>X</button>
|
||||
</div>
|
||||
<div className={classes.textWrap}>
|
||||
<span className={classes.text} title={label}>
|
||||
{label}
|
||||
</span>
|
||||
</div>
|
||||
<div className={classes.rightEdge} onClick={onEditClick}>
|
||||
<button className={classes.buttonEdit}>
|
||||
<PencilIcon size={1.1} color={Color(theme.primary).set('rgb.g', 200).hex()} />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
|
||||
|
||||
import React from "react";
|
||||
import { ComponentMeta, ComponentStory } from '@storybook/react';
|
||||
import { DAGFlow } from '.';
|
||||
import { useState } from 'react';
|
||||
import * as dgconst from "components/DAGSvgs/constants";
|
||||
import { makeAumationStateEditor } from '../../utils/AutomationState';
|
||||
|
||||
|
||||
export default {
|
||||
title: 'DAGFlow/Viewer',
|
||||
component: DAGFlow,
|
||||
parameters: { actions: { argTypesRegex: '^on.*' } },
|
||||
args: {
|
||||
dims: {
|
||||
nodeHeight: dgconst.NODE_HEIGHT,
|
||||
nodeWidth: dgconst.NODE_WIDTH,
|
||||
distanceFactor: dgconst.DISTANCE_FACTOR,
|
||||
circleSize: dgconst.CIRCLE_SIZE,
|
||||
padding: dgconst.PADDING,
|
||||
}
|
||||
}
|
||||
} as ComponentMeta<typeof DAGFlow>
|
||||
|
||||
const Template: ComponentStory<typeof DAGFlow> = args => {
|
||||
const [state, setState] = useState(args.automation)
|
||||
const triggerEditor = makeAumationStateEditor(state, setState).triggers;
|
||||
return <div className="page">
|
||||
<DAGFlow
|
||||
{...args}
|
||||
automation={state}
|
||||
opts={{
|
||||
triggers: {
|
||||
onAdd: () => triggerEditor.add({
|
||||
platform: 'event',
|
||||
$smType: 'trigger',
|
||||
event_type: 'test',
|
||||
}),
|
||||
onDelete: triggerEditor.delete,
|
||||
onEdit: () => { },
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
|
||||
export const Simple = Template.bind({})
|
||||
Simple.args = {
|
||||
...Simple.args,
|
||||
automation: {
|
||||
tags: {
|
||||
"Room": "Bathroom",
|
||||
"For": "Toliet",
|
||||
"Type": "Smell",
|
||||
"Use": "Flushing"
|
||||
},
|
||||
metadata: {
|
||||
id: "random",
|
||||
alias: "Random",
|
||||
description: "Example Metadata",
|
||||
trigger_variables: {
|
||||
'wowo': '!'
|
||||
},
|
||||
mode: 'single',
|
||||
},
|
||||
trigger: [
|
||||
{
|
||||
"$smType": undefined,
|
||||
"platform": "numeric_state",
|
||||
"entity_id": "test",
|
||||
},
|
||||
{
|
||||
"$smType": undefined,
|
||||
"platform": "homeassistant",
|
||||
"event": "start",
|
||||
}
|
||||
],
|
||||
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",
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const EmptyStart = Template.bind({})
|
||||
EmptyStart.args = {
|
||||
...EmptyStart.args,
|
||||
automation: {
|
||||
tags: {},
|
||||
metadata: {
|
||||
id: "random",
|
||||
alias: "Random",
|
||||
description: "Example Metadata",
|
||||
trigger_variables: {
|
||||
'wowo': '!'
|
||||
},
|
||||
mode: 'single',
|
||||
},
|
||||
trigger: [
|
||||
],
|
||||
sequence: []
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { FC } from "react";
|
||||
|
||||
import ReactFlow, { MiniMap, Controls } from 'react-flow-renderer';
|
||||
import { AutomationData } from "types/automations";
|
||||
import { triggerToFlow } from "./triggerConvertor";
|
||||
import { DAGNode } from "./DAGNode";
|
||||
import { DAGFlowDims, TriggerMakerOptions } from "./types";
|
||||
import { DAGCircle } from './DAGCircle';
|
||||
|
||||
const nodeTypes = {
|
||||
'dagnode': DAGNode,
|
||||
'dagcircle': DAGCircle,
|
||||
}
|
||||
|
||||
interface Props {
|
||||
automation: AutomationData;
|
||||
dims: DAGFlowDims;
|
||||
opts: {
|
||||
triggers: TriggerMakerOptions;
|
||||
}
|
||||
}
|
||||
|
||||
export const DAGFlow: FC<Props> = ({
|
||||
automation,
|
||||
opts,
|
||||
dims,
|
||||
}) => {
|
||||
const {
|
||||
nodes,
|
||||
edges,
|
||||
} = triggerToFlow(automation.trigger, dims, opts.triggers);
|
||||
return (
|
||||
<ReactFlow
|
||||
nodeTypes={nodeTypes}
|
||||
nodes={nodes}
|
||||
edges={edges}
|
||||
snapToGrid
|
||||
onlyRenderVisibleElements
|
||||
nodesConnectable={false}
|
||||
nodesDraggable={false}
|
||||
>
|
||||
<MiniMap />
|
||||
<Controls />
|
||||
</ReactFlow>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
import { Styles } from "jss";
|
||||
import Color from 'chroma-js';
|
||||
import { createAppUseStyles } from "styles/theme";
|
||||
import { Node } from "types/graphs";
|
||||
|
||||
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',
|
||||
}
|
||||
return {
|
||||
root: ({ nodeHeight, nodeWidth }) => ({
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
justifyContent: 'flex-end',
|
||||
height: nodeHeight,
|
||||
maxHeight: nodeHeight,
|
||||
maxWidth: nodeWidth,
|
||||
width: nodeWidth,
|
||||
fontSize: 10,
|
||||
}),
|
||||
inner: ({ color }) => ({
|
||||
position: 'relative',
|
||||
display: 'flex',
|
||||
flexDirection: 'row',
|
||||
backgroundColor: theme.secondary,
|
||||
borderRadius: '1em',
|
||||
flex: 1,
|
||||
height: `100%`,
|
||||
maxHeight: `100%`,
|
||||
maxWidth: '100%',
|
||||
marginTop: '2.5px',
|
||||
marginBottom: '2.5px',
|
||||
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'
|
||||
}),
|
||||
textWrap: {
|
||||
display: 'flex',
|
||||
flex: 4,
|
||||
textAlign: 'center',
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center',
|
||||
height: '100%',
|
||||
maxHeight: '100%',
|
||||
maxWidth: "50%",
|
||||
},
|
||||
text: {
|
||||
textOverflow: 'ellipsis',
|
||||
overflow: "hidden",
|
||||
whiteSpace: "break-spaces",
|
||||
cursor: 'default',
|
||||
},
|
||||
buttonDelete: {
|
||||
...buttonStyle,
|
||||
"&:hover": {
|
||||
color: theme.primary,
|
||||
}
|
||||
},
|
||||
buttonEdit: {
|
||||
...buttonStyle,
|
||||
"&:hover": {
|
||||
transform: "rotate(45deg)"
|
||||
}
|
||||
},
|
||||
leftEdge: {
|
||||
...edgeStyle,
|
||||
borderLef: `1px ${theme.secondaryAccent} solid`,
|
||||
borderTopRightRadius: 0,
|
||||
borderBottomRightRadius: 0,
|
||||
},
|
||||
rightEdge: {
|
||||
...edgeStyle,
|
||||
borderRight: `1px ${theme.secondaryAccent} solid`,
|
||||
borderTopLeftRadius: 0,
|
||||
borderBottomLeftRadius: 0,
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
export const useCircleStyles = createAppUseStyles<{
|
||||
size: number,
|
||||
hasOnFunction: boolean,
|
||||
hasRemoveFunction: boolean,
|
||||
backgroundColor?: string,
|
||||
}>(theme => ({
|
||||
root: ({ size, hasOnFunction, hasRemoveFunction, backgroundColor }) => {
|
||||
const baseColor = backgroundColor ? backgroundColor : Color(theme.primary).set('rgb.g', 100).hex();
|
||||
const hoverColor = (hasOnFunction && !hasRemoveFunction) ? Color(theme.primary).set('rgb.g', 150).hex() : baseColor
|
||||
return {
|
||||
backgroundColor: baseColor,
|
||||
borderRadius: hasRemoveFunction ? 10 : 500,
|
||||
height: size,
|
||||
width: size,
|
||||
cursor: hasOnFunction ? "pointer" : 'default',
|
||||
"&:hover": {
|
||||
backgroundColor: hoverColor,
|
||||
},
|
||||
fontSize: `${size * 0.4}px`,
|
||||
display: 'flex',
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center',
|
||||
}
|
||||
},
|
||||
icon: {
|
||||
"&:hover": {
|
||||
borderRadius: 500,
|
||||
background: Color(theme.primary).set('rgb.g', 150).hex(),
|
||||
}
|
||||
},
|
||||
trashIcon: {
|
||||
padding: 1,
|
||||
"&:hover": {
|
||||
borderRadius: 500,
|
||||
background: Color(theme.primary).set('rgb.g', 150).hex(),
|
||||
cursor: 'pointer',
|
||||
}
|
||||
}
|
||||
}))
|
||||
@@ -0,0 +1,87 @@
|
||||
import { Edge, Node } from "react-flow-renderer";
|
||||
import { AutomationTrigger } from 'types/automations/triggers';
|
||||
import { DAGFlowDims, FlowData, TriggerMakerOptions, DAGFlowNode } from './types';
|
||||
|
||||
// export const makeTriggerMakerOptions = (
|
||||
// triggers: AutomationTrigger[],
|
||||
// onUpdate: (t: AutomationTrigger[]) => void,
|
||||
// ): TriggerMakerOptions => ({
|
||||
// onAdd: () => onUpdate([...triggers, {
|
||||
// alias: "",
|
||||
|
||||
// }])
|
||||
// })
|
||||
|
||||
export const triggerToFlow = (
|
||||
trigger: AutomationTrigger[],
|
||||
{
|
||||
distanceFactor,
|
||||
nodeHeight,
|
||||
nodeWidth,
|
||||
circleSize,
|
||||
padding,
|
||||
}: DAGFlowDims,
|
||||
opts: TriggerMakerOptions,
|
||||
): FlowData => {
|
||||
// outputs
|
||||
const nodes: DAGFlowNode[] = [],
|
||||
edges: Edge[] = [];
|
||||
|
||||
// target node
|
||||
const condPos = { x: padding + nodeWidth * 2, y: padding + nodeHeight * 0.25 };
|
||||
nodes.push({
|
||||
id: `c`,
|
||||
type: 'dagcircle',
|
||||
position: condPos,
|
||||
data: {
|
||||
size: circleSize,
|
||||
backgroundColor: 'blue',
|
||||
}
|
||||
})
|
||||
|
||||
// convert all triggers to dag nodes
|
||||
trigger.forEach((t, i) => {
|
||||
const flowId = `t-${i}`;
|
||||
nodes.push({
|
||||
id: flowId,
|
||||
type: 'dagnode',
|
||||
data: {
|
||||
label: t.alias ?? t.platform,
|
||||
height: nodeHeight,
|
||||
width: nodeWidth,
|
||||
color: 'red',
|
||||
onEdit: () => opts.onEdit(i),
|
||||
onXClick: () => opts.onDelete(i, flowId),
|
||||
},
|
||||
position: { x: padding, y: padding + nodeHeight * distanceFactor * i }
|
||||
})
|
||||
edges.push({
|
||||
id: `e(${flowId})-(c)`,
|
||||
source: flowId,
|
||||
target: 'c',
|
||||
})
|
||||
});
|
||||
|
||||
// create a 'add button'
|
||||
nodes.push({
|
||||
id: `trigger-add`,
|
||||
type: 'dagcircle',
|
||||
position: {
|
||||
x: padding + nodeWidth * 0.4,
|
||||
y: padding + nodeHeight * distanceFactor * (nodes.length - 1),
|
||||
},
|
||||
data: {
|
||||
size: circleSize,
|
||||
backgroundColor: 'green',
|
||||
onAdd: opts.onAdd,
|
||||
disableSource: true,
|
||||
disableTarget: true,
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
return {
|
||||
nodes,
|
||||
edges
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { Edge, Node } from 'react-flow-renderer';
|
||||
import { AutomationTrigger } from 'types/automations/triggers';
|
||||
import { DAGCircleProps } from './DAGCircle';
|
||||
import { DAGNodeProps } from './DAGNode';
|
||||
|
||||
|
||||
export type DAGFlowDims = {
|
||||
padding: number;
|
||||
nodeHeight: number;
|
||||
nodeWidth: number;
|
||||
// addHeight: number;
|
||||
// addWidth: number;
|
||||
circleSize: number;
|
||||
distanceFactor: number;
|
||||
}
|
||||
|
||||
export type FlowData = {
|
||||
nodes: Node[],
|
||||
edges: Edge[],
|
||||
}
|
||||
|
||||
export type TriggerMakerOptions = {
|
||||
onAdd: () => void,
|
||||
onEdit: (i: number) => void,
|
||||
onDelete: (i: number) => void,
|
||||
}
|
||||
export type DAGFlowNode = Node<DAGNodeProps | DAGCircleProps>;
|
||||
Reference in new issue
Block a user