Files
Iris/src/js/views/Playlist.js

417 lines
11 KiB
JavaScript
Raw Normal View History

import React from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import ErrorMessage from '../components/ErrorMessage';
import Button from '../components/Button';
2019-09-05 15:36:11 +12:00
import TrackList from '../components/TrackList';
import Thumbnail from '../components/Thumbnail';
2020-08-08 22:17:31 +12:00
import PinButton from '../components/Fields/PinButton';
import { nice_number } from '../components/NiceNumber';
2020-07-08 20:15:14 +12:00
import { Dater, dater } from '../components/Dater';
2019-09-05 15:36:11 +12:00
import LazyLoadListener from '../components/LazyLoadListener';
import FollowButton from '../components/Fields/FollowButton';
import Loader from '../components/Loader';
2019-09-05 15:36:11 +12:00
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,
decodeMopidyUri,
} from '../util/helpers';
import { collate } from '../util/format';
2020-07-08 20:15:14 +12:00
import { i18n, I18n } from '../locale';
2019-09-05 15:36:11 +12:00
class Playlist extends React.Component {
2019-09-05 15:36:11 +12:00
constructor(props) {
super(props);
let { uri } = props;
2019-09-05 15:36:11 +12:00
// 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)}`);
2019-09-05 15:36:11 +12:00
}
}
componentDidMount() {
const { coreActions: { loadItem }, uri } = this.props;
2019-09-05 15:36:11 +12:00
this.setWindowTitle();
loadItem(uri);
2019-09-05 15:36:11 +12:00
}
componentDidUpdate = ({
uri: prevUri,
playlist: prevPlaylist,
}) => {
const {
uri,
playlist,
coreActions: {
loadItem,
},
history: {
push,
},
} = this.props;
if (prevPlaylist && playlist && prevPlaylist.moved_to !== playlist.moved_to) {
push(`/playlist/${encodeURIComponent(playlist.moved_to)}`);
2019-09-05 15:36:11 +12:00
}
if (uri !== prevUri) {
loadItem(uri);
2019-09-05 15:36:11 +12:00
}
if (!prevPlaylist && playlist) this.setWindowTitle(playlist);
if (prevUri !== uri && playlist) this.setWindowTitle(playlist);
2019-09-05 15:36:11 +12:00
}
2020-07-08 20:15:14 +12:00
setWindowTitle = (playlist = this.props.playlist) => {
const { uiActions: { setWindowTitle } } = this.props;
setWindowTitle(
playlist ? i18n('playlist.title_window', { name: playlist.name }) : i18n('playlist.title')
);
2019-09-05 15:36:11 +12:00
}
2020-07-08 20:15:14 +12:00
handleContextMenu = (e) => {
const {
uiActions: {
showContextMenu,
},
playlist,
uri,
} = this.props;
showContextMenu({
2019-09-05 15:36:11 +12:00
e,
context: 'playlist',
2020-07-08 20:15:14 +12:00
items: [playlist],
uris: [uri],
});
2019-09-05 15:36:11 +12:00
}
2020-07-08 20:15:14 +12:00
play = () => {
const {
mopidyActions: {
playPlaylist,
},
playlist: {
uri,
},
} = this.props;
2020-07-08 20:15:14 +12:00
playPlaylist(uri);
2019-09-05 15:36:11 +12:00
}
// TODO: Once deletion occurs, remove playlist from global playlists list
2020-07-08 20:15:14 +12:00
delete = () => {
const {
mopidyActions: {
deletePlaylist,
},
playlist: {
uri,
},
} = this.props;
deletePlaylist(uri);
2019-09-05 15:36:11 +12:00
}
2020-07-08 20:15:14 +12:00
reorderTracks = (indexes, index) => {
const {
coreActions: {
reorderPlaylistTracks,
},
playlist: {
uri,
snapshot_id,
},
} = this.props;
reorderPlaylistTracks(uri, indexes, index, snapshot_id);
2019-09-05 15:36:11 +12:00
}
2020-07-08 20:15:14 +12:00
removeTracks = (tracks_indexes) => {
const {
coreActions: {
removeTracksFromPlaylist,
},
playlist: {
uri,
},
} = this.props;
removeTracksFromPlaylist(uri, tracks_indexes);
2019-09-05 15:36:11 +12:00
}
2020-07-08 20:15:14 +12:00
inLibrary = () => {
const { uri } = this.props;
const libraryName = `${uriSource(uri)}_library_playlists`;
const { [libraryName]: library } = this.props;
if (!library) return false;
2020-07-08 20:15:14 +12:00
return library.indexOf(uri) > -1;
2019-09-05 15:36:11 +12:00
}
2020-08-08 22:17:31 +12:00
togglePinned = () => {
const {
uri,
coreActions: {
addPinned,
removePinned,
},
} = this.props;
if (this.isPinned()) {
removePinned(uri);
} else {
addPinned(uri);
}
}
2020-07-08 20:15:14 +12:00
renderActions = () => {
const {
uri,
playlist: {
can_edit,
name,
in_library,
2020-07-08 20:15:14 +12:00
},
} = this.props;
switch (uriSource(uri)) {
2019-09-05 15:36:11 +12:00
case 'm3u':
return (
<div className="actions">
<Button
type="primary"
onClick={this.play}
tracking={{ category: 'Playlist', action: 'Play' }}
>
2020-07-08 20:15:14 +12:00
<I18n path="actions.play" />
</Button>
<Button
to={`/playlist/${encodeURIComponent(uri)}/edit`}
tracking={{ category: 'Playlist', action: 'Edit' }}
>
2020-07-08 20:15:14 +12:00
<I18n path="actions.edit" />
</Button>
<PinButton item={{ uri, name }} />
2020-07-08 20:15:14 +12:00
<ContextMenuTrigger onTrigger={this.handleContextMenu} />
2019-09-05 15:36:11 +12:00
</div>
);
case 'spotify':
2020-07-08 20:15:14 +12:00
if (can_edit) {
2019-09-05 15:36:11 +12:00
return (
<div className="actions">
<Button
type="primary"
onClick={this.play}
tracking={{ category: 'Playlist', action: 'Play' }}
>
2020-07-08 20:15:14 +12:00
<I18n path="actions.play" />
</Button>
<Button
to={`/playlist/${encodeURIComponent(uri)}/edit`}
tracking={{ category: 'Playlist', action: 'Edit' }}
>
2020-07-08 20:15:14 +12:00
<I18n path="actions.edit" />
</Button>
<PinButton item={{ uri, name }} />
2020-07-08 20:15:14 +12:00
<ContextMenuTrigger onTrigger={this.handleContextMenu} />
2019-09-05 15:36:11 +12:00
</div>
);
}
return (
<div className="actions">
<Button
type="primary"
onClick={this.play}
tracking={{ category: 'Playlist', action: 'Play' }}
>
2020-07-08 20:15:14 +12:00
<I18n path="actions.play" />
</Button>
2020-07-08 20:15:14 +12:00
<FollowButton
uri={uri}
is_following={in_library}
2020-07-08 20:15:14 +12:00
/>
<PinButton item={{ uri, name }} />
2020-07-08 20:15:14 +12:00
<ContextMenuTrigger onTrigger={this.handleContextMenu} />
2019-09-05 15:36:11 +12:00
</div>
);
default:
return (
<div className="actions">
<Button
type="primary"
onClick={this.play}
tracking={{ category: 'Playlist', action: 'Play' }}
>
2020-07-08 20:15:14 +12:00
<I18n path="actions.play" />
</Button>
<PinButton item={{ uri, name }} />
2020-07-08 20:15:14 +12:00
<ContextMenuTrigger onTrigger={this.handleContextMenu} />
2019-09-05 15:36:11 +12:00
</div>
);
}
}
2020-07-08 20:15:14 +12:00
render = () => {
const {
uri,
playlist,
2020-07-08 20:15:14 +12:00
load_queue,
slim_mode,
} = this.props;
const playlist_id = getFromUri('playlistid', uri);
2019-09-05 15:36:11 +12:00
if (!playlist) {
2020-07-08 20:15:14 +12:00
if (isLoading(load_queue, [`spotify_playlists/${playlist_id}?`])) {
return <Loader body loading />
2019-09-05 15:36:11 +12:00
}
return (
<ErrorMessage type="not-found" title="Not found">
<p>
2020-07-08 20:15:14 +12:00
<I18n path="errors.uri_not_found" uri={uri} />
2019-09-05 15:36:11 +12:00
</p>
</ErrorMessage>
);
2019-09-05 15:36:11 +12:00
}
let context = 'playlist';
if (playlist.can_edit) {
context = 'editable-playlist';
}
return (
<div className="view playlist-view content-wrapper preserve-3d">
<div className="thumbnail-wrapper">
<Thumbnail size="large" glow canZoom images={playlist.images} type="playlist" />
2019-09-05 15:36:11 +12:00
</div>
<div className="title">
<h1>{playlist.name}</h1>
2020-07-08 20:15:14 +12:00
{playlist.description && (
<h2
className="description"
dangerouslySetInnerHTML={{ __html: playlist.description }}
/>
)}
2019-09-05 15:36:11 +12:00
<ul className="details">
2020-07-08 20:15:14 +12:00
{!slim_mode && (
<li className="source">
<Icon type="fontawesome" name={sourceIcon(playlist.uri)} />
</li>
)}
{playlist.user && (
2020-07-08 20:15:14 +12:00
<li>
<URILink
type="user"
uri={playlist.user.uri}
2020-07-08 20:15:14 +12:00
>
{playlist.user.name}
2020-07-08 20:15:14 +12:00
</URILink>
</li>
)}
<li>
2020-07-08 20:15:14 +12:00
<I18n path="specs.tracks" count={playlist.tracks_total || 0} />
2019-09-05 15:36:11 +12:00
</li>
2020-07-08 20:15:14 +12:00
{!slim_mode && playlist.tracks && playlist.tracks_total > 0 && (
<li><Dater type="total-time" data={playlist.tracks} /></li>
)}
{!slim_mode && playlist.followers !== undefined && (
<li>
2020-07-08 20:15:14 +12:00
<I18n path="specs.followers" count={nice_number(playlist.followers)} />
</li>
2020-07-08 20:15:14 +12:00
)}
{!slim_mode && playlist.last_modified_date && (
<li>
2020-07-08 20:15:14 +12:00
<I18n path="specs.edited" date={dater('ago', playlist.last_modified_date)} />
</li>
2020-07-08 20:15:14 +12:00
)}
2019-09-05 15:36:11 +12:00
</ul>
</div>
{this.renderActions()}
{isLoading(load_queue, [`spotify_playlists/${playlist_id}/tracks`]) ? (
<Loader body loading />
) : (
<section className="list-wrapper">
<TrackList
uri={playlist.uri}
className="playlist-track-list"
track_context={context}
tracks={playlist.tracks}
removeTracks={this.removeTracks}
reorderTracks={this.reorderTracks}
/>
</section>
)}
2019-09-05 15:36:11 +12:00
</div>
);
}
}
const mapStateToProps = (state, ownProps) => {
const {
ui: {
allow_reporting,
slim_mode,
theme,
load_queue,
} = {},
core: {
items,
} = {},
spotify: {
library_playlists: spotify_library_playlists,
authorization: spotify_authorized,
me = {},
} = {},
mopidy: {
library_playlists: local_library_playlists,
} = {},
} = state;
const uri = decodeMopidyUri(ownProps.match.params.uri);
2019-09-05 15:36:11 +12:00
return {
uri,
allow_reporting,
slim_mode,
theme,
load_queue,
playlist: items[uri] || null,
spotify_library_playlists,
local_library_playlists,
spotify_authorized,
spotify_userid: (me && me.id) || null,
2019-09-05 15:36:11 +12:00
};
};
const mapDispatchToProps = (dispatch) => ({
coreActions: bindActionCreators(coreActions, dispatch),
uiActions: bindActionCreators(uiActions, dispatch),
mopidyActions: bindActionCreators(mopidyActions, dispatch),
spotifyActions: bindActionCreators(spotifyActions, dispatch),
});
export default connect(mapStateToProps, mapDispatchToProps)(Playlist);