latest work
This commit is contained in:
1 parent
acb12e73e8
commit
b368be0ff4
8 files changed
+228
-25
No files matched your search
@@ -0,0 +1,3 @@
|
||||
.automation-list-box {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import { groupAutomations } from './AutomationListBox';
|
||||
import { AutomationData } from 'types/automations/index';
|
||||
|
||||
test('converting time to a string', () => {
|
||||
const automations: AutomationData[] = [
|
||||
{
|
||||
metadata: {
|
||||
id: '1',
|
||||
alias: '',
|
||||
description: '',
|
||||
mode: 'single',
|
||||
tags: {
|
||||
"Room": 'Living Room',
|
||||
"Type": 'Lights'
|
||||
}
|
||||
},
|
||||
sequence: [],
|
||||
trigger: [],
|
||||
},
|
||||
{
|
||||
metadata: {
|
||||
id: '2',
|
||||
alias: '',
|
||||
description: '',
|
||||
mode: 'single',
|
||||
tags: {
|
||||
"Room": 'Living Room',
|
||||
"Type": 'Climate'
|
||||
}
|
||||
},
|
||||
sequence: [],
|
||||
trigger: [],
|
||||
},
|
||||
{
|
||||
metadata: {
|
||||
id: '3',
|
||||
alias: '',
|
||||
description: '',
|
||||
mode: 'single',
|
||||
tags: {
|
||||
"Room": 'Kitchen',
|
||||
"Type": 'Climate'
|
||||
}
|
||||
},
|
||||
sequence: [],
|
||||
trigger: [],
|
||||
},
|
||||
];
|
||||
expect(groupAutomations(automations, [0, 1], ['Room', "Type"])).toBe({
|
||||
"Living Room": {
|
||||
"Lights": ["1"],
|
||||
"Climate": ["2"],
|
||||
},
|
||||
"Kitchen": {
|
||||
"Climate": ["3"],
|
||||
}
|
||||
})
|
||||
expect(groupAutomations(automations, [1, 0], ['Room', "Type"])).toBe({
|
||||
"Lights": {
|
||||
"Living Room": ['1'],
|
||||
},
|
||||
"Climate": {
|
||||
"Living Room": ['2'],
|
||||
"Kitchen": ['3']
|
||||
}
|
||||
})
|
||||
expect(groupAutomations(automations, [1], ['Room', "Type"])).toBe({
|
||||
"Lights": ['1'],
|
||||
"Climate": ['2', '3']
|
||||
})
|
||||
expect(groupAutomations(automations, [0], ['Room', "Type"])).toBe({
|
||||
"Living Room": ['1', '2'],
|
||||
"Kitchen": ['3']
|
||||
})
|
||||
});
|
||||
@@ -0,0 +1,93 @@
|
||||
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";
|
||||
|
||||
|
||||
export type Props = {
|
||||
automations: AutomationData[];
|
||||
}
|
||||
|
||||
export const AutomationListBox: FC<Props> = ({
|
||||
automations
|
||||
}) => {
|
||||
|
||||
const tags = getTagList(automations);
|
||||
const [searchText, setSearchText] = useState('');
|
||||
const [selected, setSelected] = useState<number[]>([]);
|
||||
const filteredAutomations = filterAutomations(automations, searchText, selected, tags);
|
||||
|
||||
|
||||
return <div className="automation-list-box">
|
||||
<div className="automation-list-box--nav">
|
||||
<InputText
|
||||
label="Search"
|
||||
value={searchText}
|
||||
onChange={setSearchText}
|
||||
/>
|
||||
<InputMultiSelect
|
||||
label="Tags"
|
||||
selected={selected}
|
||||
onChange={setSelected}
|
||||
options={tags}
|
||||
/>
|
||||
</div>
|
||||
<div className="automation-list-box--body">
|
||||
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
|
||||
const getTagList = (autos: AutomationData[]): string[] => autos
|
||||
.map(a => Object.keys(a.metadata.tags))
|
||||
.reduce((all, keys) => all.concat(keys))
|
||||
|
||||
const filterAutomations = (autos: AutomationData[], searchText: string, selected: number[], tags: string[]) => autos.filter(a => {
|
||||
if (searchText.length > 0) {
|
||||
if (
|
||||
!(
|
||||
a.metadata.description.toLocaleLowerCase().includes(searchText) ||
|
||||
a.metadata.alias.toLocaleLowerCase().includes(searchText)
|
||||
)
|
||||
) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if (selected.length > 0) {
|
||||
for (const i of selected) {
|
||||
if (!a.metadata.tags[tags[i]]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
export type GroupedAutos = Record<string, GroupedAutos | string[]>;
|
||||
export const groupAutomations = (autos: AutomationData[], selected: number[], tags: string[]) => {
|
||||
const out: GroupedAutos = {};
|
||||
for (const a of autos) {
|
||||
let where = out;
|
||||
let tag: string = '';
|
||||
let lastKey: string | null = null;
|
||||
for (const i of selected) {
|
||||
tag = tags[i];
|
||||
if (lastKey !== null) {
|
||||
where = where[lastKey]
|
||||
}
|
||||
if (!where[a.metadata.tags[tag]]) {
|
||||
where[a.metadata.tags[tag]] = {}
|
||||
}
|
||||
lastKey = a.metadata.tags[tag]
|
||||
}
|
||||
if (tag) {
|
||||
if (!where[a.metadata.tags[tag]]) {
|
||||
where[a.metadata.tags[tag]] = []
|
||||
}
|
||||
where[a.metadata.tags[tag]].push(a.metadata.id)
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
File renamed without changes.
@@ -1,4 +1,4 @@
|
||||
import "./styles.css";
|
||||
import "./index.css";
|
||||
import { AutomationEditor } from "components/AutomationEditor";
|
||||
import { Button } from "components/Inputs/Button";
|
||||
import { FC, useState } from "react";
|
||||
|
||||
Reference in new issue
Block a user