From 4adaf01b3cdc86d560f9c5786a1b66690b757179 Mon Sep 17 00:00:00 2001 From: Renard Date: Mon, 9 Mar 2020 18:48:23 -0300 Subject: Fix: multiple sources --- src/App.js | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/src/App.js b/src/App.js index 71ce407..50177ed 100644 --- a/src/App.js +++ b/src/App.js @@ -29,13 +29,14 @@ class App extends Component { selectedStream: 0 }; this.streamCount = 0; + this.streamName = ""; } - checkStream(result) { + checkStream(source) { const prevCount = this.streamCount; - if (result.source !== undefined) { - if (Array.isArray(result)) { - this.streamCount = result.length; + if (source !== undefined) { + if (Array.isArray(source)) { + this.streamCount = source.length; } else { this.streamCount = 1; } @@ -109,8 +110,8 @@ class App extends Component { .then(response => response.json()) .then(result => { this.debug(result); - this.checkStream(result); const source = result.icestats.source; + const countChanged = !this.checkStream(source); if (source !== undefined) { let multipleStreams = Array.isArray(source); if (!multipleStreams && Array.isArray(this.state.response.source)) { @@ -135,13 +136,29 @@ class App extends Component { }) ); } + let prevSelectedStream = this.state.selectedStream; + if (countChanged) { + let prevStream = source.find( + s => s.server_name === this.streamName + ); + prevSelectedStream = source.indexOf(prevStream); + if (prevSelectedStream === -1) { + prevSelectedStream = 0; // TODO: Mostrar mensaje + } + if (this.state.selectedStream !== prevSelectedStream) { + this.refreshPlayer(); + } + } this.setState( { response: result.icestats, streamsAvailable: streams, - multipleSources: multipleStreams + multipleSources: multipleStreams, + selectedStream: prevSelectedStream }, - () => this.compareSongs() + () => { + this.compareSongs(); + } ); } } else { @@ -170,6 +187,7 @@ class App extends Component { }, () => { const { source } = this.state.response; + this.checkStream(source); if (source !== undefined && Array.isArray(source)) { let streams = []; source.map((stream, index) => @@ -185,12 +203,14 @@ class App extends Component { streamsAvailable: streams, ready: true }); + this.streamName = source[0].server_name; } else if (source !== undefined) { this.setState({ multipleSources: false, selectedStream: 0, ready: true }); + this.streamName = source.server_name; } } ) @@ -276,8 +296,12 @@ class App extends Component { fluid selection options={streamsAvailable} + value={selectedStream} onChange={(e, d) => this.setState({ selectedStream: d.value }, () => { + this.streamName = this.state.response.source[ + d.value + ].server_name; let audio = document.getElementById("player"); if (audio !== null) { audio.pause(); -- cgit v1.2.1-18-gbd029