Files
Iris/src/js/views/Album.js

317 lines
9.5 KiB
JavaScript
Raw Normal View History

2019-09-05 15:36:11 +12:00
import React from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
2019-08-21 17:02:38 +12:00
2019-09-05 15:36:11 +12:00
import ErrorMessage from '../components/ErrorMessage';
import Header from '../components/Header';
import TrackList from '../components/TrackList';
import Thumbnail from '../components/Thumbnail';
import Parallax from '../components/Parallax';
import LinksSentence from '../components/LinksSentence';
import Loader from '../components/Loader';
2019-09-05 15:36:11 +12:00
import FollowButton from '../components/Fields/FollowButton';
import NiceNumber from '../components/NiceNumber';
import Dater from '../components/Dater';
import LazyLoadListener from '../components/LazyLoadListener';
import ContextMenuTrigger from '../components/ContextMenuTrigger';
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 * as lastfmActions from '../services/lastfm/actions';
import {
uriSource,
getFromUri,
isLoading,
sourceIcon,
} from '../util/helpers';
import { collate } from '../util/format';
2019-08-21 17:02:38 +12:00
export class Album extends React.Component {
componentDidMount() {
this.setWindowTitle();
this.props.coreActions.loadAlbum(this.props.uri);
// We already have the album in our index, so it won't fire componentWillReceiveProps
if (this.props.album) {
if (this.props.album.artists && this.props.album.wiki === undefined) {
this.props.lastfmActions.getAlbum(
this.props.album.uri,
this.props.album.artists[0].name,
2019-09-05 15:36:11 +12:00
this.props.album.name,
2019-08-21 17:02:38 +12:00
);
}
}
}
handleContextMenu(e) {
e.preventDefault();
2019-09-05 15:36:11 +12:00
const data = { uris: [this.props.uri] };
this.props.uiActions.showContextMenu(e, data, 'album', 'click');
2019-08-21 17:02:38 +12:00
}
componentWillReceiveProps(nextProps) {
// if our URI has changed, fetch new album
if (nextProps.uri != this.props.uri) {
this.props.coreActions.loadAlbum(nextProps.uri);
// if mopidy has just connected AND we're a local album, go get
} else if (!this.props.mopidy_connected && nextProps.mopidy_connected) {
if (uriSource(nextProps.uri) != 'spotify') {
2019-08-21 17:02:38 +12:00
this.props.coreActions.loadAlbum(nextProps.uri);
}
}
// We have just received our full album or our album artists
if (
2019-09-05 15:36:11 +12:00
(!this.props.album && nextProps.album)
|| (!this.props.album.artists && nextProps.album.artists)
2019-08-21 17:02:38 +12:00
) {
if (nextProps.album.wiki === undefined && nextProps.artists.length > 0) {
this.props.lastfmActions.getAlbum(
nextProps.album.uri,
nextProps.album.artists[0].name,
2019-09-05 15:36:11 +12:00
nextProps.album.name,
2019-08-21 17:02:38 +12:00
);
}
}
if (!this.props.album && nextProps.album) {
this.setWindowTitle(nextProps.album);
}
}
setWindowTitle(album = this.props.album) {
if (album) {
2019-09-05 15:36:11 +12:00
let artists = '';
2019-08-21 17:02:38 +12:00
if (album.artists_uris && this.props.artists) {
2019-09-05 15:36:11 +12:00
for (let i = 0; i < album.artists_uris.length; i++) {
const uri = album.artists_uris[i];
2019-08-21 17:02:38 +12:00
if (this.props.artists.hasOwnProperty(uri)) {
2019-09-05 15:36:11 +12:00
if (artists != '') {
artists += ', ';
2019-08-21 17:02:38 +12:00
}
artists += this.props.artists[uri].name;
}
}
}
this.props.uiActions.setWindowTitle(
2019-09-05 15:36:11 +12:00
`${album.name} by ${artists} (album)`,
2019-08-21 17:02:38 +12:00
);
} else {
2019-09-05 15:36:11 +12:00
this.props.uiActions.setWindowTitle('Album');
2019-08-21 17:02:38 +12:00
}
}
handleContextMenu(e) {
2019-09-05 15:36:11 +12:00
const data = {
e,
context: 'album',
2019-08-21 17:02:38 +12:00
items: [this.props.album],
2019-09-05 15:36:11 +12:00
uris: [this.props.uri],
2019-08-21 17:02:38 +12:00
};
this.props.uiActions.showContextMenu(data);
}
loadMore() {
2020-02-14 10:55:10 +13:00
this.props.spotifyActions.getMore(
this.props.album.tracks_more, {
parent_type: 'album',
parent_key: this.props.album.uri,
records_type: 'track',
},
null,
{
album: {
uri: this.props.album.uri,
name: this.props.album.name,
},
},
);
2019-08-21 17:02:38 +12:00
}
play() {
this.props.mopidyActions.playURIs([this.props.uri], this.props.uri);
}
inLibrary() {
const library = `${uriSource(this.props.uri)}_library_albums`;
2019-08-21 17:02:38 +12:00
return (
this.props[library] && this.props[library].indexOf(this.props.uri) > -1
);
}
render() {
if (!this.props.album) {
if (
isLoading(this.props.load_queue, [
`spotify_albums/${getFromUri('albumid', this.props.uri)}`,
2019-08-21 17:02:38 +12:00
])
) {
return <Loader body loading />;
2019-08-21 17:02:38 +12:00
}
2019-09-05 15:36:11 +12:00
return (
<ErrorMessage type="not-found" title="Not found">
<p>
Could not find album with URI "
{encodeURIComponent(this.props.uri)}
2019-09-05 15:36:11 +12:00
"
</p>
</ErrorMessage>
);
2019-08-21 17:02:38 +12:00
}
const album = collate(this.props.album, {
2019-08-21 17:02:38 +12:00
tracks: this.props.tracks,
2019-09-05 15:36:11 +12:00
artists: this.props.artists,
2019-08-21 17:02:38 +12:00
});
if (
2019-09-05 15:36:11 +12:00
!album.tracks_uris
|| (album.tracks_uris && !album.tracks)
|| album.tracks_uris.length !== album.tracks.length
2019-08-21 17:02:38 +12:00
) {
var is_loading_tracks = true;
} else {
var is_loading_tracks = false;
}
return (
<div className="view album-view content-wrapper preserve-3d">
<Parallax image={album.images ? album.images.huge : null} blur />
<div className="thumbnail-wrapper">
2019-08-30 08:32:47 +12:00
<Thumbnail size="large" glow canZoom images={album.images} />
2019-08-21 17:02:38 +12:00
</div>
<div className="title">
<h1>{album.name}</h1>
<ul className="details">
{!this.props.slim_mode ? (
<li className="source">
<Icon type="fontawesome" name={sourceIcon(album.uri)} />
2019-08-21 17:02:38 +12:00
</li>
) : null}
{album.artists && album.artists.length > 0 ? (
<li>
<LinksSentence items={album.artists} />
2019-08-21 17:02:38 +12:00
</li>
) : null}
{album.release_date ? (
<li>
<Dater type="date" data={album.release_date} />
</li>
) : null}
{album.tracks ? (
<li>
2020-02-14 10:55:10 +13:00
{album.tracks_total || album.tracks.length}
{' '}
tracks
</li>
2019-09-05 15:36:11 +12:00
) : null}
2019-08-21 17:02:38 +12:00
{!this.props.slim_mode && album.tracks ? (
<li>
<Dater type="total-time" data={album.tracks} />
</li>
) : null}
{!this.props.slim_mode && album.play_count ? (
<li>
<NiceNumber value={album.play_count} />
{' '}
2019-09-05 15:36:11 +12:00
plays
2019-08-21 17:02:38 +12:00
</li>
) : null}
{!this.props.slim_mode && album.listeners ? (
<li>
<NiceNumber value={album.listeners} />
{' '}
2019-09-05 15:36:11 +12:00
listeners
2019-08-21 17:02:38 +12:00
</li>
) : null}
</ul>
</div>
<div className="actions">
2019-09-05 15:36:11 +12:00
<button className="button button--primary" onClick={(e) => this.play()}>
2019-08-21 17:02:38 +12:00
Play
</button>
{uriSource(this.props.uri) == 'spotify' ? (
2019-08-21 17:02:38 +12:00
<FollowButton
className="secondary"
uri={this.props.uri}
addText="Add to library"
removeText="Remove from library"
is_following={this.inLibrary()}
/>
) : null}
2019-09-05 15:36:11 +12:00
<ContextMenuTrigger onTrigger={(e) => this.handleContextMenu(e)} />
2019-08-21 17:02:38 +12:00
</div>
<section className="list-wrapper">
<TrackList
className="album-track-list"
tracks={album.tracks}
uri={album.uri}
/>
<LazyLoadListener
loadKey={album.tracks_more}
showLoader={is_loading_tracks}
loadMore={() => this.loadMore()}
/>
</section>
{album.wiki ? (
<section className="wiki">
<h4 className="wiki__title">About</h4>
<div className="wiki__text">
<p>{album.wiki}</p>
<br />
<div className="mid_grey-text">
Published:
{' '}
2019-09-05 15:36:11 +12:00
{album.wiki_publish_date}
2019-08-21 17:02:38 +12:00
</div>
</div>
</section>
) : null}
</div>
);
}
2016-10-03 14:44:27 +13:00
}
const mapStateToProps = (state, ownProps) => {
2019-09-05 15:36:11 +12:00
const uri = decodeURIComponent(ownProps.match.params.uri);
2019-08-21 17:02:38 +12:00
return {
2019-09-05 15:36:11 +12:00
uri,
2019-08-21 17:02:38 +12:00
slim_mode: state.ui.slim_mode,
theme: state.ui.theme,
load_queue: state.ui.load_queue,
tracks: state.core.tracks,
artists: state.core.artists,
album:
state.core.albums && state.core.albums[uri] !== undefined
? state.core.albums[uri]
: false,
albums: state.core.albums,
spotify_library_albums: state.spotify.library_albums,
local_library_albums: state.mopidy.library_albums,
spotify_authorized: state.spotify.authorization,
2019-09-05 15:36:11 +12:00
mopidy_connected: state.mopidy.connected,
2019-08-21 17:02:38 +12:00
};
};
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),
lastfmActions: bindActionCreators(lastfmActions, dispatch),
});
2019-08-21 17:02:38 +12:00
export default connect(
mapStateToProps,
2019-09-05 15:36:11 +12:00
mapDispatchToProps,
2019-08-21 17:02:38 +12:00
)(Album);