From 05931eb8e92c13aff48f03ce2857b9eb19922bbf Mon Sep 17 00:00:00 2001 From: Stein Ivar Berghei Date: Fri, 27 Oct 2023 11:35:04 +0200 Subject: [PATCH] fixup! Time for React! --- src/app/controls.tsx | 98 ++++++++++++++++++++++++++++++++++++++++++++ src/app/events.tsx | 2 +- src/app/playlist.tsx | 95 ++++++++++++++++++++++++++++++++---------- src/app/root.tsx | 2 + 4 files changed, 175 insertions(+), 22 deletions(-) create mode 100644 src/app/controls.tsx diff --git a/src/app/controls.tsx b/src/app/controls.tsx new file mode 100644 index 0000000..6f7ed99 --- /dev/null +++ b/src/app/controls.tsx @@ -0,0 +1,98 @@ +import React, { useState } from "react"; +import CSS from "csstype"; +import { on } from "./events"; +import ws from "./ws"; + +export default function Controls() { + const [channel, setChannel] = useState(""); + const [title, setTitle] = useState(""); + const [pos, setPos] = useState(0); + const [len, setLen] = useState(0); + const [song, setSong] = useState(""); + const [pause, setPause] = useState(true); + + const linkStyle: CSS.Properties = { + textDecoration: "none", + }; + + const Next = () => { + ws.send( + JSON.stringify({ + event: "next", + }) + ); + }; + + const Prev = () => { + ws.send( + JSON.stringify({ + event: "next", + }) + ); + }; + + const msToTime = (duration: number) => { + var milliseconds = (duration % 1000) / 100, + seconds = Math.floor((duration / 1000) % 60), + minutes = Math.floor((duration / (1000 * 60)) % 60), + minutes = minutes < 10 ? 0 + minutes : minutes; + seconds = seconds < 10 ? 0 + seconds : seconds; + + return ( + minutes.toString().padStart(2, "0") + + ":" + + seconds.toString().padStart(2, "0") + ); + }; + + on("dnd:song_info", (e: any) => { + setChannel(e.detail.channel); + setTitle(e.detail.current); + setPos(e.detail.position); + setLen(e.detail.len); + setSong("https://youtu.be/" + e.detail.song); + setPause(e.detail.pause); + }); + + on("dnd:song_position", (e: any) => { + setPos(e.detail.position); + setLen(e.detail.len); + }); + + return ( +
+
+ + {channel} + - + {title} + + +
+ + + {msToTime(pos)} / {msToTime(len)} + + +
+
+
+ ); +} diff --git a/src/app/events.tsx b/src/app/events.tsx index 456ea09..2e852b2 100644 --- a/src/app/events.tsx +++ b/src/app/events.tsx @@ -9,7 +9,7 @@ function on(eventType: any, listener: { (event: any): void; (this: Document, ev: function once(eventType: any, listener: (arg0: any) => void) { on(eventType, handleEventOnce); - function handleEventOnce(event) { + function handleEventOnce(event: any) { listener(event); off(eventType, handleEventOnce); } diff --git a/src/app/playlist.tsx b/src/app/playlist.tsx index e59bc80..d5e7654 100644 --- a/src/app/playlist.tsx +++ b/src/app/playlist.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState, useCallback } from "react"; +import React, { useEffect, useState } from "react"; import CSS from "csstype"; import { on } from "./events"; import ws from "./ws"; @@ -11,6 +11,9 @@ export default function Playlists() { const [playlists, setPlaylists] = useState([]); const [active, setActive] = useState(""); + const [title, setTitle] = useState(""); + const [url, setUrl] = useState(""); + const [output, setOutput] = useState(""); const activeStyle: CSS.Properties = { backgroundColor: "burlywood", @@ -31,7 +34,6 @@ export default function Playlists() { }, []); const Play = (e: any) => { - console.log(e.target.dataset); ws.send( JSON.stringify({ event: "load_playlist", @@ -48,30 +50,81 @@ export default function Playlists() { ); }; + const AddPlaylist = () => { + if (title == "" || url == "") { + setOutput("Title or Url is empty!"); + } + + ws.send( + JSON.stringify({ + event: "add_playlist", + payload: { + title: title, + url: url, + }, + }) + ); + }; + on("dnd:song_info", (e: any) => { setActive(e.detail.playlist); }); + on("dnd:new_playlist", (e: any) => { + fetchPlaylists(); + setOutput("New playlist was added: " + e.details.title); + }); + return ( -
-
- {playlists.map((playlist) => { - return ( -
- {playlist.Title} -
- ); - })} -
- Stop + <> +
+
+ {playlists.map((playlist) => { + return ( +
+ {playlist.Title} +
+ ); + })} +
+ Stop +
-
-
+ +
+
+ setTitle(e.target.value)} + /> + setUrl(e.target.value)} + /> + +
+
+
+

{output}

+
+ ); } diff --git a/src/app/root.tsx b/src/app/root.tsx index 1feb12f..804dddf 100644 --- a/src/app/root.tsx +++ b/src/app/root.tsx @@ -3,6 +3,7 @@ import { createRoot } from "react-dom/client"; import { trigger, on } from "./events"; import Playlists from "./playlist"; import ws from "./ws"; +import Controls from "./controls"; function Content() { ws.onmessage = (e) => { @@ -14,6 +15,7 @@ function Content() { <>

Playlists

+ ); }