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,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>
);
};