Automation.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. "use client";
  2. import { useEffect, useState } from "react";
  3. import useSWR from "swr";
  4. import { Button, Icon, Steps } from "tiny-ui";
  5. import { fetcher, isArray, isEmpty } from "lib/utils";
  6. import { useScreenSize } from "components";
  7. const { Step } = Steps;
  8. const Automation = ({
  9. clients,
  10. names,
  11. presets,
  12. automation,
  13. onAutomation,
  14. onPower,
  15. onPreset,
  16. busy
  17. }) => {
  18. const [current, setCurrent] = useState(-1);
  19. const [preset, setPreset] = useState();
  20. const [total, setTotal] = useState();
  21. const { data, mutate } = useSWR("/api/state", fetcher);
  22. const screenSize = useScreenSize();
  23. const getTitle = (indx) => presets?.[indx]?.name || indx;
  24. const getName = (v) => {
  25. let indx = clients?.findIndex((o) => o === v);
  26. return indx > -1 ? names?.[indx] : v;
  27. };
  28. const getDescription = (indx) => {
  29. return (
  30. (automation?.[indx] &&
  31. Object.keys(automation?.[indx])
  32. ?.map((o) => getName(o))
  33. ?.join(", ")) ||
  34. null
  35. );
  36. };
  37. useEffect(() => {
  38. setTotal((automation && Object.keys(automation)?.length) || 0);
  39. }, [automation]);
  40. useEffect(() => {
  41. if (current > -1 && current < total && (!preset || preset !== current)) {
  42. onAutomation(current + 1, () => {
  43. mutate();
  44. });
  45. }
  46. }, [current, total]);
  47. useEffect(() => {
  48. if (preset && preset !== current) setCurrent(preset);
  49. }, [preset]);
  50. useEffect(() => {
  51. if (isArray(data) && !isEmpty(data)) {
  52. let ps = [...new Set(data?.map((o) => o?.ps))];
  53. if (ps.length === 1 && ps?.[0] !== 200) setPreset(ps?.[0] - 1);
  54. }
  55. }, [data]);
  56. let steps = Object.keys(automation).map((o, i) => {
  57. return (
  58. <Step
  59. key={i}
  60. title={
  61. <div style={{ display: "flex", flexDirection: "row" }}>
  62. <div>{getTitle(o)}</div>
  63. {current === o - 1 ? (
  64. <div style={{ marginLeft: "3rem" }}>
  65. <Button
  66. title="Reapply"
  67. round
  68. icon={<Icon name="loader-circle" />}
  69. onClick={() => {
  70. onAutomation(o, () => {
  71. mutate();
  72. });
  73. }}
  74. />
  75. </div>
  76. ) : null}
  77. </div>
  78. }
  79. description={getDescription(o)}
  80. ></Step>
  81. );
  82. });
  83. return (
  84. <>
  85. <div className="commandbar">
  86. <Button.Group size={screenSize?.width > 550 ? "lg" : "md"} round>
  87. <Button
  88. title="Previous"
  89. icon={<Icon name="arrow-left" />}
  90. onClick={() => {
  91. let next = current - 1;
  92. if (next < 0) next = 0;
  93. setCurrent(next);
  94. }}
  95. />
  96. <Button
  97. title="Reapply"
  98. icon={<Icon name="loader-circle" />}
  99. onClick={() => {
  100. onAutomation(current + 1, () => {
  101. mutate();
  102. });
  103. }}
  104. />
  105. <Button
  106. title="Next"
  107. icon={<Icon name="arrow-right" />}
  108. onClick={() => {
  109. let next = current + 1;
  110. if (next > total) next = 0;
  111. setCurrent(next);
  112. }}
  113. />
  114. <Button
  115. title="Initial state"
  116. icon={<Icon name="no-idea" />}
  117. onClick={() => {
  118. setCurrent(-1);
  119. setPreset(null);
  120. onPreset(200, () => {
  121. mutate();
  122. });
  123. }}
  124. />
  125. <Button
  126. title="Power on"
  127. onClick={() => {
  128. onPower("on", () => {
  129. mutate();
  130. });
  131. }}
  132. >
  133. On
  134. </Button>
  135. <Button
  136. title="Power off"
  137. onClick={() => {
  138. onPower("off", () => {
  139. mutate();
  140. });
  141. }}
  142. >
  143. Off
  144. </Button>
  145. </Button.Group>
  146. </div>
  147. <div>
  148. <Steps
  149. current={current}
  150. direction="vertical"
  151. onChange={(v) => setCurrent(v)}
  152. >
  153. {steps}
  154. </Steps>
  155. </div>
  156. </>
  157. );
  158. };
  159. export default Automation;