import "./index.css"; import { FC, useState } from "react"; import { MetadataBox } from "../MetadataBox"; import { AutomationManagerAutoUpdatable, AutomationManagerItem, } from "../types"; import { TagDB } from "../TagDB"; export type ListBoxGroupProps = AutomationManagerItem & { onAutomationUpdate: ( a: AutomationManagerAutoUpdatable, aid: string, eid: string ) => void; onAutomationDelete: (aid: string) => void; onSelect: (aid: string) => void; initialOpenState?: boolean; tagsDB: TagDB; }; export const ListBoxGroup: FC = ({ title, type, data, isSelected, initialOpenState, tagsDB, ...events }) => { const [open, setOpen] = useState(initialOpenState ?? false); const openIcon = !open ? "⊕" : "⊖"; const totalAutomations = countAutomations({ title, type, data, } as any); return (
setOpen(!open)} className="automation-manager--list-box-group--title" > {title} {openIcon} ( {totalAutomations} automation {totalAutomations === 1 ? "" : "s"})
{type === "items" ? data.map((auto) => ( events.onAutomationDelete(auto.id)} onSelect={() => events.onSelect(auto.id)} onDescriptionUpdate={(description) => events.onAutomationUpdate( { ...auto, description, }, auto.id, auto.entityId ) } onTitleUpdate={(title) => events.onAutomationUpdate( { ...auto, title, }, auto.id, auto.entityId ) } onStateUpdate={(state) => events.onAutomationUpdate( { ...auto, state, }, auto.id, auto.entityId ) } /> )) : data.map((group, i) => ( ))}
); }; const countAutomations = (item: AutomationManagerItem): number => { if (item.type === "items") { return item.data.length; } else { return item.data.reduce((total = 0, n) => total + countAutomations(n), 0); } };