moved control for tag update to tag db
renamed list to manager
This commit is contained in:
1 parent
05d09830bb
commit
02ec81c16a
26 files changed
+669
-383
No files matched your search
@@ -0,0 +1,35 @@
|
||||
.automation-manager--list-box-group--title {
|
||||
margin: 0.5em;
|
||||
}
|
||||
|
||||
.automation-manager--list-box-group--title .icon {
|
||||
font-size: 1.2em;
|
||||
}
|
||||
|
||||
.automation-manager--list-box-group--title:hover,
|
||||
.automation-manager--list-box-group.selected .automation-manager--list-box-group--title {
|
||||
color: var(--mui-primary-main);
|
||||
cursor: pointer;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.automation-manager--list-box-group {
|
||||
border-color: var(--mui-text-secondary);
|
||||
border-width: 0.125px;
|
||||
border-bottom-style: solid;
|
||||
border-radius: 10px;
|
||||
border-bottom-right-radius: 0;
|
||||
}
|
||||
|
||||
.automation-manager--list-box-group--list.close {
|
||||
height: 0;
|
||||
opacity: 0;
|
||||
display: none;
|
||||
}
|
||||
|
||||
.automation-manager--list-box-group .automation-manager--list-box-group {
|
||||
margin-left: 0.5em;
|
||||
margin-bottom: 0.5em;
|
||||
border-left: 1px solid var(--mui-text-secondary);
|
||||
border-top-left-radius: 0;
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
import "styles/root.css";
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { Page } from "components/Page";
|
||||
import { ListBoxGroup } from ".";
|
||||
|
||||
export default {
|
||||
title: "App/AutomationList/ListBoxGroup",
|
||||
component: ListBoxGroup,
|
||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||
args: {
|
||||
title: "Example",
|
||||
type: "items",
|
||||
isSelected: false,
|
||||
data: [
|
||||
{
|
||||
id: "1",
|
||||
title: "Example",
|
||||
entityId: "automation.1",
|
||||
description: "Amazing automation!",
|
||||
tags: {},
|
||||
state: "on",
|
||||
isSelected: false,
|
||||
},
|
||||
{
|
||||
id: "2",
|
||||
title: "Example 2",
|
||||
entityId: "automation.2",
|
||||
description: "Cool!",
|
||||
state: "on",
|
||||
isSelected: false,
|
||||
tags: {
|
||||
room: "main bedroom",
|
||||
routine: "night time",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
} as ComponentMeta<typeof ListBoxGroup>;
|
||||
|
||||
const Template: ComponentStory<typeof ListBoxGroup> = (args) => {
|
||||
return (
|
||||
<Page>
|
||||
<ListBoxGroup {...args} />
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
|
||||
export const Basic = Template.bind({});
|
||||
export const Nested = Template.bind({});
|
||||
Nested.args = {
|
||||
title: "Top Group",
|
||||
type: "group",
|
||||
isSelected: false,
|
||||
data: [
|
||||
{
|
||||
title: "Subgroup 1",
|
||||
type: "group",
|
||||
isSelected: true,
|
||||
data: [
|
||||
{
|
||||
type: "items",
|
||||
isSelected: true,
|
||||
title: "Subsub group",
|
||||
data: [
|
||||
{
|
||||
id: "1",
|
||||
entityId: "automation.1",
|
||||
description: "",
|
||||
title: "Automation #1",
|
||||
state: "on",
|
||||
isSelected: true,
|
||||
tags: {},
|
||||
},
|
||||
{
|
||||
id: "2",
|
||||
entityId: "automation.2",
|
||||
description: "another one",
|
||||
title: "Automation #2",
|
||||
state: "off",
|
||||
isSelected: false,
|
||||
tags: {},
|
||||
},
|
||||
{
|
||||
id: "3",
|
||||
entityId: "automation.3",
|
||||
description: "bad one",
|
||||
title: "Automation #3",
|
||||
state: "offing",
|
||||
isSelected: false,
|
||||
tags: {
|
||||
type: "bad",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title: "Subgroup 2",
|
||||
type: "group",
|
||||
isSelected: false,
|
||||
data: [],
|
||||
},
|
||||
{
|
||||
title: "Other",
|
||||
type: "items",
|
||||
isSelected: false,
|
||||
data: [
|
||||
{
|
||||
id: "4",
|
||||
entityId: "automation.4",
|
||||
description: "Cool thing",
|
||||
title: "Automation #4",
|
||||
isSelected: false,
|
||||
state: "on",
|
||||
tags: {
|
||||
type: "good",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
@@ -0,0 +1,112 @@
|
||||
import "./index.css";
|
||||
|
||||
import { FC, useState } from "react";
|
||||
import { MetadataBox } from "../MetadataBox";
|
||||
import { AutomationListAutoUpdatable, AutomationListItem } from "../types";
|
||||
import { TagDB } from "../TagDB";
|
||||
|
||||
export type ListBoxGroupProps = AutomationListItem & {
|
||||
onAutomationUpdate: (
|
||||
a: AutomationListAutoUpdatable,
|
||||
aid: string,
|
||||
eid: string
|
||||
) => void;
|
||||
onAutomationDelete: (aid: string) => void;
|
||||
onSelect: (aid: string) => void;
|
||||
initialOpenState?: boolean;
|
||||
tagsDB: TagDB;
|
||||
};
|
||||
export const ListBoxGroup: FC<ListBoxGroupProps> = ({
|
||||
title,
|
||||
type,
|
||||
data,
|
||||
isSelected,
|
||||
initialOpenState,
|
||||
tagsDB,
|
||||
...events
|
||||
}) => {
|
||||
const [open, setOpen] = useState(initialOpenState ?? false);
|
||||
|
||||
const openIcon = !open ? "⊕" : "⊖";
|
||||
const totalAutomations = countAutomations({
|
||||
title,
|
||||
type,
|
||||
data,
|
||||
} as any);
|
||||
|
||||
return (
|
||||
<div
|
||||
className={[
|
||||
"automation-manager--list-box-group",
|
||||
isSelected ? "selected" : "",
|
||||
].join(" ")}
|
||||
>
|
||||
<div
|
||||
onClick={() => setOpen(!open)}
|
||||
className="automation-manager--list-box-group--title"
|
||||
>
|
||||
<b>{title}</b> <span className="icon">{openIcon}</span> (
|
||||
{totalAutomations} automation
|
||||
{totalAutomations === 1 ? "" : "s"})
|
||||
</div>
|
||||
<div
|
||||
className={[
|
||||
"automation-manager--list-box-group--list",
|
||||
open ? "open" : "close",
|
||||
].join(" ")}
|
||||
>
|
||||
{type === "items"
|
||||
? data.map((auto) => (
|
||||
<MetadataBox
|
||||
key={auto.id}
|
||||
{...auto}
|
||||
tagsDB={tagsDB}
|
||||
onDelete={() => events.onAutomationDelete(auto.id)}
|
||||
onSelect={() => events.onSelect(auto.id)}
|
||||
onDescriptionUpdate={(description) =>
|
||||
events.onAutomationUpdate(
|
||||
{
|
||||
...auto,
|
||||
description,
|
||||
},
|
||||
auto.id,
|
||||
auto.entityId
|
||||
)
|
||||
}
|
||||
onTitleUpdate={(title) =>
|
||||
events.onAutomationUpdate(
|
||||
{
|
||||
...auto,
|
||||
title,
|
||||
},
|
||||
auto.id,
|
||||
auto.entityId
|
||||
)
|
||||
}
|
||||
onStateUpdate={(state) =>
|
||||
events.onAutomationUpdate(
|
||||
{
|
||||
...auto,
|
||||
state,
|
||||
},
|
||||
auto.id,
|
||||
auto.entityId
|
||||
)
|
||||
}
|
||||
/>
|
||||
))
|
||||
: data.map((group, i) => (
|
||||
<ListBoxGroup {...events} {...group} tagsDB={tagsDB} key={i} />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const countAutomations = (item: AutomationListItem): number => {
|
||||
if (item.type === "items") {
|
||||
return item.data.length;
|
||||
} else {
|
||||
return item.data.reduce((total = 0, n) => total + countAutomations(n), 0);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,123 @@
|
||||
.automation-manager--metadatabox .metadatabox--tags {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
flex-wrap: wrap;
|
||||
gap: 0.25em;
|
||||
margin-top: 0.25em;
|
||||
padding: 0.25em;
|
||||
}
|
||||
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags.modified {
|
||||
border: 1px solid var(--mui-success-light);
|
||||
border-radius: 10px;
|
||||
}
|
||||
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .tag,
|
||||
.automation-manager--metadatabox .metadatabox--tags .buttons .tag-add {
|
||||
display: flex;
|
||||
max-width: fit-content;
|
||||
align-items: center;
|
||||
gap: 0.25em;
|
||||
|
||||
border-color: var(--mui-grey-400);
|
||||
border-style: solid;
|
||||
border-width: 1px;
|
||||
border-radius: 0.5em;
|
||||
padding: 0.1em 0.5em;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .buttons {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
gap: 0.25em;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .buttons .tag-add {
|
||||
cursor: pointer;
|
||||
color: var(--mui-primary-light);
|
||||
border-color: var(--mui-primary-main);
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .buttons .tag-save {
|
||||
cursor: pointer;
|
||||
border-radius: 0.25em;
|
||||
color: var(--mui-success-main);
|
||||
height: 1em;
|
||||
padding: 0.1em;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .buttons .tag-add:hover {
|
||||
background-color: var(--mui-primary-light);
|
||||
color: var(--mui-primary-contrastText);
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .buttons .tag-save:hover {
|
||||
background-color: var(--mui-success-light);
|
||||
color: var(--mui-success-contrastText);
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .tag .delete-tag {
|
||||
font-size: 1em;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .tag .delete-tag:hover {
|
||||
cursor: pointer;
|
||||
color: var(--mui-error-light);
|
||||
outline: 1px solid var(--mui-error-dark);
|
||||
border-radius: 20px;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .tag .inner {
|
||||
font-size: 1em;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
gap: 0.25em;
|
||||
|
||||
cursor: text;
|
||||
user-select: none;
|
||||
|
||||
text-overflow: ellipsis;
|
||||
overflow: hidden;
|
||||
word-wrap: break-word;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .tag .inner span,
|
||||
.automation-manager--metadatabox .metadatabox--tags .tag .inner b {
|
||||
font-size: 0.9em;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
word-wrap: break-word;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .tag .edit-icon {
|
||||
height: 0.7em;
|
||||
width: 0.7em;
|
||||
padding: 0.15em;
|
||||
cursor: text;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--tags .tag:hover .edit-icon {
|
||||
transform: rotate(10deg);
|
||||
}
|
||||
|
||||
.metadatabox--tags--modal {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
gap: 0.25em;
|
||||
}
|
||||
|
||||
.metadatabox--tags--modal .input-autotext {
|
||||
min-width: calc(max(10em, 100%));
|
||||
}
|
||||
|
||||
.metadatabox--tags--modal .buttons {
|
||||
display: flex;
|
||||
gap: 0.5em;
|
||||
justify-content: space-evenly;
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
import "./Tags.css";
|
||||
|
||||
import { FC, ReactNode, useState } from "react";
|
||||
|
||||
import DeleteIcon from "@mui/icons-material/ClearTwoTone";
|
||||
import EditIcon from "@mui/icons-material/EditOutlined";
|
||||
import SaveIcon from "@mui/icons-material/SaveOutlined";
|
||||
|
||||
import { cleanUpUndefined } from "utils/helpers";
|
||||
import { Modal } from "components/Modal";
|
||||
import { Button } from "components/Inputs/Button";
|
||||
import InputAutoText from "components/Inputs/InputAutoText";
|
||||
|
||||
import { TagDB } from "../TagDB";
|
||||
|
||||
export type TagsProps = {
|
||||
automationId: string;
|
||||
tagsDB: TagDB;
|
||||
};
|
||||
|
||||
export type TagModalState =
|
||||
| {
|
||||
open: false;
|
||||
}
|
||||
| {
|
||||
open: true;
|
||||
tag: [string, string];
|
||||
isNew: boolean;
|
||||
};
|
||||
|
||||
export const Tags: FC<TagsProps> = ({ automationId, tagsDB }) => {
|
||||
const [modalState, setModal] = useState<TagModalState>({ open: false });
|
||||
const tags = tagsDB.getTags(automationId);
|
||||
const onUpdate = (t: Record<string, string>) =>
|
||||
tagsDB.update(automationId, t);
|
||||
|
||||
let innerModal: ReactNode = <></>;
|
||||
if (modalState.open) {
|
||||
const validTag =
|
||||
modalState.tag[0].trim().length > 0 &&
|
||||
modalState.tag[1].trim().length > 0;
|
||||
innerModal = (
|
||||
<div className="metadatabox--tags--modal">
|
||||
<InputAutoText
|
||||
className="name"
|
||||
options={tagsDB.getTagNames(Object.keys(tags))}
|
||||
label="Tag Name"
|
||||
value={modalState.tag[0]}
|
||||
onChange={(v) =>
|
||||
setModal({
|
||||
...modalState,
|
||||
tag: [v ?? "", modalState.tag[1]],
|
||||
})
|
||||
}
|
||||
/>
|
||||
<InputAutoText
|
||||
className="value"
|
||||
options={tagsDB.getTagValues(modalState.tag[0])}
|
||||
label="Tag Value"
|
||||
value={modalState.tag[1]}
|
||||
onChange={(v) =>
|
||||
setModal({
|
||||
...modalState,
|
||||
tag: [modalState.tag[0], v ?? ""],
|
||||
})
|
||||
}
|
||||
/>
|
||||
<div className="buttons">
|
||||
<Button onClick={() => setModal({ open: false })} color="info">
|
||||
Close
|
||||
</Button>
|
||||
<Button
|
||||
disabled={!validTag}
|
||||
onClick={() => {
|
||||
onUpdate({
|
||||
...tags,
|
||||
[modalState.tag[0]]: modalState.tag[1],
|
||||
});
|
||||
setModal({ open: false });
|
||||
}}
|
||||
color="success"
|
||||
>
|
||||
{modalState.isNew ? "Create" : "Save"}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className={[
|
||||
"metadatabox--tags",
|
||||
tagsDB.isModified(automationId) ? "modified" : "",
|
||||
].join(" ")}
|
||||
>
|
||||
<Modal open={modalState.open}>{innerModal}</Modal>
|
||||
{Object.entries(tags).map(([tagName, tagValue]) => (
|
||||
<div key={tagName} className="tag">
|
||||
<DeleteIcon
|
||||
onClick={() =>
|
||||
onUpdate(
|
||||
cleanUpUndefined({
|
||||
...tags,
|
||||
[tagName]: undefined,
|
||||
})
|
||||
)
|
||||
}
|
||||
className="delete-tag"
|
||||
/>
|
||||
<div
|
||||
className="inner"
|
||||
onClick={() =>
|
||||
setModal({
|
||||
open: true,
|
||||
isNew: false,
|
||||
tag: [tagName, tagValue],
|
||||
})
|
||||
}
|
||||
>
|
||||
<b>{tagName}: </b>
|
||||
<span>{tagValue}</span>
|
||||
</div>
|
||||
<EditIcon
|
||||
className="edit-icon"
|
||||
onClick={() =>
|
||||
setModal({
|
||||
open: true,
|
||||
isNew: false,
|
||||
tag: [tagName, tagValue],
|
||||
})
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
<div className="buttons">
|
||||
<span
|
||||
className="tag-add"
|
||||
onClick={() =>
|
||||
setModal({
|
||||
open: true,
|
||||
isNew: true,
|
||||
tag: ["", ""],
|
||||
})
|
||||
}
|
||||
>
|
||||
+
|
||||
</span>
|
||||
{tagsDB.isModified(automationId) && (
|
||||
<SaveIcon
|
||||
className="tag-save"
|
||||
onClick={() => tagsDB.save(automationId)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,74 @@
|
||||
.automation-manager--metadatabox {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
justify-content: space-around;
|
||||
gap: 0.25em;
|
||||
|
||||
padding: 0.25em;
|
||||
background-color: var(--mui-background-paper);
|
||||
|
||||
border-style: solid;
|
||||
border-width: 1px;
|
||||
border-color: transparent;
|
||||
border-radius: 10px;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox:hover, .automation-manager--metadatabox.selected {
|
||||
border-color: var(--mui-action-focus);
|
||||
background-color: var(--mui-action-focus);
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--title .input-text-view {
|
||||
max-width: fit-content;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--title {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--title .title {
|
||||
font-size: 1.25em;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--title .issue {
|
||||
padding: 0 0.5em;
|
||||
user-select: none;
|
||||
max-width: fit-content;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--title .description {
|
||||
font-size: 0.9em;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .delete:hover {
|
||||
cursor: pointer;
|
||||
color: var(--mui-error-light);
|
||||
background-color: var(--mui-error-dark);
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .select:hover,
|
||||
.automation-manager--metadatabox.selected.automation-manager--metadatabox .select {
|
||||
cursor: pointer;
|
||||
color: var(--mui-primary-light);
|
||||
background-color: var(--mui-primary-dark);
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox.selected.automation-manager--metadatabox .select:hover {
|
||||
color: var(--mui-secondary-light);
|
||||
background-color: var(--mui-secondary-dark);
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--switch {
|
||||
display: flex;
|
||||
cursor: pointer;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
text-align: center;
|
||||
font-size: 0.75em;
|
||||
user-select: none;
|
||||
max-width: 5em;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
.automation-manager--metadatabox.mobile .buttons {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5em;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
import "styles/root.css";
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { Page } from "components/Page";
|
||||
import { MetadataBox } from ".";
|
||||
import { useTagDB } from "../TagDB";
|
||||
import { useState } from "react";
|
||||
|
||||
export default {
|
||||
title: "App/AutomationList/MetadataBox",
|
||||
component: MetadataBox,
|
||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||
args: {
|
||||
id: "dummy",
|
||||
title: "Example",
|
||||
description: "Cool!",
|
||||
state: "on",
|
||||
isSelected: false,
|
||||
tags: {
|
||||
room: "main bedroom",
|
||||
routine: "night time",
|
||||
},
|
||||
},
|
||||
} as ComponentMeta<typeof MetadataBox>;
|
||||
|
||||
const Template: ComponentStory<typeof MetadataBox> = (args) => {
|
||||
const [autoTags, setTags] = useState([{ id: args.id, tags: args.tags }]);
|
||||
const tagsDB = useTagDB(autoTags, (aid, t) =>
|
||||
setTags(
|
||||
autoTags
|
||||
.filter(({ id }) => id !== aid)
|
||||
.concat({
|
||||
id: aid,
|
||||
tags: t,
|
||||
})
|
||||
)
|
||||
);
|
||||
return (
|
||||
<Page>
|
||||
<MetadataBox {...args} tagsDB={tagsDB} />
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
|
||||
export const Basic = Template.bind({});
|
||||
export const IsSelected = Template.bind({});
|
||||
IsSelected.args = {
|
||||
...IsSelected.args,
|
||||
isSelected: true,
|
||||
};
|
||||
export const InvalidState = Template.bind({});
|
||||
InvalidState.args = {
|
||||
...InvalidState.args,
|
||||
state: "unavailable",
|
||||
};
|
||||
export const WithIssue = Template.bind({});
|
||||
WithIssue.args = {
|
||||
...WithIssue.args,
|
||||
issue: "look at me",
|
||||
};
|
||||
@@ -0,0 +1,91 @@
|
||||
import "./index.css";
|
||||
import "./index.mobile.css";
|
||||
import { FC } from "react";
|
||||
|
||||
import Switch from "@mui/material/Switch";
|
||||
import Alert from "@mui/material/Alert";
|
||||
|
||||
import DeleteForeverIcon from "@mui/icons-material/DeleteForever";
|
||||
import SelectIcon from "@mui/icons-material/HighlightAltSharp";
|
||||
|
||||
import { InputTextView } from "components/Inputs/InputTextView";
|
||||
import { ButtonIcon } from "components/Icons/ButtonIcons";
|
||||
import { AutomationListAuto } from "../types";
|
||||
import { Tags } from "./Tags";
|
||||
import { TagDB } from "../TagDB";
|
||||
import useWindowSize from "utils/useWindowSize";
|
||||
|
||||
export type MetadataBoxProps = AutomationListAuto & {
|
||||
onDelete: () => void;
|
||||
onSelect: () => void;
|
||||
onStateUpdate: (s: "on" | "off") => void;
|
||||
onTitleUpdate: (t: string) => void;
|
||||
onDescriptionUpdate: (t: string) => void;
|
||||
tagsDB: TagDB;
|
||||
isSelected: boolean;
|
||||
};
|
||||
export const MetadataBox: FC<MetadataBoxProps> = (props) => {
|
||||
const isValidState = ["on", "off"].includes(props.state);
|
||||
const { isMobile } = useWindowSize();
|
||||
return (
|
||||
<div
|
||||
className={[
|
||||
"automation-manager--metadatabox",
|
||||
props.isSelected ? "selected" : "",
|
||||
isMobile ? "mobile" : "",
|
||||
].join(" ")}
|
||||
>
|
||||
<div
|
||||
className="metadatabox--switch"
|
||||
title={!isValidState ? "Invalid state" : `Automation is ${props.state}`}
|
||||
>
|
||||
<Switch
|
||||
checked={props.state !== "off"}
|
||||
onChange={() =>
|
||||
props.onStateUpdate(props.state === "on" ? "off" : "on")
|
||||
}
|
||||
disabled={!isValidState}
|
||||
color={!isValidState ? "error" : "info"}
|
||||
/>
|
||||
{!isValidState && `Invalid state: '${props.state}'`}
|
||||
</div>
|
||||
<div
|
||||
className="metadatabox--title"
|
||||
title={`ID=${props.id}, EntityID=${props.entityId}`}
|
||||
>
|
||||
<InputTextView
|
||||
className="title"
|
||||
placeholder="Title"
|
||||
value={props.title}
|
||||
onChange={props.onTitleUpdate}
|
||||
/>
|
||||
<InputTextView
|
||||
className="description"
|
||||
placeholder="Description"
|
||||
value={props.description}
|
||||
onChange={props.onDescriptionUpdate}
|
||||
/>
|
||||
<Tags automationId={props.id} tagsDB={props.tagsDB} />
|
||||
{props.issue ? (
|
||||
<Alert severity="warning" className="issue">
|
||||
{props.issue}
|
||||
</Alert>
|
||||
) : (
|
||||
<></>
|
||||
)}
|
||||
</div>
|
||||
<div className="buttons">
|
||||
<ButtonIcon
|
||||
className="select"
|
||||
icon={<SelectIcon />}
|
||||
onClick={props.onSelect}
|
||||
/>
|
||||
<ButtonIcon
|
||||
className="delete"
|
||||
icon={<DeleteForeverIcon />}
|
||||
onClick={props.onDelete}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,28 @@
|
||||
.automation-manager--sidebar {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
--nav-height: 10em;
|
||||
--bottom-height: 3em;
|
||||
position: relative;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.automation-manager--sidebar-box--nav {
|
||||
max-height: var(--nav-height);
|
||||
}
|
||||
|
||||
.automation-manager--sidebar .automation-manager--sidebar-box--list {
|
||||
flex: 1;
|
||||
max-height: calc(100vh - var(--bottom-height) - var(--nav-height));
|
||||
min-height: calc(100vh - var(--bottom-height) - var(--nav-height));
|
||||
overflow: auto;
|
||||
box-shadow: 1px 1px var(--mui-primary-light);
|
||||
}
|
||||
|
||||
.automation-manager--sidebar-box--bottom-buttons {
|
||||
height: 100%;
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
import "styles/root.css";
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { Page } from "components/Page";
|
||||
import { AutomationListSidebar } from ".";
|
||||
import { useTagDB } from "../TagDB";
|
||||
import { useState } from "react";
|
||||
import { AutomationListAuto } from "../types";
|
||||
|
||||
export default {
|
||||
title: "App/AutomationList/Sidebar",
|
||||
component: AutomationListSidebar,
|
||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||
args: {},
|
||||
} as ComponentMeta<typeof AutomationListSidebar>;
|
||||
|
||||
const Template: ComponentStory<typeof AutomationListSidebar> = (args) => {
|
||||
const [automations, setAutomations] = useState(args.automations);
|
||||
const [selectedAutomationId, setSelAutoId] = useState(
|
||||
args.selectedAutomationId
|
||||
);
|
||||
const onTagUpdate = (tags: Record<string, string>, aid: string) => {
|
||||
const index = automations.findIndex(({ id }) => id === aid);
|
||||
if (index >= 0) {
|
||||
setAutomations([
|
||||
...automations.slice(0, index),
|
||||
{
|
||||
...automations[index],
|
||||
tags,
|
||||
},
|
||||
...automations.slice(index + 1),
|
||||
]);
|
||||
}
|
||||
};
|
||||
return (
|
||||
<Page>
|
||||
<AutomationListSidebar
|
||||
selectedAutomationId={selectedAutomationId}
|
||||
tagsDB={useTagDB(automations, (a, t) => onTagUpdate(t, a))}
|
||||
automations={automations}
|
||||
onSelectedAutomationId={(aid) => {
|
||||
args.onSelectedAutomationId(aid);
|
||||
setSelAutoId(aid);
|
||||
}}
|
||||
onAutomationDelete={(aid) => {
|
||||
args.onAutomationDelete(aid);
|
||||
setAutomations(automations.filter(({ id }) => id !== aid));
|
||||
}}
|
||||
onAutomationUpdate={(a, aid, eid) => {
|
||||
args.onAutomationUpdate(a, aid, eid);
|
||||
const index = automations.findIndex(({ id }) => id === aid);
|
||||
if (index >= 0) {
|
||||
setAutomations([
|
||||
...automations.slice(0, index),
|
||||
{
|
||||
...automations[index],
|
||||
...a,
|
||||
},
|
||||
...automations.slice(index + 1),
|
||||
]);
|
||||
}
|
||||
}}
|
||||
onAutomationAdd={() => {
|
||||
args.onAutomationAdd();
|
||||
const id = String(Date.now());
|
||||
setAutomations([
|
||||
...automations,
|
||||
{
|
||||
id,
|
||||
description: "",
|
||||
title: "new",
|
||||
tags: {},
|
||||
entityId: "automation." + id,
|
||||
state: "on",
|
||||
},
|
||||
]);
|
||||
setSelAutoId(id);
|
||||
}}
|
||||
/>
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
|
||||
const make = (automations: AutomationListAuto[]) => {
|
||||
const Basic = Template.bind({});
|
||||
Basic.args = {
|
||||
...Basic.args,
|
||||
automations,
|
||||
};
|
||||
return Basic;
|
||||
};
|
||||
const makeAuto = (data: Partial<AutomationListAuto>): AutomationListAuto => {
|
||||
const id = `${Date.now()}_${Math.random().toString(26).slice(3, 6)}`;
|
||||
return {
|
||||
id,
|
||||
entityId: `automation.${id}`,
|
||||
title: "",
|
||||
description: "",
|
||||
state: "on",
|
||||
tags: {},
|
||||
...data,
|
||||
};
|
||||
};
|
||||
|
||||
export const Empty = make([]);
|
||||
export const Basic = make([
|
||||
makeAuto({ title: "example 1" }),
|
||||
makeAuto({ title: "example 2", state: "off" }),
|
||||
makeAuto({ title: "example 3", state: "invalid" }),
|
||||
makeAuto({ title: "example 4", issue: "I have an issue!" }),
|
||||
]);
|
||||
export const Tagged = make([
|
||||
makeAuto({ title: "example 1", tags: { room: "bathroom", floor: "1" } }),
|
||||
makeAuto({ title: "example 2", tags: { type: "routine" } }),
|
||||
makeAuto({
|
||||
title: "example 3",
|
||||
tags: { room: "living room", type: "climate" },
|
||||
}),
|
||||
makeAuto({
|
||||
title: "example 4",
|
||||
tags: { room: "bedroom", floor: "2", type: "button" },
|
||||
}),
|
||||
makeAuto({
|
||||
title: "example 5",
|
||||
tags: { room: "bedroom", floor: "2", type: "routine", mode: "party" },
|
||||
}),
|
||||
]);
|
||||
@@ -0,0 +1,89 @@
|
||||
import "./index.css";
|
||||
|
||||
import { FC, useState } from "react";
|
||||
|
||||
import InputText from "components/Inputs/InputText";
|
||||
import InputMultiSelect from "components/Inputs/InputMultiSelect";
|
||||
|
||||
import { ListBoxGroup } from "../ListBoxGroup";
|
||||
import { filterAutomations } from "../helpers";
|
||||
import { TagDB } from "../TagDB";
|
||||
import { convertGroupingToItems, makeGrouping } from "../automationGrouper";
|
||||
import { AutomationListAuto, AutomationListAutoUpdatable } from "../types";
|
||||
import { Button } from "components/Inputs/Button";
|
||||
|
||||
export type AutomationListSidebarProps = {
|
||||
tagsDB: TagDB;
|
||||
automations: AutomationListAuto[];
|
||||
selectedAutomationId: string | null;
|
||||
onAutomationUpdate: (
|
||||
a: AutomationListAutoUpdatable,
|
||||
aid: string,
|
||||
eid: string
|
||||
) => void;
|
||||
onAutomationDelete: (aid: string) => void;
|
||||
onAutomationAdd: () => void;
|
||||
onSelectedAutomationId: (aid: string) => void;
|
||||
};
|
||||
export const AutomationListSidebar: FC<AutomationListSidebarProps> = ({
|
||||
automations,
|
||||
tagsDB,
|
||||
onAutomationAdd,
|
||||
selectedAutomationId,
|
||||
onSelectedAutomationId,
|
||||
...events
|
||||
}) => {
|
||||
const [searchText, setSearchText] = useState("");
|
||||
const [selectedTagIdx, setSelectedTagIdx] = useState<number[]>([]);
|
||||
|
||||
const tags = tagsDB.getTagNames([]);
|
||||
|
||||
if (searchText.trim().length > 0) {
|
||||
automations = filterAutomations(
|
||||
automations,
|
||||
searchText.toLocaleLowerCase()
|
||||
);
|
||||
}
|
||||
|
||||
const grouping = makeGrouping(
|
||||
automations,
|
||||
selectedTagIdx.map((i) => tags[i]),
|
||||
selectedAutomationId
|
||||
);
|
||||
|
||||
const items = grouping.top.map((i) =>
|
||||
convertGroupingToItems(i, selectedAutomationId, automations, grouping)
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="automation-manager--sidebar">
|
||||
<div className="automation-manager--sidebar-box--nav">
|
||||
<InputText label="Search" value={searchText} onChange={setSearchText} />
|
||||
<InputMultiSelect
|
||||
label="Tags"
|
||||
selected={selectedTagIdx}
|
||||
onChange={setSelectedTagIdx}
|
||||
options={tags}
|
||||
max={3}
|
||||
/>
|
||||
</div>
|
||||
<div className="automation-manager--sidebar-box--list">
|
||||
{items.map((item, i) => (
|
||||
<ListBoxGroup
|
||||
{...item}
|
||||
key={i}
|
||||
onSelect={onSelectedAutomationId}
|
||||
{...events}
|
||||
tagsDB={tagsDB}
|
||||
initialOpenState={true}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
<div className="automation-manager--sidebar-box--bottom-buttons">
|
||||
<Button onClick={onAutomationAdd} color="primary">
|
||||
Add
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,93 @@
|
||||
import { useEffect, useState } from "react";
|
||||
export type TagDB = ReturnType<typeof useTagDB>;
|
||||
export const useTagDB = (
|
||||
automations: Array<{
|
||||
id: string;
|
||||
tags: Record<string, string>;
|
||||
}>,
|
||||
onUpdate: (aid: string, tags: Record<string, string>) => void
|
||||
) => {
|
||||
const [{ automationTags, changed }, setState] = useState({
|
||||
automationTags: convertAutoListToMap(automations),
|
||||
changed: new Set<string>(),
|
||||
});
|
||||
const allTags = computeAllTags(automationTags);
|
||||
const allTagNames = Object.keys(allTags);
|
||||
const isModified = changed.size > 0;
|
||||
|
||||
useEffect(() => {
|
||||
console.log("updating...");
|
||||
setState({
|
||||
automationTags: convertAutoListToMap(automations),
|
||||
changed: new Set(),
|
||||
});
|
||||
}, [automations, setState]);
|
||||
|
||||
return {
|
||||
getTags(automationId: string) {
|
||||
return automationTags[automationId] ?? {};
|
||||
},
|
||||
getTagNames(myTags: string[]): string[] {
|
||||
return allTagNames.filter((n) => !myTags.includes(n)).sort();
|
||||
},
|
||||
getTagValues(tagName: string): string[] {
|
||||
return allTags[tagName]
|
||||
? Array.from(allTags[tagName].values()).sort()
|
||||
: [];
|
||||
},
|
||||
isModified(aid: string) {
|
||||
return changed.has(aid);
|
||||
},
|
||||
update(aid: string, tags: Record<string, string>) {
|
||||
if (JSON.stringify(tags) !== JSON.stringify(automationTags[aid])) {
|
||||
setState({
|
||||
changed: changed.add(aid),
|
||||
automationTags: {
|
||||
...automationTags,
|
||||
[aid]: tags,
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
save(aid: string | null) {
|
||||
if (isModified) {
|
||||
if (aid) {
|
||||
onUpdate(aid, automationTags[aid]);
|
||||
} else {
|
||||
for (const aid of changed.values()) {
|
||||
onUpdate(aid, automationTags[aid]);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const convertAutoListToMap = (
|
||||
automations: Array<{
|
||||
id: string;
|
||||
tags: Record<string, string>;
|
||||
}>
|
||||
) => {
|
||||
const automationTags: Record<string, Record<string, string>> = {};
|
||||
automations.forEach(({ id, tags }) => {
|
||||
automationTags[id] = tags;
|
||||
});
|
||||
return automationTags;
|
||||
};
|
||||
|
||||
const computeAllTags = (
|
||||
automations: Record<string, Record<string, string>>
|
||||
) => {
|
||||
const allTags: Record<string, Set<string>> = {};
|
||||
Object.values(automations).forEach((tags) => {
|
||||
Object.keys(tags).forEach((tagName) => {
|
||||
const tagValue = tags[tagName];
|
||||
if (!allTags[tagName]) {
|
||||
allTags[tagName] = new Set();
|
||||
}
|
||||
allTags[tagName].add(tagValue);
|
||||
});
|
||||
});
|
||||
return allTags;
|
||||
};
|
||||
@@ -0,0 +1,137 @@
|
||||
import { convertGroupingToItems, makeGrouping } from "./automationGrouper";
|
||||
import { bigMockAutoList, createMockAuto } from "utils/mocks";
|
||||
import { AutomationListAuto } from "./types";
|
||||
|
||||
const automations: AutomationListAuto[] = [
|
||||
createMockAuto({
|
||||
Room: "Living Room",
|
||||
Type: "Lights",
|
||||
}),
|
||||
createMockAuto({
|
||||
Room: "Living Room",
|
||||
Type: "Climate",
|
||||
}),
|
||||
createMockAuto({
|
||||
Room: "Kitchen",
|
||||
Type: "Climate",
|
||||
}),
|
||||
createMockAuto({
|
||||
Type: "Climate",
|
||||
}),
|
||||
createMockAuto({
|
||||
Room: "Living Room",
|
||||
}),
|
||||
createMockAuto({}),
|
||||
].map<AutomationListAuto>((a) => ({
|
||||
title: a.metadata.alias ?? "",
|
||||
entityId: "automation." + a.metadata.id,
|
||||
description: a.metadata.description ?? "",
|
||||
state: "on",
|
||||
tags: a.tags,
|
||||
id: a.metadata.id,
|
||||
isSelected: false,
|
||||
}));
|
||||
|
||||
const bigList = bigMockAutoList.map<AutomationListAuto>((a) => ({
|
||||
entityId: "automation." + a.metadata.id,
|
||||
title: a.metadata.alias ?? "",
|
||||
description: a.metadata.description ?? "",
|
||||
state: "on",
|
||||
tags: a.tags,
|
||||
id: a.metadata.id,
|
||||
isSelected: false,
|
||||
}));
|
||||
|
||||
test("no grouping", () => {
|
||||
const grouped = makeGrouping(automations, [], automations[0].id);
|
||||
expect(grouped.top).toStrictEqual([0]);
|
||||
expect(grouped.getAutomations(0)).toStrictEqual([0, 1, 2, 3, 4, 5]);
|
||||
});
|
||||
test("1 grouping", () => {
|
||||
const grouped = makeGrouping(automations, ["Room"], automations[0].id);
|
||||
expect(grouped.top).toHaveLength(3);
|
||||
expect(grouped.getAutomations(0)).toStrictEqual([0, 1, 4]);
|
||||
expect(grouped.getAutomations(1)).toStrictEqual([2]);
|
||||
expect(grouped.getAutomations(2)).toStrictEqual([3, 5]);
|
||||
});
|
||||
test("2 groupings", () => {
|
||||
const grouped = makeGrouping(
|
||||
automations,
|
||||
["Room", "Type"],
|
||||
automations[0].id
|
||||
);
|
||||
expect(grouped.top).toHaveLength(3);
|
||||
expect(grouped.getAutomations(grouped.top[0])).toHaveLength(0);
|
||||
expect(grouped.getSubGroups(grouped.top[0])).toHaveLength(3);
|
||||
expect(grouped.getAutomations(grouped.top[1])).toHaveLength(0);
|
||||
expect(grouped.getSubGroups(grouped.top[1])).toHaveLength(1);
|
||||
expect(grouped.getAutomations(grouped.top[2])).toHaveLength(2);
|
||||
expect(grouped.getSubGroups(grouped.top[2])).toHaveLength(0);
|
||||
});
|
||||
test("bigAutoList groupings has not duplicate ids", () => {
|
||||
const grouped = makeGrouping(bigList, ["Room", "Remote"], bigList[0].id);
|
||||
const flattenAndCount = (gid: number): Record<number, number[]> => {
|
||||
const subgroups = grouped.getSubGroups(gid);
|
||||
if (subgroups.length > 0) {
|
||||
return flattenAndConsolidate(subgroups);
|
||||
} else {
|
||||
return grouped
|
||||
.getAutomations(gid)
|
||||
.reduce((sum: Record<number, number[]>, b) => {
|
||||
sum[b] = (sum[b] ?? []).concat(gid);
|
||||
return sum;
|
||||
}, {});
|
||||
}
|
||||
};
|
||||
const flattenAndConsolidate = (gids: number[]): Record<number, number[]> =>
|
||||
gids.map(flattenAndCount).reduce((a, b) => {
|
||||
for (const k of Object.keys(b).map(Number)) {
|
||||
a[k] = (a[k] ?? []).concat(b[k]);
|
||||
}
|
||||
return a;
|
||||
}, {});
|
||||
const countedAutos = flattenAndConsolidate(grouped.top);
|
||||
for (const key of Object.keys(countedAutos).map(Number)) {
|
||||
expect(countedAutos[key]).toHaveLength(1);
|
||||
}
|
||||
});
|
||||
|
||||
test("convert grouper to items when no tags are selected", () => {
|
||||
const grouping = makeGrouping(automations, [], null);
|
||||
const items = grouping.top.map((i) =>
|
||||
convertGroupingToItems(i, "", automations, grouping)
|
||||
);
|
||||
expect(items.filter((i) => i.type === "group")).toHaveLength(0);
|
||||
expect(items).toHaveLength(1);
|
||||
expect(items[0].data).toHaveLength(automations.length);
|
||||
});
|
||||
|
||||
test("convert grouper to items when 1 tag is selected", () => {
|
||||
const grouping = makeGrouping(automations, ["Room"], null);
|
||||
const items = grouping.top.map((i) =>
|
||||
convertGroupingToItems(i, "", automations, grouping)
|
||||
);
|
||||
expect(grouping.top).toHaveLength(3);
|
||||
expect(items.filter((i) => i.type === "group")).toHaveLength(0);
|
||||
expect(items).toHaveLength(3);
|
||||
expect(items[0].data).toHaveLength(3);
|
||||
expect(items[1].data).toHaveLength(1);
|
||||
expect(items[2].data).toHaveLength(2);
|
||||
expect(items[0].title).toEqual("Living Room");
|
||||
expect(items[1].title).toEqual("Kitchen");
|
||||
expect(items[2].title).toEqual("");
|
||||
});
|
||||
|
||||
test("convert grouper to items when 2 tag is selected", () => {
|
||||
const grouping = makeGrouping(automations, ["Room", "Type"], null);
|
||||
const items = grouping.top.map((i) =>
|
||||
convertGroupingToItems(i, "", automations, grouping)
|
||||
);
|
||||
expect(grouping.top).toHaveLength(3);
|
||||
expect(items.filter((i) => i.type === "group")).toHaveLength(2);
|
||||
expect(items.filter((i) => i.type === "items")).toHaveLength(1);
|
||||
expect(items).toHaveLength(3);
|
||||
expect(items[0].data).toHaveLength(3);
|
||||
expect(items[1].data).toHaveLength(1);
|
||||
expect(items[2].data).toHaveLength(2);
|
||||
});
|
||||
@@ -0,0 +1,181 @@
|
||||
import { AutomationListAuto, AutomationListItem } from "./types";
|
||||
|
||||
const makeGrouper = () => {
|
||||
const groupIdNames: string[] = [];
|
||||
const groupSubGroups: Array<Set<number>> = []; // groupIdx => Array<groupIdx>
|
||||
const groupAutomations: number[][] = []; // groupIdx => Array<automationIdx>
|
||||
const groupData: Array<{
|
||||
name: string;
|
||||
selected: boolean;
|
||||
isOther: boolean;
|
||||
isTop: boolean;
|
||||
}> = []; // source of truth for groupIdx
|
||||
const topGroups: number[] = [];
|
||||
|
||||
return {
|
||||
groupIdNames,
|
||||
groupSubGroups,
|
||||
groupAutomations,
|
||||
groupData,
|
||||
topGroups,
|
||||
addNew(
|
||||
groupId: string,
|
||||
groupName: string,
|
||||
theChosenOne: boolean,
|
||||
isTopLevel: boolean,
|
||||
isOther: boolean = false
|
||||
): number {
|
||||
groupIdNames.push(groupId);
|
||||
groupSubGroups.push(new Set());
|
||||
groupAutomations.push([]);
|
||||
groupData.push({
|
||||
name: groupName,
|
||||
selected: theChosenOne,
|
||||
isTop: isTopLevel,
|
||||
isOther,
|
||||
});
|
||||
const groupIdx = groupIdNames.length - 1;
|
||||
if (isTopLevel) {
|
||||
topGroups.push(groupIdx);
|
||||
}
|
||||
return groupIdx;
|
||||
},
|
||||
updateGroup(
|
||||
groupIdx: number,
|
||||
update: {
|
||||
isChosen: boolean;
|
||||
lastGroupIdx: number | null;
|
||||
autoIdx: number | null;
|
||||
}
|
||||
) {
|
||||
if (update.isChosen) {
|
||||
groupData[groupIdx].selected = true;
|
||||
}
|
||||
if (update.lastGroupIdx !== null) {
|
||||
groupSubGroups[update.lastGroupIdx].add(groupIdx);
|
||||
}
|
||||
if (update.autoIdx !== null) {
|
||||
groupAutomations[groupIdx].push(update.autoIdx);
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export type AutomationGrouping = ReturnType<typeof makeGrouping>;
|
||||
export const makeGrouping = (
|
||||
autos: AutomationListAuto[],
|
||||
selectedTags: string[],
|
||||
selectedAutomationId: string | null
|
||||
) => {
|
||||
const grouper = makeGrouper();
|
||||
|
||||
if (selectedTags.length === 0) {
|
||||
grouper.addNew("$", "", selectedAutomationId !== null, true, true);
|
||||
grouper.groupAutomations = [autos.map((_, i) => i)];
|
||||
} else {
|
||||
autos.forEach((auto, autoIdx) => {
|
||||
const theChosenOne = selectedAutomationId === auto.id;
|
||||
let lastGroupIdx: number | null = null;
|
||||
let lastGroupId: string = "$";
|
||||
for (let ti = 0; ti < selectedTags.length; ti++) {
|
||||
const isTopLevel = ti === 0;
|
||||
const selTag = selectedTags[ti];
|
||||
if (selTag in auto.tags) {
|
||||
const groupName = auto.tags[selTag];
|
||||
const groupId = `${lastGroupId}>${selTag}:${groupName}`;
|
||||
let groupIdx = grouper.groupIdNames.indexOf(groupId);
|
||||
if (groupIdx < 0) {
|
||||
groupIdx = grouper.addNew(
|
||||
groupId,
|
||||
groupName,
|
||||
theChosenOne,
|
||||
isTopLevel
|
||||
);
|
||||
}
|
||||
grouper.updateGroup(groupIdx, {
|
||||
isChosen: theChosenOne,
|
||||
lastGroupIdx,
|
||||
autoIdx: ti === selectedTags.length - 1 ? autoIdx : null,
|
||||
});
|
||||
lastGroupIdx = groupIdx;
|
||||
lastGroupId = groupId;
|
||||
} else {
|
||||
const groupId = `${lastGroupId}>${selTag}:$`;
|
||||
let groupIdx = grouper.groupIdNames.indexOf(groupId);
|
||||
if (groupIdx < 0) {
|
||||
groupIdx = grouper.addNew(
|
||||
groupId,
|
||||
"",
|
||||
theChosenOne,
|
||||
isTopLevel,
|
||||
true
|
||||
);
|
||||
}
|
||||
grouper.updateGroup(groupIdx, {
|
||||
isChosen: theChosenOne,
|
||||
lastGroupIdx,
|
||||
autoIdx,
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
const sortGroupIds = (groupIds: number[]) =>
|
||||
groupIds.sort((a, b) => {
|
||||
const adata = grouper.groupData[a];
|
||||
const bdata = grouper.groupData[b];
|
||||
if (adata.isOther) {
|
||||
return 1;
|
||||
} else {
|
||||
return adata.name > bdata.name ? -1 : 1;
|
||||
}
|
||||
});
|
||||
return {
|
||||
top: sortGroupIds(grouper.topGroups),
|
||||
getSubGroups(groupIdx: number) {
|
||||
return sortGroupIds(
|
||||
Array.from(grouper.groupSubGroups[groupIdx].values())
|
||||
);
|
||||
},
|
||||
getAutomations(groupIdx: number) {
|
||||
return grouper.groupAutomations[groupIdx];
|
||||
},
|
||||
getData(groupIdx: number) {
|
||||
return grouper.groupData[groupIdx];
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export const convertGroupingToItems = (
|
||||
currentGroupIdx: number,
|
||||
selectedAutomationId: string | null,
|
||||
autos: AutomationListAuto[],
|
||||
grouping: AutomationGrouping
|
||||
): AutomationListItem => {
|
||||
const groupData = grouping.getData(currentGroupIdx);
|
||||
const automationIds = grouping.getAutomations(currentGroupIdx);
|
||||
|
||||
if (automationIds.length > 0) {
|
||||
return {
|
||||
type: "items",
|
||||
title: groupData.name,
|
||||
data: automationIds.map((i) => {
|
||||
return {
|
||||
...autos[i],
|
||||
isSelected: autos[i].id === selectedAutomationId,
|
||||
};
|
||||
}),
|
||||
isSelected: groupData.selected,
|
||||
};
|
||||
}
|
||||
const subgroups = grouping.getSubGroups(currentGroupIdx);
|
||||
return {
|
||||
type: "group",
|
||||
title: groupData.name,
|
||||
data: subgroups.map((i) =>
|
||||
convertGroupingToItems(i, selectedAutomationId, autos, grouping)
|
||||
),
|
||||
isSelected: groupData.selected,
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,82 @@
|
||||
import { HassEntities } from "home-assistant-js-websocket";
|
||||
import { AutomationMetadata } from "types/automations";
|
||||
import { TagDB } from "./TagDB";
|
||||
import { AutomationListAuto } from "./types";
|
||||
|
||||
export const consolidateAutomations = (
|
||||
haEntities: HassEntities,
|
||||
configAutomationMetadatas: AutomationMetadata[],
|
||||
tagsDB: TagDB
|
||||
): AutomationListAuto[] => {
|
||||
const configData: Record<string, AutomationMetadata> =
|
||||
configAutomationMetadatas.reduce(
|
||||
(all, n) => ({
|
||||
...all,
|
||||
[n.id]: n,
|
||||
}),
|
||||
{}
|
||||
);
|
||||
|
||||
const out = Object.entries(haEntities)
|
||||
.filter(([entityId, _]) => entityId.toLowerCase().startsWith("automation."))
|
||||
.map<AutomationListAuto>(([entityId, entityData]) => {
|
||||
const autoId = entityData.attributes.id;
|
||||
let title = entityData.attributes.friendly_name ?? "";
|
||||
let description = "";
|
||||
let issue: string | undefined = ["on", "off"].includes(entityData.state)
|
||||
? undefined
|
||||
: `recived invalid state "${entityData.state}, this could be because you have a lingering automation that was not cleared by homeassistant, try to reboot HA or manually clear this automation from HA Database."`;
|
||||
|
||||
if (configData[autoId]) {
|
||||
title = configData[entityData.attributes.id].alias ?? title;
|
||||
description =
|
||||
configData[entityData.attributes.id].description ?? description;
|
||||
delete configData[autoId];
|
||||
} else {
|
||||
issue = "failed to find this automation in '/config' folder";
|
||||
}
|
||||
|
||||
return {
|
||||
id: autoId,
|
||||
entityId,
|
||||
title,
|
||||
description,
|
||||
state: entityData.state,
|
||||
tags: tagsDB.getTags(autoId),
|
||||
issue,
|
||||
};
|
||||
});
|
||||
|
||||
Object.values(configData).forEach((autoMetadata) => {
|
||||
out.push({
|
||||
id: autoMetadata.id,
|
||||
entityId: "",
|
||||
title: autoMetadata.alias ?? "",
|
||||
description: autoMetadata.description ?? "",
|
||||
state: "unregistered",
|
||||
tags: tagsDB.getTags(autoMetadata.id),
|
||||
issue:
|
||||
"homeassistant did not load this automation, try to manually reload automations or restart homeassistant.",
|
||||
});
|
||||
});
|
||||
|
||||
return out;
|
||||
};
|
||||
|
||||
export const filterAutomations = (
|
||||
autos: AutomationListAuto[],
|
||||
searchText: string
|
||||
): AutomationListAuto[] =>
|
||||
autos.filter((a) => {
|
||||
if (searchText.length > 0) {
|
||||
if (
|
||||
!(
|
||||
(a.description ?? "").toLocaleLowerCase().includes(searchText) ||
|
||||
(a.title ?? "").toLocaleLowerCase().includes(searchText)
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
@@ -0,0 +1,50 @@
|
||||
import { ListData } from "apiService/types";
|
||||
import { HAEntitiesState } from "haService/HAEntities";
|
||||
import { FC } from "react";
|
||||
import { AutomationData } from "types/automations";
|
||||
import { consolidateAutomations } from "./helpers";
|
||||
import { AutomationListSidebar } from "./Sidebar";
|
||||
import { useAutomationManagerState } from "./state";
|
||||
import { useTagDB } from "./TagDB";
|
||||
|
||||
export type AutomationListProps = {
|
||||
haEntites: HAEntitiesState;
|
||||
configAutomations: ListData<AutomationData>;
|
||||
onUpdate: (i: number, auto: AutomationData) => void;
|
||||
onUpdateTags: (id: string, tags: Record<string, string>) => void;
|
||||
onUpdateState: (id: string, state: string) => void;
|
||||
onTrigger: (id: string, state: string) => void;
|
||||
onAdd: () => void;
|
||||
onRemove: (i: number) => void;
|
||||
};
|
||||
|
||||
export const AutomationList: FC<AutomationListProps> = (props) => {
|
||||
const state = useAutomationManagerState();
|
||||
|
||||
if (!props.haEntites.ready) {
|
||||
return <div className="automation-manager loading">...</div>;
|
||||
}
|
||||
|
||||
const tagsDB = useTagDB(
|
||||
props.configAutomations.data.map(({ metadata: { id }, tags }) => ({
|
||||
id,
|
||||
tags,
|
||||
})),
|
||||
props.onUpdateTags
|
||||
);
|
||||
const automations = consolidateAutomations(
|
||||
props.haEntites.collection.state,
|
||||
props.configAutomations.data.map(({ metadata }) => metadata),
|
||||
tagsDB
|
||||
);
|
||||
return (
|
||||
<div className="automation-manager">
|
||||
{/* <AutomationListSidebar
|
||||
automations={automations}
|
||||
tagsDB={tagsDB}
|
||||
selectedAutomationId={state.current}
|
||||
onTagUpdate
|
||||
/> */}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
import { useState } from "react";
|
||||
import { useCookies } from "react-cookie";
|
||||
|
||||
export const useAutomationManagerState = () => {
|
||||
const [
|
||||
cookies,
|
||||
setCookies,
|
||||
// eslint-disable-next-line
|
||||
_,
|
||||
] = useCookies(["selectedAutomation"]);
|
||||
let initialCurrent = null;
|
||||
try {
|
||||
initialCurrent = cookies.selectedAutomation ?? null;
|
||||
} catch (_) {}
|
||||
|
||||
const [current, setCurrent] = useState<string | null>(initialCurrent);
|
||||
|
||||
return {
|
||||
current,
|
||||
setCurrent(i: string | null) {
|
||||
setCurrent(i);
|
||||
setCookies("selectedAutomation", i === null ? undefined : i);
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
export type AutomationListAutoUpdatable = {
|
||||
title: string;
|
||||
description: string;
|
||||
state: "on" | "off" | string;
|
||||
};
|
||||
export type AutomationListAuto = AutomationListAutoUpdatable & {
|
||||
id: string;
|
||||
entityId: string;
|
||||
tags: Record<string, string>;
|
||||
issue?: string;
|
||||
};
|
||||
|
||||
export type AutomationListItem = {
|
||||
title: string;
|
||||
isSelected: boolean;
|
||||
} & (
|
||||
| {
|
||||
type: "group";
|
||||
data: Array<AutomationListItem>;
|
||||
}
|
||||
| {
|
||||
type: "items";
|
||||
data: Array<AutomationListAuto & { isSelected: boolean }>;
|
||||
}
|
||||
);
|
||||
Reference in new issue
Block a user