Hidable triggers/conditions

This commit is contained in:
ari committed 2022-09-24 14:24:03 -04:00
1 parent 51bac6f0e9
commit 8fea6f1082
25 files changed
+704 -300

No files matched your search

@@ -0,0 +1,92 @@
import { DEFAULT_DIMS } from "components/DAGGraph/elements/constants";
import { makeStory } from "devUtils";
import { NodesRow } from ".";
import { createUpdater } from "../updater/index";
const { make, componentMeta } = makeStory({
Component: NodesRow,
BaseTemplate: (props) => {
const updater = createUpdater({
openModal: () => {},
action: {
data: [],
onUpdate: () => {},
},
condition: {
data: [],
onUpdate: () => {},
},
trigger: {
data: [],
onUpdate: () => {},
},
});
return <NodesRow {...props} stateUpdater={updater} />;
},
meta: {
title: "DAGGraph/NodesRow/Single",
args: {
type: "trigger",
data: [],
sequenceNodeDims: DEFAULT_DIMS.node,
},
},
});
export default componentMeta;
export const Empty = make({});
export const One = make({
data: [
{
platform: "calendar",
event: "start",
},
],
});
export const Many = make({
data: [
{
platform: "calendar",
event: "start",
},
{
platform: "homeassistant",
event: "start",
},
{
platform: "homeassistant",
event: "start",
},
{
platform: "homeassistant",
event: "start",
},
{
platform: "homeassistant",
event: "start",
},
{
platform: "homeassistant",
event: "start",
},
{
platform: "homeassistant",
event: "start",
},
{
platform: "homeassistant",
event: "start",
},
{
platform: "homeassistant",
event: "start",
},
{
platform: "homeassistant",
event: "start",
},
].map((n, id) => ({
...n,
id: `Trigger ${id}`,
})),
});
@@ -0,0 +1,95 @@
import { DEFAULT_DIMS } from "components/DAGGraph/elements/constants";
import { makeStory } from "devUtils";
import { createUpdater } from "../updater";
import { TriggerConditionNodeRow } from ".";
const { make, componentMeta } = makeStory({
Component: TriggerConditionNodeRow,
BaseTemplate: (props) => {
const updater = createUpdater({
openModal: () => {},
action: {
data: [],
onUpdate: () => {},
},
condition: {
data: [],
onUpdate: () => {},
},
trigger: {
data: [],
onUpdate: () => {},
},
});
return <TriggerConditionNodeRow {...props} stateUpdater={updater} />;
},
meta: {
title: "DAGGraph/NodesRow/TriggerAndConditions",
args: {
condition: [],
trigger: [],
sequenceNodeDims: DEFAULT_DIMS.node,
},
},
});
export default componentMeta;
export const Empty = make({});
export const One = make({
trigger: [
{
platform: "calendar",
event: "start",
},
],
condition: [
{
condition: "and",
conditions: [],
},
],
});
export const Many = make({
trigger: [
{
platform: "calendar",
event: "start",
},
{
platform: "homeassistant",
event: "start",
},
{
platform: "homeassistant",
event: "start",
},
{
platform: "homeassistant",
event: "start",
},
{
platform: "homeassistant",
event: "start",
},
{
platform: "homeassistant",
event: "start",
},
{
platform: "homeassistant",
event: "start",
},
{
platform: "homeassistant",
event: "start",
},
{
platform: "homeassistant",
event: "start",
},
{
platform: "homeassistant",
event: "start",
},
],
});
@@ -0,0 +1,87 @@
/* .nodes-row--outer {
--nodes-row-height: 10em;
min-width: 100%;
width: 100%;
max-width: 100%;
min-height: var(--nodes-row-height);
height: var(--nodes-row-height);
max-height: var(--nodes-row-height);
overflow: auto;
}
.nodes-row--inner {
max-width: calc(100% - 4em);
width: calc(100% - 4em);
min-height: calc(var(--nodes-row-height) - 2em);
max-height: calc(var(--nodes-row-height) - 2em);
gap: 2em;
padding-left: 1em;
padding-right: 1em;
border-style: solid;
border-top-width: 0.125em;
border-left-width: 1px;
border-right-width: 1px;
border-radius: 10px;
border-bottom-color: var(--mui-grey-500);
border-left-color: var(--mui-grey-700);
border-right-color: var(--mui-grey-700);
border-top-color: var(--sequence-node-color);
overflow: auto;
} */
.nodes-row--inner.collection-nodes--wrap {
--node-height: none !important;
--node-width: 100% !important;
margin: 0;
display: flex;
flex: 1;
min-width: 95%;
max-width: 95%;
}
.nodes-row--inner.collection-nodes--wrap .collection-nodes {
min-width: 90%;
min-height: 8em;
max-height: 8em;
overflow: hidden;
}
.trigger-condition-nodes-row {
gap: 0.25em;
}
.fullscreen .trigger-condition-nodes-row .nodes-row--inner {
opacity: 0;
height: 0;
z-index: -10;
}
.nodes-row--inner.collection-nodes--wrap .collection-nodes .collection-nodes--inner {
min-width: inherit;
width: inherit;
max-width: inherit;
padding-bottom: 0 !important;
padding-left: 0 !important;
min-height: 5em;
max-height: 7em;
}
.mobile .trigger-condition-nodes-row {
flex-direction: column;
}
.mobile .nodes-row.collection-nodes--wrap .collection-nodes {
width: 80%;
}
@@ -0,0 +1,103 @@
import "./index.css";
//* <div className="nodes-row--inner row">
// {data.map((node, i) => {
// return (
// <SequenceNodeElement
// key={i}
// {...makeCollectionNodeMakerChildProps(
// collectionType,
// node,
// i,
// stateUpdater,
// namer
// )}
// />
// );
// })}
// </div>*/
import { AutomationNode, AutomationNodeMapping } from "types/automations";
import { useHA } from "services/ha";
import { CollectionNodeElement } from "components/DAGGraph/nodes/CollectionNode/CollectionNodeElement";
import { Size } from "components/DAGGraph/elements/types";
import { DAGGraphUpdater } from "components/DAGGraph/updater";
import { makeCollectionNodeMakerChildProps } from "components/DAGGraph/elements/helpers";
import { AutomationCondition } from "types/automations/conditions";
import { AutomationTrigger } from "types/automations/triggers";
import { FC } from "react";
import { SequenceNodeElement } from "components/DAGGraph/nodes/SequenceNode/SequenceNodeElement";
export type NodesRowProps<
K extends keyof AutomationNodeMapping,
T extends AutomationNode<K>
> = {
type: K;
data: T[];
stateUpdater: DAGGraphUpdater;
sequenceNodeDims: Size;
};
export function NodesRow<
K extends keyof AutomationNodeMapping,
T extends AutomationNode<K>
>({
type: collectionType,
data,
sequenceNodeDims,
stateUpdater,
}: NodesRowProps<K, T>) {
const { namer } = useHA();
return (
<div className="nodes-row--outer center row">
<CollectionNodeElement
className="nodes-row--inner"
width={0}
height={0}
nodes={data.map((node, i) =>
makeCollectionNodeMakerChildProps(
collectionType,
node,
i,
stateUpdater,
namer
)
)}
onAddNode={() => stateUpdater.basic[collectionType].addNode(null)}
collectionType={collectionType}
sequenceNode={sequenceNodeDims}
flipped
/>
</div>
);
}
export type TriggerConditionNodeRowProps = {
condition: AutomationCondition[];
trigger: AutomationTrigger[];
stateUpdater: DAGGraphUpdater;
sequenceNodeDims: Size;
};
export const TriggerConditionNodeRow: FC<TriggerConditionNodeRowProps> = ({
condition,
trigger,
stateUpdater,
sequenceNodeDims,
}) => {
return (
<div className={["trigger-condition-nodes-row column"].join(" ")}>
<NodesRow
data={trigger}
type="trigger"
stateUpdater={stateUpdater}
sequenceNodeDims={sequenceNodeDims}
/>
<NodesRow
data={condition}
type="condition"
stateUpdater={stateUpdater}
sequenceNodeDims={sequenceNodeDims}
/>
</div>
);
};
@@ -11,7 +11,6 @@ const { make, componentMeta } = makeStory({
});
export default componentMeta;
export const Example = make({});
export const Loading = make({
state: {
+11 -12
View File
@@ -1,8 +1,8 @@
import "./index.css";
import { FC } from "react";
import { FC, useEffect } from "react";
import CircularProgress from "@mui/material/CircularProgress";
import ReactFlow, { Controls, ReactFlowProvider } from "react-flow-renderer";
import ReactFlow, { Controls, useReactFlow } from "react-flow-renderer";
import { NodeEditor } from "components/NodeEditor";
import { Modal } from "components/Modal";
@@ -10,20 +10,13 @@ import { Modal } from "components/Modal";
import { nodeTypes } from "../nodes";
import { DAGGraphBoardProps } from "./types";
export const DAGGraphBoard: FC<DAGGraphBoardProps> = (props) => {
return (
<ReactFlowProvider>
<DAGGraphBoardInner {...props} />
</ReactFlowProvider>
);
};
export const DAGGraphBoardInner: FC<DAGGraphBoardProps> = ({
export const DAGGraphBoard: FC<DAGGraphBoardProps> = ({
modalState,
state,
closeModal,
additionalControls,
}) => {
const reactFlow = useReactFlow();
// render unready or errored states
if (!state.ready) {
return (
@@ -37,6 +30,12 @@ export const DAGGraphBoardInner: FC<DAGGraphBoardProps> = ({
return <div className="dag-graphboard errored">{state.data.error}</div>;
}
// useEffect(() => {
// if (state.ready) {
// reactFlow.fitView();
// }
// }, [state.ready, reactFlow]);
// modal
let modalBody = <></>;
if (modalState) {
@@ -70,7 +69,7 @@ export const DAGGraphBoardInner: FC<DAGGraphBoardProps> = ({
nodesDraggable={false}
{...elements}
>
<Controls showInteractive={false} showFitView>
<Controls showInteractive={false} showFitView={false}>
{additionalControls}
</Controls>
</ReactFlow>
@@ -2,17 +2,46 @@ import { XYPosition } from "react-flow-renderer";
import AddBox from "@mui/icons-material/AddBox";
import { getDescriptionFromAutomationNode } from "utils/formatting";
import { getDescriptionFromAutomationNode, Namer } from "utils/formatting";
import { validateNode } from "utils/automations";
import { AutomationCondition } from "types/automations/conditions";
import { AutomationTrigger } from "types/automations/triggers";
import { DAGDims, ElementMaker, ElementMakerProps } from "./types";
import { DAGDims, ElementMaker, ElementMakerProps, Size } from "./types";
import { DAGElementsOutputState } from "./outputState";
import { convertFailuresToSequenceNodeDataProps } from "./util";
import { ButtonNodeMaker } from "../nodes/ButtonNode";
import { CollectionNodeMaker } from "../nodes/CollectionNode";
import { DAGGraphUpdater } from "../updater/index";
import { AutomationActionData } from "types/automations";
export function makeCollectionNodeMakerChildProps<
NodeType extends keyof AutomationActionData,
Node extends AutomationActionData[NodeType][0]
>(
nodeType: NodeType,
node: Node,
index: number,
stateUpdater: DAGGraphUpdater,
namer: Namer
) {
const failures = validateNode(node, [nodeType]);
if (failures) {
return {
enabled: node.enabled ?? true,
...stateUpdater.createNodeActions(nodeType, index, {}),
...convertFailuresToSequenceNodeDataProps(failures),
};
}
return {
enabled: node.enabled ?? true,
label: getDescriptionFromAutomationNode(node, namer, true),
...stateUpdater.createNodeActions(nodeType, index, {}),
};
}
export const makeTriggerNodes: ElementMaker<AutomationTrigger> = (
nodes,
@@ -24,23 +53,15 @@ export const makeTriggerNodes: ElementMaker<AutomationTrigger> = (
...dims.collection,
collectionType: "trigger",
onAddNode: () => stateUpdater.basic.trigger.addNode(null),
nodes: nodes.map((node, index) => {
const failures = validateNode(node, ["trigger"]);
if (failures) {
return {
enabled: node.enabled ?? true,
...stateUpdater.createNodeActions("trigger", index, {}),
...convertFailuresToSequenceNodeDataProps(failures),
};
}
return {
enabled: node.enabled ?? true,
label: getDescriptionFromAutomationNode(node, namer, true),
...stateUpdater.createNodeActions("trigger", index, {}),
};
}),
nodes: nodes.map((node, index) =>
makeCollectionNodeMakerChildProps(
"trigger",
node,
index,
stateUpdater,
namer
)
),
})
);
return outputState;
@@ -57,23 +78,15 @@ export const makeConditionNodes: ElementMaker<AutomationCondition> = (
collectionType: "condition",
hasInput: !!lastNodeId,
onAddNode: () => stateUpdater.basic.condition.addNode(null),
nodes: nodes.map((node, index) => {
const failures = validateNode(node, ["condition"]);
if (failures) {
return {
enabled: node.enabled ?? true,
...stateUpdater.createNodeActions("condition", index, {}),
...convertFailuresToSequenceNodeDataProps(failures),
};
}
return {
enabled: node.enabled ?? true,
label: getDescriptionFromAutomationNode(node, namer, true),
...stateUpdater.createNodeActions("condition", index, {}),
};
}),
nodes: nodes.map((node, index) =>
makeCollectionNodeMakerChildProps(
"condition",
node,
index,
stateUpdater,
namer
)
),
})
);
if (lastNodeId && outputState.lastNodeId) {
@@ -24,15 +24,19 @@ const { make, componentMeta } = makeStory({
state,
setState
);
const elementData = useAutomationNodes(state, {
dims: {
...DEFAULT_DIMS,
flipped: props.flipped,
const elementData = useAutomationNodes(
state,
{
dims: {
...DEFAULT_DIMS,
flipped: props.flipped,
},
namer,
openModal: setModalState,
stateUpdater: updater,
},
namer,
openModal: setModalState,
stateUpdater: updater,
});
false
);
return (
<DAGGraphBoard
modalState={modalState}
@@ -28,52 +28,57 @@ import * as helpers from "./helpers";
export const useAutomationNodes = (
automation: AutomationActionData,
args: ElementMakerBaseProps
args: ElementMakerBaseProps,
hideConditionAndTriggers: boolean
) => {
const dims: DAGDims = {
...args.dims,
};
const state = useDAGElementsState();
const outputState = new DAGElementsOutputState(dims.position, dims);
outputState.extend(
helpers.makeTriggerNodes(automation.trigger, {
...args,
state,
let actionStartPos = dims.position;
if (!hideConditionAndTriggers) {
outputState.extend(
helpers.makeTriggerNodes(automation.trigger, {
...args,
state,
dims,
position: dims.position,
nodeId: `${dims.flipped}-trigger`,
nodeIndex: 0,
})
);
outputState.extend(
helpers.makeConditionNodes(automation.condition, {
...args,
state,
dims,
position: distance.moveFromTo(
"collection",
"collection",
outputState.getLastNodePos(),
dims
),
nodeId: `${dims.flipped}-condition`,
nodeIndex: 1,
lastNodeId: outputState.lastNodeId,
})
);
actionStartPos = distance.moveFromTo(
"collection",
"node",
outputState.getLastNodePos(),
dims,
position: dims.position,
nodeId: `${dims.flipped}-trigger`,
nodeIndex: 0,
})
);
outputState.extend(
helpers.makeConditionNodes(automation.condition, {
...args,
state,
dims,
position: distance.moveFromTo(
"collection",
"collection",
outputState.getLastNodePos(),
dims
),
nodeId: `${dims.flipped}-condition`,
nodeIndex: 1,
lastNodeId: outputState.lastNodeId,
})
);
true
);
}
outputState.extend(
makeSequenceNodes(automation.action, {
...args,
state,
dims,
position: distance.moveFromTo(
"collection",
"node",
outputState.getLastNodePos(),
dims,
true
),
position: actionStartPos,
nodeId: `${dims.flipped}-action`,
nodeIndex: 2,
lastNodeId: outputState.lastNodeId,
+30 -16
View File
@@ -8,6 +8,7 @@ import { DEFAULT_DIMS } from "./elements/constants";
import { DAGGraphBoard } from "./board";
import { AutomationTrigger } from "types/automations/triggers";
import { AutomationCondition } from "types/automations/conditions";
import { NodesRow, TriggerConditionNodeRow } from "./NodesRow";
export const DAGAutomationGraph: FC<{
action: AutomationSequenceNode[];
@@ -17,6 +18,7 @@ export const DAGAutomationGraph: FC<{
onTriggerUpdate: (t: AutomationTrigger[]) => void;
onConditionUpdate: (c: AutomationCondition[]) => void;
isFlipped: boolean;
useNodesRow: boolean;
additionalControls?: ReactNode;
}> = (props) => {
const { namer } = useHA();
@@ -37,6 +39,10 @@ export const DAGAutomationGraph: FC<{
onUpdate: props.onTriggerUpdate as any,
},
});
const dims = {
...DEFAULT_DIMS,
flipped: props.isFlipped,
};
const elementData = useAutomationNodes(
{
action: props.action,
@@ -44,26 +50,34 @@ export const DAGAutomationGraph: FC<{
trigger: props.trigger,
},
{
dims: {
...DEFAULT_DIMS,
flipped: props.isFlipped,
},
dims,
namer,
openModal: setModalState,
stateUpdater: updater,
}
},
props.useNodesRow
);
return (
<DAGGraphBoard
modalState={modalState}
closeModal={() => setModalState(undefined)}
state={{
ready: true,
data: {
elements: elementData,
},
}}
additionalControls={props.additionalControls}
/>
<>
{props.useNodesRow && (
<TriggerConditionNodeRow
trigger={props.trigger}
condition={props.condition}
stateUpdater={updater}
sequenceNodeDims={dims.node}
/>
)}
<DAGGraphBoard
modalState={modalState}
closeModal={() => setModalState(undefined)}
state={{
ready: true,
data: {
elements: elementData,
},
}}
additionalControls={props.additionalControls}
/>
</>
);
};
@@ -15,87 +15,89 @@ import { useConfirm } from "material-ui-confirm";
import { useSnackbar } from "notistack";
import { useLang } from "services/lang";
export const CollectionNodeElement: FC<PropsWithChildren<CollectionNodeProps>> =
({
onAddNode,
nodes,
sequenceNode,
height,
width,
flipped,
collectionType,
onDelete,
title,
children,
}) => {
const lang = useLang();
const color = convertNodeTypeToSequenceNodeColor(collectionType);
const nodeColor = useSequenceNodeColor(color);
const confirm = useConfirm();
const snackbr = useSnackbar();
const trueTile = title ?? collectionType;
export const CollectionNodeElement: FC<
PropsWithChildren<CollectionNodeProps & { className?: string }>
> = ({
onAddNode,
nodes,
sequenceNode,
height,
width,
flipped,
collectionType,
onDelete,
title,
children,
className,
}) => {
const lang = useLang();
const color = convertNodeTypeToSequenceNodeColor(collectionType);
const nodeColor = useSequenceNodeColor(color);
const confirm = useConfirm();
const snackbr = useSnackbar();
const trueTile = title ?? collectionType;
return (
<div
className="collection-nodes--wrap"
style={
{
"--node-color": nodeColor,
"--node-height": `${height}px`,
"--node-width": `${width}px`,
} as any
}
>
<div className="collection-nodes">
{!!onDelete && (
<ButtonIcon
className="delete-icon"
icon={<DeleteForeverIcon />}
onClick={() => {
confirm({
description: `Are you sure you want to delete ${trueTile}?`,
return (
<div
className={["collection-nodes--wrap", className ?? ""].join(" ")}
style={
{
"--node-color": nodeColor,
"--node-height": `${height}px`,
"--node-width": `${width}px`,
} as any
}
>
<div className="collection-nodes">
{!!onDelete && (
<ButtonIcon
className="delete-icon"
icon={<DeleteForeverIcon />}
onClick={() => {
confirm({
description: `Are you sure you want to delete ${trueTile}?`,
})
.then(() => {
onDelete();
snackbr.enqueueSnackbar(lang.get("DELETED"), {
variant: "info",
});
})
.then(() => {
onDelete();
snackbr.enqueueSnackbar(lang.get("DELETED"), {
.catch(() =>
snackbr.enqueueSnackbar(
lang.get("NOT_DELETED_THING", {
item: trueTile,
}),
{
variant: "info",
});
})
.catch(() =>
snackbr.enqueueSnackbar(
lang.get("NOT_DELETED_THING", {
item: trueTile,
}),
{
variant: "info",
}
)
);
}}
borderless
color="secondary"
}
)
);
}}
borderless
color="secondary"
/>
)}
<span className="collection-nodes--title">
{prettyName(collectionType)}s
</span>
<div className="collection-nodes--inner">
{nodes.map((n, i) => (
<SequenceNodeElement
key={i}
color={color}
flipped={flipped}
{...n}
{...sequenceNode}
/>
)}
<span className="collection-nodes--title">
{prettyName(collectionType)}s
</span>
<div className="collection-nodes--inner">
{nodes.map((n, i) => (
<SequenceNodeElement
key={i}
color={color}
flipped={flipped}
{...n}
{...sequenceNode}
/>
))}
</div>
<ButtonIcon className="add" icon={<Add />} onClick={onAddNode} />
{children}
<span className="collection-nodes--total">
{nodes.length} node{nodes.length !== 1 ? "s" : ""}
</span>
))}
</div>
<ButtonIcon className="add" icon={<Add />} onClick={onAddNode} />
{children}
<span className="collection-nodes--total">
{nodes.length} node{nodes.length !== 1 ? "s" : ""}
</span>
</div>
);
};
</div>
);
};
@@ -11,8 +11,6 @@
width: var(--sequence-node-width);
min-height: var(--sequence-node-height);
min-width: var(--sequence-node-width);
/* outline: 1px dotted white; */
}
.sequence-node.disabled .sequence-node--inner {
@@ -86,7 +86,6 @@ export const createDummyUpdater = (): DAGUpdaterInput<any> => ({
data: [],
onUpdate: console.warn,
});
// export const createScriptConditionFieldUpdater = (f: ScriptConditionField, )
export type DAGGraphUpdater = ReturnType<typeof createUpdater>;
export type DAGGraphChooseUpdater = ReturnType<