"use client"; import { useEffect, useState } from "react"; import useSWR from "swr"; import { Button, Icon, Steps } from "tiny-ui"; import { fetcher, isArray, isEmpty } from "lib/utils"; import { useScreenSize } from "components"; const { Step } = Steps; const Automation = ({ clients, names, presets, automation, onAutomation, onPower, onPreset, busy }) => { const [current, setCurrent] = useState(-1); const [preset, setPreset] = useState(); const [total, setTotal] = useState(); const { data, mutate } = useSWR("/api/state", fetcher); const screenSize = useScreenSize(); 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 ); }; useEffect(() => { setTotal((automation && Object.keys(automation)?.length) || 0); }, [automation]); useEffect(() => { if (current > -1 && current < total && (!preset || preset !== current)) { onAutomation(current + 1, () => { mutate(); }); } }, [current, total]); useEffect(() => { if (preset && preset !== current) setCurrent(preset); }, [preset]); useEffect(() => { if (isArray(data) && !isEmpty(data)) { let ps = [...new Set(data?.map((o) => o?.ps))]; if (ps.length === 1 && ps?.[0] !== 200) setPreset(ps?.[0] - 1); } }, [data]); let steps = Object.keys(automation).map((o, i) => { return (
{getTitle(o)}
{current === o - 1 ? (
) : null} } description={getDescription(o)} >
); }); return ( <>
550 ? "lg" : "md"} round>
setCurrent(v)} > {steps}
); }; export default Automation;