page.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use client";
  2. import { useEffect, useState } from "react";
  3. import { Col, Loader, Row } from "tiny-ui";
  4. import { WLEDClient } from "wled-client";
  5. import { Automation, Layout } from "components";
  6. import automation from "data/automation.json";
  7. import presets from "data/presets.json";
  8. const CLIENTS = process.env.CLIENTS || [];
  9. const NAMES = process.env.NAMES || [];
  10. export default function Home() {
  11. const [names, setNames] = useState(null);
  12. const [clients, setClients] = useState(null);
  13. const [connections, setConnections] = useState(null);
  14. useEffect(() => {
  15. let t = [];
  16. let c = [];
  17. let n = [];
  18. for (let client of CLIENTS) {
  19. c.push(client);
  20. t.push(new WLEDClient(client));
  21. }
  22. for (let name of NAMES) {
  23. n.push(name);
  24. }
  25. setClients(c);
  26. setConnections(t);
  27. setNames(n);
  28. }, []);
  29. const handlePreset = async (payload) => {
  30. if (payload?.clients && payload?.value) {
  31. for (let client of payload?.clients) {
  32. let indx = clients.findIndex((o) => o === client);
  33. if (connections[indx]) {
  34. let wled = connections[indx];
  35. await wled.setPreset(payload?.value);
  36. await wled.reinit();
  37. }
  38. }
  39. }
  40. };
  41. return (
  42. <Layout page="home">
  43. <Row style={{ backgroundColor: "#fff" }}>
  44. <Col style={{ margin: "2rem" }}>
  45. {(connections && clients && (
  46. <Automation
  47. clients={clients}
  48. connections={connections}
  49. names={names}
  50. presets={presets}
  51. automation={automation}
  52. onPreset={handlePreset}
  53. />
  54. )) || <Loader size="lg" />}
  55. </Col>
  56. </Row>
  57. </Layout>
  58. );
  59. return;
  60. }