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 { TrashIcon } from "components/Icons"; export type Props = { automations: AutomationData[]; selected: number; onSelectAutomation: (i: number) => void; onRemove: (i: number) => void; onAdd: () => void; } export const AutomationListBox: FC = ({ automations, selected, onSelectAutomation, onAdd, onRemove, }) => { const tags = getTagList(automations); const [searchText, setSearchText] = useState(''); const [selectedTagIdx, setSelectedTagIdx] = useState([]); const filteredAutomations = filterAutomations(automations, searchText, selectedTagIdx, tags); const groups = groupAutomations(filteredAutomations.map(([a, _]) => a), selectedTagIdx, tags); return
{convertGroupsToItems(groups, filteredAutomations, onSelectAutomation, onRemove, selected)}
Showing Automations: {filteredAutomations.length} out of {automations.length}
} 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, selected: number[], tags: string[]): Array<[AutomationData, number]> => autos.map<[AutomationData, number]>((a, i) => [a, i]).filter(([a, i]) => { 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.tags[tags[i]]) { return false } } } return true }) export const groupAutomations = ( autos: AutomationData[], selected: number[], tags: string[], ) => { if (selected.length === 0) { return autos.map((_, i) => i); } const out: any = {}; for (let ai = 0; ai < autos.length; ai++) { const a = autos[ai]; 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.tags[tag]]) { where[a.tags[tag]] = {} } lastKey = a.tags[tag] } if (lastKey !== null) { if (!where[lastKey] || !Array.isArray(where[lastKey])) { where[lastKey] = [] } where[lastKey].push(ai) } } return out; } const convertGroupsToItems = ( groups: any, autos: Array<[AutomationData, number]>, onSelectAutomation: (i: number) => void, onRemove: (i: number) => void, selected: number, ) => { if (Array.isArray(groups)) { return groups.map(i => { const [auto, autoIndex] = autos[i]; return
onSelectAutomation(autoIndex)} >
{auto.metadata.alias.slice(0, 15)} ({auto.metadata.id.slice(0, 5)}) {auto.metadata.description.slice(0, 25)}
{Object.keys(auto.tags).map(tagName => {tagName}: {auto.tags[tagName]} )}
}) } else { return Object.keys(groups).map(groupName => { return }) } } const AutomationListBoxGroup: FC<{ groupName: string, groups: any, autos: Array<[AutomationData, number]>, onSelectAutomation: (i: number) => void, onRemove: (i: number) => void; selected: number, }> = ({ groupName, groups, autos, onSelectAutomation, onRemove, selected, }) => { const [open, setOpen] = useState(false); return
setOpen(!open)}>{groupName} {!open ? "⊕" : "⊖"} {open && convertGroupsToItems(groups, autos, onSelectAutomation, onRemove, selected)}
}