tag autocomplete
This commit is contained in:
1 parent
fb8eb4f38c
commit
5729b74c89
22 files changed
+515
-191
No files matched your search
@@ -15,7 +15,7 @@ export const useMockAPI = (
|
||||
method = "POST",
|
||||
data = {}
|
||||
}) {
|
||||
console.debug("[[mockCall]]", method, path, data);
|
||||
// console.debug("[[mockCall]]", method, path, data);
|
||||
if (path === AUTOMTAION_LIST) {
|
||||
return {
|
||||
ok: true,
|
||||
|
||||
@@ -64,24 +64,6 @@
|
||||
padding: 1em 0.25em 0.2em 0.25em;
|
||||
}
|
||||
|
||||
.automation-editor--info-box--tag--error {
|
||||
width: 100%;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.automation-editor--info-box--tag--error.hide {
|
||||
height: 0;
|
||||
opacity: 0;
|
||||
display: none;
|
||||
}
|
||||
|
||||
.automation-editor--info-box--tag--error.show {
|
||||
height: fit-content;
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.automation-editor--info-box--tag--trash, .automation-editor--info-box--tag--add {
|
||||
height: 2em;
|
||||
width: 2em;
|
||||
|
||||
@@ -1,12 +1,20 @@
|
||||
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { makeTagDB } from "components/AutomationList/TagDB";
|
||||
import { Page } from "components/Page";
|
||||
import { useState } from "react";
|
||||
import { AutoInfoBox } from "./AutoInfoBox";
|
||||
import { defaultAutomation } from 'utils/defaults';
|
||||
|
||||
export default {
|
||||
title: 'App/AutomationList/Editor/InfoBox',
|
||||
component: AutoInfoBox,
|
||||
parameters: { actions: { argTypesRegex: '^on.*' } },
|
||||
args: {
|
||||
tagDB: makeTagDB([
|
||||
defaultAutomation("test")
|
||||
])
|
||||
}
|
||||
} as ComponentMeta<typeof AutoInfoBox>;
|
||||
|
||||
|
||||
@@ -15,6 +23,7 @@ export const NoTags: ComponentStory<typeof AutoInfoBox> = args => {
|
||||
return (
|
||||
<Page>
|
||||
<AutoInfoBox
|
||||
{...args}
|
||||
className=""
|
||||
metadata={metadata}
|
||||
tags={tags}
|
||||
|
||||
@@ -5,12 +5,15 @@ import InputText from "components/Inputs/InputText";
|
||||
import { FC, useRef, useState } from 'react';
|
||||
import { AddIcon, TrashIcon } from "components/Icons";
|
||||
import { Button } from "components/Inputs/Button";
|
||||
import InputAutoText from "components/Inputs/InputAutoText";
|
||||
import { TagDB } from "components/AutomationList/TagDB";
|
||||
|
||||
interface Props {
|
||||
className: string;
|
||||
metadata: AutomationMetadata;
|
||||
tags: Array<[string, string]>;
|
||||
onUpdate: (m: AutomationMetadata, tags: Array<[string, string]>) => void;
|
||||
tagDB: TagDB;
|
||||
}
|
||||
export const AutoInfoBox: FC<Props> = ({
|
||||
metadata,
|
||||
@@ -18,6 +21,7 @@ export const AutoInfoBox: FC<Props> = ({
|
||||
onUpdate,
|
||||
className,
|
||||
children,
|
||||
tagDB,
|
||||
}) => {
|
||||
// state
|
||||
const [newTag, setNewTag] = useState<[string, string]>(["", ""])
|
||||
@@ -25,6 +29,7 @@ export const AutoInfoBox: FC<Props> = ({
|
||||
const errorTimeoutId = useRef(-1);
|
||||
|
||||
// aliases
|
||||
const foundNewTagNames = tagDB.getTagNames(tags.map(([n, _]) => n));
|
||||
const setError = (index: number | 'new', msg: string) => {
|
||||
_setError([index, msg])
|
||||
if (errorTimeoutId.current >= 0) {
|
||||
@@ -68,7 +73,9 @@ export const AutoInfoBox: FC<Props> = ({
|
||||
if (error) {
|
||||
return setError('new', error)
|
||||
}
|
||||
onUpdate(metadata, tags.concat([newTag]));
|
||||
onUpdate(metadata, tags.concat([
|
||||
newTag.map(x => x.trim()) as [string, string]
|
||||
]));
|
||||
setNewTag(['', '']);
|
||||
}
|
||||
const onRemoveTag = (tagIndex: number) => onUpdate(metadata, [
|
||||
@@ -96,22 +103,40 @@ export const AutoInfoBox: FC<Props> = ({
|
||||
<h1>Tags</h1>
|
||||
<div className="automation-editor--info-box--tag-list">
|
||||
{tags.map(([tagName, tagValue], tagIndex) => <div className="automation-editor--info-box--tag">
|
||||
<InputText value={tagName} label="Name" onChange={v => updateTagName(v, tagIndex)} />
|
||||
<InputText value={tagValue} label="Tag" onChange={v => onUpdateTags(v, tagIndex)} />
|
||||
<InputAutoText
|
||||
value={tagName}
|
||||
label="Name"
|
||||
onChange={v => updateTagName(v, tagIndex)}
|
||||
options={foundNewTagNames}
|
||||
error={errorIndex === tagIndex ? error : undefined}
|
||||
/>
|
||||
<InputAutoText
|
||||
value={tagValue}
|
||||
label="Tag"
|
||||
onChange={v => onUpdateTags(v, tagIndex)}
|
||||
options={tagDB.getTagValues(tagName)}
|
||||
error={errorIndex === tagIndex ? error : undefined}
|
||||
/>
|
||||
<Button className="automation-editor--info-box--tag--trash" onClick={() => onRemoveTag(tagIndex)} ><TrashIcon /></Button>
|
||||
<span className={["automation-editor--info-box--tag--error", (errorIndex === tagIndex) ? "show" : "hide"].join(" ")}>
|
||||
{error}
|
||||
</span>
|
||||
</div>)}
|
||||
</div>
|
||||
<hr />
|
||||
<div className="automation-editor--info-box--tag">
|
||||
<InputText value={newTag[0]} label="Name" onChange={v => setNewTag([v, newTag[1]])} onEnter={addNewTag} />
|
||||
<InputText value={newTag[1]} label="Tag" onChange={v => setNewTag([newTag[0], v])} onEnter={addNewTag} />
|
||||
<InputAutoText
|
||||
value={newTag[0]}
|
||||
label="Name"
|
||||
onChange={v => setNewTag([v, newTag[1]])}
|
||||
options={foundNewTagNames}
|
||||
error={errorIndex === 'new' ? error : undefined}
|
||||
/>
|
||||
<InputAutoText
|
||||
value={newTag[1]}
|
||||
label="Tag"
|
||||
onChange={v => setNewTag([newTag[0], v])}
|
||||
options={tagDB.getTagValues(newTag[0])}
|
||||
error={errorIndex === 'new' ? error : undefined}
|
||||
/>
|
||||
<Button className="automation-editor--info-box--tag--add" onClick={addNewTag} ><AddIcon /></Button>
|
||||
<span className={["automation-editor--info-box--tag--error", (errorIndex === 'new') ? "show" : "hide"].join(" ")}>
|
||||
{error}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -4,6 +4,7 @@ import { AutomationEditor } from './index';
|
||||
import { useState } from 'react';
|
||||
import * as dgconst from 'components/DAGFlow/constants';
|
||||
import { Page } from "components/Page";
|
||||
import { makeTagDB } from "components/AutomationList/TagDB";
|
||||
|
||||
|
||||
export default {
|
||||
@@ -11,7 +12,7 @@ export default {
|
||||
component: AutomationEditor,
|
||||
parameters: { actions: { argTypesRegex: '^on.*' } },
|
||||
args: {
|
||||
dims: dgconst.DEFAULT_DIMS
|
||||
dims: dgconst.DEFAULT_DIMS,
|
||||
}
|
||||
} as ComponentMeta<typeof AutomationEditor>
|
||||
|
||||
@@ -20,6 +21,7 @@ const Template: ComponentStory<typeof AutomationEditor> = args => {
|
||||
return <Page>
|
||||
<AutomationEditor
|
||||
{...args}
|
||||
tagDB={makeTagDB([state as any])}
|
||||
automation={state}
|
||||
onUpdate={s => {
|
||||
window.setTimeout(() => setState(s), 3000)
|
||||
|
||||
@@ -10,16 +10,19 @@ import { SafeDAGAutomationFlow } from "components/DAGFlow/safe";
|
||||
import { DAGAutomationFlowDims } from "components/DAGFlow/types";
|
||||
import { MiniFailure } from 'types/validators/helper';
|
||||
import InputYaml from "components/Inputs/InputYaml";
|
||||
import { TagDB } from "components/AutomationList/TagDB";
|
||||
|
||||
interface Props {
|
||||
automation?: AutomationData;
|
||||
dims: DAGAutomationFlowDims;
|
||||
onUpdate: (auto: AutomationData) => void;
|
||||
tagDB: TagDB;
|
||||
}
|
||||
export const AutomationEditor: FC<Props> = ({
|
||||
dims,
|
||||
automation: propsAutos,
|
||||
onUpdate: propsOnUpdate,
|
||||
tagDB,
|
||||
}) => {
|
||||
// state
|
||||
const {
|
||||
@@ -55,6 +58,7 @@ export const AutomationEditor: FC<Props> = ({
|
||||
metadata={state.data.metadata}
|
||||
tags={state.data.tags}
|
||||
onUpdate={updateMetadata}
|
||||
tagDB={tagDB}
|
||||
>
|
||||
<ButtonIcon
|
||||
className="automation-editor--info-box--icon"
|
||||
|
||||
@@ -5,7 +5,8 @@
|
||||
}
|
||||
|
||||
.automation-list-box--body {
|
||||
overflow: auto;
|
||||
overflow-y: auto;
|
||||
overflow-x: hidden;
|
||||
height: 100%;
|
||||
flex: 1;
|
||||
user-select: none;
|
||||
@@ -24,7 +25,15 @@
|
||||
padding-bottom: 0.5em;
|
||||
border-width: 1px;
|
||||
border-bottom-style: solid;
|
||||
border-color: white;
|
||||
border-color: var(--primary-accent);
|
||||
}
|
||||
|
||||
.automation-list-box--body--group.other {
|
||||
border-color: var(--green);
|
||||
}
|
||||
|
||||
.automation-list-box--body--group.other>b {
|
||||
color: var(--change);
|
||||
}
|
||||
|
||||
.automation-list-box--body--group>.automation-list-box--body--group {
|
||||
|
||||
@@ -2,62 +2,64 @@ import { groupAutomations, getTagList } from './AutomationListBox';
|
||||
import { AutomationData } from 'types/automations/index';
|
||||
import { createMockAuto } from 'utils/mocks';
|
||||
|
||||
const makeDummy = (id: string, tags: Record<string, string>) => ({
|
||||
condition: [],
|
||||
metadata: {
|
||||
id,
|
||||
alias: '',
|
||||
description: '',
|
||||
mode: 'single',
|
||||
},
|
||||
tags,
|
||||
sequence: [],
|
||||
trigger: [],
|
||||
})
|
||||
|
||||
test('group automations', () => {
|
||||
const automations: AutomationData[] = [
|
||||
{
|
||||
condition: [],
|
||||
metadata: {
|
||||
id: '1',
|
||||
alias: '',
|
||||
description: '',
|
||||
mode: 'single',
|
||||
},
|
||||
tags: {
|
||||
"Room": 'Living Room',
|
||||
"Type": 'Lights'
|
||||
},
|
||||
sequence: [],
|
||||
trigger: [],
|
||||
},
|
||||
{
|
||||
condition: [],
|
||||
metadata: {
|
||||
id: '2',
|
||||
alias: '',
|
||||
description: '',
|
||||
mode: 'single',
|
||||
},
|
||||
tags: {
|
||||
"Room": 'Living Room',
|
||||
"Type": 'Climate'
|
||||
},
|
||||
sequence: [],
|
||||
trigger: [],
|
||||
},
|
||||
{
|
||||
condition: [],
|
||||
metadata: {
|
||||
id: '3',
|
||||
alias: '',
|
||||
description: '',
|
||||
mode: 'single',
|
||||
},
|
||||
tags: {
|
||||
"Room": 'Kitchen',
|
||||
"Type": 'Climate'
|
||||
},
|
||||
sequence: [],
|
||||
trigger: [],
|
||||
},
|
||||
makeDummy('1', {
|
||||
"Room": 'Living Room',
|
||||
"Type": 'Lights'
|
||||
}),
|
||||
makeDummy('2', {
|
||||
"Room": 'Living Room',
|
||||
"Type": 'Climate'
|
||||
}),
|
||||
makeDummy('3', {
|
||||
"Room": 'Kitchen',
|
||||
"Type": 'Climate'
|
||||
}),
|
||||
makeDummy('4', {
|
||||
"Type": 'Climate'
|
||||
}),
|
||||
makeDummy('5', {
|
||||
"Room": 'Living Room'
|
||||
}),
|
||||
makeDummy('6', {}),
|
||||
];
|
||||
expect(groupAutomations(automations, [], ['Room', "Type"])).toStrictEqual([
|
||||
0, 1, 2, 3, 4, 5,
|
||||
])
|
||||
expect(groupAutomations(automations, [1], ['Room', "Type"])).toStrictEqual({
|
||||
"Lights": [0],
|
||||
"Climate": [1, 2, 3],
|
||||
"": [4, 5]
|
||||
})
|
||||
expect(groupAutomations(automations, [0], ['Room', "Type"])).toStrictEqual({
|
||||
"Living Room": [0, 1, 4],
|
||||
"Kitchen": [2],
|
||||
"": [3, 5]
|
||||
})
|
||||
expect(groupAutomations(automations, [0, 1], ['Room', "Type"])).toStrictEqual({
|
||||
"Living Room": {
|
||||
"Lights": [0],
|
||||
"Climate": [1],
|
||||
"": [4],
|
||||
},
|
||||
"Kitchen": {
|
||||
"Climate": [2],
|
||||
}
|
||||
},
|
||||
"": [3, 5]
|
||||
})
|
||||
expect(groupAutomations(automations, [1, 0], ['Room', "Type"])).toStrictEqual({
|
||||
"Lights": {
|
||||
@@ -65,22 +67,11 @@ test('group automations', () => {
|
||||
},
|
||||
"Climate": {
|
||||
"Living Room": [1],
|
||||
"Kitchen": [2]
|
||||
}
|
||||
"Kitchen": [2],
|
||||
"": [3],
|
||||
},
|
||||
"": [4, 5]
|
||||
})
|
||||
expect(groupAutomations(automations, [1], ['Room', "Type"])).toStrictEqual({
|
||||
"Lights": [0],
|
||||
"Climate": [1, 2]
|
||||
})
|
||||
expect(groupAutomations(automations, [0], ['Room', "Type"])).toStrictEqual({
|
||||
"Living Room": [0, 1],
|
||||
"Kitchen": [2]
|
||||
})
|
||||
|
||||
|
||||
expect(groupAutomations(automations, [], ['Room', "Type"])).toStrictEqual([
|
||||
0, 1, 2
|
||||
])
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import InputMultiSelect from "components/Inputs/InputMultiSelect";
|
||||
import InputText from "components/Inputs/InputText";
|
||||
import { Button } from "components/Inputs/Button";
|
||||
import { TrashIcon } from "components/Icons";
|
||||
import { MetadataBox } from "./AutomationMetadataBox";
|
||||
|
||||
|
||||
export type Props = {
|
||||
@@ -78,13 +79,6 @@ const filterAutomations = (autos: AutomationData[], searchText: string, selected
|
||||
return false
|
||||
}
|
||||
}
|
||||
if (selected.length > 0) {
|
||||
for (const i of selected) {
|
||||
if (!a.tags[tags[i]]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
@@ -105,13 +99,21 @@ export const groupAutomations = (
|
||||
let lastKey: string | null = null;
|
||||
for (const i of selected) {
|
||||
tag = tags[i];
|
||||
if (lastKey !== null) {
|
||||
if (lastKey) {
|
||||
where = where[lastKey]
|
||||
}
|
||||
if (!where[a.tags[tag]]) {
|
||||
where[a.tags[tag]] = {}
|
||||
if (a.tags[tag]) {
|
||||
if (!where[a.tags[tag]]) {
|
||||
where[a.tags[tag]] = {}
|
||||
}
|
||||
lastKey = a.tags[tag]
|
||||
} else {
|
||||
if (!where[""]) {
|
||||
where[""] = []
|
||||
}
|
||||
lastKey = ""
|
||||
break
|
||||
}
|
||||
lastKey = a.tags[tag]
|
||||
}
|
||||
if (lastKey !== null) {
|
||||
if (!where[lastKey] || !Array.isArray(where[lastKey])) {
|
||||
@@ -148,7 +150,12 @@ const convertGroupsToItems = (
|
||||
</div>
|
||||
})
|
||||
} else {
|
||||
return Object.keys(groups).map(groupName => {
|
||||
return Object.keys(groups).sort((a, b) => {
|
||||
if (!a) {
|
||||
return 1
|
||||
}
|
||||
return a < b ? 1 : -1
|
||||
}).map(groupName => {
|
||||
return <AutomationListBoxGroup
|
||||
key={groupName}
|
||||
groupName={groupName}
|
||||
@@ -162,33 +169,9 @@ const convertGroupsToItems = (
|
||||
}
|
||||
}
|
||||
|
||||
export const MetadataBox: FC<{
|
||||
metadata: AutomationMetadata,
|
||||
tags: Record<string, string>,
|
||||
}> = (auto) => {
|
||||
let title = <span>{"BadAuto<<Missing Metadata>>"}</span>
|
||||
if (auto.metadata) {
|
||||
title = <>
|
||||
<b>{String(auto.metadata.alias ?? "").slice(0, 15)} <span>({String(auto.metadata.id).slice(0, 5)})</span></b>
|
||||
<span>{String(auto.metadata.description ?? "").slice(0, 25)}</span>
|
||||
</>
|
||||
}
|
||||
return <div
|
||||
className="automation-list-box--body--item--title"
|
||||
>
|
||||
{title}
|
||||
<div
|
||||
className="automation-list-box--body--item--tags"
|
||||
>
|
||||
{Object.keys(auto.tags).map(tagName => <span key={tagName}>
|
||||
<b>{tagName}:</b> {auto.tags[tagName]}
|
||||
</span>)}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
|
||||
const AutomationListBoxGroup: FC<{
|
||||
export const AutomationListBoxGroup: FC<{
|
||||
groupName: string,
|
||||
groups: any,
|
||||
autos: Array<[AutomationData, number]>,
|
||||
@@ -204,8 +187,13 @@ const AutomationListBoxGroup: FC<{
|
||||
selected,
|
||||
}) => {
|
||||
const [open, setOpen] = useState(false);
|
||||
return <div className="automation-list-box--body--group">
|
||||
<b onClick={() => setOpen(!open)}>{groupName} {!open ? "⊕" : "⊖"}</b>
|
||||
const isOther = !groupName;
|
||||
const openIcon = !open ? "⊕" : "⊖";
|
||||
const total = isOther ? <small>
|
||||
{groups.length} item{groups.length !== 1 ? 's' : ''} (untagged)
|
||||
</small> : <></>
|
||||
return <div className={["automation-list-box--body--group", isOther ? 'other' : ''].join(' ')}>
|
||||
<b onClick={() => setOpen(!open)}>{groupName} {openIcon} {total}</b>
|
||||
{open && convertGroupsToItems(groups, autos, onSelectAutomation, onRemove, selected)}
|
||||
</div>
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
|
||||
import { TrashIcon } from "components/Icons";
|
||||
import { Button } from "components/Inputs/Button";
|
||||
import { MetadataBox } from "./AutomationMetadataBox";
|
||||
import { AutomationData } from "types/automations";
|
||||
import { AutomationGrouper } from "./automationGrouper";
|
||||
|
||||
|
||||
|
||||
export const convertGroupsToItems = (
|
||||
autos: Array<[AutomationData, number]>,
|
||||
groups: AutomationGrouper,
|
||||
currentNode: number,
|
||||
onSelectAutomation: (i: number) => void,
|
||||
onRemove: (i: number) => void,
|
||||
) => {
|
||||
if (Array.isArray(groups)) {
|
||||
return groups.map(i => {
|
||||
const [auto, autoIndex] = autos[i];
|
||||
let title = "BadAuto<<Missing Metadata>>"
|
||||
if (auto.metadata) {
|
||||
title = `Name = ${auto.metadata.alias}\nID = ${auto.metadata.id}\nDescription = ${auto.metadata.description}`
|
||||
}
|
||||
return <div
|
||||
key={i}
|
||||
className={["automation-list-box--body--item", selected === autoIndex ? 'selected' : ''].join(' ')}
|
||||
title={title}
|
||||
onClick={() => onSelectAutomation(autoIndex)}
|
||||
>
|
||||
<MetadataBox metadata={auto.metadata} tags={auto.tags} />
|
||||
<Button onClick={() => onRemove(autoIndex)}><TrashIcon /></Button>
|
||||
</div>
|
||||
})
|
||||
} else {
|
||||
return Object.keys(groups).sort((a, b) => {
|
||||
if (!a) {
|
||||
return 1
|
||||
}
|
||||
return a < b ? 1 : -1
|
||||
}).map(groupName => {
|
||||
return <AutomationListBoxGroup
|
||||
key={groupName}
|
||||
groupName={groupName}
|
||||
groups={groups[groupName]}
|
||||
autos={autos}
|
||||
onSelectAutomation={onSelectAutomation}
|
||||
onRemove={onRemove}
|
||||
selected={selected}
|
||||
/>
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { FC } from "react"
|
||||
import { AutomationMetadata } from "types/automations"
|
||||
|
||||
export const MetadataBox: FC<{
|
||||
metadata: AutomationMetadata,
|
||||
tags: Record<string, string>,
|
||||
}> = (auto) => {
|
||||
let title = <span>{"BadAuto<<Missing Metadata>>"}</span>
|
||||
if (auto.metadata) {
|
||||
title = <>
|
||||
<b>{String(auto.metadata.alias ?? "").slice(0, 15)} <span>({String(auto.metadata.id).slice(0, 5)})</span></b>
|
||||
<span>{String(auto.metadata.description ?? "").slice(0, 25)}</span>
|
||||
</>
|
||||
}
|
||||
return <div
|
||||
className="automation-list-box--body--item--title"
|
||||
>
|
||||
{title}
|
||||
<div
|
||||
className="automation-list-box--body--item--tags"
|
||||
>
|
||||
{Object.keys(auto.tags).map(tagName => <span key={tagName}>
|
||||
<b>{tagName}:</b> {auto.tags[tagName]}
|
||||
</span>)}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { AutomationData } from "types/automations";
|
||||
|
||||
export type TagDB = ReturnType<typeof makeTagDB>;
|
||||
export const makeTagDB = (automations: AutomationData[]) => {
|
||||
const allTags: Record<string, string[]> = {};
|
||||
automations.forEach(({ tags }) =>
|
||||
Object.keys(tags).forEach(tagName => {
|
||||
const tagValue = tags[tagName];
|
||||
allTags[tagName] = (allTags[tagName] ?? []).concat([tagValue]);
|
||||
})
|
||||
);
|
||||
const allTagNames = Object.keys(allTags);
|
||||
return {
|
||||
getTagNames(myTags: string[]): string[] {
|
||||
return allTagNames.filter(n => !myTags.includes(n))
|
||||
},
|
||||
getTagValues(tagName: string): string[] {
|
||||
return allTags[tagName] ?? [];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { makeGrouping } from "./automationGrouper";
|
||||
import { AutomationData } from 'types/automations/index';
|
||||
import { 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)
|
||||
});
|
||||
@@ -0,0 +1,116 @@
|
||||
import { AutomationData } from "types/automations";
|
||||
|
||||
const makeGrouper = () => {
|
||||
const groupIdNames: string[] = []
|
||||
const groupSubGroups: number[][] = []
|
||||
const groupAutomations: number[][] = []
|
||||
const groupData: Array<{
|
||||
name: string;
|
||||
selected: boolean;
|
||||
}> = [];
|
||||
const topGroups: number[] = [];
|
||||
|
||||
return {
|
||||
groupIdNames,
|
||||
groupSubGroups,
|
||||
groupAutomations,
|
||||
groupData,
|
||||
topGroups,
|
||||
addNew(groupId: string, groupName: string, theChosenOne: boolean, isTopLevel: boolean): number {
|
||||
groupIdNames.push(groupId);
|
||||
groupSubGroups.push([]);
|
||||
groupAutomations.push([]);
|
||||
groupData.push({
|
||||
name: groupName,
|
||||
selected: theChosenOne,
|
||||
})
|
||||
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].push(groupIdx);
|
||||
}
|
||||
if (update.autoIdx !== null) {
|
||||
groupAutomations[groupIdx].push(update.autoIdx)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export type AutomationGrouper = ReturnType<typeof makeGrouping>;
|
||||
export const makeGrouping = (
|
||||
autos: AutomationData[],
|
||||
selectedTags: string[],
|
||||
selectedAutomationIdx: number,
|
||||
) => {
|
||||
const grouper = makeGrouper();
|
||||
|
||||
if (selectedTags.length === 0) {
|
||||
grouper.addNew("$", "", selectedAutomationIdx >= 0, true)
|
||||
grouper.groupAutomations = [autos.map((_, i) => i)];
|
||||
} else {
|
||||
autos.forEach((auto, autoIdx) => {
|
||||
const theChosenOne = selectedAutomationIdx === autoIdx;
|
||||
let lastGroupIdx: number | null = null;
|
||||
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 = `${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;
|
||||
} else {
|
||||
let groupIdx = grouper.groupIdNames.indexOf(`${selTag}:$`);
|
||||
if (groupIdx < 0) {
|
||||
groupIdx = grouper.addNew(`${selTag}:$`, "", theChosenOne, isTopLevel);
|
||||
}
|
||||
grouper.updateGroup(groupIdx, {
|
||||
isChosen: theChosenOne,
|
||||
lastGroupIdx,
|
||||
autoIdx,
|
||||
})
|
||||
break
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
console.log(grouper)
|
||||
return {
|
||||
top: grouper.topGroups,
|
||||
getSubGroups(groupIdx: number) {
|
||||
return grouper.groupSubGroups[groupIdx]
|
||||
},
|
||||
getAutomations(groupIdx: number) {
|
||||
return grouper.groupAutomations[groupIdx]
|
||||
},
|
||||
getData(groupIdx: number) {
|
||||
return grouper.groupData[groupIdx]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import { ApiService } from "apiService/core";
|
||||
import { AutomationListBox } from "./AutomationListBox";
|
||||
import { DAGAutomationFlowDims } from 'components/DAGFlow/types';
|
||||
import useWindowSize from "utils/useWindowSize";
|
||||
import { makeTagDB } from "./TagDB";
|
||||
|
||||
|
||||
interface AutomationListParams {
|
||||
@@ -34,6 +35,7 @@ export const AutomationList: FC<AutomationListParams> = ({
|
||||
const { isMobile } = useWindowSize();
|
||||
// alias
|
||||
const currentAuto = automations.length > 0 ? automations[current] : null;
|
||||
const tagDB = makeTagDB(automations);
|
||||
// render
|
||||
return <div className={["automation-list--root", isMobile ? 'mobile' : ''].join(' ')}>
|
||||
<div className={["automation-list--list", hideList ? "hide" : "show"].join(' ')}>
|
||||
@@ -64,6 +66,7 @@ export const AutomationList: FC<AutomationListParams> = ({
|
||||
automation={currentAuto}
|
||||
onUpdate={a => onUpdate(current, a)}
|
||||
dims={dims}
|
||||
tagDB={tagDB}
|
||||
/> : <></>}
|
||||
</div>
|
||||
{children}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import TextField from "@mui/material/TextField";
|
||||
import Autocomplete from "@mui/material/Autocomplete";
|
||||
|
||||
export interface InputAutoTextProps {
|
||||
label: string;
|
||||
value: string;
|
||||
options?: string[];
|
||||
error?: string;
|
||||
onChange: (v: string) => void;
|
||||
}
|
||||
export default function InputAutoText({
|
||||
value = "",
|
||||
label,
|
||||
options = [],
|
||||
onChange,
|
||||
error,
|
||||
}: InputAutoTextProps) {
|
||||
|
||||
return <Autocomplete
|
||||
freeSolo
|
||||
value={value}
|
||||
options={options}
|
||||
onChange={(_e, v) => onChange(
|
||||
v === null ? "" : v
|
||||
)}
|
||||
renderInput={params => <TextField
|
||||
{...params}
|
||||
variant="filled"
|
||||
label={label}
|
||||
error={error !== undefined}
|
||||
helperText={error}
|
||||
onChange={e => {
|
||||
e.preventDefault();
|
||||
onChange(
|
||||
e.target.value
|
||||
)
|
||||
}}
|
||||
/>}
|
||||
/>
|
||||
}
|
||||
@@ -42,3 +42,27 @@ export const EntityId: ComponentStory<typeof InputTextBubble> = args => {
|
||||
<InputEntity value={value} onChange={setValue} />
|
||||
</Page>
|
||||
}
|
||||
|
||||
|
||||
export const ManyOptions: ComponentStory<typeof InputTextBubble> = args => {
|
||||
|
||||
const [value, setValue] = useState<string | string[]>('hello')
|
||||
return <Page>
|
||||
<InputTextBubble {...args} value={value} onChange={setValue} />
|
||||
</Page>
|
||||
}
|
||||
|
||||
ManyOptions.args = {
|
||||
...ManyOptions.args,
|
||||
options: [
|
||||
'sensor.humidity_bathroom',
|
||||
'sensor.humidity_bedroom',
|
||||
'sensor.humidity_laundry',
|
||||
'sensor.temp_bathroom',
|
||||
'sensor.temp_bedroom',
|
||||
'switch.living_room',
|
||||
'switch.bathroom',
|
||||
'light.bedroom_main',
|
||||
'light.bedroom_second',
|
||||
]
|
||||
}
|
||||
@@ -1,55 +1,46 @@
|
||||
import { FC, useState } from "react";
|
||||
import InputText from "./InputText";
|
||||
import { useInputBubblesStyles } from "./styles";
|
||||
import Chip from "@mui/material/Chip";
|
||||
import TextField from "@mui/material/TextField";
|
||||
import Autocomplete from "@mui/material/Autocomplete";
|
||||
import { FC } from "react";
|
||||
|
||||
export interface Props {
|
||||
label: string;
|
||||
textBoxFor?: string;
|
||||
value: string | string[];
|
||||
options?: string[];
|
||||
onChange: (v: string[]) => void;
|
||||
additionalTooltipFilters?: Record<string, string>;
|
||||
}
|
||||
export default function InputTextBubble({
|
||||
value = "",
|
||||
label,
|
||||
textBoxFor,
|
||||
options = [],
|
||||
onChange,
|
||||
additionalTooltipFilters = {},
|
||||
}: Props) {
|
||||
const { classes } = useInputBubblesStyles({});
|
||||
let selected: string[];
|
||||
if (typeof value === 'string') {
|
||||
selected = [value]
|
||||
} else {
|
||||
selected = value
|
||||
}
|
||||
const [text, setText] = useState('');
|
||||
const addNew = () => {
|
||||
if (text) {
|
||||
onChange([text].concat(selected))
|
||||
setText("")
|
||||
}
|
||||
};
|
||||
|
||||
return <InputText
|
||||
label={label}
|
||||
value={text}
|
||||
textBoxFor={textBoxFor}
|
||||
onChange={t => setText(t)}
|
||||
onEnter={() => addNew()}
|
||||
additionalTooltipFilters={additionalTooltipFilters}
|
||||
>
|
||||
<div className={classes.bubbles}>
|
||||
{selected.map((s, i) => <div className={classes.bubble} onClick={() => onChange([
|
||||
...selected.slice(0, i),
|
||||
...selected.slice(i + 1),
|
||||
])}>
|
||||
{s}
|
||||
<span className={classes.deleteIcon}>X</span>
|
||||
</div>)}
|
||||
</div>
|
||||
<button className={classes.addBtn} onClick={() => addNew()}>+</button>
|
||||
</InputText>
|
||||
const selected = Array.isArray(value) ? value : [value];
|
||||
|
||||
return <Autocomplete
|
||||
multiple
|
||||
freeSolo
|
||||
value={selected}
|
||||
options={options}
|
||||
onChange={(_e, v) => onChange(
|
||||
v === null ? [] :
|
||||
Array.isArray(v) ? v : [v]
|
||||
)}
|
||||
renderInput={params => <TextField
|
||||
{...params}
|
||||
variant="filled"
|
||||
label={label}
|
||||
/>}
|
||||
renderTags={(tagValue, getTagProps) =>
|
||||
tagValue.map((option, index) => (
|
||||
<Chip
|
||||
label={option}
|
||||
{...getTagProps({ index })}
|
||||
/>
|
||||
))
|
||||
}
|
||||
/>
|
||||
}
|
||||
|
||||
export const InputEntity: FC<{
|
||||
@@ -62,5 +53,4 @@ export const InputEntity: FC<{
|
||||
value={value}
|
||||
onChange={onChange}
|
||||
label="Entity ID"
|
||||
textBoxFor="entity_id"
|
||||
/>
|
||||
@@ -1,6 +1,7 @@
|
||||
import { OptionManager } from './OptionManager';
|
||||
import { AutomationTriggerDevice } from 'types/automations/triggers';
|
||||
import InputText from 'components/Inputs/InputText';
|
||||
import { InputEntity } from 'components/Inputs/InputTextBubble';
|
||||
|
||||
|
||||
export const TriggerDevice: OptionManager<AutomationTriggerDevice> = {
|
||||
@@ -34,7 +35,7 @@ export const TriggerDevice: OptionManager<AutomationTriggerDevice> = {
|
||||
...state,
|
||||
device_id
|
||||
})} />
|
||||
<InputText label="Entity" value={state.entity_id ?? ""} onChange={entity_id => setState({
|
||||
<InputEntity value={state.entity_id ?? ""} onChange={entity_id => setState({
|
||||
...state,
|
||||
entity_id
|
||||
})} />
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import "./index.css";
|
||||
import { FC } from "react";
|
||||
|
||||
console.log(process.env)
|
||||
export const VersionBox: FC = () => {
|
||||
const version = process.env.REACT_APP_BUILD_VERSION ?? "n/a";
|
||||
return <div className="version-box">
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import * as st from 'superstruct'
|
||||
import { AutomationTimeString, AutomationTime } from './common'
|
||||
import { AutomationTimeString, AutomationTime, EntityId } from './common'
|
||||
|
||||
|
||||
const AutomationTriggerBase = {
|
||||
@@ -95,7 +95,7 @@ export const AutomationTriggerZone = st.type({
|
||||
export const AutomationTriggerDevice = st.type({
|
||||
...AutomationTriggerBase,
|
||||
platform: st.literal('device'),
|
||||
entity_id: st.optional(st.string()),
|
||||
entity_id: st.optional(EntityId),
|
||||
device_id: st.string(),
|
||||
domain: st.string(),
|
||||
type: st.string(),
|
||||
|
||||
Reference in new issue
Block a user