tag autocomplete

This commit is contained in:
ari committed 2022-05-26 21:49:28 -04:00
1 parent fb8eb4f38c
commit 5729b74c89
22 files changed
+515 -191

No files matched your search

@@ -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}