diff --git a/src/js/app.tsx b/src/js/app.tsx index 7dfe717..54806f0 100644 --- a/src/js/app.tsx +++ b/src/js/app.tsx @@ -1,92 +1,61 @@ import { createRoot } from "react-dom/client"; -import React from "react"; import { Menu } from "./menu"; -import { createContext, useState } from "react"; +import { createContext, useEffect, useState } from "react"; import { MediaContainer } from "./media"; import { MinistreamApiClient } from "./api"; +import React from "react"; -type ThemeContextType = "light" | "dark"; - -const ThemeContext = createContext("light"); - -type AppState = { - streamList: string[] - selectedStream: string | undefined +interface AppProps { + api: MinistreamApiClient } -type AppProps = {} -export class App extends React.Component { - apiClient: MinistreamApiClient - constructor(props: AppProps) { - super(props) - this.state = { - streamList: [], - selectedStream: undefined - } - this.apiClient = new MinistreamApiClient() - this.updateSelect = this.updateSelect.bind(this) +export function App({ api }: AppProps) { + const [streamList, setStreamList] = useState([]) + const [selectedStream, setSelectedStream] = useState(null) + + const updateSelect = (item: string) => { + setSelectedStream(item) } - async componentDidMount(): Promise { - await this.updateStreamList() - setInterval(() => { - this.updateStreamList() - }, 10000) - } - - async updateStreamList() { - const streams = await this.apiClient.listStreams() - const sortedStreams = streams.sort((a, b) => { - if (a > b) { - return -1 - } else if (a < b) { - return 1 + const updateStreamList = () => { + api.listStreams().then((list) => { + setStreamList(list) + if (list.length != streamList.length) { + setStreamList(list) } - return 0 - }) - if (sortedStreams.length != this.state.streamList.length) { - this.setStreamList(streams) - } - - if (!sortedStreams.every((_, idx) => { - return sortedStreams[idx] === this.state.streamList[idx] - })) { - this.setStreamList(streams) - } - } - - setStreamList(streamList: string[]) { - console.log("stream list updated") - this.setState((state) => { - return { selectedStream: state.selectedStream, streamList: streamList } + if (!list.every((_, idx) => { + return list[idx] === streamList[idx] + })) { + setStreamList(list) + } }) } - updateSelect(selected: string): void { - if (selected !== this.state.selectedStream) { - this.setState((state) => { - return { streamList: state.streamList, selectedStream: selected } - }) - } - } + setInterval(() => { + updateStreamList() + }, 10000) - render() { - return ( - <> - - - - ) - } + useEffect(() => { + updateStreamList() + },[]) + + + return ( + <> + + + + ) } const rootElement = document.getElementById("app"); if (rootElement) { const root = createRoot(rootElement); - root.render() + const api = new MinistreamApiClient() + root.render() } \ No newline at end of file diff --git a/src/js/media.tsx b/src/js/media.tsx index 2ea5df7..c76a9d7 100644 --- a/src/js/media.tsx +++ b/src/js/media.tsx @@ -1,9 +1,10 @@ -import React, { ComponentProps, useRef, useEffect, useState } from "react" +import { ComponentProps, useRef, useEffect, useState } from "react" import { MinistreamApiClient } from "./api" import { resolve } from "path" +import React from "react" type MediaContainerProps = { - selectedStream: string | undefined + selectedStream: string | null api: MinistreamApiClient }