Set title and convert all class component to functional #4
111
src/js/app.tsx
111
src/js/app.tsx
@ -1,92 +1,61 @@
|
|||||||
import { createRoot } from "react-dom/client";
|
import { createRoot } from "react-dom/client";
|
||||||
import React from "react";
|
|
||||||
import { Menu } from "./menu";
|
import { Menu } from "./menu";
|
||||||
import { createContext, useState } from "react";
|
import { createContext, useEffect, useState } from "react";
|
||||||
import { MediaContainer } from "./media";
|
import { MediaContainer } from "./media";
|
||||||
import { MinistreamApiClient } from "./api";
|
import { MinistreamApiClient } from "./api";
|
||||||
|
import React from "react";
|
||||||
|
|
||||||
type ThemeContextType = "light" | "dark";
|
interface AppProps {
|
||||||
|
api: MinistreamApiClient
|
||||||
const ThemeContext = createContext<ThemeContextType>("light");
|
|
||||||
|
|
||||||
type AppState = {
|
|
||||||
streamList: string[]
|
|
||||||
selectedStream: string | undefined
|
|
||||||
}
|
}
|
||||||
type AppProps = {}
|
|
||||||
|
|
||||||
export class App extends React.Component<AppProps, AppState> {
|
export function App({ api }: AppProps) {
|
||||||
apiClient: MinistreamApiClient
|
const [streamList, setStreamList] = useState<string[]>([])
|
||||||
constructor(props: AppProps) {
|
const [selectedStream, setSelectedStream] = useState<string | null>(null)
|
||||||
super(props)
|
|
||||||
this.state = {
|
const updateSelect = (item: string) => {
|
||||||
streamList: [],
|
setSelectedStream(item)
|
||||||
selectedStream: undefined
|
|
||||||
}
|
|
||||||
this.apiClient = new MinistreamApiClient()
|
|
||||||
this.updateSelect = this.updateSelect.bind(this)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async componentDidMount(): Promise<void> {
|
const updateStreamList = () => {
|
||||||
await this.updateStreamList()
|
api.listStreams().then((list) => {
|
||||||
setInterval(() => {
|
setStreamList(list)
|
||||||
this.updateStreamList()
|
if (list.length != streamList.length) {
|
||||||
}, 10000)
|
setStreamList(list)
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
return 0
|
|
||||||
})
|
|
||||||
|
|
||||||
if (sortedStreams.length != this.state.streamList.length) {
|
if (!list.every((_, idx) => {
|
||||||
this.setStreamList(streams)
|
return list[idx] === streamList[idx]
|
||||||
}
|
})) {
|
||||||
|
setStreamList(list)
|
||||||
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 }
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
updateSelect(selected: string): void {
|
setInterval(() => {
|
||||||
if (selected !== this.state.selectedStream) {
|
updateStreamList()
|
||||||
this.setState((state) => {
|
}, 10000)
|
||||||
return { streamList: state.streamList, selectedStream: selected }
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
useEffect(() => {
|
||||||
return (
|
updateStreamList()
|
||||||
<>
|
},[])
|
||||||
<Menu
|
|
||||||
items={this.state.streamList}
|
|
||||||
selectedItem={this.state.selectedStream}
|
return (
|
||||||
selectCallback={this.updateSelect}
|
<>
|
||||||
/>
|
<Menu
|
||||||
<MediaContainer selectedStream={this.state.selectedStream} api={this.apiClient} />
|
items={streamList}
|
||||||
</>
|
selectedItem={selectedStream}
|
||||||
)
|
selectCallback={updateSelect}
|
||||||
}
|
/>
|
||||||
|
<MediaContainer selectedStream={selectedStream} api={api} />
|
||||||
|
</>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const rootElement = document.getElementById("app");
|
const rootElement = document.getElementById("app");
|
||||||
if (rootElement) {
|
if (rootElement) {
|
||||||
const root = createRoot(rootElement);
|
const root = createRoot(rootElement);
|
||||||
root.render(<App />)
|
const api = new MinistreamApiClient()
|
||||||
|
root.render(<App api={api}/>)
|
||||||
}
|
}
|
@ -1,9 +1,10 @@
|
|||||||
import React, { ComponentProps, useRef, useEffect, useState } from "react"
|
import { ComponentProps, useRef, useEffect, useState } from "react"
|
||||||
import { MinistreamApiClient } from "./api"
|
import { MinistreamApiClient } from "./api"
|
||||||
import { resolve } from "path"
|
import { resolve } from "path"
|
||||||
|
import React from "react"
|
||||||
|
|
||||||
type MediaContainerProps = {
|
type MediaContainerProps = {
|
||||||
selectedStream: string | undefined
|
selectedStream: string | null
|
||||||
api: MinistreamApiClient
|
api: MinistreamApiClient
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user