diff --git a/webapp/src/components/AutomationEditor/NodesRow/index.css b/webapp/src/components/AutomationEditor/NodesRow/index.css new file mode 100644 index 0000000..027ffeb --- /dev/null +++ b/webapp/src/components/AutomationEditor/NodesRow/index.css @@ -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; +} \ No newline at end of file diff --git a/webapp/src/components/AutomationEditor/NodesRow/index.stories.tsx b/webapp/src/components/AutomationEditor/NodesRow/index.stories.tsx new file mode 100644 index 0000000..7b763b5 --- /dev/null +++ b/webapp/src/components/AutomationEditor/NodesRow/index.stories.tsx @@ -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 ( + { + 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", + }, + ], +}); diff --git a/webapp/src/components/AutomationEditor/NodesRow/index.tsx b/webapp/src/components/AutomationEditor/NodesRow/index.tsx new file mode 100644 index 0000000..b4b5fc5 --- /dev/null +++ b/webapp/src/components/AutomationEditor/NodesRow/index.tsx @@ -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 +> = { + type: K; + data: T[]; + onUpdate: (d: T[]) => void; + sequenceNodeDims: Size; +}; + +export function NodesRow< + K extends keyof AutomationNodeMapping, + T extends AutomationNode +>({ type: collectionType, data, sequenceNodeDims }: NodesRowProps) { + const { namer } = useHA(); + return ( +
+ ({ + enabled: node.enabled ?? true, + label: getDescriptionFromAutomationNode(node, namer, true), + onXClick: () => {}, + }))} + onAddNode={() => {}} + collectionType={collectionType} + title="if" + sequenceNode={sequenceNodeDims} + flipped + /> +
+ ); +} diff --git a/webapp/src/components/AutomationEditor/state.ts b/webapp/src/components/AutomationEditor/state.ts index 01e594d..5de19a6 100644 --- a/webapp/src/components/AutomationEditor/state.ts +++ b/webapp/src/components/AutomationEditor/state.ts @@ -43,19 +43,20 @@ export const useAutomatioEditorState = ( }; const makeUpdate = (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") { diff --git a/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.tsx b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.tsx index c3f93df..306dc25 100644 --- a/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.tsx +++ b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.tsx @@ -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 = ({ - 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 = (props) => { return ( -
-
- {!!onDelete && ( - } - 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" - /> - )} - - {prettyName(collectionType)}s - -
- {nodes.map((n, i) => ( - - ))} -
- } onClick={onAddNode} /> - <> - - - - <> - - - - - - {nodes.length} node{nodes.length !== 1 ? "s" : ""} - -
-
+ + <> + + + + <> + + + + + ); }; diff --git a/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNodeElement.tsx b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNodeElement.tsx new file mode 100644 index 0000000..3a22af3 --- /dev/null +++ b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNodeElement.tsx @@ -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> = + ({ + 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 ( +
+
+ {!!onDelete && ( + } + 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" + /> + )} + + {prettyName(collectionType)}s + +
+ {nodes.map((n, i) => ( + + ))} +
+ } onClick={onAddNode} /> + {children} + + {nodes.length} node{nodes.length !== 1 ? "s" : ""} + +
+
+ ); + }; diff --git a/webapp/src/components/DAGGraph/nodes/CollectionNode/index.tsx b/webapp/src/components/DAGGraph/nodes/CollectionNode/index.tsx index dda302d..cf8fd38 100644 --- a/webapp/src/components/DAGGraph/nodes/CollectionNode/index.tsx +++ b/webapp/src/components/DAGGraph/nodes/CollectionNode/index.tsx @@ -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, }) diff --git a/webapp/src/components/DAGGraph/nodes/CollectionNode/types.ts b/webapp/src/components/DAGGraph/nodes/CollectionNode/types.ts index ff98919..a44d851 100644 --- a/webapp/src/components/DAGGraph/nodes/CollectionNode/types.ts +++ b/webapp/src/components/DAGGraph/nodes/CollectionNode/types.ts @@ -20,5 +20,4 @@ export type CollectionNodeDataProps = Size & { export type CollectionNodeProps = CollectionNodeDataProps & { sequenceNode: Size; flipped: boolean; - color: SequenceNodeColor; }; diff --git a/webapp/src/components/DAGGraph/nodes/SequenceNode/util.ts b/webapp/src/components/DAGGraph/nodes/SequenceNode/util.ts index b33c738..9faa0f6 100644 --- a/webapp/src/components/DAGGraph/nodes/SequenceNode/util.ts +++ b/webapp/src/components/DAGGraph/nodes/SequenceNode/util.ts @@ -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";