import { useEffect, useState } from "react"; import { Button, Drawer, Icon, Steps, Typography } from "tiny-ui"; import { useLocalStorage } from "lib/state"; import { isBlank } from "lib/utils"; const { Step } = Steps; const { Heading, Paragraph, Text } = Typography; const Automation = ({ automation, presets, clients, names, onPreset, onSync, onPower, ...props }) => { const [current, setCurrent] = useLocalStorage(-1); const [total, setTotal] = useState(); const [visible, setVisible] = useState(false); const getTitle = (indx) => presets?.[indx]?.name || indx; const getName = (v) => { let indx = clients?.findIndex((o) => o === v); return indx > -1 ? names?.[indx] : v; }; const getDescription = (indx) => { return ( (automation?.[indx] && Object.keys(automation?.[indx]) ?.map((o) => getName(o)) ?.join(", ")) || null ); }; const handleChange = (indx) => { let keys = Object.keys(automation?.[indx]); let preset = { clients: keys || [], value: indx }; let power = { on: [], off: [] }; let sync = { enable: [], disable: [] }; // loop the keys and determine what to do for (let key of keys) { if (automation?.[indx]?.[key]?.on === true) power.on.push(key); else power.off.push(key); if (automation?.[indx]?.[key]?.sync === true) sync.enable.push(key); else sync.disable.push(key); } if (!isBlank(power?.on)) onPower({ clients: power?.on, value: "on" }); // power on if (!isBlank(power?.off)) onPower({ clients: power?.off, value: "off" }); // power off if (!isBlank(sync?.enable)) onSync({ clients: sync?.enable, value: "enable" }); // enable sync if (!isBlank(sync?.disable)) onSync({ clients: sync?.disable, value: "disable" }); // disable sync onPreset(preset); // apply the preset }; useEffect(() => { setTotal((automation && Object.keys(automation)) || 0); }, [automation]); useEffect(() => { if (current > -1 && current < total) handleChange(current + 1); }, [current]); let steps = Object.keys(automation).map((o, i) => { return ( {getTitle(o)} {current === o - 1 ? (
) : null} } description={getDescription(o)} >
); }); return ( <>
setCurrent(v)} > {steps} setVisible(false)} visible={visible} > <> Select one of the presets from the left to start applying preset automation. You can use the navigation buttons above to advance forward{" "} and backward{" "} through the preset list. If a preset fails to apply to all of the intended WLED instances then use the Reapply button {" "} within the navigation menu bar or within the target step. ); }; export default Automation;