import React from 'react'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; import Link from '../components/Link'; import Icon from '../components/Icon'; import Parallax from '../components/Parallax'; import TrackList from '../components/TrackList'; import Dater from '../components/Dater'; import ArtistSentence from '../components/ArtistSentence'; import Thumbnail from '../components/Thumbnail'; import Header from '../components/Header'; import URILink from '../components/URILink'; import LazyLoadListener from '../components/LazyLoadListener'; import * as helpers from '../helpers'; import * as uiActions from '../services/ui/actions'; import * as pusherActions from '../services/pusher/actions'; import * as spotifyActions from '../services/spotify/actions'; import * as mopidyActions from '../services/mopidy/actions'; class Queue extends React.Component { constructor(props){ super(props); this.state = { limit: 50, per_page: 50 } } componentWillMount(){ // Before we mount, restore any limit defined in our location state var state = (this.props.location.state ? this.props.location.state : {}); if (state.limit){ this.setState({ limit: state.limit }); } } componentDidMount(){ this.props.uiActions.setWindowTitle("Now playing"); } loadMore(){ var new_limit = this.state.limit + this.state.per_page; this.setState({limit: new_limit}); // Set our pagination to location state var state = (this.props.location && this.props.location.state ? this.props.location.state : {}); state.limit = new_limit; this.props.history.replace({state: state}); } removeTracks(track_indexes){ var tlids = []; for (var i = 0; i < track_indexes.length; i++){ var track = this.props.queue[track_indexes[i]]; if (track.tlid !== undefined){ tlids.push(track.tlid); } } if (tlids.length > 0){ this.props.mopidyActions.removeTracks(tlids); } } playTrack(track){ this.props.mopidyActions.changeTrack(track.tlid); } playTracks(tracks){ this.props.mopidyActions.changeTrack(tracks[0].tlid); } reorderTracks(indexes, index){ this.props.mopidyActions.reorderTracklist(indexes, index); } renderQueueStats(){ var total_time = 0 return (
{this.props.current_tracklist.length} tracks   |   {this.props.current_tracklist.length > 0 ? : 0 mins}
) } renderArtwork(image){ if (!image){ return ( {this.props.radio_enabled ? : null} ) } var uri = null if (this.props.current_track.album && this.props.current_track.album.uri){ uri = this.props.current_track.album.uri; } return ( {this.props.radio_enabled ? : null} ) } render(){ var current_track = null; var tracks = []; // Apply our lazy-load-rendering let total_queue_tracks = this.props.queue.length; let queue_tracks = this.props.queue.slice(0, this.state.limit); if (queue_tracks && this.props.tracks){ for (let queue_track of queue_tracks){ let track = Object.assign({}, queue_track); // If we have the track in our index, merge it in. // We prioritise queue track over index track as queue has unique data, like which track // is playing and tlids. if (this.props.tracks.hasOwnProperty(track.uri)){ track = Object.assign( {}, this.props.tracks[track.uri], track, { playing: (this.props.current_track && this.props.current_track.tlid == track.tlid) } ); } // Now merge in our queue metadata if (this.props.queue_metadata["tlid_"+track.tlid] !== undefined){ track = Object.assign( {}, track, this.props.queue_metadata["tlid_"+track.tlid] ); } // Siphon off this track if it's a full representation of our current track (by tlid) if (this.props.current_track && this.props.current_track.uri == track.uri){ current_track = track; } // Now add our compiled track for our tracklist tracks.push(track); } } var current_track_image = null; if (current_track && this.props.current_track_uri){ if (current_track.images !== undefined && current_track.images){ current_track_image = current_track.images.large; } } var options = ( {this.props.spotify_enabled ? Radio {this.props.radio && this.props.radio.enabled ? On : null} : null} History {this.props.mopidyActions.clearTracklist(); this.props.uiActions.hideContextMenu();}}> Clear Add URI ) return (
Now playing
{this.props.theme == 'dark' && }
{this.renderArtwork(current_track_image)}
{current_track ? {current_track.name} : -}
{current_track ? : }
this.removeTracks(track_indexes)} playTracks={tracks => this.playTracks(tracks)} playTrack={track => this.playTrack(track)} reorderTracks={(indexes, index) => this.reorderTracks(indexes, index)} />
this.state.limit ? this.state.limit : total_queue_tracks} showLoader={this.state.limit < total_queue_tracks} loadMore={() => this.loadMore()} />
); } } const mapStateToProps = (state, ownProps) => { return { theme: state.ui.theme, spotify_enabled: state.spotify.enabled, radio: state.core.radio, radio_enabled: (state.core.radio && state.core.radio.enabled ? true : false), tracks: state.core.tracks, artists: state.core.artists, albums: state.core.albums, queue: state.core.queue, queue_tlids: state.core.queue_tlids, queue_metadata: state.core.queue_metadata, current_track_uri: state.core.current_track_uri, current_track: state.core.current_track } } const mapDispatchToProps = (dispatch) => { return { uiActions: bindActionCreators(uiActions, dispatch), pusherActions: bindActionCreators(pusherActions, dispatch), spotifyActions: bindActionCreators(spotifyActions, dispatch), mopidyActions: bindActionCreators(mopidyActions, dispatch) } } export default connect(mapStateToProps, mapDispatchToProps)(Queue)