experimenting with noderow
This commit is contained in:
1 parent
5115dd9490
commit
bee584d85c
9 files changed
+242
-135
No files matched your search
@@ -0,0 +1,9 @@
|
||||
.automation-editor--nodes-row .collection-nodes--wrap {
|
||||
--node-height: 12em !important;
|
||||
--node-width: 100% !important;
|
||||
}
|
||||
|
||||
.automation-editor--nodes-row .collection-nodes--wrap .collection-nodes {
|
||||
min-width: 90%;
|
||||
min-height: 10em;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import { DEFAULT_DIMS } from "components/DAGGraph/elements/constants";
|
||||
import { makeStory } from "devUtils";
|
||||
import { useState } from "react";
|
||||
import { NodesRow } from ".";
|
||||
|
||||
const { make, componentMeta } = makeStory({
|
||||
Component: NodesRow,
|
||||
BaseTemplate: (props) => {
|
||||
const [data, setData] = useState(props.data);
|
||||
return (
|
||||
<NodesRow
|
||||
{...props}
|
||||
data={data}
|
||||
onUpdate={(up) => {
|
||||
setData(up);
|
||||
props.onUpdate(up);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
},
|
||||
meta: {
|
||||
title: "App/AutomationEditor/NodesRow",
|
||||
args: {
|
||||
data: [],
|
||||
sequenceNodeDims: DEFAULT_DIMS.node,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export default componentMeta;
|
||||
export const Empty = make({});
|
||||
export const One = make({
|
||||
type: "trigger",
|
||||
data: [
|
||||
{
|
||||
platform: "calendar",
|
||||
event: "test",
|
||||
},
|
||||
],
|
||||
});
|
||||
@@ -0,0 +1,42 @@
|
||||
import "./index.css";
|
||||
|
||||
import { AutomationNode, AutomationNodeMapping } from "types/automations";
|
||||
import { useHA } from "haService";
|
||||
import { CollectionNodeElement } from "components/DAGGraph/nodes/CollectionNode/CollectionNodeElement";
|
||||
import { getDescriptionFromAutomationNode } from "utils/formatting";
|
||||
import { Size } from "components/DAGGraph/elements/types";
|
||||
|
||||
export type NodesRowProps<
|
||||
K extends keyof AutomationNodeMapping,
|
||||
T extends AutomationNode<K>
|
||||
> = {
|
||||
type: K;
|
||||
data: T[];
|
||||
onUpdate: (d: T[]) => void;
|
||||
sequenceNodeDims: Size;
|
||||
};
|
||||
|
||||
export function NodesRow<
|
||||
K extends keyof AutomationNodeMapping,
|
||||
T extends AutomationNode<K>
|
||||
>({ type: collectionType, data, sequenceNodeDims }: NodesRowProps<K, T>) {
|
||||
const { namer } = useHA();
|
||||
return (
|
||||
<div className="automation-editor--nodes-row">
|
||||
<CollectionNodeElement
|
||||
width={0}
|
||||
height={0}
|
||||
nodes={data.map((node) => ({
|
||||
enabled: node.enabled ?? true,
|
||||
label: getDescriptionFromAutomationNode(node, namer, true),
|
||||
onXClick: () => {},
|
||||
}))}
|
||||
onAddNode={() => {}}
|
||||
collectionType={collectionType}
|
||||
title="if"
|
||||
sequenceNode={sequenceNodeDims}
|
||||
flipped
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -43,19 +43,20 @@ export const useAutomatioEditorState = (
|
||||
};
|
||||
const makeUpdate =
|
||||
<K extends keyof EditorData, D extends EditorData[K]>(name: K) =>
|
||||
(data: D) =>
|
||||
(data: D) => {
|
||||
(state.status === "changed" || state.status === "unchanged") &&
|
||||
update({
|
||||
status: "changed",
|
||||
previous:
|
||||
state.status === "unchanged"
|
||||
? [state.data]
|
||||
: state.previous.concat([state.data]),
|
||||
data: {
|
||||
...state.data,
|
||||
[name]: data,
|
||||
},
|
||||
});
|
||||
update({
|
||||
status: "changed",
|
||||
previous:
|
||||
state.status === "unchanged"
|
||||
? [state.data]
|
||||
: state.previous.concat([state.data]),
|
||||
data: {
|
||||
...state.data,
|
||||
[name]: data,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const saveAndUpdate = (data: EditorData) => {
|
||||
if (state.status === "changed") {
|
||||
|
||||
@@ -1,123 +1,45 @@
|
||||
import "./CollectionNode.css";
|
||||
|
||||
import Add from "@mui/icons-material/Add";
|
||||
import { ButtonIcon } from "components/Icons/ButtonIcons";
|
||||
import { FC } from "react";
|
||||
import { SequenceNodeElement } from "../SequenceNode/SequenceNodeElement";
|
||||
import { CollectionNodeProps } from "./types";
|
||||
import { Handle, Position } from "react-flow-renderer";
|
||||
import { useSequenceNodeColor } from "../SequenceNode/util";
|
||||
import { prettyName } from "utils/formatting";
|
||||
import DeleteForeverIcon from "@mui/icons-material/DeleteForever";
|
||||
import { useConfirm } from "material-ui-confirm";
|
||||
import { useSnackbar } from "notistack";
|
||||
|
||||
export const CollectionNode: FC<CollectionNodeProps> = ({
|
||||
onAddNode,
|
||||
nodes,
|
||||
sequenceNode,
|
||||
height,
|
||||
width,
|
||||
flipped,
|
||||
color,
|
||||
collectionType,
|
||||
onDelete,
|
||||
title,
|
||||
}) => {
|
||||
const nodeColor = useSequenceNodeColor(color);
|
||||
const confirm = useConfirm();
|
||||
const snackbr = useSnackbar();
|
||||
const trueTile = title ?? collectionType;
|
||||
import { CollectionNodeElement } from "./CollectionNodeElement";
|
||||
|
||||
export const CollectionNode: FC<CollectionNodeProps> = (props) => {
|
||||
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}?`,
|
||||
})
|
||||
.then(() => {
|
||||
onDelete();
|
||||
snackbr.enqueueSnackbar("Deleted.", {
|
||||
variant: "info",
|
||||
});
|
||||
})
|
||||
.catch(() =>
|
||||
snackbr.enqueueSnackbar(`Did not delete ${trueTile}.`, {
|
||||
variant: "info",
|
||||
})
|
||||
);
|
||||
}}
|
||||
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}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
<ButtonIcon className="add" icon={<Add />} onClick={onAddNode} />
|
||||
<>
|
||||
<Handle
|
||||
id="default"
|
||||
key="source-def"
|
||||
type="source"
|
||||
position={flipped ? Position.Bottom : Position.Right}
|
||||
/>
|
||||
<Handle
|
||||
id="head"
|
||||
key="source-head"
|
||||
type="source"
|
||||
position={flipped ? Position.Top : Position.Left}
|
||||
/>
|
||||
</>
|
||||
<>
|
||||
<Handle
|
||||
id="default"
|
||||
key="target-def"
|
||||
type="target"
|
||||
position={flipped ? Position.Top : Position.Left}
|
||||
/>
|
||||
<Handle
|
||||
id="side"
|
||||
key="target-side"
|
||||
type="target"
|
||||
position={!flipped ? Position.Right : Position.Left}
|
||||
/>
|
||||
<Handle
|
||||
id="return"
|
||||
key="target-return"
|
||||
type="target"
|
||||
position={!flipped ? Position.Bottom : Position.Right}
|
||||
/>
|
||||
</>
|
||||
<span className="collection-nodes--total">
|
||||
{nodes.length} node{nodes.length !== 1 ? "s" : ""}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<CollectionNodeElement {...props}>
|
||||
<>
|
||||
<Handle
|
||||
id="default"
|
||||
key="source-def"
|
||||
type="source"
|
||||
position={props.flipped ? Position.Bottom : Position.Right}
|
||||
/>
|
||||
<Handle
|
||||
id="head"
|
||||
key="source-head"
|
||||
type="source"
|
||||
position={props.flipped ? Position.Top : Position.Left}
|
||||
/>
|
||||
</>
|
||||
<>
|
||||
<Handle
|
||||
id="default"
|
||||
key="target-def"
|
||||
type="target"
|
||||
position={props.flipped ? Position.Top : Position.Left}
|
||||
/>
|
||||
<Handle
|
||||
id="side"
|
||||
key="target-side"
|
||||
type="target"
|
||||
position={!props.flipped ? Position.Right : Position.Left}
|
||||
/>
|
||||
<Handle
|
||||
id="return"
|
||||
key="target-return"
|
||||
type="target"
|
||||
position={!props.flipped ? Position.Bottom : Position.Right}
|
||||
/>
|
||||
</>
|
||||
</CollectionNodeElement>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,94 @@
|
||||
import "./CollectionNode.css";
|
||||
|
||||
import Add from "@mui/icons-material/Add";
|
||||
import { ButtonIcon } from "components/Icons/ButtonIcons";
|
||||
import { FC, PropsWithChildren } from "react";
|
||||
import { SequenceNodeElement } from "../SequenceNode/SequenceNodeElement";
|
||||
import { CollectionNodeProps } from "./types";
|
||||
import {
|
||||
convertNodeTypeToSequenceNodeColor,
|
||||
useSequenceNodeColor,
|
||||
} from "../SequenceNode/util";
|
||||
import { prettyName } from "utils/formatting";
|
||||
import DeleteForeverIcon from "@mui/icons-material/DeleteForever";
|
||||
import { useConfirm } from "material-ui-confirm";
|
||||
import { useSnackbar } from "notistack";
|
||||
|
||||
export const CollectionNodeElement: FC<PropsWithChildren<CollectionNodeProps>> =
|
||||
({
|
||||
onAddNode,
|
||||
nodes,
|
||||
sequenceNode,
|
||||
height,
|
||||
width,
|
||||
flipped,
|
||||
collectionType,
|
||||
onDelete,
|
||||
title,
|
||||
children,
|
||||
}) => {
|
||||
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}?`,
|
||||
})
|
||||
.then(() => {
|
||||
onDelete();
|
||||
snackbr.enqueueSnackbar("Deleted.", {
|
||||
variant: "info",
|
||||
});
|
||||
})
|
||||
.catch(() =>
|
||||
snackbr.enqueueSnackbar(`Did not delete ${trueTile}.`, {
|
||||
variant: "info",
|
||||
})
|
||||
);
|
||||
}}
|
||||
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}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
<ButtonIcon className="add" icon={<Add />} onClick={onAddNode} />
|
||||
{children}
|
||||
<span className="collection-nodes--total">
|
||||
{nodes.length} node{nodes.length !== 1 ? "s" : ""}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -1,18 +1,12 @@
|
||||
import { NodeManager } from "components/DAGGraph/nodes/NodeManager";
|
||||
import { CollectionNode } from "./CollectionNode";
|
||||
import { CollectionNodeDataProps } from "./types";
|
||||
import { SequenceNodeColor } from "../SequenceNode/types";
|
||||
|
||||
export const CollectionNodeMaker = NodeManager.register(
|
||||
"CollectionNode",
|
||||
CollectionNode,
|
||||
(pre: CollectionNodeDataProps, { node, flipped }) => ({
|
||||
...pre,
|
||||
color: (pre.collectionType === "condition"
|
||||
? "blue"
|
||||
: pre.collectionType === "action"
|
||||
? "green"
|
||||
: "red") as SequenceNodeColor,
|
||||
sequenceNode: node,
|
||||
flipped,
|
||||
})
|
||||
|
||||
@@ -20,5 +20,4 @@ export type CollectionNodeDataProps = Size & {
|
||||
export type CollectionNodeProps = CollectionNodeDataProps & {
|
||||
sequenceNode: Size;
|
||||
flipped: boolean;
|
||||
color: SequenceNodeColor;
|
||||
};
|
||||
@@ -1,5 +1,6 @@
|
||||
import { SequenceNodeColor } from "./types";
|
||||
import { useTheme } from "@mui/material/styles";
|
||||
import { AutomationNodeMapping } from "types/automations";
|
||||
|
||||
export const useSequenceNodeColor = (color: SequenceNodeColor): string => {
|
||||
const theme = useTheme();
|
||||
@@ -16,3 +17,8 @@ export const useSequenceNodeColor = (color: SequenceNodeColor): string => {
|
||||
? theme.palette.error.dark
|
||||
: "none";
|
||||
};
|
||||
|
||||
export const convertNodeTypeToSequenceNodeColor = (
|
||||
type: keyof AutomationNodeMapping
|
||||
): SequenceNodeColor =>
|
||||
type === "condition" ? "blue" : type === "action" ? "green" : "red";
|
||||
Reference in new issue
Block a user