Automation.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { useEffect, useState } from "react";
  2. import { Button, Icon, Steps, Typography } from "tiny-ui";
  3. import { useLocalStorage } from "lib/state";
  4. const { Step } = Steps;
  5. const { Paragraph } = Typography;
  6. const Automation = ({
  7. clients,
  8. names,
  9. presets,
  10. automation,
  11. onAutomation,
  12. onPower,
  13. busy,
  14. ...props
  15. }) => {
  16. const [current, setCurrent] = useLocalStorage("preset", -1, 3600);
  17. const [total, setTotal] = useState();
  18. const [help, setHelp] = useState(false);
  19. const getTitle = (indx) => presets?.[indx]?.name || indx;
  20. const getName = (v) => {
  21. let indx = clients?.findIndex((o) => o === v);
  22. return indx > -1 ? names?.[indx] : v;
  23. };
  24. const getDescription = (indx) => {
  25. return (
  26. (automation?.[indx] &&
  27. Object.keys(automation?.[indx])
  28. ?.map((o) => getName(o))
  29. ?.join(", ")) ||
  30. null
  31. );
  32. };
  33. useEffect(() => {
  34. setTotal((automation && Object.keys(automation)?.length) || 0);
  35. }, [automation]);
  36. useEffect(() => {
  37. if (current > -1 && current < total) {
  38. onAutomation(current + 1);
  39. }
  40. }, [current, total]);
  41. let steps = Object.keys(automation).map((o, i) => {
  42. return (
  43. <Step
  44. key={i}
  45. title={
  46. <div style={{ display: "flex", flexDirection: "row" }}>
  47. <div>{getTitle(o)}</div>
  48. {current === o - 1 ? (
  49. <div style={{ marginLeft: "3rem" }}>
  50. <Button
  51. title="Reapply"
  52. round
  53. icon={<Icon name="loader-circle" />}
  54. onClick={() => {
  55. onAutomation(o);
  56. }}
  57. />
  58. </div>
  59. ) : null}
  60. </div>
  61. }
  62. description={getDescription(o)}
  63. ></Step>
  64. );
  65. });
  66. return (
  67. <>
  68. <div style={{ position: "sticky", top: "60px", marginBottom: "2rem" }}>
  69. <Button.Group size="md" round>
  70. <Button
  71. title="Previous"
  72. icon={<Icon name="arrow-left" />}
  73. onClick={() => {
  74. let next = current - 1;
  75. if (next < 0) next = 0;
  76. setCurrent(next);
  77. }}
  78. />
  79. <Button
  80. title="Reapply"
  81. icon={<Icon name="loader-circle" />}
  82. onClick={() => {
  83. onAutomation(current + 1);
  84. }}
  85. />
  86. <Button
  87. title="Next"
  88. icon={<Icon name="arrow-right" />}
  89. onClick={() => {
  90. let next = current + 1;
  91. if (next > total) next = 0;
  92. setCurrent(next);
  93. }}
  94. />
  95. </Button.Group>
  96. <Button.Group size="md" round>
  97. <Button
  98. title="Power on"
  99. onClick={() => {
  100. onPower("on");
  101. }}
  102. >
  103. On
  104. </Button>
  105. <Button
  106. title="Power off"
  107. onClick={() => {
  108. onPower("off");
  109. }}
  110. >
  111. Off
  112. </Button>
  113. </Button.Group>
  114. <Button.Group size="md" round>
  115. <Button
  116. title="Help"
  117. icon={<Icon name="question-fill" />}
  118. onClick={() => {
  119. setHelp(true);
  120. }}
  121. />
  122. </Button.Group>
  123. {(busy && (
  124. <Button.Group size="md" round>
  125. <Button title="Busy" icon={<Icon spin name="loader-3quarter" />} />
  126. </Button.Group>
  127. )) ||
  128. null}
  129. </div>
  130. <Steps
  131. current={current}
  132. direction="vertical"
  133. onChange={(v) => setCurrent(v)}
  134. >
  135. {steps}
  136. </Steps>
  137. </>
  138. );
  139. };
  140. export default Automation;