page.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. "use client";
  2. import { useEffect, useState } from "react";
  3. import { Col, Row, Tag } from "tiny-ui";
  4. import { Automation, Layout, Notification } from "components";
  5. import automation from "data/automation.json";
  6. import config from "data/config.json";
  7. import presets from "data/presets.json";
  8. import { isFunction } from "lib/utils";
  9. export default function Home() {
  10. const [names, setNames] = useState(null);
  11. const [clients, setClients] = useState(null);
  12. const [busy, setBusy] = useState(false);
  13. useEffect(() => {
  14. let c = [];
  15. let n = [];
  16. for (let client of Object.keys(config)) {
  17. c.push(client);
  18. n.push(config[client]);
  19. }
  20. setClients(c);
  21. setNames(n);
  22. }, []);
  23. const handlePreset = (id, cb) => {
  24. setBusy(true);
  25. fetch(`/api/preset/${id}`)
  26. .then((resp) => resp && resp.json())
  27. .then((result) => {
  28. setBusy(false);
  29. Notification({
  30. title: `Preset set to ${id}`,
  31. description: (
  32. <>
  33. {result?.map((o, i) => (
  34. <div key={i} className="notification">
  35. <strong>
  36. {names[i]} ({clients[i]})
  37. </strong>
  38. {o &&
  39. Object.keys(o)?.map((v, k) => (
  40. <Tag key={k}>
  41. <strong>{v}: </strong> {JSON.stringify(o[v])}
  42. </Tag>
  43. ))}
  44. </div>
  45. ))}
  46. </>
  47. )
  48. });
  49. if (isFunction(cb)) cb();
  50. })
  51. .catch((err) => {
  52. setBusy(false);
  53. });
  54. };
  55. const handleAutomation = (id, cb) => {
  56. setBusy(true);
  57. fetch(`/api/automation/${id}`)
  58. .then((resp) => resp && resp.json())
  59. .then((result) => {
  60. setBusy(false);
  61. Notification({
  62. title: `Automation set to ${id}`,
  63. description: (
  64. <>
  65. {result?.map((o, i) => (
  66. <div key={i} className="notification">
  67. <strong>
  68. {names[i]} ({clients[i]})
  69. </strong>
  70. {o &&
  71. Object.keys(o)?.map((v, k) => (
  72. <Tag key={k}>
  73. <strong>{v}: </strong> {JSON.stringify(o[v])}
  74. </Tag>
  75. ))}
  76. </div>
  77. ))}
  78. </>
  79. )
  80. });
  81. if (isFunction(cb)) cb();
  82. })
  83. .catch((err) => {
  84. setBusy(false);
  85. });
  86. };
  87. const handlePower = (value, cb) => {
  88. setBusy(true);
  89. fetch(`/api/power/${value}`)
  90. .then((resp) => resp && resp.json())
  91. .then((result) => {
  92. setBusy(false);
  93. Notification({
  94. title: `Power set to ${value}`,
  95. description: (
  96. <>
  97. {result?.map((o, i) => (
  98. <div key={i} className="notification">
  99. <strong>
  100. {names[i]} ({clients[i]})
  101. </strong>
  102. {o &&
  103. Object.keys(o)?.map((v, k) => (
  104. <Tag key={k}>
  105. <strong>{v}: </strong> {JSON.stringify(o[v])}
  106. </Tag>
  107. ))}
  108. </div>
  109. ))}
  110. </>
  111. )
  112. });
  113. if (isFunction(cb)) cb();
  114. })
  115. .catch((err) => {
  116. setBusy(false);
  117. });
  118. };
  119. return (
  120. <Layout page="home">
  121. <Row>
  122. <Col style={{ margin: "2rem" }}>
  123. <Automation
  124. automation={automation}
  125. clients={clients}
  126. names={names}
  127. presets={presets}
  128. onAutomation={handleAutomation}
  129. onPower={handlePower}
  130. onPreset={handlePreset}
  131. busy={busy}
  132. />
  133. </Col>
  134. </Row>
  135. </Layout>
  136. );
  137. return;
  138. }