Automation.js 3.3 KB

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