minor clean up
This commit is contained in:
1 parent
f16d25d41e
commit
05d09830bb
5 files changed
+122
-131
No files matched your search
@@ -6,7 +6,7 @@
|
||||
margin-right: 5em;
|
||||
}
|
||||
|
||||
.automation-list {
|
||||
.automation-list .automation-list-box--list {
|
||||
max-height: 100vh;
|
||||
overflow: auto;
|
||||
}
|
||||
@@ -3,135 +3,111 @@ import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { Page } from "components/Page";
|
||||
import { AutomationListSidebar } from ".";
|
||||
import { makeTagDB } from "../TagDB";
|
||||
import { useMockHAEntities } from "haService/HAEntities.mock";
|
||||
import { createMockAuto } from "utils/mocks";
|
||||
import { useState } from "react";
|
||||
import { AutomationListAuto } from "../types";
|
||||
|
||||
export default {
|
||||
title: "App/AutomationList/Sidebar",
|
||||
component: AutomationListSidebar,
|
||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||
args: {
|
||||
title: "Example",
|
||||
description: "Cool!",
|
||||
state: "on",
|
||||
tags: {
|
||||
room: "main bedroom",
|
||||
routine: "night time",
|
||||
},
|
||||
},
|
||||
args: {},
|
||||
} as ComponentMeta<typeof AutomationListSidebar>;
|
||||
|
||||
export const Loading: ComponentStory<typeof AutomationListSidebar> = (args) => {
|
||||
const Template: ComponentStory<typeof AutomationListSidebar> = (args) => {
|
||||
const [automations, setAutomations] = useState(args.automations);
|
||||
return (
|
||||
<Page>
|
||||
<AutomationListSidebar
|
||||
{...args}
|
||||
haEntites={useMockHAEntities({ loading: true })}
|
||||
tagsDB={makeTagDB([])}
|
||||
configAutomationMetadatas={[]}
|
||||
/>
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
|
||||
export const Mocked: ComponentStory<typeof AutomationListSidebar> = (args) => {
|
||||
const [mockAutos, setAutos] = useState([
|
||||
createMockAuto({ room: "bedroom" }),
|
||||
createMockAuto({ room: "bathroom", type: "routine" }),
|
||||
createMockAuto({ type: "routine" }),
|
||||
createMockAuto(),
|
||||
createMockAuto({ type: "missing in ws" }),
|
||||
]);
|
||||
const [mockEntitiesStates, setMES] = useState<Record<string, string>>({
|
||||
...mockAutos.slice(0, 4).reduce((all, auto, i) => {
|
||||
const eid = `automation.number_${i}`;
|
||||
return {
|
||||
...all,
|
||||
[eid]: i % 2 == 0 ? "on" : "off",
|
||||
};
|
||||
}, {}),
|
||||
"automation.number_not_in_config": "on",
|
||||
});
|
||||
const mockEntities = mockAutos.slice(0, 4).reduce((all, auto, i) => {
|
||||
const eid = `automation.number_${i}`;
|
||||
return {
|
||||
...all,
|
||||
[eid]: {
|
||||
entity_id: eid,
|
||||
attributes: {
|
||||
id: auto.metadata.id,
|
||||
},
|
||||
state: mockEntitiesStates[eid] ?? "n/a",
|
||||
},
|
||||
};
|
||||
}, {});
|
||||
const haEntities = useMockHAEntities({
|
||||
loading: false,
|
||||
entities: {
|
||||
...mockEntities,
|
||||
"automation.number_not_in_config": {
|
||||
entity_id: "automation.number_not_in_config",
|
||||
state: mockEntitiesStates["automation.number_not_in_config"] ?? "n/a",
|
||||
attributes: {
|
||||
id: "1235",
|
||||
friendly_name: "What am I?",
|
||||
},
|
||||
} as any,
|
||||
},
|
||||
entitySource: {},
|
||||
});
|
||||
return (
|
||||
<Page>
|
||||
<AutomationListSidebar
|
||||
haEntites={haEntities}
|
||||
tagsDB={makeTagDB(mockAutos)}
|
||||
configAutomationMetadatas={mockAutos.map(({ metadata }) => metadata)}
|
||||
tagsDB={makeTagDB(automations)}
|
||||
automations={automations}
|
||||
onAutomationDelete={(aid) => {
|
||||
args.onAutomationDelete(aid);
|
||||
setAutos(mockAutos.filter(({ metadata }) => metadata.id !== aid));
|
||||
setAutomations(automations.filter(({ id }) => id !== aid));
|
||||
}}
|
||||
onAutomationUpdate={(a, aid, eid) => {
|
||||
args.onAutomationUpdate(a, aid, eid);
|
||||
const index = mockAutos.findIndex(
|
||||
({ metadata }) => metadata.id === aid
|
||||
);
|
||||
const index = automations.findIndex(({ id }) => id === aid);
|
||||
if (index >= 0) {
|
||||
setAutos([
|
||||
...mockAutos.slice(0, index),
|
||||
setAutomations([
|
||||
...automations.slice(0, index),
|
||||
{
|
||||
...mockAutos[index],
|
||||
metadata: {
|
||||
...mockAutos[index].metadata,
|
||||
alias: a.title,
|
||||
description: a.description,
|
||||
},
|
||||
...automations[index],
|
||||
...a,
|
||||
},
|
||||
...mockAutos.slice(index + 1),
|
||||
...automations.slice(index + 1),
|
||||
]);
|
||||
}
|
||||
setMES({
|
||||
...mockEntitiesStates,
|
||||
[eid]: a.state,
|
||||
});
|
||||
}}
|
||||
onTagUpdate={(tags, aid) => {
|
||||
args.onTagUpdate(tags, aid);
|
||||
const index = mockAutos.findIndex(
|
||||
({ metadata }) => metadata.id === aid
|
||||
);
|
||||
const index = automations.findIndex(({ id }) => id === aid);
|
||||
if (index >= 0) {
|
||||
setAutos([
|
||||
...mockAutos.slice(0, index),
|
||||
setAutomations([
|
||||
...automations.slice(0, index),
|
||||
{
|
||||
...mockAutos[index],
|
||||
...automations[index],
|
||||
tags,
|
||||
},
|
||||
...mockAutos.slice(index + 1),
|
||||
...automations.slice(index + 1),
|
||||
]);
|
||||
}
|
||||
}}
|
||||
onAutomationAdd={() => {
|
||||
args.onAutomationAdd();
|
||||
const id = String(Date.now());
|
||||
setAutomations([
|
||||
...automations,
|
||||
{
|
||||
id,
|
||||
description: "",
|
||||
title: "new",
|
||||
tags: {},
|
||||
entityId: "automation." + id,
|
||||
isSelected: true,
|
||||
state: "on",
|
||||
},
|
||||
]);
|
||||
}}
|
||||
/>
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
|
||||
const make = (automations: AutomationListAuto[]) => {
|
||||
const Basic = Template.bind({});
|
||||
Basic.args = {
|
||||
...Basic.args,
|
||||
automations,
|
||||
};
|
||||
return Basic;
|
||||
};
|
||||
const makeAuto = (data: Partial<AutomationListAuto>): AutomationListAuto => ({
|
||||
id: String(Date.now()),
|
||||
entityId: `automation.${Date.now()}`,
|
||||
title: "",
|
||||
description: "",
|
||||
state: "on",
|
||||
isSelected: false,
|
||||
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" },
|
||||
}),
|
||||
]);
|
||||
@@ -2,23 +2,19 @@ import "./index.css";
|
||||
|
||||
import { FC, useState } from "react";
|
||||
|
||||
import Skeleton from "@mui/material/Skeleton";
|
||||
|
||||
import { HAEntitiesState } from "haService/HAEntities";
|
||||
import { AutomationMetadata } from "types/automations";
|
||||
import InputText from "components/Inputs/InputText";
|
||||
import InputMultiSelect from "components/Inputs/InputMultiSelect";
|
||||
|
||||
import { ListBoxGroup } from "../ListBoxGroup";
|
||||
import { consolidateAutomations, filterAutomations } from "../helpers";
|
||||
import { filterAutomations } from "../helpers";
|
||||
import { TagDB } from "../TagDB";
|
||||
import { convertGroupingToItems, makeGrouping } from "../automationGrouper";
|
||||
import { AutomationListAutoUpdatable } from "../types";
|
||||
import { AutomationListAuto, AutomationListAutoUpdatable } from "../types";
|
||||
import { Button } from "components/Inputs/Button";
|
||||
|
||||
export type AutomationListSidebarProps = {
|
||||
configAutomationMetadatas: AutomationMetadata[];
|
||||
tagsDB: TagDB;
|
||||
haEntites: HAEntitiesState;
|
||||
automations: AutomationListAuto[];
|
||||
onTagUpdate: (t: Record<string, string>, aid: string) => void;
|
||||
onAutomationUpdate: (
|
||||
a: AutomationListAutoUpdatable,
|
||||
@@ -26,11 +22,12 @@ export type AutomationListSidebarProps = {
|
||||
eid: string
|
||||
) => void;
|
||||
onAutomationDelete: (aid: string) => void;
|
||||
onAutomationAdd: () => void;
|
||||
};
|
||||
export const AutomationListSidebar: FC<AutomationListSidebarProps> = ({
|
||||
configAutomationMetadatas,
|
||||
automations,
|
||||
tagsDB,
|
||||
haEntites,
|
||||
onAutomationAdd,
|
||||
...events
|
||||
}) => {
|
||||
const [searchText, setSearchText] = useState("");
|
||||
@@ -38,16 +35,7 @@ export const AutomationListSidebar: FC<AutomationListSidebarProps> = ({
|
||||
const [selectedAutomationId, setSelectedAutomationId] =
|
||||
useState<null | string>(null);
|
||||
|
||||
if (!haEntites.ready) {
|
||||
return <Skeleton className="automation-list loading" />;
|
||||
}
|
||||
const tags = tagsDB.getTagNames([]);
|
||||
let automations = consolidateAutomations(
|
||||
haEntites.collection.state,
|
||||
configAutomationMetadatas,
|
||||
selectedAutomationId ?? "",
|
||||
tagsDB
|
||||
);
|
||||
|
||||
if (searchText.trim().length > 0) {
|
||||
automations = filterAutomations(
|
||||
@@ -78,16 +66,23 @@ export const AutomationListSidebar: FC<AutomationListSidebarProps> = ({
|
||||
max={3}
|
||||
/>
|
||||
</div>
|
||||
{items.map((item, i) => (
|
||||
<ListBoxGroup
|
||||
{...item}
|
||||
key={i}
|
||||
onSelect={setSelectedAutomationId}
|
||||
{...events}
|
||||
tagsDB={tagsDB}
|
||||
initialOpenState={true}
|
||||
/>
|
||||
))}
|
||||
<div className="automation-list-box--list">
|
||||
{items.map((item, i) => (
|
||||
<ListBoxGroup
|
||||
{...item}
|
||||
key={i}
|
||||
onSelect={setSelectedAutomationId}
|
||||
{...events}
|
||||
tagsDB={tagsDB}
|
||||
initialOpenState={true}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
<div className="automation-list-box--bottom">
|
||||
<Button onClick={onAutomationAdd} color="primary">
|
||||
Add
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -1,10 +1,13 @@
|
||||
import { AutomationData } from "types/automations";
|
||||
|
||||
export type TagDB = ReturnType<typeof makeTagDB>;
|
||||
export const makeTagDB = (automations: AutomationData[]) => {
|
||||
export const makeTagDB = (
|
||||
automations: Array<{
|
||||
id: string;
|
||||
tags: Record<string, string>;
|
||||
}>
|
||||
) => {
|
||||
const allTags: Record<string, Set<string>> = {};
|
||||
const automationTags: Record<string, Record<string, string>> = {};
|
||||
automations.forEach(({ metadata: { id }, tags }) => {
|
||||
automations.forEach(({ id, tags }) => {
|
||||
automationTags[id] = tags;
|
||||
Object.keys(tags).forEach((tagName) => {
|
||||
const tagValue = tags[tagName];
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import { ListData } from "apiService/types";
|
||||
import { HAEntitiesState } from "haService/HAEntities";
|
||||
import { FC } from "react";
|
||||
import { AutomationData } from "types/automations";
|
||||
|
||||
export type AutomationListProps = {
|
||||
haEntites: HAEntitiesState;
|
||||
configAutomations: ListData<AutomationData>;
|
||||
onUpdate: (i: number, auto: AutomationData) => void;
|
||||
onUpdateTags: (id: string, tags: Record<string, string>) => void;
|
||||
onAdd: (auto: AutomationData) => void;
|
||||
onRemove: (i: number) => void;
|
||||
};
|
||||
|
||||
export const AutomationList: FC<AutomationListProps> = (props) => {
|
||||
return <></>;
|
||||
};
|
||||
Reference in new issue
Block a user