clean up
This commit is contained in:
1 parent
2a827c24f6
commit
57c86093a4
29 files changed
+145
-1901
No files matched your search
@@ -11,11 +11,11 @@ def find_hass_config() -> HassConfig:
|
||||
if config("USE_EXAMPLE_REPO", False, cast=bool):
|
||||
print("using USE_EXAMPLE_REPO")
|
||||
from tests.utils import (
|
||||
HA_CONFIG3_EXAMPLE,
|
||||
HA_CONFIG2_EXAMPLE,
|
||||
create_copy_of_example_config,
|
||||
)
|
||||
|
||||
path = create_copy_of_example_config(HA_CONFIG3_EXAMPLE)
|
||||
path = create_copy_of_example_config(HA_CONFIG2_EXAMPLE)
|
||||
print("|-> ", path)
|
||||
return HassConfig(path)
|
||||
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import App from "./App";
|
||||
import { withQuery } from "@storybook/addon-queryparams";
|
||||
import { makeStory } from "devUtils";
|
||||
|
||||
const book = {
|
||||
title: "App/App",
|
||||
decorators: [withQuery],
|
||||
component: App
|
||||
};
|
||||
const { make, componentMeta } = makeStory({
|
||||
Component: App,
|
||||
meta: {
|
||||
title: "App",
|
||||
decorators: [withQuery],
|
||||
},
|
||||
});
|
||||
export default componentMeta;
|
||||
|
||||
export const Basic = () => <App />
|
||||
|
||||
export default book;
|
||||
export const RootApp = make({});
|
||||
@@ -1,25 +1,27 @@
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { makeTagDB } from "components/AutomationList/TagDB";
|
||||
import { Page } from "components/Page";
|
||||
import { useTagDB } from "components/AutomationManager/TagDB";
|
||||
import { useState } from "react";
|
||||
import { AutoInfoBox } from "./AutoInfoBox";
|
||||
import { defaultAutomation } from "utils/defaults";
|
||||
import { AutoInfoBox, AutoInfoBoxProps } from "./AutoInfoBox";
|
||||
import { makeStory } from "devUtils";
|
||||
|
||||
export default {
|
||||
title: "App/AutomationList/Editor/InfoBox",
|
||||
component: AutoInfoBox,
|
||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||
args: {
|
||||
tagDB: makeTagDB([defaultAutomation("test")]),
|
||||
const { componentMeta, make } = makeStory({
|
||||
meta: {
|
||||
title: "App/AutomationEditor/InfoBox",
|
||||
},
|
||||
} as ComponentMeta<typeof AutoInfoBox>;
|
||||
Component: (args: {
|
||||
metadata: AutoInfoBoxProps["metadata"];
|
||||
tags: AutoInfoBoxProps["tags"];
|
||||
onUpdate: AutoInfoBoxProps["onUpdate"];
|
||||
}) => {
|
||||
const [[metadata, tags], setState] = useState([args.metadata, args.tags]);
|
||||
const tagDB = useTagDB(
|
||||
[{ id: metadata.id, tags: Object.fromEntries(tags) }],
|
||||
(_, t) => setState([metadata, Object.entries(t)])
|
||||
);
|
||||
|
||||
export const NoTags: ComponentStory<typeof AutoInfoBox> = (args) => {
|
||||
const [[metadata, tags], setState] = useState([args.metadata, args.tags]);
|
||||
return (
|
||||
<Page>
|
||||
return (
|
||||
<AutoInfoBox
|
||||
{...args}
|
||||
tagDB={tagDB}
|
||||
metadata={metadata}
|
||||
tags={tags}
|
||||
onUpdate={(m, t) => {
|
||||
@@ -27,11 +29,12 @@ export const NoTags: ComponentStory<typeof AutoInfoBox> = (args) => {
|
||||
setState([m, t]);
|
||||
}}
|
||||
/>
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
NoTags.args = {
|
||||
...NoTags.args,
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
export default componentMeta;
|
||||
export const NoTags = make({
|
||||
tags: [],
|
||||
metadata: {
|
||||
id: "random",
|
||||
@@ -42,11 +45,9 @@ NoTags.args = {
|
||||
},
|
||||
mode: "single",
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
export const SomeTags: ComponentStory<typeof AutoInfoBox> = NoTags.bind({});
|
||||
SomeTags.args = {
|
||||
...SomeTags.args,
|
||||
export const SomeTags = make({
|
||||
tags: [
|
||||
["Room", "Bathroom"],
|
||||
["Type", "Climate"],
|
||||
@@ -60,4 +61,4 @@ SomeTags.args = {
|
||||
},
|
||||
mode: "single",
|
||||
},
|
||||
};
|
||||
});
|
||||
@@ -5,17 +5,17 @@ import InputText from "components/Inputs/InputText";
|
||||
import { FC, ReactNode, useRef, useState } from "react";
|
||||
import { AddIcon, TrashIcon } from "components/Icons";
|
||||
import InputAutoText from "components/Inputs/InputAutoText";
|
||||
import { TagDB } from "components/AutomationList/TagDB";
|
||||
import { TagDB } from "components/AutomationManager/TagDB";
|
||||
import { ButtonIcon } from "components/Icons/ButtonIcons";
|
||||
|
||||
interface Props {
|
||||
export interface AutoInfoBoxProps {
|
||||
metadata: AutomationMetadata;
|
||||
tags: Array<[string, string]>;
|
||||
onUpdate: (m: AutomationMetadata, tags: Array<[string, string]>) => void;
|
||||
tagDB: TagDB;
|
||||
children?: ReactNode;
|
||||
}
|
||||
export const AutoInfoBox: FC<Props> = ({
|
||||
export const AutoInfoBox: FC<AutoInfoBoxProps> = ({
|
||||
metadata,
|
||||
tags,
|
||||
onUpdate,
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import React from "react";
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { AutomationEditor } from "./index";
|
||||
import { useState } from "react";
|
||||
import { Page } from "components/Page";
|
||||
import { makeTagDB } from "components/AutomationList/TagDB";
|
||||
import { useTagDB } from "components/AutomationManager/TagDB";
|
||||
import { DEFAULT_DIMS } from "components/DAGGraph/elements/constants";
|
||||
|
||||
export default {
|
||||
title: "App/AutomationList/Editor",
|
||||
title: "App/AutomationEditor",
|
||||
component: AutomationEditor,
|
||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||
args: {
|
||||
@@ -21,7 +20,10 @@ const Template: ComponentStory<typeof AutomationEditor> = (args) => {
|
||||
<Page>
|
||||
<AutomationEditor
|
||||
{...args}
|
||||
tagDB={makeTagDB(state ? [state] : [])}
|
||||
tagDB={useTagDB(
|
||||
state ? [{ id: state.metadata.id, tags: state.tags }] : [],
|
||||
() => {}
|
||||
)}
|
||||
automation={state}
|
||||
onUpdate={(s) => {
|
||||
window.setTimeout(() => setState(s), 3000);
|
||||
|
||||
@@ -10,7 +10,7 @@ import { Button } from "components/Inputs/Button";
|
||||
import { useAutomatioEditorState, EditorData } from "./state";
|
||||
import { MiniFailure } from "types/validators/helper";
|
||||
import InputYaml from "components/Inputs/InputYaml";
|
||||
import { TagDB } from "components/AutomationList/TagDB";
|
||||
import { TagDB } from "components/AutomationManager/TagDB";
|
||||
import Skeleton from "@mui/material/Skeleton";
|
||||
import LinearProgress from "@mui/material/LinearProgress";
|
||||
import { useCookies } from "react-cookie";
|
||||
|
||||
@@ -1,187 +0,0 @@
|
||||
import "styles/root.css";
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { useState } from "react";
|
||||
import { AutomationList } from ".";
|
||||
import { bigMockAutoList } from "utils/mocks";
|
||||
import { Page } from "components/Page";
|
||||
import { DEFAULT_DIMS } from "components/DAGGraph/elements/constants";
|
||||
import { AutomationData } from "types/automations";
|
||||
|
||||
export default {
|
||||
title: "App/AutomationList",
|
||||
component: AutomationList,
|
||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||
args: {
|
||||
dims: DEFAULT_DIMS,
|
||||
},
|
||||
} as ComponentMeta<typeof AutomationList>;
|
||||
|
||||
const Template: ComponentStory<typeof AutomationList> = (args) => {
|
||||
const [autos, setAutos] = useState(args.automations.data);
|
||||
return (
|
||||
<Page>
|
||||
<AutomationList
|
||||
{...args}
|
||||
automations={{
|
||||
...args.automations,
|
||||
data: autos,
|
||||
}}
|
||||
onAdd={(a) => {
|
||||
args.onAdd(a);
|
||||
setAutos([...autos, a]);
|
||||
}}
|
||||
onRemove={(i) => {
|
||||
args.onRemove(i);
|
||||
setAutos([...autos.slice(0, i), ...autos.slice(i + 1)]);
|
||||
}}
|
||||
onUpdate={(i, a) => {
|
||||
args.onUpdate(i, a);
|
||||
setAutos([...autos.slice(0, i), a, ...autos.slice(i + 1)]);
|
||||
}}
|
||||
/>
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
|
||||
const make = (autos: AutomationData[]) => {
|
||||
const NewTemp = Template.bind({});
|
||||
NewTemp.args = {
|
||||
...NewTemp.args,
|
||||
automations: {
|
||||
data: autos,
|
||||
params: {
|
||||
offset: 0,
|
||||
limit: 100,
|
||||
},
|
||||
totalItems: autos.length,
|
||||
},
|
||||
};
|
||||
|
||||
return NewTemp;
|
||||
};
|
||||
|
||||
export const EmptyStart = make([]);
|
||||
|
||||
export const FewAutos = make(bigMockAutoList);
|
||||
|
||||
export const BadAutmations = make([
|
||||
{
|
||||
metadata: {
|
||||
alias: "Set Thermostat to 76 at 8am",
|
||||
description: "",
|
||||
mode: "single",
|
||||
} as any,
|
||||
trigger: [
|
||||
{
|
||||
at: "08:00:00",
|
||||
platform: "time",
|
||||
},
|
||||
],
|
||||
condition: [
|
||||
{
|
||||
condition: "state",
|
||||
entity_id: "climate.thermostat",
|
||||
state: "cool",
|
||||
},
|
||||
],
|
||||
sequence: [
|
||||
{
|
||||
data: {
|
||||
temperature: 76,
|
||||
},
|
||||
service: "climate.set_temperature",
|
||||
target: {
|
||||
entity_id: "climate.thermostat",
|
||||
},
|
||||
},
|
||||
],
|
||||
tags: {},
|
||||
},
|
||||
{
|
||||
condition: [],
|
||||
tags: {},
|
||||
metadata: {
|
||||
id: "Bad Choose Example",
|
||||
alias: "Bad Choose",
|
||||
description: "Example Metadata",
|
||||
trigger_variables: {
|
||||
wowo: "!",
|
||||
},
|
||||
mode: "single",
|
||||
},
|
||||
trigger: [],
|
||||
sequence: [
|
||||
{
|
||||
choose: [],
|
||||
},
|
||||
] as any,
|
||||
},
|
||||
{
|
||||
condition: [],
|
||||
tags: {},
|
||||
metadata: {
|
||||
id: "random",
|
||||
alias: "Bad Trigger",
|
||||
description: "Example Metadata",
|
||||
trigger_variables: {
|
||||
wowo: "!",
|
||||
},
|
||||
mode: "single",
|
||||
},
|
||||
trigger: ["haha I am a string"] as any,
|
||||
sequence: [],
|
||||
},
|
||||
{
|
||||
condition: [],
|
||||
tags: {},
|
||||
metadata: {} as any,
|
||||
trigger: [],
|
||||
sequence: [],
|
||||
},
|
||||
]);
|
||||
|
||||
export const FewAutosWithSlowLoader: ComponentStory<typeof AutomationList> = (
|
||||
args
|
||||
) => {
|
||||
const [autos, setAutos] = useState(args.automations.data);
|
||||
return (
|
||||
<Page>
|
||||
<AutomationList
|
||||
{...args}
|
||||
automations={{
|
||||
...args.automations,
|
||||
data: autos,
|
||||
}}
|
||||
onAdd={(a) => {
|
||||
window.setTimeout(() => {
|
||||
args.onAdd(a);
|
||||
setAutos([...autos, a]);
|
||||
}, 10000);
|
||||
}}
|
||||
onRemove={(i) => {
|
||||
window.setTimeout(() => {
|
||||
args.onRemove(i);
|
||||
setAutos([...autos.slice(0, i), ...autos.slice(i + 1)]);
|
||||
}, 10000);
|
||||
}}
|
||||
onUpdate={(i, a) => {
|
||||
window.setTimeout(() => {
|
||||
args.onUpdate(i, a);
|
||||
setAutos([...autos.slice(0, i), a, ...autos.slice(i + 1)]);
|
||||
}, 10000);
|
||||
}}
|
||||
/>
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
FewAutosWithSlowLoader.args = {
|
||||
...FewAutosWithSlowLoader.args,
|
||||
automations: {
|
||||
data: bigMockAutoList,
|
||||
params: {
|
||||
limit: 100,
|
||||
offset: 0,
|
||||
},
|
||||
totalItems: 100,
|
||||
},
|
||||
};
|
||||
@@ -1,84 +0,0 @@
|
||||
.automation-list-box {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.automation-list-box--body {
|
||||
overflow-y: auto;
|
||||
overflow-x: hidden;
|
||||
height: 100%;
|
||||
flex: 1;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.automation-list-box--body>* {
|
||||
padding-top: 0.5em;
|
||||
padding-bottom: 0.5em;
|
||||
}
|
||||
|
||||
.automation-list-box--body--group {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
padding-left: 1em;
|
||||
border-radius: 10px;
|
||||
padding-bottom: 0.5em;
|
||||
border-width: 1px;
|
||||
border-bottom-style: solid;
|
||||
border-color: var(--mui-grey-400);
|
||||
}
|
||||
|
||||
.automation-list-box--body--group.other {
|
||||
border-color: var(--mui-success-main);
|
||||
}
|
||||
|
||||
.automation-list-box--body--group.other>b {
|
||||
color: var(--mui-success-light);
|
||||
}
|
||||
|
||||
.automation-list-box--body--group>.automation-list-box--body--group {
|
||||
border-left-style: solid;
|
||||
border-top-left-radius: 0;
|
||||
margin-left: 0.75em;
|
||||
padding-left: 0.5em;
|
||||
}
|
||||
|
||||
.automation-list-box--body--group>b {
|
||||
width: 100%;
|
||||
cursor: pointer;
|
||||
padding: 0.1em;
|
||||
}
|
||||
|
||||
.automation-list-box--body--group>b:hover,
|
||||
.automation-list-box--body--group.selected>b {
|
||||
background-color: var(--mui-grey-700);
|
||||
color: var(--mui-secondary-contrastText);
|
||||
}
|
||||
|
||||
|
||||
.automation-list-box--bottom {
|
||||
margin-top: 0.5em;
|
||||
margin-bottom: 0.5em;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: space-around;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.automation-list-box--bottom>.automation-list-box--bottom--add {
|
||||
background-color: var(--mui-primary-main);
|
||||
color: var(--mui-primary-contrastText);
|
||||
padding: 0.5em 2em;
|
||||
}
|
||||
|
||||
.automation-list-box--bottom>.automation-list-box--bottom--add:hover {
|
||||
color: var(--mui-primary-light);
|
||||
}
|
||||
|
||||
.automation-list-box--bottom--load svg {
|
||||
color: var(--mui-primary-main);
|
||||
}
|
||||
|
||||
.automation-list-box--bottom--load.less svg {
|
||||
transform: rotate(180deg);
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { useState } from "react";
|
||||
import { AutomationListBox } from "./AutomationListBox";
|
||||
import { createMockAuto } from "utils/mocks";
|
||||
import { Page } from "components/Page";
|
||||
import { CookiesProvider } from "react-cookie";
|
||||
import { makeTagDB } from "./TagDB";
|
||||
import { AutomationData } from "types/automations";
|
||||
|
||||
export default {
|
||||
title: "App/AutomationList/Box",
|
||||
component: AutomationListBox,
|
||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||
args: {},
|
||||
} as ComponentMeta<typeof AutomationListBox>;
|
||||
|
||||
const Template: ComponentStory<typeof AutomationListBox> = (args) => {
|
||||
const [selected, setSelected] = useState(args.selected);
|
||||
return (
|
||||
<Page>
|
||||
<CookiesProvider>
|
||||
<AutomationListBox
|
||||
{...args}
|
||||
tagsDB={makeTagDB(args.automations.data)}
|
||||
automations={args.automations}
|
||||
selected={selected}
|
||||
onSelectAutomation={(i) => {
|
||||
setSelected(i);
|
||||
args.onSelectAutomation(i);
|
||||
}}
|
||||
/>
|
||||
</CookiesProvider>
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
|
||||
const make = (autos: AutomationData[]) => {
|
||||
const NewTemp = Template.bind({});
|
||||
NewTemp.args = {
|
||||
...NewTemp.args,
|
||||
automations: {
|
||||
data: autos,
|
||||
params: {
|
||||
offset: 0,
|
||||
limit: 100,
|
||||
},
|
||||
totalItems: autos.length,
|
||||
},
|
||||
};
|
||||
|
||||
return NewTemp;
|
||||
};
|
||||
|
||||
export const EmptyStart = make([]);
|
||||
|
||||
export const FewAutos = make([
|
||||
createMockAuto({
|
||||
Room: "Bathroom",
|
||||
Type: "Lights",
|
||||
NewDevice: "Yes",
|
||||
Remote: "No",
|
||||
Floor: "1",
|
||||
}),
|
||||
createMockAuto({ Room: "Bathroom", Type: "Climate" }),
|
||||
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(),
|
||||
]);
|
||||
|
||||
export const OneAuto = make([
|
||||
createMockAuto({
|
||||
Type: "Climate",
|
||||
}),
|
||||
]);
|
||||
@@ -1,22 +0,0 @@
|
||||
import { getTagList } from "./AutomationListBox";
|
||||
import { createMockAuto } from "utils/mocks";
|
||||
|
||||
test("getTagList", () => {
|
||||
expect(
|
||||
getTagList([
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
])
|
||||
).toStrictEqual([]);
|
||||
|
||||
expect(
|
||||
getTagList([
|
||||
createMockAuto({ Room: "Kitchen", Area: "Oven" }),
|
||||
createMockAuto({ Room: "Attic" }),
|
||||
createMockAuto({ Room: "Office" }),
|
||||
createMockAuto({ Area: "Balcony" }),
|
||||
])
|
||||
).toStrictEqual(["Room", "Area"]);
|
||||
});
|
||||
@@ -1,195 +0,0 @@
|
||||
import "./AutomationListBox.css";
|
||||
import { FC, useState } from "react";
|
||||
import { AutomationData } from "types/automations";
|
||||
import InputMultiSelect from "components/Inputs/InputMultiSelect";
|
||||
import InputText from "components/Inputs/InputText";
|
||||
import { Button } from "components/Inputs/Button";
|
||||
import { makeGrouping } from "./automationGrouper";
|
||||
import { AutomationListBoxGroup } from "./AutomationListBoxGroup";
|
||||
import { useCookies } from "react-cookie";
|
||||
import { TagDB } from "components/AutomationList/TagDB";
|
||||
// import { ButtonIcon } from "components/Icons/ButtonIcons";
|
||||
// import ReadMoreOutlinedIcon from "@mui/icons-material/ReadMoreOutlined";
|
||||
import { ListData, ListParams } from "apiService/types";
|
||||
import Chip from "@mui/material/Chip";
|
||||
import InfoOutlinedIcon from "@mui/icons-material/InfoOutlined";
|
||||
|
||||
export type AutomationListBoxProps = {
|
||||
automations: ListData<AutomationData>;
|
||||
selected: number;
|
||||
onSelectAutomation: (i: number) => void;
|
||||
onRemove: (i: number) => void;
|
||||
onAdd: () => void;
|
||||
onUpdateTags: (id: string, tags: Record<string, string>) => void;
|
||||
onUpdate: (a: AutomationData, i: number) => void;
|
||||
onLoadMore: (p: ListParams) => void;
|
||||
tagsDB: TagDB;
|
||||
};
|
||||
|
||||
const useAutomationListBoxState = () => {
|
||||
const [
|
||||
cookies,
|
||||
setCookies,
|
||||
// eslint-disable-next-line
|
||||
_,
|
||||
] = useCookies(["albSearchText", "albSelected"]);
|
||||
let initialSelected = [];
|
||||
try {
|
||||
initialSelected = JSON.parse(cookies.albSelected);
|
||||
if (!Array.isArray(initialSelected)) {
|
||||
initialSelected = [initialSelected];
|
||||
}
|
||||
initialSelected = initialSelected.map(Number);
|
||||
// eslint-disable-next-line
|
||||
} catch (_) {}
|
||||
const [searchText, setSearchText] = useState(cookies.albSearchText ?? "");
|
||||
const [selectedTagIdx, setSelectedTagIdx] =
|
||||
useState<number[]>(initialSelected);
|
||||
|
||||
return {
|
||||
searchText,
|
||||
setSearchText(v: string) {
|
||||
setSearchText(v);
|
||||
setCookies("albSearchText", v);
|
||||
},
|
||||
setSelectedTagIdx(n: number[]) {
|
||||
setSelectedTagIdx(n);
|
||||
setCookies("albSelected", JSON.stringify(n));
|
||||
},
|
||||
selectedTagIdx,
|
||||
};
|
||||
};
|
||||
|
||||
export const AutomationListBox: FC<AutomationListBoxProps> = ({
|
||||
automations: {
|
||||
data: automations,
|
||||
// params: autoParams,
|
||||
totalItems: totalAutomations,
|
||||
},
|
||||
selected,
|
||||
onSelectAutomation,
|
||||
onAdd,
|
||||
onRemove,
|
||||
// onLoadMore,
|
||||
onUpdateTags,
|
||||
tagsDB,
|
||||
onUpdate,
|
||||
}) => {
|
||||
const tags = getTagList(automations);
|
||||
const { searchText, setSearchText, selectedTagIdx, setSelectedTagIdx } =
|
||||
useAutomationListBoxState();
|
||||
const filteredAutomations = filterAutomations(
|
||||
automations,
|
||||
searchText.toLowerCase()
|
||||
);
|
||||
const grouping = makeGrouping(
|
||||
filteredAutomations.map(([a, _]) => a),
|
||||
selectedTagIdx.map((i) => tags[i]),
|
||||
selected
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="automation-list-box">
|
||||
<div className="automation-list-box--nav">
|
||||
<InputText label="Search" value={searchText} onChange={setSearchText} />
|
||||
<InputMultiSelect
|
||||
label="Tags"
|
||||
selected={selectedTagIdx}
|
||||
onChange={setSelectedTagIdx}
|
||||
options={tags}
|
||||
max={3}
|
||||
/>
|
||||
</div>
|
||||
<div className="automation-list-box--body">
|
||||
<AutomationListBoxGroup
|
||||
events={{
|
||||
onSelectAutomation,
|
||||
onRemove,
|
||||
}}
|
||||
autos={filteredAutomations}
|
||||
grouping={grouping}
|
||||
selectedAutomationIdx={selected}
|
||||
tagsDB={tagsDB}
|
||||
onSave={onUpdate}
|
||||
/>
|
||||
</div>
|
||||
<div className="automation-list-box--bottom">
|
||||
<Button className="automation-list-box--bottom--add" onClick={onAdd}>
|
||||
Add
|
||||
</Button>
|
||||
{automations.length < totalAutomations ? (
|
||||
<>
|
||||
<Chip
|
||||
label={`Only loaded ${automations.length} out of ${totalAutomations}`}
|
||||
variant="outlined"
|
||||
color="warning"
|
||||
icon={<InfoOutlinedIcon />}
|
||||
onClick={() =>
|
||||
window.open(
|
||||
"https://github.com/asosnovsky/Shortumation/issues/78",
|
||||
"_blank"
|
||||
)
|
||||
}
|
||||
/>
|
||||
<small>support for more is not yet implemented</small>
|
||||
</>
|
||||
) : (
|
||||
""
|
||||
)}
|
||||
{/* {autoParams.offset > 0 && (
|
||||
<ButtonIcon
|
||||
borderless
|
||||
className="automation-list-box--bottom--load less"
|
||||
icon={<ReadMoreOutlinedIcon />}
|
||||
onClick={() => onLoadMore()}
|
||||
/>
|
||||
)} */}
|
||||
{/* {filteredAutomations.length} out of {totalAutomations} */}
|
||||
{/* {autoParams.offset + automations.length < totalAutomations && (
|
||||
<ButtonIcon
|
||||
borderless
|
||||
className="automation-list-box--bottom--load more"
|
||||
icon={<ReadMoreOutlinedIcon />}
|
||||
/>
|
||||
)} */}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export const getTagList = (autos: AutomationData[]): string[] =>
|
||||
Object.keys(
|
||||
autos
|
||||
.map((a) => Object.keys(a.tags))
|
||||
.reduce((all: any, keys) => {
|
||||
for (const k of keys) {
|
||||
all[k] = 1;
|
||||
}
|
||||
return all;
|
||||
}, {})
|
||||
);
|
||||
|
||||
const filterAutomations = (
|
||||
autos: AutomationData[],
|
||||
searchText: string
|
||||
): Array<[AutomationData, number]> =>
|
||||
autos
|
||||
.map<[AutomationData, number]>((a, i) => [a, i])
|
||||
.filter(([a, i]) => {
|
||||
if (!a.metadata) {
|
||||
return true;
|
||||
}
|
||||
if (searchText.length > 0) {
|
||||
if (
|
||||
!(
|
||||
(a.metadata.description ?? "")
|
||||
.toLocaleLowerCase()
|
||||
.includes(searchText) ||
|
||||
(a.metadata.alias ?? "").toLocaleLowerCase().includes(searchText)
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
@@ -1,50 +0,0 @@
|
||||
.automation-list-box--body--item:hover, .automation-list-box--body--item.selected {
|
||||
outline-color: var(--mui-grey-400);
|
||||
outline-style: solid;
|
||||
outline-width: 1px;
|
||||
background-color: var(--mui-secondary-contrastText);
|
||||
}
|
||||
|
||||
.automation-list-box--body--item:hover>.automation-list-box--body--item--tags>span {
|
||||
border-color: var(--mui-common-black);
|
||||
}
|
||||
|
||||
.automation-list-box--body--item--tags--add:hover {
|
||||
border-color: var(--mui-common-black);
|
||||
color: var(--mui-grey-400);
|
||||
background-color: var(--mui-secondary-contrastText);
|
||||
}
|
||||
|
||||
.automation-list-box--body--item {
|
||||
padding-left: 1em;
|
||||
cursor: pointer;
|
||||
display: flex;
|
||||
gap: 1em;
|
||||
}
|
||||
|
||||
.automation-list-box--body--item::before {
|
||||
content: "⊚ ";
|
||||
}
|
||||
|
||||
.automation-list-box--body--item--buttons {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 100%;
|
||||
gap: 0.25em;
|
||||
margin: 0.5em
|
||||
}
|
||||
|
||||
.automation-list-box--body--item--buttons>button {
|
||||
padding: 0.25em;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.automation-list-box--body--item--buttons .save {
|
||||
color: var(--mui-success-light)
|
||||
}
|
||||
|
||||
.automation-list-box--body--item--buttons .save:hover {
|
||||
color: var(--mui-success-main)
|
||||
}
|
||||
@@ -1,257 +0,0 @@
|
||||
import "./AutomationListBoxGroup.css";
|
||||
|
||||
import { TrashIcon } from "components/Icons";
|
||||
import { MetadataBox } from "./AutomationMetadataBox";
|
||||
import { AutomationData } from "types/automations";
|
||||
import { AutomationGrouping } from "./automationGrouper";
|
||||
import { FC, useEffect, useState } from "react";
|
||||
import { ButtonIcon } from "components/Icons/ButtonIcons";
|
||||
import { TagDB } from "./TagDB";
|
||||
import Save from "@mui/icons-material/Save";
|
||||
|
||||
export type AutomationListBoxEvents = {
|
||||
onSelectAutomation: (i: number) => void;
|
||||
onRemove: (i: number) => void;
|
||||
};
|
||||
export type AutomationListBoxGroups = {
|
||||
grouping: AutomationGrouping;
|
||||
currentNode: number;
|
||||
};
|
||||
export type AutomationListBoxProps = {
|
||||
events: AutomationListBoxEvents;
|
||||
groups: AutomationListBoxGroups;
|
||||
autos: Array<[AutomationData, number]>;
|
||||
selectedAutomationIdx: number;
|
||||
};
|
||||
export type AutomationListBoxGroupProps = {
|
||||
events: AutomationListBoxEvents;
|
||||
grouping: AutomationGrouping;
|
||||
autos: Array<[AutomationData, number]>;
|
||||
selectedAutomationIdx: number;
|
||||
tagsDB: TagDB;
|
||||
onSave: (a: AutomationData, i: number) => void;
|
||||
};
|
||||
export const AutomationListBoxGroup: FC<AutomationListBoxGroupProps> = ({
|
||||
events,
|
||||
grouping,
|
||||
autos,
|
||||
selectedAutomationIdx,
|
||||
tagsDB,
|
||||
onSave,
|
||||
}) => {
|
||||
if (grouping.top.length === 1) {
|
||||
return (
|
||||
<>
|
||||
{convertGroupsToItems(
|
||||
autos,
|
||||
{
|
||||
grouping,
|
||||
currentNode: grouping.top[0],
|
||||
},
|
||||
events,
|
||||
selectedAutomationIdx,
|
||||
tagsDB,
|
||||
onSave
|
||||
)}
|
||||
</>
|
||||
);
|
||||
} else {
|
||||
return (
|
||||
<>
|
||||
{grouping.top.map((groupIdx) => (
|
||||
<AutomationListBoxGroupItem
|
||||
key={groupIdx}
|
||||
autos={autos}
|
||||
groups={{
|
||||
grouping,
|
||||
currentNode: groupIdx,
|
||||
}}
|
||||
events={events}
|
||||
selectedAutomationIdx={selectedAutomationIdx}
|
||||
tagsDB={tagsDB}
|
||||
onSave={onSave}
|
||||
/>
|
||||
))}
|
||||
</>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export const convertGroupsToItems = (
|
||||
autos: Array<[AutomationData, number]>,
|
||||
{ grouping, currentNode }: AutomationListBoxGroups,
|
||||
events: AutomationListBoxEvents,
|
||||
selectedAutomationIdx: number,
|
||||
tagsDB: TagDB,
|
||||
onSave: (a: AutomationData, i: number) => void
|
||||
) => {
|
||||
const automations = grouping.getAutomations(currentNode);
|
||||
if (automations.length > 0) {
|
||||
return automations.map((autoIndex) => (
|
||||
<AutomationListBoxItem
|
||||
key={`${autoIndex}-${autos[autoIndex][1]}`}
|
||||
events={events}
|
||||
auto={autos[autoIndex]}
|
||||
isSelected={selectedAutomationIdx === autos[autoIndex][1]}
|
||||
tagsDB={tagsDB}
|
||||
onSave={(a) => onSave(a, autoIndex)}
|
||||
/>
|
||||
));
|
||||
}
|
||||
const subgroups = grouping.getSubGroups(currentNode);
|
||||
if (subgroups.length > 0) {
|
||||
return subgroups.map((groupIdx) => (
|
||||
<AutomationListBoxGroupItem
|
||||
key={groupIdx}
|
||||
autos={autos}
|
||||
groups={{
|
||||
grouping,
|
||||
currentNode: groupIdx,
|
||||
}}
|
||||
events={events}
|
||||
selectedAutomationIdx={selectedAutomationIdx}
|
||||
tagsDB={tagsDB}
|
||||
onSave={onSave}
|
||||
/>
|
||||
));
|
||||
}
|
||||
return <></>;
|
||||
};
|
||||
|
||||
export const AutomationListBoxGroupItem: FC<{
|
||||
events: AutomationListBoxEvents;
|
||||
groups: AutomationListBoxGroups;
|
||||
autos: Array<[AutomationData, number]>;
|
||||
selectedAutomationIdx: number;
|
||||
tagsDB: TagDB;
|
||||
onSave: (a: AutomationData, i: number) => void;
|
||||
}> = ({ autos, groups, events, selectedAutomationIdx, tagsDB, onSave }) => {
|
||||
const groupData = groups.grouping.getData(groups.currentNode);
|
||||
const automations = groups.grouping.getAutomations(groups.currentNode);
|
||||
const [open, setOpen] = useState(false);
|
||||
const isOther = groupData.isOther;
|
||||
const openIcon = !open ? "⊕" : "⊖";
|
||||
const total = isOther ? (
|
||||
<small>
|
||||
{automations.length} automation{automations.length !== 1 ? "s" : ""}{" "}
|
||||
{groupData.isOther ? "" : "(untagged)"}
|
||||
</small>
|
||||
) : (
|
||||
<></>
|
||||
);
|
||||
return (
|
||||
<div
|
||||
className={[
|
||||
"automation-list-box--body--group",
|
||||
isOther ? "other" : "",
|
||||
groupData.selected ? "selected" : "",
|
||||
].join(" ")}
|
||||
>
|
||||
<b onClick={() => setOpen(!open)}>
|
||||
{groupData.name} {openIcon} {total}
|
||||
</b>
|
||||
{open &&
|
||||
convertGroupsToItems(
|
||||
autos,
|
||||
groups,
|
||||
events,
|
||||
selectedAutomationIdx,
|
||||
tagsDB,
|
||||
onSave
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export const AutomationListBoxItem: FC<{
|
||||
auto: [AutomationData, number];
|
||||
isSelected: boolean;
|
||||
events: AutomationListBoxEvents;
|
||||
tagsDB: TagDB;
|
||||
onSave: (a: AutomationData) => void;
|
||||
}> = ({
|
||||
events: { onRemove, onSelectAutomation },
|
||||
auto: [auto, autoIndex],
|
||||
isSelected,
|
||||
tagsDB,
|
||||
onSave,
|
||||
}) => {
|
||||
// state
|
||||
const [{ tags, isModified, alias }, setState] = useState({
|
||||
isModified: false,
|
||||
tags: Object.entries(auto.tags),
|
||||
alias: auto.metadata.alias ?? "",
|
||||
});
|
||||
|
||||
// effects
|
||||
useEffect(() => {
|
||||
setState({
|
||||
isModified: false,
|
||||
tags: Object.entries(auto.tags),
|
||||
alias: auto.metadata.alias ?? "",
|
||||
});
|
||||
}, [auto]);
|
||||
|
||||
// alias
|
||||
const setAlias = (a: string) =>
|
||||
setState({
|
||||
isModified: true,
|
||||
tags,
|
||||
alias: a,
|
||||
});
|
||||
const setTags = (t: [string, string][]) =>
|
||||
setState({
|
||||
isModified: true,
|
||||
tags: t,
|
||||
alias,
|
||||
});
|
||||
// sub elements
|
||||
let title = "BadAuto<<Missing Metadata>>";
|
||||
if (auto.metadata) {
|
||||
title = `Name = ${auto.metadata.alias}\nID = ${auto.metadata.id}\nDescription = ${auto.metadata.description}`;
|
||||
}
|
||||
|
||||
// render
|
||||
return (
|
||||
<div
|
||||
key={autoIndex}
|
||||
className={[
|
||||
"automation-list-box--body--item",
|
||||
isSelected ? "selected" : "",
|
||||
isModified ? "modded" : "",
|
||||
].join(" ")}
|
||||
title={title}
|
||||
onClick={() => onSelectAutomation(autoIndex)}
|
||||
>
|
||||
<MetadataBox
|
||||
metadata={{
|
||||
...auto.metadata,
|
||||
alias,
|
||||
}}
|
||||
tags={tags}
|
||||
tagsDB={tagsDB}
|
||||
onTagUpdate={setTags}
|
||||
onAliasUpdate={setAlias}
|
||||
/>
|
||||
<div className="automation-list-box--body--item--buttons">
|
||||
{isModified && (
|
||||
<ButtonIcon
|
||||
className="save"
|
||||
icon={<Save />}
|
||||
onClick={() =>
|
||||
onSave({
|
||||
...auto,
|
||||
tags: Object.fromEntries(tags),
|
||||
metadata: {
|
||||
...auto.metadata,
|
||||
alias,
|
||||
},
|
||||
})
|
||||
}
|
||||
/>
|
||||
)}
|
||||
<ButtonIcon onClick={() => onRemove(autoIndex)} icon={<TrashIcon />} />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -1,101 +0,0 @@
|
||||
.automation-list-box--body--item--title {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
padding-left: 0.25em;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.automation-list-box--body--item--title>b {
|
||||
font-size: 1em;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.25em;
|
||||
}
|
||||
|
||||
.automation-list-box--body--item--title .input-text-view span {
|
||||
max-height: 1.5em;
|
||||
}
|
||||
|
||||
.automation-list-box--body--item--title>span, .automation-list-box--body--item--title>b>span {
|
||||
font-size: 0.75em;
|
||||
}
|
||||
|
||||
|
||||
.automation-list-box--body--item--tags {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
font-size: 0.75em;
|
||||
justify-content: left;
|
||||
align-items: flex-start;
|
||||
overflow: auto;
|
||||
max-height: 100%;
|
||||
gap: 0.25em;
|
||||
}
|
||||
|
||||
.automation-list-box--body--item--tags>span {
|
||||
border-color: var(--mui-grey-400);
|
||||
border-style: solid;
|
||||
border-width: 1px;
|
||||
border-radius: 0.5em;
|
||||
padding: 0.1em 0.5em;
|
||||
}
|
||||
|
||||
.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(--mui-common-black);
|
||||
}
|
||||
|
||||
.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(--mui-grey-400);
|
||||
border-radius: 20px;
|
||||
}
|
||||
|
||||
.automation-list-box--body--item--tags--item>.text .icon:hover svg {
|
||||
fill: var(--mui-common-black);
|
||||
}
|
||||
|
||||
.automation-list-box--body--item--tags--item .delete {
|
||||
font-size: 1em;
|
||||
color: var(--mui-error-light);
|
||||
}
|
||||
|
||||
.automation-list-box--body--item--tags--item .delete:hover {
|
||||
color: var(--mui-error-dark);
|
||||
outline: 1px solid var(--mui-error-dark);
|
||||
border-radius: 20px;
|
||||
}
|
||||
@@ -1,181 +0,0 @@
|
||||
import "./AutomationMetadataBox.css";
|
||||
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 DeleteIcon from "@mui/icons-material/ClearTwoTone";
|
||||
import { InputTextView } from "components/Inputs/InputTextView";
|
||||
|
||||
export const MetadataBox: FC<{
|
||||
metadata: AutomationMetadata;
|
||||
onAliasUpdate: (t: string) => void;
|
||||
onTagUpdate: (t: [string, string][]) => void;
|
||||
tags: [string, string][];
|
||||
tagsDB: TagDB;
|
||||
}> = ({ tags, onTagUpdate, onAliasUpdate, tagsDB, metadata }) => {
|
||||
// state
|
||||
const [newTag, setNewTag] = useState<
|
||||
| {
|
||||
on: false;
|
||||
}
|
||||
| {
|
||||
on: true;
|
||||
name: string;
|
||||
value: string;
|
||||
}
|
||||
>({ on: false });
|
||||
// alias
|
||||
const tagOptions = tagsDB.getTagNames(tags.map((t) => t[0]));
|
||||
|
||||
// render
|
||||
let title = <span>{"BadAuto<<Missing Metadata>>"}</span>;
|
||||
if (metadata) {
|
||||
title = (
|
||||
<>
|
||||
<b className="text">
|
||||
<InputTextView
|
||||
value={metadata.alias ?? ""}
|
||||
onChange={onAliasUpdate}
|
||||
/>
|
||||
<span>({String(metadata.id).slice(0, 10)})</span>
|
||||
</b>
|
||||
<span>{String(metadata.description ?? "").slice(0, 25)}</span>
|
||||
</>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<div className="automation-list-box--body--item--title">
|
||||
{title}
|
||||
<div className="automation-list-box--body--item--tags">
|
||||
{tags.map(([tagName, tagValue], tagIndex) => (
|
||||
<MetadataBoxTag
|
||||
options={[tagOptions, tagsDB.getTagValues(tagName)]}
|
||||
key={tagIndex}
|
||||
value={[tagName, tagValue]}
|
||||
onDelete={() =>
|
||||
onTagUpdate([
|
||||
...tags.slice(0, tagIndex),
|
||||
...tags.slice(tagIndex + 1),
|
||||
])
|
||||
}
|
||||
onChange={([newName, newValue]) =>
|
||||
onTagUpdate([
|
||||
...tags.slice(0, tagIndex),
|
||||
[newName, newValue],
|
||||
...tags.slice(tagIndex + 1),
|
||||
])
|
||||
}
|
||||
/>
|
||||
))}
|
||||
{newTag.on ? (
|
||||
<MetadataBoxTag
|
||||
options={[tagOptions, tagsDB.getTagValues(newTag.name)]}
|
||||
key="new"
|
||||
value={[newTag.name, newTag.value]}
|
||||
initialState={true}
|
||||
onChange={([name, value]) => {
|
||||
setNewTag({
|
||||
on: true,
|
||||
name,
|
||||
value,
|
||||
});
|
||||
}}
|
||||
onToggle={(em) => {
|
||||
if (!em) {
|
||||
if (newTag.name.trim() !== "" && newTag.value.trim() !== "") {
|
||||
onTagUpdate(tags.concat([[newTag.name, newTag.value]]));
|
||||
setNewTag({
|
||||
on: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
setNewTag({
|
||||
on: false,
|
||||
});
|
||||
}}
|
||||
/>
|
||||
) : (
|
||||
<span
|
||||
className="automation-list-box--body--item--tags--add"
|
||||
onClick={() =>
|
||||
setNewTag({
|
||||
on: true,
|
||||
name: "",
|
||||
value: "",
|
||||
})
|
||||
}
|
||||
>
|
||||
+
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export const MetadataBoxTag: FC<{
|
||||
value: [string, string];
|
||||
onChange: (t: [string, string]) => void;
|
||||
options: [string[], string[]];
|
||||
initialState?: boolean;
|
||||
onToggle?: (em: boolean) => void;
|
||||
onDelete?: () => void;
|
||||
}> = (props) => {
|
||||
const [editMode, setEditMode] = useState(
|
||||
props.initialState ? props.initialState : false
|
||||
);
|
||||
const toggle = () => {
|
||||
if (editMode) {
|
||||
if (props.value[0].trim() === "" || props.value[1].trim() === "") {
|
||||
props.onToggle && props.onToggle(true);
|
||||
return setEditMode(true);
|
||||
}
|
||||
}
|
||||
props.onToggle && props.onToggle(!editMode);
|
||||
setEditMode(!editMode);
|
||||
};
|
||||
return (
|
||||
<span className="automation-list-box--body--item--tags--item">
|
||||
{props.onDelete && (
|
||||
<DeleteIcon onClick={props.onDelete} className="delete" />
|
||||
)}
|
||||
{editMode ? (
|
||||
<InputAutoComplete
|
||||
multiple={false}
|
||||
className="text"
|
||||
disableChips
|
||||
options={props.options[0]}
|
||||
label=""
|
||||
value={props.value[0]}
|
||||
onEnter={toggle}
|
||||
onChange={(v) => props.onChange([v ?? "", props.value[1]])}
|
||||
onlyShowLabel
|
||||
endAdornment={<CheckMarkIcon className="icon" onClick={toggle} />}
|
||||
/>
|
||||
) : (
|
||||
<b className="text" onClick={toggle}>
|
||||
{props.value[0]}:
|
||||
</b>
|
||||
)}
|
||||
{editMode ? (
|
||||
<InputAutoComplete
|
||||
multiple={false}
|
||||
className="text"
|
||||
disableChips
|
||||
options={props.options[1]}
|
||||
label=""
|
||||
value={props.value[1]}
|
||||
onEnter={toggle}
|
||||
onChange={(v) => props.onChange([props.value[0], v ?? ""])}
|
||||
onlyShowLabel
|
||||
endAdornment={<CheckMarkIcon className="icon" onClick={toggle} />}
|
||||
/>
|
||||
) : (
|
||||
<span className="text" onClick={toggle}>
|
||||
{props.value[1]}
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
);
|
||||
};
|
||||
@@ -1,30 +0,0 @@
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
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>
|
||||
);
|
||||
};
|
||||
@@ -1,63 +0,0 @@
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { ConnectedAutomationList } from ".";
|
||||
import { createMockAuto } from "utils/mocks";
|
||||
import { useMockApiService } from "apiService";
|
||||
import { Page } from "components/Page";
|
||||
import { DEFAULT_DIMS } from "components/DAGGraph/elements/constants";
|
||||
|
||||
export default {
|
||||
title: "App/AutomationList/MockConnected",
|
||||
component: ConnectedAutomationList,
|
||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||
args: {
|
||||
dims: DEFAULT_DIMS,
|
||||
initialAutomations: [],
|
||||
},
|
||||
} as ComponentMeta<typeof ConnectedAutomationList>;
|
||||
|
||||
const Template: ComponentStory<any> = (args) => {
|
||||
const api = useMockApiService(args.initialAutomations);
|
||||
return (
|
||||
<Page>
|
||||
<ConnectedAutomationList {...args} api={api} />
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
|
||||
export const EmptyStart = Template.bind({});
|
||||
|
||||
export const FewAutos = Template.bind({});
|
||||
FewAutos.args = {
|
||||
...FewAutos.args,
|
||||
initialAutomations: [
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
],
|
||||
};
|
||||
|
||||
export const Loading: ComponentStory<any> = (args) => {
|
||||
const api = useMockApiService(args.initialAutomations);
|
||||
return (
|
||||
<Page>
|
||||
<ConnectedAutomationList
|
||||
{...args}
|
||||
api={{
|
||||
...api,
|
||||
state: {
|
||||
automations: {
|
||||
ready: false,
|
||||
},
|
||||
},
|
||||
}}
|
||||
/>
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
@@ -1,26 +0,0 @@
|
||||
import { AutomationData } from "types/automations";
|
||||
|
||||
export type TagDB = ReturnType<typeof makeTagDB>;
|
||||
export const makeTagDB = (automations: AutomationData[]) => {
|
||||
const allTags: Record<string, Set<string>> = {};
|
||||
automations.forEach(({ tags }) =>
|
||||
Object.keys(tags).forEach((tagName) => {
|
||||
const tagValue = tags[tagName];
|
||||
if (!allTags[tagName]) {
|
||||
allTags[tagName] = new Set();
|
||||
}
|
||||
allTags[tagName].add(tagValue);
|
||||
})
|
||||
);
|
||||
const allTagNames = Object.keys(allTags);
|
||||
return {
|
||||
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()
|
||||
: [];
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -1,72 +0,0 @@
|
||||
import { makeGrouping } from "./automationGrouper";
|
||||
import { AutomationData } from 'types/automations/index';
|
||||
import { bigMockAutoList, createMockAuto } from 'utils/mocks';
|
||||
|
||||
const automations: AutomationData[] = [
|
||||
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({}),
|
||||
];
|
||||
|
||||
test('no grouping', () => {
|
||||
const grouped = makeGrouping(automations, [], 0);
|
||||
expect(grouped.top).toStrictEqual([0])
|
||||
expect(grouped.getAutomations(0)).toStrictEqual([0, 1, 2, 3, 4, 5])
|
||||
});
|
||||
test('1 grouping', () => {
|
||||
const grouped = makeGrouping(automations, ["Room"], 0);
|
||||
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"], 0);
|
||||
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(bigMockAutoList, ['Room', "Remote"], 0);
|
||||
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)
|
||||
}
|
||||
})
|
||||
@@ -1,130 +0,0 @@
|
||||
import { AutomationData } from "types/automations";
|
||||
|
||||
const makeGrouper = () => {
|
||||
const groupIdNames: string[] = []
|
||||
const groupSubGroups: Array<Set<number>> = []
|
||||
const groupAutomations: number[][] = []
|
||||
const groupData: Array<{
|
||||
name: string;
|
||||
selected: boolean;
|
||||
isOther: boolean;
|
||||
isTop: boolean;
|
||||
}> = [];
|
||||
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: AutomationData[],
|
||||
selectedTags: string[],
|
||||
selectedAutomationIdx: number,
|
||||
) => {
|
||||
const grouper = makeGrouper();
|
||||
|
||||
if (selectedTags.length === 0) {
|
||||
grouper.addNew("$", "", selectedAutomationIdx >= 0, true, true)
|
||||
grouper.groupAutomations = [autos.map((_, i) => i)];
|
||||
} else {
|
||||
autos.forEach((auto, autoIdx) => {
|
||||
const theChosenOne = selectedAutomationIdx === autoIdx;
|
||||
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]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
/* root elements */
|
||||
.automation-list--root {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.automation-list--list {
|
||||
flex: 1;
|
||||
max-width: 400px;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.automation-list--viewer {
|
||||
overflow: hidden;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
/* Loading state */
|
||||
.automation-list--root.loading {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.automation-list--root.loading>* {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.automation-list--root.loading .automation-list--circle-loader {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
display: flex;
|
||||
}
|
||||
|
||||
/* list children: on hide */
|
||||
.automation-list--list.hide>.automation-list-box {
|
||||
width: 0;
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
.automation-list--list.hide {
|
||||
min-width: 35px;
|
||||
max-width: 35px;
|
||||
}
|
||||
|
||||
.automation-list--list.hide>.automation-list--list-hide {
|
||||
transform: rotate(180deg);
|
||||
left: 0;
|
||||
}
|
||||
|
||||
/* list children: normal */
|
||||
.automation-list--list {
|
||||
border-right: solid;
|
||||
border-width: 1px;
|
||||
}
|
||||
|
||||
.automation-list--list .automation-list--list-hide {
|
||||
position: absolute;
|
||||
bottom: 0em;
|
||||
left: 0.5em;
|
||||
max-width: 1.5em;
|
||||
max-height: 1.5em;
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
/* none-hidden */
|
||||
.automation-list--root.mobile {
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.automation-list--root.mobile>.automation-list--list {
|
||||
max-width: 100vw;
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
.automation-list--root.mobile .automation-editor {
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.automation-list--root.mobile .automation-list--list-hide {
|
||||
transform: rotate(90deg);
|
||||
left: 0.25em;
|
||||
}
|
||||
|
||||
|
||||
/* list hide */
|
||||
.automation-list--root.mobile .automation-list--list.hide {
|
||||
width: 100vw;
|
||||
min-height: 0;
|
||||
max-height: 0;
|
||||
}
|
||||
|
||||
.automation-list--root.mobile .automation-list--list.hide .automation-list--list-hide {
|
||||
transform: rotate(270deg);
|
||||
position: fixed;
|
||||
top: 0.5em;
|
||||
z-index: 5;
|
||||
}
|
||||
|
||||
.automation-list--root.mobile .automation-editor--flow-wrapper>.automation-editor--flow-wrapper--toolbar .input-text-view,
|
||||
.automation-list--root.mobile .automation-editor--flow-wrapper>.automation-editor--flow-wrapper--toolbar .id {
|
||||
display: none;
|
||||
}
|
||||
@@ -1,213 +0,0 @@
|
||||
import "./index.css";
|
||||
import "./index.mobile.css";
|
||||
import { AutomationEditor } from "components/AutomationEditor";
|
||||
import { FC, ReactNode, useState, useRef, useEffect } from "react";
|
||||
import { AutomationData } from "types/automations";
|
||||
import { defaultAutomation } from "utils/defaults";
|
||||
import { ButtonIcon } from "components/Icons/ButtonIcons";
|
||||
import { ApiService } from "apiService/core";
|
||||
import { AutomationListBox } from "./AutomationListBox";
|
||||
import useWindowSize from "utils/useWindowSize";
|
||||
import { makeTagDB } from "./TagDB";
|
||||
import LinearProgress from "@mui/material/LinearProgress";
|
||||
import CircularProgress from "@mui/material/CircularProgress";
|
||||
import { useCookies } from "react-cookie";
|
||||
import { useHA } from "haService";
|
||||
import { ArrowBack } from "@mui/icons-material";
|
||||
import { useSnackbar } from "notistack";
|
||||
import { DAGDims } from "components/DAGGraph/elements/types";
|
||||
import { ListData, ListParams } from "apiService/types";
|
||||
|
||||
interface AutomationListParams {
|
||||
automations: ListData<AutomationData>;
|
||||
onUpdate: (i: number, auto: AutomationData) => void;
|
||||
onUpdateTags: (id: string, tags: Record<string, string>) => void;
|
||||
onAdd: (auto: AutomationData) => void;
|
||||
onRemove: (i: number) => void;
|
||||
onLoadMore: (p: ListParams) => void;
|
||||
dims: DAGDims;
|
||||
children?: ReactNode;
|
||||
}
|
||||
|
||||
export const useAutomationListState = () => {
|
||||
const [
|
||||
cookies,
|
||||
setCookies,
|
||||
// eslint-disable-next-line
|
||||
_,
|
||||
] = useCookies(["alCurrent"]);
|
||||
let initialCurrent = 0;
|
||||
try {
|
||||
initialCurrent = Number(cookies.alCurrent ?? "0");
|
||||
} catch (_) {}
|
||||
|
||||
const [hideList, setHideList] = useState(false);
|
||||
const [current, setCurrent] = useState(initialCurrent);
|
||||
|
||||
return {
|
||||
hideList,
|
||||
setHideList,
|
||||
current,
|
||||
setCurrent(i: number) {
|
||||
setCurrent(i);
|
||||
setCookies("alCurrent", String(i));
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export const AutomationList: FC<AutomationListParams> = ({
|
||||
automations,
|
||||
onLoadMore,
|
||||
onUpdate,
|
||||
onUpdateTags,
|
||||
onAdd,
|
||||
onRemove,
|
||||
dims,
|
||||
children,
|
||||
}) => {
|
||||
// state
|
||||
const snackbr = useSnackbar();
|
||||
const { hideList, setHideList, current, setCurrent } =
|
||||
useAutomationListState();
|
||||
const { isMobile } = useWindowSize();
|
||||
const firstRender = useRef(true);
|
||||
// alias
|
||||
const currentAuto =
|
||||
automations.data.length > 0
|
||||
? automations.data[current - automations.params.offset]
|
||||
: null;
|
||||
const tagDB = makeTagDB(automations.data);
|
||||
// effect
|
||||
useEffect(() => {
|
||||
if (firstRender.current) {
|
||||
snackbr.enqueueSnackbar("Saved.", { variant: "info" });
|
||||
}
|
||||
firstRender.current = true;
|
||||
// eslint-disable-next-line
|
||||
}, [automations]);
|
||||
// render
|
||||
return (
|
||||
<div
|
||||
className={["automation-list--root", isMobile ? "mobile" : ""].join(" ")}
|
||||
>
|
||||
<div
|
||||
className={["automation-list--list", hideList ? "hide" : "show"].join(
|
||||
" "
|
||||
)}
|
||||
>
|
||||
<AutomationListBox
|
||||
automations={automations}
|
||||
onUpdate={(a, i) => onUpdate(i, a)}
|
||||
onUpdateTags={onUpdateTags}
|
||||
onAdd={() => {
|
||||
onAdd(defaultAutomation(String(Date.now())));
|
||||
setCurrent(automations.data.length);
|
||||
if (isMobile) {
|
||||
setHideList(true);
|
||||
}
|
||||
}}
|
||||
onSelectAutomation={(i) => {
|
||||
setCurrent(i);
|
||||
if (isMobile) {
|
||||
setHideList(true);
|
||||
}
|
||||
}}
|
||||
onLoadMore={onLoadMore}
|
||||
selected={current}
|
||||
onRemove={onRemove}
|
||||
tagsDB={tagDB}
|
||||
/>
|
||||
<ButtonIcon
|
||||
onClick={() => setHideList(!hideList)}
|
||||
className="automation-list--list-hide"
|
||||
icon={<ArrowBack />}
|
||||
/>
|
||||
</div>
|
||||
<div className="automation-list--viewer">
|
||||
{currentAuto ? (
|
||||
<AutomationEditor
|
||||
automation={currentAuto}
|
||||
onUpdate={(a) => onUpdate(current, a)}
|
||||
dims={{
|
||||
...dims,
|
||||
flipped: isMobile,
|
||||
}}
|
||||
tagDB={tagDB}
|
||||
/>
|
||||
) : (
|
||||
<></>
|
||||
)}
|
||||
</div>
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
interface ConnectedAutomationListParams {
|
||||
dims: DAGDims;
|
||||
api: ApiService;
|
||||
}
|
||||
export const ConnectedAutomationList: FC<ConnectedAutomationListParams> = ({
|
||||
api: {
|
||||
state: { automations },
|
||||
...methods
|
||||
},
|
||||
dims,
|
||||
}) => {
|
||||
const { reloadAutomations } = useHA();
|
||||
const [saving, setSaving] = useState(false);
|
||||
const updateTags = async (id: string, tags: Record<string, string>) => {
|
||||
const resp = await methods.updateTags({
|
||||
automation_id: id,
|
||||
tags,
|
||||
});
|
||||
setSaving(true);
|
||||
await reloadAutomations();
|
||||
setSaving(false);
|
||||
return resp;
|
||||
};
|
||||
const removeAutomation = async (args: any) => {
|
||||
const resp = await methods.removeAutomation(args);
|
||||
setSaving(true);
|
||||
await reloadAutomations();
|
||||
setSaving(false);
|
||||
return resp;
|
||||
};
|
||||
const updateAutomation = async (args: any) => {
|
||||
const resp = await methods.updateAutomation(args);
|
||||
setSaving(true);
|
||||
await reloadAutomations();
|
||||
setSaving(false);
|
||||
return resp;
|
||||
};
|
||||
|
||||
if (!automations.ready) {
|
||||
return (
|
||||
<div className="automation-list--root loading">
|
||||
<LinearProgress />
|
||||
<div className="automation-list--circle-loader">
|
||||
<CircularProgress />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
if (!automations.ok) {
|
||||
return <span>Error {automations.error}</span>;
|
||||
}
|
||||
return (
|
||||
<>
|
||||
{saving && <LinearProgress />}
|
||||
<AutomationList
|
||||
dims={dims}
|
||||
automations={automations.data}
|
||||
onUpdateTags={updateTags}
|
||||
onLoadMore={() => {}}
|
||||
onAdd={(auto) =>
|
||||
updateAutomation({ auto, index: automations.data.totalItems + 1 })
|
||||
}
|
||||
onRemove={(index) => removeAutomation({ index })}
|
||||
onUpdate={(index, auto) => updateAutomation({ index, auto })}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
};
|
||||
@@ -59,9 +59,9 @@ export const ListBoxGroup: FC<ListBoxGroupProps> = ({
|
||||
].join(" ")}
|
||||
>
|
||||
{type === "items"
|
||||
? data.map((auto) => (
|
||||
? data.map((auto, i) => (
|
||||
<MetadataBox
|
||||
key={auto.id}
|
||||
key={`${i}-${auto.id}`}
|
||||
{...auto}
|
||||
tagsDB={tagsDB}
|
||||
onDelete={() => events.onAutomationDelete(auto.id)}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
--bottom-height: 3em;
|
||||
position: relative;
|
||||
height: 100%;
|
||||
min-width: 25vw;
|
||||
min-width: calc(min(max(25vw, 35em), 50vw));
|
||||
}
|
||||
|
||||
.automation-manager--sidebar-box--nav {
|
||||
|
||||
@@ -12,6 +12,8 @@ export const useHAEntities = (): HAEntitiesState => {
|
||||
new Set(Object.keys(state).map((x) => x.split(".")[0])).keys()
|
||||
).sort(),
|
||||
}));
|
||||
entities.collection?.subscribe(console.log);
|
||||
console.log("ha entities refreshing...");
|
||||
const entitySource = useHassCollection(entitySourceColl);
|
||||
return createHAEntitiesState(entities, entitySource);
|
||||
};
|
||||
|
||||
@@ -1,59 +1,62 @@
|
||||
import { Collection, Connection } from "home-assistant-js-websocket";
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useState, useEffect } from "react";
|
||||
import { useHAConnection } from "./connection";
|
||||
|
||||
type _HACollectionState<T> = {
|
||||
ready: false,
|
||||
collection?: Collection<T>,
|
||||
} | {
|
||||
ready: true,
|
||||
collection: Collection<T>,
|
||||
}
|
||||
export type HACollectionState<T, D = {}> = _HACollectionState<T> & {
|
||||
additional: Partial<D>
|
||||
}
|
||||
export const useHassCollection = <T, D extends {} = {}>(
|
||||
getHACollection: (c: Connection) => Collection<T>,
|
||||
updateAdditionalProps?: (n: T | {}) => D,
|
||||
): HACollectionState<T, D> => {
|
||||
const [state, setState] = useState<_HACollectionState<T>>({
|
||||
ready: false
|
||||
});
|
||||
const conn = useHAConnection();
|
||||
|
||||
useEffect(() => {
|
||||
if (conn.status === 'loaded' && !state.ready) {
|
||||
const collection = getHACollection(conn.connection);
|
||||
setState({
|
||||
ready: false,
|
||||
collection,
|
||||
})
|
||||
const unsub = collection.subscribe(d => {
|
||||
if (d && !state.ready) {
|
||||
setState({
|
||||
ready: true,
|
||||
collection,
|
||||
})
|
||||
unsub();
|
||||
} else if (!d && state.ready) {
|
||||
setState({
|
||||
ready: true,
|
||||
collection,
|
||||
})
|
||||
unsub();
|
||||
}
|
||||
});
|
||||
return unsub
|
||||
}
|
||||
}, [conn, getHACollection, state.ready]);
|
||||
|
||||
return {
|
||||
...state,
|
||||
get additional(): Partial<D> {
|
||||
if (state.ready && updateAdditionalProps) {
|
||||
return updateAdditionalProps(state.collection.state)
|
||||
}
|
||||
return {}
|
||||
}
|
||||
type _HACollectionState<T> =
|
||||
| {
|
||||
ready: false;
|
||||
collection?: Collection<T>;
|
||||
}
|
||||
}
|
||||
| {
|
||||
ready: true;
|
||||
collection: Collection<T>;
|
||||
};
|
||||
export type HACollectionState<T, D = {}> = _HACollectionState<T> & {
|
||||
additional: Partial<D>;
|
||||
};
|
||||
export const useHassCollection = <T, D extends {} = {}>(
|
||||
getHACollection: (c: Connection) => Collection<T>,
|
||||
updateAdditionalProps?: (n: T | {}) => D
|
||||
): HACollectionState<T, D> => {
|
||||
const [state, setState] = useState<_HACollectionState<T>>({
|
||||
ready: false,
|
||||
});
|
||||
const conn = useHAConnection();
|
||||
|
||||
useEffect(() => {
|
||||
if (conn.status === "loaded" && !state.ready) {
|
||||
const collection = getHACollection(conn.connection);
|
||||
setState({
|
||||
ready: false,
|
||||
collection,
|
||||
});
|
||||
const unsub = collection.subscribe((d) => {
|
||||
console.log("updating collection", d);
|
||||
if (d && !state.ready) {
|
||||
setState({
|
||||
ready: true,
|
||||
collection,
|
||||
});
|
||||
// unsub();
|
||||
} else if (!d && state.ready) {
|
||||
setState({
|
||||
ready: true,
|
||||
collection,
|
||||
});
|
||||
// unsub();
|
||||
}
|
||||
});
|
||||
return unsub;
|
||||
}
|
||||
}, [conn, getHACollection, state.ready]);
|
||||
|
||||
return {
|
||||
...state,
|
||||
get additional(): Partial<D> {
|
||||
if (state.ready && updateAdditionalProps) {
|
||||
return updateAdditionalProps(state.collection.state);
|
||||
}
|
||||
return {};
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,16 @@
|
||||
import { useConnectedApiService } from "apiService";
|
||||
import { makeStory } from "devUtils";
|
||||
import { AutomationRoute } from "./automation";
|
||||
|
||||
const { make, componentMeta } = makeStory({
|
||||
Component: () => {
|
||||
return <AutomationRoute api={useConnectedApiService()} />;
|
||||
},
|
||||
meta: {
|
||||
title: "App/Routes",
|
||||
},
|
||||
});
|
||||
|
||||
export default componentMeta;
|
||||
|
||||
export const Automations = make({});
|
||||
@@ -1,13 +1,27 @@
|
||||
import { ApiService } from "apiService/core";
|
||||
import { ConnectedAutomationList } from "components/AutomationList";
|
||||
import { DEFAULT_DIMS } from "components/DAGGraph/elements/constants";
|
||||
import { Page } from "components/Page";
|
||||
import { FC } from "react";
|
||||
import { useHA } from "haService";
|
||||
import { AutomationManager } from "components/AutomationManager";
|
||||
|
||||
export const AutomationRoute: FC<{ api: ApiService }> = ({ api }) => {
|
||||
const ha = useHA();
|
||||
return (
|
||||
<Page>
|
||||
<ConnectedAutomationList dims={DEFAULT_DIMS} api={api} />
|
||||
<AutomationManager
|
||||
api={api}
|
||||
onAutomationStateChange={(eid, on) => {
|
||||
ha.callService(
|
||||
"automation",
|
||||
on ? "turn_on" : "turn_off",
|
||||
{},
|
||||
{
|
||||
entity_id: eid,
|
||||
}
|
||||
);
|
||||
}}
|
||||
haEntities={ha.entities}
|
||||
/>
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user