page.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. export default function Home() {
  9. const [names, setNames] = useState(null);
  10. const [clients, setClients] = useState(null);
  11. const [busy, setBusy] = useState(false);
  12. useEffect(() => {
  13. let c = [];
  14. let n = [];
  15. for (let client of Object.keys(config)) {
  16. c.push(client);
  17. n.push(config[client]);
  18. }
  19. setClients(c);
  20. setNames(n);
  21. }, []);
  22. const handleAutomation = (id) => {
  23. setBusy(true);
  24. fetch(`/api/automation/${id}`)
  25. .then((resp) => resp && resp.json())
  26. .then((result) => {
  27. setBusy(false);
  28. Notification({
  29. title: `Automation set to ${id}`,
  30. description: (
  31. <>
  32. {result?.map((o, i) => (
  33. <div key={i} style={{ margin: "0 0 1rem 0" }}>
  34. <strong>
  35. {names[i]} ({clients[i]})
  36. </strong>
  37. {o &&
  38. Object.keys(o)?.map((v, k) => (
  39. <Tag key={k}>
  40. <strong>{v}: </strong> {JSON.stringify(o[v])}
  41. </Tag>
  42. ))}
  43. </div>
  44. ))}
  45. </>
  46. )
  47. });
  48. })
  49. .catch((err) => {
  50. setBusy(false);
  51. });
  52. };
  53. const handlePower = (value) => {
  54. setBusy(true);
  55. fetch(`/api/power/${value}`)
  56. .then((resp) => resp && resp.json())
  57. .then((result) => {
  58. setBusy(false);
  59. Notification({
  60. title: `Power set to ${value}`,
  61. description: (
  62. <>
  63. {result?.map((o, i) => (
  64. <div key={i} style={{ margin: "0 0 1rem 0" }}>
  65. <strong>
  66. {names[i]} ({clients[i]})
  67. </strong>
  68. {o &&
  69. Object.keys(o)?.map((v, k) => (
  70. <Tag key={k}>
  71. <strong>{v}: </strong> {JSON.stringify(o[v])}
  72. </Tag>
  73. ))}
  74. </div>
  75. ))}
  76. </>
  77. )
  78. });
  79. })
  80. .catch((err) => {
  81. setBusy(false);
  82. });
  83. };
  84. return (
  85. <Layout page="home">
  86. <Row style={{ backgroundColor: "#fff" }}>
  87. <Col style={{ margin: "2rem" }}>
  88. <Automation
  89. automation={automation}
  90. clients={clients}
  91. names={names}
  92. presets={presets}
  93. onAutomation={handleAutomation}
  94. onPower={handlePower}
  95. busy={busy}
  96. />
  97. </Col>
  98. </Row>
  99. </Layout>
  100. );
  101. return;
  102. }