import React from 'react'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; import Link from '../components/Link'; import ErrorMessage from '../components/ErrorMessage'; import Header from '../components/Header'; import Thumbnail from '../components/Thumbnail'; import LinksSentence from '../components/LinksSentence'; import LastfmLoveButton from '../components/Fields/LastfmLoveButton'; import { Dater } from '../components/Dater'; import SelectField from '../components/Fields/SelectField'; import ContextMenuTrigger from '../components/ContextMenuTrigger'; import Icon from '../components/Icon'; import Loader from '../components/Loader'; import * as coreActions from '../services/core/actions'; import * as uiActions from '../services/ui/actions'; import * as mopidyActions from '../services/mopidy/actions'; import * as spotifyActions from '../services/spotify/actions'; import * as lastfmActions from '../services/lastfm/actions'; import * as geniusActions from '../services/genius/actions'; import { isLoading, getFromUri, sourceIcon, uriSource, uriType, } from '../util/helpers'; class Track extends React.Component { componentDidMount() { this.props.coreActions.loadTrack(this.props.uri); if (this.props.track) { this.setWindowTitle(this.props.track); if (this.props.genius_authorized && this.props.track.artists && !this.props.track.lyrics_results) { this.props.geniusActions.findTrackLyrics(this.props.track); } } } handleContextMenu(e) { e.preventDefault(); const data = { uris: [this.props.uri] }; this.props.uiActions.showContextMenu(e, data, 'track', 'click'); } componentDidUpdate = ({ uri: prevUri, track: prevTrack, mopidy_connected: prev_mopidy_connected, }) => { const { uri, track, genius_authorized, lastfm_authorized, mopidy_connected, coreActions: { loadTrack, }, geniusActions: { findTrackLyrics, }, lastfmActions: { getTrack, }, } = this.props; // if our URI has changed, fetch new track if (prevUri !== uri) { loadTrack(uri); if (genius_authorized && track.artists) findTrackLyrics(track); // if mopidy has just connected AND we're not a Spotify track, go get } else if (!prev_mopidy_connected && mopidy_connected) { if (uriSource(uri) !== 'spotify') loadTrack(uri); } // We have just received our full track or our track artists if ((!prevTrack && track) || (!prevTrack.artists && track.artists)) { this.setWindowTitle(track); if (lastfm_authorized) getTrack(track.uri); if (genius_authorized && !track.lyrics_results) findTrackLyrics(track); } if (!prevTrack && track) this.setWindowTitle(track); } setWindowTitle(track = this.props.track) { if (track) { let artists = ''; for (let i = 0; i < track.artists.length; i++) { if (artists != '') { artists += ', '; } artists += track.artists[i].name; } this.props.uiActions.setWindowTitle(`${track.name} by ${artists} (track)`); } else { this.props.uiActions.setWindowTitle('Track'); } } handleContextMenu(e) { const data = { e, context: 'track', items: [this.props.track], uris: [this.props.uri], }; this.props.uiActions.showContextMenu(data); } play() { this.props.mopidyActions.playURIs([this.props.uri], this.props.uri); } renderLyricsSelector() { const { track, geniusActions, } = this.props; if (track.lyrics_results === undefined || track.lyrics_results === null) { return null; } if (track.lyrics_results.length <= 0) { return (
Want track lyrics? Authorize Genius under Settings .
) : null} {this.props.genius_authorized ? this.renderLyricsSelector() : null} {this.props.genius_authorized ? this.renderLyrics() : null}