import React from 'react'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; import ReactGA from 'react-ga'; import ErrorMessage from '../components/ErrorMessage'; import Link from '../components/Link'; import TrackList from '../components/TrackList'; import Thumbnail from '../components/Thumbnail'; import Parallax from '../components/Parallax'; import NiceNumber from '../components/NiceNumber'; import Dater from '../components/Dater'; import LazyLoadListener from '../components/LazyLoadListener'; import FollowButton from '../components/Fields/FollowButton'; import Loader from '../components/Loader'; import ContextMenuTrigger from '../components/ContextMenuTrigger'; import URILink from '../components/URILink'; import Icon from '../components/Icon'; 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 { uriSource, getFromUri, isLoading, sourceIcon, } from '../util/helpers'; import { collate } from '../util/format'; class Playlist extends React.Component { constructor(props) { super(props); let { uri } = props; // Spotify upgraded their playlists URI to remove user component (Sept 2018) // We accept the old format, and redirect to the new one if (uri.includes('spotify:user:')) { uri = uri.replace(/spotify:user:([^:]*?):/i, 'spotify:'); props.history.push(`/playlist/${encodeURIComponent(uri)}`); } } componentDidMount() { this.setWindowTitle(); this.props.coreActions.loadPlaylist(this.props.uri); } componentDidUpdate = ({ uri: prevUri, playlist: prevPlaylist, mopidy_connected: prev_mopidy_connected, }) => { const { uri, playlist, mopidy_connected, coreActions: { loadPlaylist, }, history: { push, }, } = this.props; if (prevPlaylist && playlist && prevPlaylist.moved_to !== playlist.moved_to) { push(`/playlist/${encodeURIComponent(playlist.moved_to)}`); } if (uri !== prevUri) { loadPlaylist(uri); } else if (!prev_mopidy_connected && mopidy_connected) { if (uriSource(uri) !== 'spotify') { loadPlaylist(uri); } } if (!prevPlaylist && playlist) this.setWindowTitle(playlist); if (prevUri !== uri && playlist) this.setWindowTitle(playlist); } setWindowTitle(playlist = this.props.playlist) { if (playlist) { this.props.uiActions.setWindowTitle(`${playlist.name} (playlist)`); } else { this.props.uiActions.setWindowTitle('Playlist'); } } loadMore() { this.props.spotifyActions.getMore( this.props.playlist.tracks_more, { parent_type: 'playlist', parent_key: this.props.playlist.uri, records_type: 'track', }, ); } handleContextMenu(e) { const data = { e, context: 'playlist', items: [this.props.playlist], uris: [this.props.uri], }; this.props.uiActions.showContextMenu(data); } play() { this.props.mopidyActions.playPlaylist(this.props.playlist.uri); } follow() { if (this.props.allow_reporting) { ReactGA.event({ category: 'Playlist', action: 'Follow', label: this.props.playlist.uri }); } this.props.spotifyActions.toggleFollowingPlaylist(this.props.playlist.uri, 'PUT'); } // TODO: Once unfollowing occurs, remove playlist from global playlists list unfollow() { if (this.props.allow_reporting) { ReactGA.event({ category: 'Playlist', action: 'Unfollow', label: this.props.playlist.uri }); } this.props.spotifyActions.toggleFollowingPlaylist(this.props.playlist.uri, 'DELETE'); } // TODO: Once deletion occurs, remove playlist from global playlists list delete() { this.props.mopidyActions.deletePlaylist(this.props.playlist.uri); } reorderTracks(indexes, index) { this.props.coreActions.reorderPlaylistTracks(this.props.playlist.uri, indexes, index, this.props.playlist.snapshot_id); } removeTracks(tracks_indexes) { this.props.coreActions.removeTracksFromPlaylist(this.props.playlist.uri, tracks_indexes); } inLibrary() { const library = `${uriSource(this.props.uri)}_library_playlists`; return (this.props[library] && this.props[library].indexOf(this.props.uri) > -1); } renderActions() { switch (uriSource(this.props.uri)) { case 'm3u': return (
Could not find playlist with URI " {encodeURIComponent(this.props.uri)} "