- 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);
|
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 {
|
.automation-list-box--body--item {
|
||||||
padding-left: 1em;
|
padding-left: 1em;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
|
|||||||
@@ -6,45 +6,66 @@ import { Page } from "components/Page";
|
|||||||
import { CookiesProvider } from "react-cookie";
|
import { CookiesProvider } from "react-cookie";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
title: 'App/AutomationList/Box',
|
title: "App/AutomationList/Box",
|
||||||
component: AutomationListBox,
|
component: AutomationListBox,
|
||||||
parameters: { actions: { argTypesRegex: '^on.*' } },
|
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||||
args: {
|
args: {},
|
||||||
}
|
|
||||||
} as ComponentMeta<typeof AutomationListBox>;
|
} 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 => {
|
export const EmptyStart = Template.bind({});
|
||||||
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({})
|
|
||||||
EmptyStart.args = {
|
EmptyStart.args = {
|
||||||
...EmptyStart.args,
|
...EmptyStart.args,
|
||||||
automations: [],
|
automations: [],
|
||||||
}
|
};
|
||||||
|
|
||||||
export const FewAutos = Template.bind({})
|
export const FewAutos = Template.bind({});
|
||||||
FewAutos.args = {
|
FewAutos.args = {
|
||||||
...FewAutos.args,
|
...FewAutos.args,
|
||||||
automations: [
|
automations: [
|
||||||
createMockAuto({ "Room": "Bathroom", "Type": "Lights", "NewDevice": "Yes", "Remote": "No", "Floor": "1" }),
|
createMockAuto({
|
||||||
createMockAuto({ "Room": "Bathroom", "Type": "Climate" }),
|
Room: "Bathroom",
|
||||||
createMockAuto({ "Room": "Living Room", "Type": "Climate" }),
|
Type: "Lights",
|
||||||
createMockAuto({ "Type": "Climate" }),
|
NewDevice: "Yes",
|
||||||
createMockAuto({ "Type": "Lights", "Routine": "Myself" }),
|
Remote: "No",
|
||||||
createMockAuto({ "Routine": "DNS", "Type": "SSL" }),
|
Floor: "1",
|
||||||
createMockAuto({ "Room": "Bedroom", "Type": "Lights", "Routine": "Baby" }),
|
}),
|
||||||
createMockAuto({ "Room": "Office", "Type": "Lights" }),
|
createMockAuto({ Room: "Bathroom", Type: "Climate" }),
|
||||||
createMockAuto({ "Room": "Office" }),
|
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(),
|
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 { makeGrouping } from "./automationGrouper";
|
||||||
import { AutomationListBoxGroup } from "./AutomationListBoxGroup";
|
import { AutomationListBoxGroup } from "./AutomationListBoxGroup";
|
||||||
import { useCookies } from "react-cookie";
|
import { useCookies } from "react-cookie";
|
||||||
|
import { TagDB } from "components/AutomationList/TagDB";
|
||||||
|
|
||||||
export type Props = {
|
export type Props = {
|
||||||
automations: AutomationData[];
|
automations: AutomationData[];
|
||||||
@@ -14,6 +15,7 @@ export type Props = {
|
|||||||
onSelectAutomation: (i: number) => void;
|
onSelectAutomation: (i: number) => void;
|
||||||
onRemove: (i: number) => void;
|
onRemove: (i: number) => void;
|
||||||
onAdd: () => void;
|
onAdd: () => void;
|
||||||
|
tagsDB: TagDB;
|
||||||
};
|
};
|
||||||
|
|
||||||
const useAutomationListBoxState = () => {
|
const useAutomationListBoxState = () => {
|
||||||
@@ -56,6 +58,7 @@ export const AutomationListBox: FC<Props> = ({
|
|||||||
onSelectAutomation,
|
onSelectAutomation,
|
||||||
onAdd,
|
onAdd,
|
||||||
onRemove,
|
onRemove,
|
||||||
|
tagsDB,
|
||||||
}) => {
|
}) => {
|
||||||
const tags = getTagList(automations);
|
const tags = getTagList(automations);
|
||||||
const { searchText, setSearchText, selectedTagIdx, setSelectedTagIdx } =
|
const { searchText, setSearchText, selectedTagIdx, setSelectedTagIdx } =
|
||||||
@@ -91,6 +94,7 @@ export const AutomationListBox: FC<Props> = ({
|
|||||||
autos={filteredAutomations}
|
autos={filteredAutomations}
|
||||||
grouping={grouping}
|
grouping={grouping}
|
||||||
selectedAutomationIdx={selected}
|
selectedAutomationIdx={selected}
|
||||||
|
tagsDB={tagsDB}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="automation-list-box--bottom">
|
<div className="automation-list-box--bottom">
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { AutomationData } from "types/automations";
|
|||||||
import { AutomationGrouping } from "./automationGrouper";
|
import { AutomationGrouping } from "./automationGrouper";
|
||||||
import { FC, useState } from "react";
|
import { FC, useState } from "react";
|
||||||
import { ButtonIcon } from "components/Icons/ButtonIcons";
|
import { ButtonIcon } from "components/Icons/ButtonIcons";
|
||||||
|
import { TagDB } from "./TagDB";
|
||||||
|
|
||||||
export type AutomationListBoxEvents = {
|
export type AutomationListBoxEvents = {
|
||||||
onSelectAutomation: (i: number) => void;
|
onSelectAutomation: (i: number) => void;
|
||||||
@@ -24,12 +25,14 @@ export type AutomationListBoxGroupProps = {
|
|||||||
grouping: AutomationGrouping;
|
grouping: AutomationGrouping;
|
||||||
autos: Array<[AutomationData, number]>;
|
autos: Array<[AutomationData, number]>;
|
||||||
selectedAutomationIdx: number;
|
selectedAutomationIdx: number;
|
||||||
|
tagsDB: TagDB;
|
||||||
};
|
};
|
||||||
export const AutomationListBoxGroup: FC<AutomationListBoxGroupProps> = ({
|
export const AutomationListBoxGroup: FC<AutomationListBoxGroupProps> = ({
|
||||||
events,
|
events,
|
||||||
grouping,
|
grouping,
|
||||||
autos,
|
autos,
|
||||||
selectedAutomationIdx,
|
selectedAutomationIdx,
|
||||||
|
tagsDB,
|
||||||
}) => {
|
}) => {
|
||||||
if (grouping.top.length === 1) {
|
if (grouping.top.length === 1) {
|
||||||
return (
|
return (
|
||||||
@@ -41,7 +44,8 @@ export const AutomationListBoxGroup: FC<AutomationListBoxGroupProps> = ({
|
|||||||
currentNode: grouping.top[0],
|
currentNode: grouping.top[0],
|
||||||
},
|
},
|
||||||
events,
|
events,
|
||||||
selectedAutomationIdx
|
selectedAutomationIdx,
|
||||||
|
tagsDB
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
@@ -58,6 +62,7 @@ export const AutomationListBoxGroup: FC<AutomationListBoxGroupProps> = ({
|
|||||||
}}
|
}}
|
||||||
events={events}
|
events={events}
|
||||||
selectedAutomationIdx={selectedAutomationIdx}
|
selectedAutomationIdx={selectedAutomationIdx}
|
||||||
|
tagsDB={tagsDB}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</>
|
</>
|
||||||
@@ -69,7 +74,8 @@ export const convertGroupsToItems = (
|
|||||||
autos: Array<[AutomationData, number]>,
|
autos: Array<[AutomationData, number]>,
|
||||||
{ grouping, currentNode }: AutomationListBoxGroups,
|
{ grouping, currentNode }: AutomationListBoxGroups,
|
||||||
events: AutomationListBoxEvents,
|
events: AutomationListBoxEvents,
|
||||||
selectedAutomationIdx: number
|
selectedAutomationIdx: number,
|
||||||
|
tagsDB: TagDB
|
||||||
) => {
|
) => {
|
||||||
const automations = grouping.getAutomations(currentNode);
|
const automations = grouping.getAutomations(currentNode);
|
||||||
if (automations.length > 0) {
|
if (automations.length > 0) {
|
||||||
@@ -79,6 +85,7 @@ export const convertGroupsToItems = (
|
|||||||
events={events}
|
events={events}
|
||||||
auto={autos[autoIndex]}
|
auto={autos[autoIndex]}
|
||||||
isSelected={selectedAutomationIdx === autos[autoIndex][1]}
|
isSelected={selectedAutomationIdx === autos[autoIndex][1]}
|
||||||
|
tagsDB={tagsDB}
|
||||||
/>
|
/>
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@@ -94,6 +101,7 @@ export const convertGroupsToItems = (
|
|||||||
}}
|
}}
|
||||||
events={events}
|
events={events}
|
||||||
selectedAutomationIdx={selectedAutomationIdx}
|
selectedAutomationIdx={selectedAutomationIdx}
|
||||||
|
tagsDB={tagsDB}
|
||||||
/>
|
/>
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@@ -105,7 +113,8 @@ export const AutomationListBoxGroupItem: FC<{
|
|||||||
groups: AutomationListBoxGroups;
|
groups: AutomationListBoxGroups;
|
||||||
autos: Array<[AutomationData, number]>;
|
autos: Array<[AutomationData, number]>;
|
||||||
selectedAutomationIdx: number;
|
selectedAutomationIdx: number;
|
||||||
}> = ({ autos, groups, events, selectedAutomationIdx }) => {
|
tagsDB: TagDB;
|
||||||
|
}> = ({ autos, groups, events, selectedAutomationIdx, tagsDB }) => {
|
||||||
const groupData = groups.grouping.getData(groups.currentNode);
|
const groupData = groups.grouping.getData(groups.currentNode);
|
||||||
const automations = groups.grouping.getAutomations(groups.currentNode);
|
const automations = groups.grouping.getAutomations(groups.currentNode);
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
@@ -131,7 +140,13 @@ export const AutomationListBoxGroupItem: FC<{
|
|||||||
{groupData.name} {openIcon} {total}
|
{groupData.name} {openIcon} {total}
|
||||||
</b>
|
</b>
|
||||||
{open &&
|
{open &&
|
||||||
convertGroupsToItems(autos, groups, events, selectedAutomationIdx)}
|
convertGroupsToItems(
|
||||||
|
autos,
|
||||||
|
groups,
|
||||||
|
events,
|
||||||
|
selectedAutomationIdx,
|
||||||
|
tagsDB
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@@ -140,10 +155,12 @@ export const AutomationListBoxItem: FC<{
|
|||||||
auto: [AutomationData, number];
|
auto: [AutomationData, number];
|
||||||
isSelected: boolean;
|
isSelected: boolean;
|
||||||
events: AutomationListBoxEvents;
|
events: AutomationListBoxEvents;
|
||||||
|
tagsDB: TagDB;
|
||||||
}> = ({
|
}> = ({
|
||||||
events: { onRemove, onSelectAutomation },
|
events: { onRemove, onSelectAutomation },
|
||||||
auto: [auto, autoIndex],
|
auto: [auto, autoIndex],
|
||||||
isSelected,
|
isSelected,
|
||||||
|
tagsDB,
|
||||||
}) => {
|
}) => {
|
||||||
let title = "BadAuto<<Missing Metadata>>";
|
let title = "BadAuto<<Missing Metadata>>";
|
||||||
if (auto.metadata) {
|
if (auto.metadata) {
|
||||||
@@ -159,7 +176,7 @@ export const AutomationListBoxItem: FC<{
|
|||||||
title={title}
|
title={title}
|
||||||
onClick={() => onSelectAutomation(autoIndex)}
|
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 />} />
|
<ButtonIcon onClick={() => onRemove(autoIndex)} icon={<TrashIcon />} />
|
||||||
</div>
|
</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 "./AutomationMetadataBox.css";
|
||||||
import { AutomationMetadata } from "types/automations"
|
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<{
|
export const MetadataBox: FC<{
|
||||||
metadata: AutomationMetadata,
|
metadata: AutomationMetadata;
|
||||||
tags: Record<string, string>,
|
tags: Record<string, string>;
|
||||||
|
tagsDB: TagDB;
|
||||||
}> = (auto) => {
|
}> = (auto) => {
|
||||||
let title = <span>{"BadAuto<<Missing Metadata>>"}</span>
|
const [tags, setTags] = useState(auto.tags);
|
||||||
if (auto.metadata) {
|
const tagOptions = auto.tagsDB.getTagNames(Object.keys(tags));
|
||||||
title = <>
|
let title = <span>{"BadAuto<<Missing Metadata>>"}</span>;
|
||||||
<b>{String(auto.metadata.alias ?? "").slice(0, 15)} <span>({String(auto.metadata.id).slice(0, 5)})</span></b>
|
if (auto.metadata) {
|
||||||
<span>{String(auto.metadata.description ?? "").slice(0, 25)}</span>
|
title = (
|
||||||
</>
|
<>
|
||||||
}
|
<b>
|
||||||
return <div
|
{String(auto.metadata.alias ?? "").slice(0, 15)}{" "}
|
||||||
className="automation-list-box--body--item--title"
|
<span>({String(auto.metadata.id).slice(0, 5)})</span>
|
||||||
>
|
</b>
|
||||||
{title}
|
<span>{String(auto.metadata.description ?? "").slice(0, 25)}</span>
|
||||||
<div
|
</>
|
||||||
className="automation-list-box--body--item--tags"
|
);
|
||||||
>
|
}
|
||||||
{Object.keys(auto.tags).map(tagName => <span key={tagName}>
|
return (
|
||||||
<b>{tagName}:</b> {auto.tags[tagName]}
|
<div className="automation-list-box--body--item--title">
|
||||||
</span>)}
|
{title}
|
||||||
</div>
|
<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>
|
</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}
|
selected={current}
|
||||||
onRemove={onRemove}
|
onRemove={onRemove}
|
||||||
|
tagsDB={tagDB}
|
||||||
/>
|
/>
|
||||||
<ButtonIcon
|
<ButtonIcon
|
||||||
onClick={() => setHideList(!hideList)}
|
onClick={() => setHideList(!hideList)}
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ export const makeOnEditAutomationConditions =
|
|||||||
) =>
|
) =>
|
||||||
() =>
|
() =>
|
||||||
openModal({
|
openModal({
|
||||||
single: true,
|
|
||||||
node:
|
node:
|
||||||
conditions.length > 1
|
conditions.length > 1
|
||||||
? {
|
? {
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ import { DAGCircle } from "./DAGCircle";
|
|||||||
import { sequenceToFlow } from "./sequenceConvertor";
|
import { sequenceToFlow } from "./sequenceConvertor";
|
||||||
import { makeSequenceUpdater } from "./updater";
|
import { makeSequenceUpdater } from "./updater";
|
||||||
import { NodeEditor } from "components/NodeEditor";
|
import { NodeEditor } from "components/NodeEditor";
|
||||||
import { MultiNodeEditor } from "components/MultiNodeEditors";
|
|
||||||
import { Modal } from "components/Modal";
|
import { Modal } from "components/Modal";
|
||||||
import { AutomationCondition } from "types/automations/conditions";
|
import { AutomationCondition } from "types/automations/conditions";
|
||||||
import { convertToFlowNode, makeOnEditAutomationConditions } from "./helpers";
|
import { convertToFlowNode, makeOnEditAutomationConditions } from "./helpers";
|
||||||
@@ -56,30 +55,19 @@ export const DAGAutomationFlow: FC<DAGAutomationFlowProps> = ({
|
|||||||
// modal
|
// modal
|
||||||
let modalBody = <></>;
|
let modalBody = <></>;
|
||||||
if (modalState) {
|
if (modalState) {
|
||||||
if (modalState.single) {
|
modalBody = (
|
||||||
modalBody = (
|
<NodeEditor
|
||||||
<NodeEditor
|
node={modalState.node}
|
||||||
node={modalState.node}
|
isErrored={modalState.isError}
|
||||||
isErrored={modalState.isError}
|
onClose={() => setModalState(null)}
|
||||||
onClose={() => setModalState(null)}
|
onSave={(n) => {
|
||||||
onSave={(n) => {
|
modalState.update(n as any);
|
||||||
modalState.update(n as any);
|
setModalState(null);
|
||||||
setModalState(null);
|
}}
|
||||||
}}
|
allowedTypes={modalState.allowedTypes}
|
||||||
allowedTypes={modalState.allowedTypes}
|
saveBtnCreateText={modalState.saveBtnCreateText}
|
||||||
saveBtnCreateText={modalState.saveBtnCreateText}
|
/>
|
||||||
/>
|
);
|
||||||
);
|
|
||||||
} else {
|
|
||||||
modalBody = (
|
|
||||||
<MultiNodeEditor
|
|
||||||
sequence={modalState.node}
|
|
||||||
onClose={() => setModalState(null)}
|
|
||||||
onSave={(n) => modalState.update(n as any)}
|
|
||||||
allowedTypes={["condition"]}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert automation to flow data
|
// convert automation to flow data
|
||||||
@@ -119,7 +107,6 @@ export const DAGAutomationFlow: FC<DAGAutomationFlowProps> = ({
|
|||||||
namer,
|
namer,
|
||||||
onAdd: () =>
|
onAdd: () =>
|
||||||
setModalState({
|
setModalState({
|
||||||
single: true,
|
|
||||||
saveBtnCreateText: true,
|
saveBtnCreateText: true,
|
||||||
node: {
|
node: {
|
||||||
platform: "device",
|
platform: "device",
|
||||||
@@ -133,7 +120,6 @@ export const DAGAutomationFlow: FC<DAGAutomationFlowProps> = ({
|
|||||||
onTriggerUpdate([...trigger.slice(0, i), ...trigger.slice(i + 1)]),
|
onTriggerUpdate([...trigger.slice(0, i), ...trigger.slice(i + 1)]),
|
||||||
onEdit: (i) =>
|
onEdit: (i) =>
|
||||||
setModalState({
|
setModalState({
|
||||||
single: true,
|
|
||||||
node: trigger[i],
|
node: trigger[i],
|
||||||
update: (t) =>
|
update: (t) =>
|
||||||
onTriggerUpdate([...trigger.slice(0, i), t, ...trigger.slice(i + 1)]),
|
onTriggerUpdate([...trigger.slice(0, i), t, ...trigger.slice(i + 1)]),
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
import { Edge, Node, XYPosition } from "react-flow-renderer";
|
import { Edge, Node, XYPosition } from "react-flow-renderer";
|
||||||
import { AutomationSequenceNode } from "types/automations";
|
import { AutomationNodeTypes, AutomationSequenceNode } from "types/automations";
|
||||||
import { DAGCircleProps } from "./DAGCircle";
|
import { DAGCircleProps } from "./DAGCircle";
|
||||||
import { DAGNodeProps } from "./DAGNode";
|
import { DAGNodeProps } from "./DAGNode";
|
||||||
import { AutomationTrigger } from "types/automations/triggers";
|
import { AutomationTrigger } from "types/automations/triggers";
|
||||||
import { MultiNodeEditorProps } from "components/MultiNodeEditors/types";
|
|
||||||
import { DAGErrorNodeProps } from "./DAGErrorNode";
|
import { DAGErrorNodeProps } from "./DAGErrorNode";
|
||||||
import { Namer } from "utils/formatting";
|
import { Namer } from "utils/formatting";
|
||||||
|
|
||||||
@@ -40,18 +39,10 @@ export type UpdateModalState<
|
|||||||
|
|
||||||
export type ModalState<
|
export type ModalState<
|
||||||
T extends AutomationSequenceNode | AutomationTrigger = any
|
T extends AutomationSequenceNode | AutomationTrigger = any
|
||||||
> =
|
> = {
|
||||||
| {
|
isError?: boolean;
|
||||||
single: true;
|
node: T;
|
||||||
isError?: boolean;
|
update: (n: T) => void;
|
||||||
node: T;
|
saveBtnCreateText?: boolean;
|
||||||
update: (n: T) => void;
|
allowedTypes: AutomationNodeTypes[];
|
||||||
saveBtnCreateText?: boolean;
|
};
|
||||||
allowedTypes: MultiNodeEditorProps["allowedTypes"];
|
|
||||||
}
|
|
||||||
| {
|
|
||||||
single: false;
|
|
||||||
node: AutomationSequenceNode[];
|
|
||||||
update: (n: AutomationSequenceNode[]) => void;
|
|
||||||
allowedTypes: ["condition"];
|
|
||||||
};
|
|
||||||
@@ -1,70 +1,45 @@
|
|||||||
import { AutomationSequenceNode } from "types/automations"
|
import { AutomationSequenceNode } from "types/automations";
|
||||||
import { ChooseAction } from "types/automations/actions";
|
import { ChooseAction } from "types/automations/actions";
|
||||||
import { ModalState } from "./types";
|
import { ModalState } from "./types";
|
||||||
import { makeSequenceUpdater } from "./updater"
|
import { makeSequenceUpdater } from "./updater";
|
||||||
|
|
||||||
const mockSequenceUpdater = (defaultState: AutomationSequenceNode[] = []) => {
|
const mockSequenceUpdater = (defaultState: AutomationSequenceNode[] = []) => {
|
||||||
let state: AutomationSequenceNode[] = [...defaultState]
|
let state: AutomationSequenceNode[] = [...defaultState];
|
||||||
const modalStates: ModalState[] = [];
|
const modalStates: ModalState[] = [];
|
||||||
|
|
||||||
const updater = makeSequenceUpdater(
|
const updater = makeSequenceUpdater(
|
||||||
state,
|
state,
|
||||||
s => {
|
(s) => {
|
||||||
state = s
|
state = s;
|
||||||
},
|
},
|
||||||
ms => modalStates.push(ms)
|
(ms) => modalStates.push(ms)
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
get state() {
|
get state() {
|
||||||
return state
|
return state;
|
||||||
},
|
},
|
||||||
modalStates,
|
modalStates,
|
||||||
updater
|
updater,
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
|
||||||
test("sequence updater updates nodes", () => {
|
test("sequence updater updates nodes", () => {
|
||||||
const mock = mockSequenceUpdater([
|
const mock = mockSequenceUpdater([
|
||||||
|
{
|
||||||
|
choose: [
|
||||||
{
|
{
|
||||||
"choose": [
|
conditions: [],
|
||||||
{
|
sequence: [],
|
||||||
"conditions": [],
|
},
|
||||||
"sequence": [],
|
],
|
||||||
}
|
default: [],
|
||||||
],
|
},
|
||||||
"default": [],
|
]);
|
||||||
}
|
expect(mock.state.length).toBe(1);
|
||||||
]);
|
expect((mock.state[0] as ChooseAction).alias).toBe(undefined);
|
||||||
expect(mock.state.length).toBe(1)
|
mock.updater.updateNode(0, {
|
||||||
expect((mock.state[0] as ChooseAction).alias).toBe(undefined)
|
...mock.state[0],
|
||||||
mock.updater.updateNode(0, {
|
alias: "Hello",
|
||||||
...mock.state[0],
|
} as any);
|
||||||
alias: "Hello"
|
expect((mock.state[0] as any).alias).toBe("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")
|
|
||||||
})
|
|
||||||
@@ -21,7 +21,6 @@ export const makeSequenceUpdater = (
|
|||||||
},
|
},
|
||||||
addNode() {
|
addNode() {
|
||||||
return openModal({
|
return openModal({
|
||||||
single: true,
|
|
||||||
saveBtnCreateText: true,
|
saveBtnCreateText: true,
|
||||||
node: {
|
node: {
|
||||||
device_id: "",
|
device_id: "",
|
||||||
@@ -85,7 +84,6 @@ export const makeSequenceUpdater = (
|
|||||||
makeOnModalOpenForNode(i: number, node: AutomationSequenceNode) {
|
makeOnModalOpenForNode(i: number, node: AutomationSequenceNode) {
|
||||||
return () =>
|
return () =>
|
||||||
openModal({
|
openModal({
|
||||||
single: true,
|
|
||||||
node,
|
node,
|
||||||
allowedTypes: ["action", "condition"],
|
allowedTypes: ["action", "condition"],
|
||||||
update: (n) => this.updateNode(i, n),
|
update: (n) => this.updateNode(i, n),
|
||||||
@@ -114,7 +112,6 @@ export const makeSequenceUpdater = (
|
|||||||
const node = sequence[i];
|
const node = sequence[i];
|
||||||
return () =>
|
return () =>
|
||||||
openModal({
|
openModal({
|
||||||
single: true,
|
|
||||||
isError: true,
|
isError: true,
|
||||||
node,
|
node,
|
||||||
allowedTypes: [getNodeType(node)],
|
allowedTypes: [getNodeType(node)],
|
||||||
|
|||||||
@@ -28,6 +28,8 @@ export type InputAutoCompletePropsBase = {
|
|||||||
onlyShowLabel?: boolean;
|
onlyShowLabel?: boolean;
|
||||||
helperText?: ReactNode;
|
helperText?: ReactNode;
|
||||||
endAdornment?: ReactNode;
|
endAdornment?: ReactNode;
|
||||||
|
onEnter?: () => void;
|
||||||
|
disableChips?: boolean;
|
||||||
} & (
|
} & (
|
||||||
| {
|
| {
|
||||||
value: string | null;
|
value: string | null;
|
||||||
@@ -203,12 +205,20 @@ export function InputAutoComplete<T extends Option>(
|
|||||||
...params.InputProps,
|
...params.InputProps,
|
||||||
endAdornment: props.endAdornment ?? params.InputProps.endAdornment,
|
endAdornment: props.endAdornment ?? params.InputProps.endAdornment,
|
||||||
}}
|
}}
|
||||||
|
onKeyDown={(e) => {
|
||||||
|
if (e.key === "Enter" && props.onEnter) {
|
||||||
|
props.onEnter();
|
||||||
|
}
|
||||||
|
}}
|
||||||
variant="filled"
|
variant="filled"
|
||||||
required={props.required}
|
required={props.required}
|
||||||
label={props.label ?? "Entity ID"}
|
label={props.label ?? "Entity ID"}
|
||||||
helperText={helperText}
|
helperText={helperText}
|
||||||
disabled={props.disabled}
|
disabled={props.disabled}
|
||||||
error={error}
|
error={error}
|
||||||
|
placeholder={
|
||||||
|
props.disableChips && !props.multiple ? props.value ?? "" : ""
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
renderTags={(tagValue, getTagProps) => {
|
renderTags={(tagValue, getTagProps) => {
|
||||||
@@ -241,6 +251,9 @@ export function InputAutoComplete<T extends Option>(
|
|||||||
})}
|
})}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
if (props.disableChips) {
|
||||||
|
return <></>;
|
||||||
|
}
|
||||||
if (props.multiple) {
|
if (props.multiple) {
|
||||||
return (
|
return (
|
||||||
<Badge badgeContent={tagValue.length} color="info">
|
<Badge badgeContent={tagValue.length} color="info">
|
||||||
|
|||||||
@@ -11,12 +11,14 @@ export interface InputAutoTextProps {
|
|||||||
required?: boolean;
|
required?: boolean;
|
||||||
endAdornment?: ReactNode;
|
endAdornment?: ReactNode;
|
||||||
onChange: (v: string) => void;
|
onChange: (v: string) => void;
|
||||||
|
onEnter?: () => void;
|
||||||
}
|
}
|
||||||
export default function InputAutoText({
|
export default function InputAutoText({
|
||||||
value = "",
|
value = "",
|
||||||
label,
|
label,
|
||||||
options = [],
|
options = [],
|
||||||
onChange,
|
onChange,
|
||||||
|
onEnter = () => {},
|
||||||
className,
|
className,
|
||||||
required,
|
required,
|
||||||
endAdornment,
|
endAdornment,
|
||||||
@@ -41,6 +43,11 @@ export default function InputAutoText({
|
|||||||
InputProps={{
|
InputProps={{
|
||||||
endAdornment,
|
endAdornment,
|
||||||
}}
|
}}
|
||||||
|
onKeyDown={(e) => {
|
||||||
|
if (e.key === "Enter") {
|
||||||
|
onEnter();
|
||||||
|
}
|
||||||
|
}}
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
onChange(e.target.value);
|
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