- removed multi node
- updated tag updater
This commit is contained in:
1 parent
72afaee719
commit
a8229eb7bd
20 files changed
+337
-550
No files matched your search
@@ -64,6 +64,12 @@
|
||||
border-color: var(--primary);
|
||||
}
|
||||
|
||||
.automation-list-box--body--item--tags--add:hover {
|
||||
border-color: var(--primary);
|
||||
color: var(--primary-accent);
|
||||
background-color: var(--secondary);
|
||||
}
|
||||
|
||||
.automation-list-box--body--item {
|
||||
padding-left: 1em;
|
||||
cursor: pointer;
|
||||
|
||||
@@ -6,45 +6,66 @@ import { Page } from "components/Page";
|
||||
import { CookiesProvider } from "react-cookie";
|
||||
|
||||
export default {
|
||||
title: 'App/AutomationList/Box',
|
||||
title: "App/AutomationList/Box",
|
||||
component: AutomationListBox,
|
||||
parameters: { actions: { argTypesRegex: '^on.*' } },
|
||||
args: {
|
||||
}
|
||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||
args: {},
|
||||
} as ComponentMeta<typeof AutomationListBox>;
|
||||
|
||||
const Template: ComponentStory<typeof AutomationListBox> = (args) => {
|
||||
const [selected, setSelected] = useState(args.selected);
|
||||
return (
|
||||
<Page>
|
||||
<CookiesProvider>
|
||||
<AutomationListBox
|
||||
{...args}
|
||||
automations={args.automations}
|
||||
selected={selected}
|
||||
onSelectAutomation={(i) => {
|
||||
setSelected(i);
|
||||
args.onSelectAutomation(i);
|
||||
}}
|
||||
/>
|
||||
</CookiesProvider>
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
|
||||
const Template: ComponentStory<typeof AutomationListBox> = args => {
|
||||
const [selected, setSelected] = useState(args.selected)
|
||||
return <Page>
|
||||
<CookiesProvider>
|
||||
<AutomationListBox {...args} automations={args.automations} selected={selected} onSelectAutomation={(i) => {
|
||||
setSelected(i)
|
||||
args.onSelectAutomation(i)
|
||||
}} />
|
||||
</CookiesProvider>
|
||||
</Page>
|
||||
}
|
||||
|
||||
export const EmptyStart = Template.bind({})
|
||||
export const EmptyStart = Template.bind({});
|
||||
EmptyStart.args = {
|
||||
...EmptyStart.args,
|
||||
automations: [],
|
||||
}
|
||||
};
|
||||
|
||||
export const FewAutos = Template.bind({})
|
||||
export const FewAutos = Template.bind({});
|
||||
FewAutos.args = {
|
||||
...FewAutos.args,
|
||||
automations: [
|
||||
createMockAuto({ "Room": "Bathroom", "Type": "Lights", "NewDevice": "Yes", "Remote": "No", "Floor": "1" }),
|
||||
createMockAuto({ "Room": "Bathroom", "Type": "Climate" }),
|
||||
createMockAuto({ "Room": "Living Room", "Type": "Climate" }),
|
||||
createMockAuto({ "Type": "Climate" }),
|
||||
createMockAuto({ "Type": "Lights", "Routine": "Myself" }),
|
||||
createMockAuto({ "Routine": "DNS", "Type": "SSL" }),
|
||||
createMockAuto({ "Room": "Bedroom", "Type": "Lights", "Routine": "Baby" }),
|
||||
createMockAuto({ "Room": "Office", "Type": "Lights" }),
|
||||
createMockAuto({ "Room": "Office" }),
|
||||
createMockAuto({
|
||||
Room: "Bathroom",
|
||||
Type: "Lights",
|
||||
NewDevice: "Yes",
|
||||
Remote: "No",
|
||||
Floor: "1",
|
||||
}),
|
||||
createMockAuto({ Room: "Bathroom", Type: "Climate" }),
|
||||
createMockAuto({ Room: "Living Room", Type: "Climate" }),
|
||||
createMockAuto({ Type: "Climate" }),
|
||||
createMockAuto({ Type: "Lights", Routine: "Myself" }),
|
||||
createMockAuto({ Routine: "DNS", Type: "SSL" }),
|
||||
createMockAuto({ Room: "Bedroom", Type: "Lights", Routine: "Baby" }),
|
||||
createMockAuto({ Room: "Office", Type: "Lights" }),
|
||||
createMockAuto({ Room: "Office" }),
|
||||
createMockAuto(),
|
||||
],
|
||||
}
|
||||
};
|
||||
|
||||
export const OneAuto = Template.bind({});
|
||||
OneAuto.args = {
|
||||
...OneAuto.args,
|
||||
automations: [
|
||||
createMockAuto({
|
||||
Type: "Climate",
|
||||
}),
|
||||
],
|
||||
};
|
||||
@@ -7,6 +7,7 @@ import { Button } from "components/Inputs/Button";
|
||||
import { makeGrouping } from "./automationGrouper";
|
||||
import { AutomationListBoxGroup } from "./AutomationListBoxGroup";
|
||||
import { useCookies } from "react-cookie";
|
||||
import { TagDB } from "components/AutomationList/TagDB";
|
||||
|
||||
export type Props = {
|
||||
automations: AutomationData[];
|
||||
@@ -14,6 +15,7 @@ export type Props = {
|
||||
onSelectAutomation: (i: number) => void;
|
||||
onRemove: (i: number) => void;
|
||||
onAdd: () => void;
|
||||
tagsDB: TagDB;
|
||||
};
|
||||
|
||||
const useAutomationListBoxState = () => {
|
||||
@@ -56,6 +58,7 @@ export const AutomationListBox: FC<Props> = ({
|
||||
onSelectAutomation,
|
||||
onAdd,
|
||||
onRemove,
|
||||
tagsDB,
|
||||
}) => {
|
||||
const tags = getTagList(automations);
|
||||
const { searchText, setSearchText, selectedTagIdx, setSelectedTagIdx } =
|
||||
@@ -91,6 +94,7 @@ export const AutomationListBox: FC<Props> = ({
|
||||
autos={filteredAutomations}
|
||||
grouping={grouping}
|
||||
selectedAutomationIdx={selected}
|
||||
tagsDB={tagsDB}
|
||||
/>
|
||||
</div>
|
||||
<div className="automation-list-box--bottom">
|
||||
|
||||
@@ -4,6 +4,7 @@ import { AutomationData } from "types/automations";
|
||||
import { AutomationGrouping } from "./automationGrouper";
|
||||
import { FC, useState } from "react";
|
||||
import { ButtonIcon } from "components/Icons/ButtonIcons";
|
||||
import { TagDB } from "./TagDB";
|
||||
|
||||
export type AutomationListBoxEvents = {
|
||||
onSelectAutomation: (i: number) => void;
|
||||
@@ -24,12 +25,14 @@ export type AutomationListBoxGroupProps = {
|
||||
grouping: AutomationGrouping;
|
||||
autos: Array<[AutomationData, number]>;
|
||||
selectedAutomationIdx: number;
|
||||
tagsDB: TagDB;
|
||||
};
|
||||
export const AutomationListBoxGroup: FC<AutomationListBoxGroupProps> = ({
|
||||
events,
|
||||
grouping,
|
||||
autos,
|
||||
selectedAutomationIdx,
|
||||
tagsDB,
|
||||
}) => {
|
||||
if (grouping.top.length === 1) {
|
||||
return (
|
||||
@@ -41,7 +44,8 @@ export const AutomationListBoxGroup: FC<AutomationListBoxGroupProps> = ({
|
||||
currentNode: grouping.top[0],
|
||||
},
|
||||
events,
|
||||
selectedAutomationIdx
|
||||
selectedAutomationIdx,
|
||||
tagsDB
|
||||
)}
|
||||
</>
|
||||
);
|
||||
@@ -58,6 +62,7 @@ export const AutomationListBoxGroup: FC<AutomationListBoxGroupProps> = ({
|
||||
}}
|
||||
events={events}
|
||||
selectedAutomationIdx={selectedAutomationIdx}
|
||||
tagsDB={tagsDB}
|
||||
/>
|
||||
))}
|
||||
</>
|
||||
@@ -69,7 +74,8 @@ export const convertGroupsToItems = (
|
||||
autos: Array<[AutomationData, number]>,
|
||||
{ grouping, currentNode }: AutomationListBoxGroups,
|
||||
events: AutomationListBoxEvents,
|
||||
selectedAutomationIdx: number
|
||||
selectedAutomationIdx: number,
|
||||
tagsDB: TagDB
|
||||
) => {
|
||||
const automations = grouping.getAutomations(currentNode);
|
||||
if (automations.length > 0) {
|
||||
@@ -79,6 +85,7 @@ export const convertGroupsToItems = (
|
||||
events={events}
|
||||
auto={autos[autoIndex]}
|
||||
isSelected={selectedAutomationIdx === autos[autoIndex][1]}
|
||||
tagsDB={tagsDB}
|
||||
/>
|
||||
));
|
||||
}
|
||||
@@ -94,6 +101,7 @@ export const convertGroupsToItems = (
|
||||
}}
|
||||
events={events}
|
||||
selectedAutomationIdx={selectedAutomationIdx}
|
||||
tagsDB={tagsDB}
|
||||
/>
|
||||
));
|
||||
}
|
||||
@@ -105,7 +113,8 @@ export const AutomationListBoxGroupItem: FC<{
|
||||
groups: AutomationListBoxGroups;
|
||||
autos: Array<[AutomationData, number]>;
|
||||
selectedAutomationIdx: number;
|
||||
}> = ({ autos, groups, events, selectedAutomationIdx }) => {
|
||||
tagsDB: TagDB;
|
||||
}> = ({ autos, groups, events, selectedAutomationIdx, tagsDB }) => {
|
||||
const groupData = groups.grouping.getData(groups.currentNode);
|
||||
const automations = groups.grouping.getAutomations(groups.currentNode);
|
||||
const [open, setOpen] = useState(false);
|
||||
@@ -131,7 +140,13 @@ export const AutomationListBoxGroupItem: FC<{
|
||||
{groupData.name} {openIcon} {total}
|
||||
</b>
|
||||
{open &&
|
||||
convertGroupsToItems(autos, groups, events, selectedAutomationIdx)}
|
||||
convertGroupsToItems(
|
||||
autos,
|
||||
groups,
|
||||
events,
|
||||
selectedAutomationIdx,
|
||||
tagsDB
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -140,10 +155,12 @@ export const AutomationListBoxItem: FC<{
|
||||
auto: [AutomationData, number];
|
||||
isSelected: boolean;
|
||||
events: AutomationListBoxEvents;
|
||||
tagsDB: TagDB;
|
||||
}> = ({
|
||||
events: { onRemove, onSelectAutomation },
|
||||
auto: [auto, autoIndex],
|
||||
isSelected,
|
||||
tagsDB,
|
||||
}) => {
|
||||
let title = "BadAuto<<Missing Metadata>>";
|
||||
if (auto.metadata) {
|
||||
@@ -159,7 +176,7 @@ export const AutomationListBoxItem: FC<{
|
||||
title={title}
|
||||
onClick={() => onSelectAutomation(autoIndex)}
|
||||
>
|
||||
<MetadataBox metadata={auto.metadata} tags={auto.tags} />
|
||||
<MetadataBox metadata={auto.metadata} tags={auto.tags} tagsDB={tagsDB} />
|
||||
<ButtonIcon onClick={() => onRemove(autoIndex)} icon={<TrashIcon />} />
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
.automation-list-box--body--item--tags--item {
|
||||
color: white;
|
||||
border-color: white;
|
||||
border-style: solid;
|
||||
border-width: 1px;
|
||||
border-radius: 0.5em;
|
||||
padding: 0.1em 0.5em;
|
||||
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
gap: 0.25em;
|
||||
max-width: min-content;
|
||||
align-items: center;
|
||||
|
||||
}
|
||||
|
||||
.automation-list-box--body--item--tags--item>.text:hover {
|
||||
text-decoration: underline;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.automation-list-box--body--item--tags--item>.text {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.automation-list-box--body--item--tags--item>.text .MuiFilledInput-root {
|
||||
padding: 0;
|
||||
min-width: 6em;
|
||||
max-width: 10em;
|
||||
background-color: var(--primary);
|
||||
}
|
||||
|
||||
.automation-list-box--body--item--tags--item>.text .icon {
|
||||
position: absolute;
|
||||
right: 0.5em;
|
||||
top: 0.5em;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.automation-list-box--body--item--tags--item>.text .icon:hover {
|
||||
background-color: var(--primary-accent);
|
||||
border-radius: 20px;
|
||||
}
|
||||
|
||||
.automation-list-box--body--item--tags--item>.text .icon:hover svg {
|
||||
fill: var(--primary);
|
||||
}
|
||||
@@ -1,27 +1,102 @@
|
||||
import { FC } from "react"
|
||||
import { AutomationMetadata } from "types/automations"
|
||||
import "./AutomationMetadataBox.css";
|
||||
import { CheckMarkIcon } from "components/Icons";
|
||||
import { FC, useState } from "react";
|
||||
import { AutomationMetadata } from "types/automations";
|
||||
import { InputAutoComplete } from "components/Inputs/InputAutoComplete";
|
||||
import { TagDB } from "./TagDB";
|
||||
import { cleanUpUndefined } from "components/NodeEditor/OptionManager/OptionManager";
|
||||
|
||||
export const MetadataBox: FC<{
|
||||
metadata: AutomationMetadata,
|
||||
tags: Record<string, string>,
|
||||
metadata: AutomationMetadata;
|
||||
tags: Record<string, string>;
|
||||
tagsDB: TagDB;
|
||||
}> = (auto) => {
|
||||
let title = <span>{"BadAuto<<Missing Metadata>>"}</span>
|
||||
if (auto.metadata) {
|
||||
title = <>
|
||||
<b>{String(auto.metadata.alias ?? "").slice(0, 15)} <span>({String(auto.metadata.id).slice(0, 5)})</span></b>
|
||||
<span>{String(auto.metadata.description ?? "").slice(0, 25)}</span>
|
||||
</>
|
||||
}
|
||||
return <div
|
||||
className="automation-list-box--body--item--title"
|
||||
>
|
||||
{title}
|
||||
<div
|
||||
className="automation-list-box--body--item--tags"
|
||||
>
|
||||
{Object.keys(auto.tags).map(tagName => <span key={tagName}>
|
||||
<b>{tagName}:</b> {auto.tags[tagName]}
|
||||
</span>)}
|
||||
</div>
|
||||
const [tags, setTags] = useState(auto.tags);
|
||||
const tagOptions = auto.tagsDB.getTagNames(Object.keys(tags));
|
||||
let title = <span>{"BadAuto<<Missing Metadata>>"}</span>;
|
||||
if (auto.metadata) {
|
||||
title = (
|
||||
<>
|
||||
<b>
|
||||
{String(auto.metadata.alias ?? "").slice(0, 15)}{" "}
|
||||
<span>({String(auto.metadata.id).slice(0, 5)})</span>
|
||||
</b>
|
||||
<span>{String(auto.metadata.description ?? "").slice(0, 25)}</span>
|
||||
</>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<div className="automation-list-box--body--item--title">
|
||||
{title}
|
||||
<div className="automation-list-box--body--item--tags">
|
||||
{Object.keys(tags).map((tagName) => (
|
||||
<MetadataBoxTag
|
||||
options={[tagOptions, auto.tagsDB.getTagValues(tagName)]}
|
||||
key={tagName}
|
||||
value={[tagName, tags[tagName]]}
|
||||
onChange={([newName, newValue]) =>
|
||||
setTags(
|
||||
cleanUpUndefined({
|
||||
...tags,
|
||||
[tagName]: undefined,
|
||||
[newName]: newValue,
|
||||
})
|
||||
)
|
||||
}
|
||||
/>
|
||||
))}
|
||||
<span className="automation-list-box--body--item--tags--add">+</span>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
export const MetadataBoxTag: FC<{
|
||||
value: [string, string];
|
||||
onChange: (t: [string, string]) => void;
|
||||
options: [string[], string[]];
|
||||
}> = (props) => {
|
||||
const [[editMode1, editMode2], setEditMode] = useState([false, false]);
|
||||
const toggle1 = () => setEditMode([!editMode1, editMode2]);
|
||||
const toggle2 = () => setEditMode([editMode1, !editMode2]);
|
||||
return (
|
||||
<span className="automation-list-box--body--item--tags--item">
|
||||
{editMode1 ? (
|
||||
<InputAutoComplete
|
||||
multiple={false}
|
||||
className="text"
|
||||
disableChips
|
||||
options={props.options[0]}
|
||||
label=""
|
||||
value={props.value[0]}
|
||||
onEnter={toggle1}
|
||||
onChange={(v) => props.onChange([v ?? "", props.value[1]])}
|
||||
onlyShowLabel
|
||||
endAdornment={<CheckMarkIcon className="icon" onClick={toggle1} />}
|
||||
/>
|
||||
) : (
|
||||
<b className="text" onClick={toggle1}>
|
||||
{props.value[0]}:
|
||||
</b>
|
||||
)}
|
||||
{editMode2 ? (
|
||||
<InputAutoComplete
|
||||
multiple={false}
|
||||
className="text"
|
||||
disableChips
|
||||
options={props.options[1]}
|
||||
label=""
|
||||
value={props.value[1]}
|
||||
onEnter={toggle2}
|
||||
onChange={(v) => props.onChange([props.value[0], v ?? ""])}
|
||||
onlyShowLabel
|
||||
endAdornment={<CheckMarkIcon className="icon" onClick={toggle2} />}
|
||||
/>
|
||||
) : (
|
||||
<span className="text" onClick={toggle2}>
|
||||
{props.value[1]}
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,31 @@
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { DEFAULT_DIMS } from "components/DAGFlow/constants";
|
||||
import { Page } from "components/Page";
|
||||
import { MetadataBoxTag } from "./AutomationMetadataBox";
|
||||
import { useState } from "react";
|
||||
|
||||
export default {
|
||||
title: "App/AutomationList/Box",
|
||||
component: MetadataBoxTag,
|
||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||
args: {
|
||||
value: ["Type", "Climate"],
|
||||
options: [[], []],
|
||||
},
|
||||
} as ComponentMeta<typeof MetadataBoxTag>;
|
||||
|
||||
export const BoxTag: ComponentStory<typeof MetadataBoxTag> = (args) => {
|
||||
const [state, setState] = useState(args.value);
|
||||
return (
|
||||
<Page>
|
||||
<MetadataBoxTag
|
||||
options={args.options}
|
||||
value={state}
|
||||
onChange={(v) => {
|
||||
setState(v);
|
||||
args.onChange(v);
|
||||
}}
|
||||
/>
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
@@ -93,6 +93,7 @@ export const AutomationList: FC<AutomationListParams> = ({
|
||||
}}
|
||||
selected={current}
|
||||
onRemove={onRemove}
|
||||
tagsDB={tagDB}
|
||||
/>
|
||||
<ButtonIcon
|
||||
onClick={() => setHideList(!hideList)}
|
||||
|
||||
@@ -36,7 +36,6 @@ export const makeOnEditAutomationConditions =
|
||||
) =>
|
||||
() =>
|
||||
openModal({
|
||||
single: true,
|
||||
node:
|
||||
conditions.length > 1
|
||||
? {
|
||||
|
||||
@@ -15,7 +15,6 @@ import { DAGCircle } from "./DAGCircle";
|
||||
import { sequenceToFlow } from "./sequenceConvertor";
|
||||
import { makeSequenceUpdater } from "./updater";
|
||||
import { NodeEditor } from "components/NodeEditor";
|
||||
import { MultiNodeEditor } from "components/MultiNodeEditors";
|
||||
import { Modal } from "components/Modal";
|
||||
import { AutomationCondition } from "types/automations/conditions";
|
||||
import { convertToFlowNode, makeOnEditAutomationConditions } from "./helpers";
|
||||
@@ -56,30 +55,19 @@ export const DAGAutomationFlow: FC<DAGAutomationFlowProps> = ({
|
||||
// modal
|
||||
let modalBody = <></>;
|
||||
if (modalState) {
|
||||
if (modalState.single) {
|
||||
modalBody = (
|
||||
<NodeEditor
|
||||
node={modalState.node}
|
||||
isErrored={modalState.isError}
|
||||
onClose={() => setModalState(null)}
|
||||
onSave={(n) => {
|
||||
modalState.update(n as any);
|
||||
setModalState(null);
|
||||
}}
|
||||
allowedTypes={modalState.allowedTypes}
|
||||
saveBtnCreateText={modalState.saveBtnCreateText}
|
||||
/>
|
||||
);
|
||||
} else {
|
||||
modalBody = (
|
||||
<MultiNodeEditor
|
||||
sequence={modalState.node}
|
||||
onClose={() => setModalState(null)}
|
||||
onSave={(n) => modalState.update(n as any)}
|
||||
allowedTypes={["condition"]}
|
||||
/>
|
||||
);
|
||||
}
|
||||
modalBody = (
|
||||
<NodeEditor
|
||||
node={modalState.node}
|
||||
isErrored={modalState.isError}
|
||||
onClose={() => setModalState(null)}
|
||||
onSave={(n) => {
|
||||
modalState.update(n as any);
|
||||
setModalState(null);
|
||||
}}
|
||||
allowedTypes={modalState.allowedTypes}
|
||||
saveBtnCreateText={modalState.saveBtnCreateText}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
// convert automation to flow data
|
||||
@@ -119,7 +107,6 @@ export const DAGAutomationFlow: FC<DAGAutomationFlowProps> = ({
|
||||
namer,
|
||||
onAdd: () =>
|
||||
setModalState({
|
||||
single: true,
|
||||
saveBtnCreateText: true,
|
||||
node: {
|
||||
platform: "device",
|
||||
@@ -133,7 +120,6 @@ export const DAGAutomationFlow: FC<DAGAutomationFlowProps> = ({
|
||||
onTriggerUpdate([...trigger.slice(0, i), ...trigger.slice(i + 1)]),
|
||||
onEdit: (i) =>
|
||||
setModalState({
|
||||
single: true,
|
||||
node: trigger[i],
|
||||
update: (t) =>
|
||||
onTriggerUpdate([...trigger.slice(0, i), t, ...trigger.slice(i + 1)]),
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import { Edge, Node, XYPosition } from "react-flow-renderer";
|
||||
import { AutomationSequenceNode } from "types/automations";
|
||||
import { AutomationNodeTypes, AutomationSequenceNode } from "types/automations";
|
||||
import { DAGCircleProps } from "./DAGCircle";
|
||||
import { DAGNodeProps } from "./DAGNode";
|
||||
import { AutomationTrigger } from "types/automations/triggers";
|
||||
import { MultiNodeEditorProps } from "components/MultiNodeEditors/types";
|
||||
import { DAGErrorNodeProps } from "./DAGErrorNode";
|
||||
import { Namer } from "utils/formatting";
|
||||
|
||||
@@ -40,18 +39,10 @@ export type UpdateModalState<
|
||||
|
||||
export type ModalState<
|
||||
T extends AutomationSequenceNode | AutomationTrigger = any
|
||||
> =
|
||||
| {
|
||||
single: true;
|
||||
isError?: boolean;
|
||||
node: T;
|
||||
update: (n: T) => void;
|
||||
saveBtnCreateText?: boolean;
|
||||
allowedTypes: MultiNodeEditorProps["allowedTypes"];
|
||||
}
|
||||
| {
|
||||
single: false;
|
||||
node: AutomationSequenceNode[];
|
||||
update: (n: AutomationSequenceNode[]) => void;
|
||||
allowedTypes: ["condition"];
|
||||
};
|
||||
> = {
|
||||
isError?: boolean;
|
||||
node: T;
|
||||
update: (n: T) => void;
|
||||
saveBtnCreateText?: boolean;
|
||||
allowedTypes: AutomationNodeTypes[];
|
||||
};
|
||||
@@ -1,70 +1,45 @@
|
||||
import { AutomationSequenceNode } from "types/automations"
|
||||
import { AutomationSequenceNode } from "types/automations";
|
||||
import { ChooseAction } from "types/automations/actions";
|
||||
import { ModalState } from "./types";
|
||||
import { makeSequenceUpdater } from "./updater"
|
||||
import { makeSequenceUpdater } from "./updater";
|
||||
|
||||
const mockSequenceUpdater = (defaultState: AutomationSequenceNode[] = []) => {
|
||||
let state: AutomationSequenceNode[] = [...defaultState]
|
||||
const modalStates: ModalState[] = [];
|
||||
let state: AutomationSequenceNode[] = [...defaultState];
|
||||
const modalStates: ModalState[] = [];
|
||||
|
||||
const updater = makeSequenceUpdater(
|
||||
state,
|
||||
s => {
|
||||
state = s
|
||||
},
|
||||
ms => modalStates.push(ms)
|
||||
);
|
||||
return {
|
||||
get state() {
|
||||
return state
|
||||
},
|
||||
modalStates,
|
||||
updater
|
||||
};
|
||||
}
|
||||
const updater = makeSequenceUpdater(
|
||||
state,
|
||||
(s) => {
|
||||
state = s;
|
||||
},
|
||||
(ms) => modalStates.push(ms)
|
||||
);
|
||||
return {
|
||||
get state() {
|
||||
return state;
|
||||
},
|
||||
modalStates,
|
||||
updater,
|
||||
};
|
||||
};
|
||||
|
||||
test("sequence updater updates nodes", () => {
|
||||
const mock = mockSequenceUpdater([
|
||||
const mock = mockSequenceUpdater([
|
||||
{
|
||||
choose: [
|
||||
{
|
||||
"choose": [
|
||||
{
|
||||
"conditions": [],
|
||||
"sequence": [],
|
||||
}
|
||||
],
|
||||
"default": [],
|
||||
}
|
||||
]);
|
||||
expect(mock.state.length).toBe(1)
|
||||
expect((mock.state[0] as ChooseAction).alias).toBe(undefined)
|
||||
mock.updater.updateNode(0, {
|
||||
...mock.state[0],
|
||||
alias: "Hello"
|
||||
} as any)
|
||||
expect((mock.state[0] as any).alias).toBe("Hello")
|
||||
})
|
||||
test('update conditions for node', () => {
|
||||
const mock = mockSequenceUpdater([
|
||||
{
|
||||
"choose": [
|
||||
{
|
||||
"conditions": [],
|
||||
"sequence": [],
|
||||
}
|
||||
],
|
||||
"default": [],
|
||||
}
|
||||
]);
|
||||
mock.updater.makeOnEditConditionsForChooseNode(0, 0)();
|
||||
|
||||
expect(mock.modalStates.length).toBe(1)
|
||||
expect((mock.modalStates[0].node as AutomationSequenceNode[]).length).toBe(0)
|
||||
mock.modalStates[0].update([
|
||||
{
|
||||
"condition": "and",
|
||||
"conditions": [],
|
||||
}
|
||||
] as any)
|
||||
expect((mock.state[0] as ChooseAction).choose[0].conditions.length).toBe(1)
|
||||
expect((mock.state[0] as ChooseAction).choose[0].conditions[0].condition).toBe("and")
|
||||
})
|
||||
conditions: [],
|
||||
sequence: [],
|
||||
},
|
||||
],
|
||||
default: [],
|
||||
},
|
||||
]);
|
||||
expect(mock.state.length).toBe(1);
|
||||
expect((mock.state[0] as ChooseAction).alias).toBe(undefined);
|
||||
mock.updater.updateNode(0, {
|
||||
...mock.state[0],
|
||||
alias: "Hello",
|
||||
} as any);
|
||||
expect((mock.state[0] as any).alias).toBe("Hello");
|
||||
});
|
||||
@@ -21,7 +21,6 @@ export const makeSequenceUpdater = (
|
||||
},
|
||||
addNode() {
|
||||
return openModal({
|
||||
single: true,
|
||||
saveBtnCreateText: true,
|
||||
node: {
|
||||
device_id: "",
|
||||
@@ -85,7 +84,6 @@ export const makeSequenceUpdater = (
|
||||
makeOnModalOpenForNode(i: number, node: AutomationSequenceNode) {
|
||||
return () =>
|
||||
openModal({
|
||||
single: true,
|
||||
node,
|
||||
allowedTypes: ["action", "condition"],
|
||||
update: (n) => this.updateNode(i, n),
|
||||
@@ -114,7 +112,6 @@ export const makeSequenceUpdater = (
|
||||
const node = sequence[i];
|
||||
return () =>
|
||||
openModal({
|
||||
single: true,
|
||||
isError: true,
|
||||
node,
|
||||
allowedTypes: [getNodeType(node)],
|
||||
|
||||
@@ -28,6 +28,8 @@ export type InputAutoCompletePropsBase = {
|
||||
onlyShowLabel?: boolean;
|
||||
helperText?: ReactNode;
|
||||
endAdornment?: ReactNode;
|
||||
onEnter?: () => void;
|
||||
disableChips?: boolean;
|
||||
} & (
|
||||
| {
|
||||
value: string | null;
|
||||
@@ -203,12 +205,20 @@ export function InputAutoComplete<T extends Option>(
|
||||
...params.InputProps,
|
||||
endAdornment: props.endAdornment ?? params.InputProps.endAdornment,
|
||||
}}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === "Enter" && props.onEnter) {
|
||||
props.onEnter();
|
||||
}
|
||||
}}
|
||||
variant="filled"
|
||||
required={props.required}
|
||||
label={props.label ?? "Entity ID"}
|
||||
helperText={helperText}
|
||||
disabled={props.disabled}
|
||||
error={error}
|
||||
placeholder={
|
||||
props.disableChips && !props.multiple ? props.value ?? "" : ""
|
||||
}
|
||||
/>
|
||||
)}
|
||||
renderTags={(tagValue, getTagProps) => {
|
||||
@@ -241,6 +251,9 @@ export function InputAutoComplete<T extends Option>(
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
if (props.disableChips) {
|
||||
return <></>;
|
||||
}
|
||||
if (props.multiple) {
|
||||
return (
|
||||
<Badge badgeContent={tagValue.length} color="info">
|
||||
|
||||
@@ -11,12 +11,14 @@ export interface InputAutoTextProps {
|
||||
required?: boolean;
|
||||
endAdornment?: ReactNode;
|
||||
onChange: (v: string) => void;
|
||||
onEnter?: () => void;
|
||||
}
|
||||
export default function InputAutoText({
|
||||
value = "",
|
||||
label,
|
||||
options = [],
|
||||
onChange,
|
||||
onEnter = () => {},
|
||||
className,
|
||||
required,
|
||||
endAdornment,
|
||||
@@ -41,6 +43,11 @@ export default function InputAutoText({
|
||||
InputProps={{
|
||||
endAdornment,
|
||||
}}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === "Enter") {
|
||||
onEnter();
|
||||
}
|
||||
}}
|
||||
onChange={(e) => {
|
||||
e.preventDefault();
|
||||
onChange(e.target.value);
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
.multinode-editor {
|
||||
max-width: 80vw;
|
||||
min-width: 60vw;
|
||||
}
|
||||
|
||||
.multinode-editor.mobile {
|
||||
max-width: 95vw;
|
||||
}
|
||||
|
||||
.multinode-editor .node-editor--root {
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.multinode-editor .node-editor--body {
|
||||
padding-bottom: 1em;
|
||||
padding-top: 0.25em;
|
||||
padding-left: 0.25em;
|
||||
padding-right: 0.25em;
|
||||
max-height: 60vh;
|
||||
}
|
||||
|
||||
.multinode-editor--navbar {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
justify-content: space-evenly;
|
||||
align-items: center;
|
||||
min-width: 20vw;
|
||||
}
|
||||
|
||||
.multinode-editor.modded>.multinode-editor--navbar * {
|
||||
border-color: var(--green-light);
|
||||
}
|
||||
|
||||
.multinode-editor.modded .multinode-editor--total {
|
||||
color: var(--green-light)
|
||||
}
|
||||
|
||||
.multinode-editor.modded .node-editor--footer--save {
|
||||
border-color: var(--green-opaque);
|
||||
background-color: var(--green-light);
|
||||
color: var(--primary);
|
||||
}
|
||||
|
||||
.multinode-editor.modded .node-editor--footer--save:hover {
|
||||
border-color: var(--green-light);
|
||||
background-color: var(--green-opaque);
|
||||
color: var(--green-light);
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
import React, { useState } from "react";
|
||||
import { ComponentMeta, ComponentStory } from '@storybook/react';
|
||||
import { MultiNodeEditor } from ".";
|
||||
import { Button } from "components/Inputs/Button";
|
||||
import { Modal } from "components/Modal";
|
||||
import { Page } from "components/Page";
|
||||
|
||||
|
||||
export default {
|
||||
title: 'MultiNodeEditor',
|
||||
component: MultiNodeEditor,
|
||||
parameters: { actions: { argTypesRegex: '^on.*' } },
|
||||
args: {
|
||||
allowedTypes: ['action', 'condition', 'trigger'],
|
||||
sequence: [
|
||||
{
|
||||
"condition": "numeric_state",
|
||||
"entity_id": [
|
||||
"sensor.kitchen_humidity"
|
||||
],
|
||||
"conditions": [],
|
||||
"above": "40"
|
||||
},
|
||||
{
|
||||
"condition": "template",
|
||||
"value_template": "states('switch.kitchen') == 'on'"
|
||||
}
|
||||
]
|
||||
}
|
||||
} as ComponentMeta<typeof MultiNodeEditor>
|
||||
|
||||
export const Simple: ComponentStory<typeof MultiNodeEditor> = args => {
|
||||
const [state, setState] = useState(args.sequence);
|
||||
|
||||
return <Page>
|
||||
<div className="center column" style={{ paddingTop: "1em" }}>
|
||||
<MultiNodeEditor {...args} sequence={state} onSave={s => {
|
||||
args.onSave && args.onSave(s)
|
||||
setState(s)
|
||||
}} />
|
||||
</div>
|
||||
</Page>
|
||||
}
|
||||
export const InAModal: ComponentStory<typeof MultiNodeEditor> = props => {
|
||||
|
||||
const [open, setOpen] = useState(false);
|
||||
const [state, setState] = useState(props.sequence);
|
||||
return <Page>
|
||||
<Button onClick={() => setOpen(!open)}>Open Editor</Button>
|
||||
<Modal open={open}>
|
||||
<MultiNodeEditor
|
||||
{...props}
|
||||
sequence={state}
|
||||
onClose={() => setOpen(!open)}
|
||||
onSave={s => {
|
||||
props.onSave && props.onSave(s)
|
||||
setState(s)
|
||||
}}
|
||||
/>
|
||||
</Modal>
|
||||
</Page>
|
||||
};
|
||||
|
||||
|
||||
export const EmptyConditionEditor = Simple.bind({})
|
||||
EmptyConditionEditor.args = {
|
||||
...EmptyConditionEditor.args,
|
||||
allowedTypes: ['condition'],
|
||||
sequence: [],
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
import "./index.css";
|
||||
import { FC } from 'react';
|
||||
import { NodeEditor } from 'components/NodeEditor';
|
||||
import { Button } from 'components/Inputs/Button';
|
||||
import { useMultiNodeEditorState } from "./state";
|
||||
import { MultiNodeEditorProps } from "./types";
|
||||
import useWindowSize from 'utils/useWindowSize';
|
||||
|
||||
|
||||
export const MultiNodeEditor: FC<MultiNodeEditorProps> = props => {
|
||||
// state
|
||||
const state = useMultiNodeEditorState(props);
|
||||
const { isMobile } = useWindowSize();
|
||||
// components
|
||||
const nextOrAddBtn = <Button
|
||||
onClick={state.moveForward}
|
||||
>
|
||||
{!state.isLast ? ">>" : "+"}
|
||||
</Button>;
|
||||
// branch
|
||||
if (state.isEmpty) {
|
||||
return <div className={["multinode-editor", state.isModified ? "modded" : '', isMobile ? 'mobile' : ''].join(' ')} style={{
|
||||
minWidth: '20vw',
|
||||
}}>
|
||||
<div className="multinode-editor--navbar">
|
||||
{nextOrAddBtn}
|
||||
</div>
|
||||
<div className='node-editor--footer' style={{ marginTop: '1em' }}>
|
||||
<Button onClick={state.onClose}>Close</Button>
|
||||
<Button onClick={state.onSaveEmpty} className="node-editor--footer--save">Save</Button>
|
||||
</div>
|
||||
<div className='column center'>
|
||||
<span>{state.currentSlideNumber}/<u className="multinode-editor--total">{state.totalSlides}</u></span>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
return <div className={["multinode-editor", state.isModified ? "modded" : '', isMobile ? 'mobile' : ''].join(' ')}>
|
||||
<div className="multinode-editor--navbar">
|
||||
<Button onClick={state.moveBack} disabled={state.currentSlideNumber === 0}>{"<<"}</Button>
|
||||
<Button onClick={state.onRemove}> Delete</Button>
|
||||
{nextOrAddBtn}
|
||||
</div>
|
||||
<NodeEditor
|
||||
key={state.currentSlideNumber}
|
||||
node={state.currentSlide}
|
||||
allowedTypes={props.allowedTypes}
|
||||
onClose={state.onClose}
|
||||
onSave={state.onSave}
|
||||
onFlags={state.updateInternalNode}
|
||||
/>
|
||||
<div className='column center'>
|
||||
<span>{state.currentSlideNumber + 1}/<u className="multinode-editor--total">{state.totalSlides}</u></span>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
@@ -1,202 +0,0 @@
|
||||
import { MultiNodeEditorProps } from "./types";
|
||||
import { useState, useRef } from "react";
|
||||
import { AutomationNode } from "../../types/automations/index";
|
||||
import { makeDefault } from "utils/automations";
|
||||
import { useConfirm, ConfirmOptions } from "material-ui-confirm";
|
||||
import { useSnackbar } from "notistack";
|
||||
|
||||
export type MultiNodeEditorInternalState = {
|
||||
slides: AutomationNode[];
|
||||
slide: number;
|
||||
multiNode: {
|
||||
isModified: boolean;
|
||||
isSaved: boolean;
|
||||
};
|
||||
internalNodeModified: boolean;
|
||||
};
|
||||
|
||||
export const useMultiNodeEditorState = (props: MultiNodeEditorProps) => {
|
||||
// state
|
||||
const confirm = useConfirm();
|
||||
const snackbr = useSnackbar();
|
||||
const [state, _setState] = useState(
|
||||
makeInitialState({
|
||||
slides: props.sequence,
|
||||
})
|
||||
);
|
||||
const justSaved = useRef(false);
|
||||
|
||||
// alias
|
||||
const { slide, slides, multiNode, internalNodeModified } = state;
|
||||
const setState = (a: MultiNodeEditorInternalState) => {
|
||||
_setState(a);
|
||||
};
|
||||
const askThenDo = async (
|
||||
options: ConfirmOptions,
|
||||
action: () => void,
|
||||
condition = () => internalNodeModified && !multiNode.isSaved
|
||||
) => {
|
||||
if (condition()) {
|
||||
return await confirm(options)
|
||||
.then(action)
|
||||
.catch(() => {
|
||||
snackbr.enqueueSnackbar("Cancelling...", {
|
||||
variant: "info",
|
||||
});
|
||||
});
|
||||
} else {
|
||||
return action();
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
get totalSlides() {
|
||||
return slides.length;
|
||||
},
|
||||
get currentSlideNumber() {
|
||||
return slide;
|
||||
},
|
||||
get currentSlide() {
|
||||
return slides[slide];
|
||||
},
|
||||
get isLast() {
|
||||
return slide >= slides.length - 1;
|
||||
},
|
||||
get isEmpty() {
|
||||
return slides.length === 0;
|
||||
},
|
||||
get isModified() {
|
||||
return (
|
||||
(internalNodeModified || multiNode.isModified) && !multiNode.isSaved
|
||||
);
|
||||
},
|
||||
moveBack() {
|
||||
if (slide <= 0) {
|
||||
return;
|
||||
}
|
||||
return askThenDo(
|
||||
{
|
||||
description:
|
||||
"Moving back without saving will cause you to lose the work, are you sure you want to do that?",
|
||||
},
|
||||
() =>
|
||||
setState({
|
||||
...state,
|
||||
slide: slide - 1,
|
||||
internalNodeModified: false,
|
||||
})
|
||||
);
|
||||
},
|
||||
moveForward() {
|
||||
const createNew = slide >= slides.length - 1;
|
||||
const action = createNew ? "Creating a new new node" : "Moving forward";
|
||||
return askThenDo(
|
||||
{
|
||||
description: `${action} without saving will cause you to lose the work, are you sure you want to do that?`,
|
||||
},
|
||||
() => {
|
||||
if (createNew) {
|
||||
setState({
|
||||
...state,
|
||||
slide: slides.length,
|
||||
slides: slides.concat([makeDefault(props.allowedTypes[0])]),
|
||||
internalNodeModified: false,
|
||||
multiNode: {
|
||||
isModified: true,
|
||||
isSaved: false,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
setState({
|
||||
...state,
|
||||
slide: slide + 1,
|
||||
internalNodeModified: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
},
|
||||
updateInternalNode(isReady: boolean, isModified: boolean) {
|
||||
if (!isReady || isModified) {
|
||||
if (!state.multiNode.isModified || state.multiNode.isSaved) {
|
||||
if (justSaved.current) {
|
||||
justSaved.current = false;
|
||||
} else {
|
||||
setState({
|
||||
...state,
|
||||
multiNode: { isModified: true, isSaved: false },
|
||||
internalNodeModified: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
onRemove() {
|
||||
setState({
|
||||
...state,
|
||||
slide: Math.max(slide - 1, 0),
|
||||
slides: slides.slice(0, slide).concat(slides.slice(slide + 1)),
|
||||
multiNode: {
|
||||
isModified: true,
|
||||
isSaved: false,
|
||||
},
|
||||
});
|
||||
},
|
||||
onSave(node: AutomationNode) {
|
||||
justSaved.current = true;
|
||||
setState({
|
||||
...state,
|
||||
slides: slides
|
||||
.slice(0, slide)
|
||||
.concat([node])
|
||||
.concat(slides.slice(slide + 1)),
|
||||
multiNode: {
|
||||
isModified: multiNode.isModified,
|
||||
isSaved: true,
|
||||
},
|
||||
internalNodeModified: false,
|
||||
});
|
||||
},
|
||||
onSaveEmpty() {
|
||||
justSaved.current = true;
|
||||
setState({
|
||||
...state,
|
||||
multiNode: {
|
||||
isModified: multiNode.isModified,
|
||||
isSaved: true,
|
||||
},
|
||||
internalNodeModified: false,
|
||||
});
|
||||
},
|
||||
async onClose() {
|
||||
if (!multiNode.isSaved && multiNode.isModified) {
|
||||
try {
|
||||
await confirm({
|
||||
description:
|
||||
"You haven't saved your work, are you ok with losing it?",
|
||||
});
|
||||
props.onSave && props.onSave(slides);
|
||||
} catch (_: any) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (justSaved.current) {
|
||||
props.onSave && props.onSave(slides);
|
||||
}
|
||||
props.onClose && props.onClose();
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const makeInitialState = (
|
||||
props: Partial<MultiNodeEditorInternalState>
|
||||
): MultiNodeEditorInternalState => ({
|
||||
slides: [],
|
||||
slide: 0,
|
||||
multiNode: {
|
||||
isModified: false,
|
||||
isSaved: false,
|
||||
},
|
||||
internalNodeModified: false,
|
||||
...props,
|
||||
});
|
||||
@@ -1,8 +0,0 @@
|
||||
import { AutomationNode, AutomationNodeMapping } from "types/automations";
|
||||
|
||||
export interface MultiNodeEditorProps {
|
||||
sequence: AutomationNode[];
|
||||
allowedTypes: Array<keyof AutomationNodeMapping>;
|
||||
onClose?: () => void;
|
||||
onSave?: (n: AutomationNode[]) => void;
|
||||
}
|
||||
Reference in new issue
Block a user