moved control for tag update to tag db

renamed list to manager
This commit is contained in:
ari committed 2022-07-28 13:24:13 -04:00
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);
}
};