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

349 lines
9.4 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 = () => {
const {
uri,
album,
coreActions: {
loadAlbum,
},
lastfmActions: {
getAlbum,
},
} = this.props;
2019-08-21 17:02:38 +12:00
this.setWindowTitle();
loadAlbum(uri);
2019-08-21 17:02:38 +12:00
if (album) {
if (album.artists && album.wiki === undefined) {
getAlbum(album.uri, album.artists[0].name, album.name);
2019-08-21 17:02:38 +12:00
}
}
}
handleContextMenu = (e) => {
const {
uri,
uiActions: {
showContextMenu,
},
} = this.props;
2019-08-21 17:02:38 +12:00
e.preventDefault();
const data = { uris: [uri] };
showContextMenu(e, data, 'album', 'click');
2019-08-21 17:02:38 +12:00
}
componentDidUpdate = ({
uri: prevUri,
album: prevAlbum,
mopidy_connected: prev_mopidy_connected,
}) => {
const {
uri,
album,
mopidy_connected,
coreActions: {
loadAlbum,
},
lastfmActions: {
getAlbum,
},
} = this.props;
2019-08-21 17:02:38 +12:00
if (uri !== prevUri) {
loadAlbum(uri);
} else if (!prev_mopidy_connected && mopidy_connected) {
if (uriSource(uri) !== 'spotify') {
loadAlbum(uri);
2019-08-21 17:02:38 +12:00
}
}
// We have just received our full album or our album artists
if ((!prevAlbum && album) || (!prevAlbum.artists && album.artists)) {
if (album.wiki === undefined && album.artists.length > 0) {
getAlbum(album.uri, album.artists[0].name, album.name);
2019-08-21 17:02:38 +12:00
}
}
if (!prevAlbum && album) this.setWindowTitle(album);
2019-08-21 17:02:38 +12:00
}
setWindowTitle = (album = this.props.album) => {
const { uiActions: { setWindowTitle } } = this.props;
2019-08-21 17:02:38 +12:00
if (album) {
2019-09-05 15:36:11 +12:00
let artists = '';
if (album.artists_uris && 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];
if (artists.hasOwnProperty(uri)) {
2019-09-05 15:36:11 +12:00
if (artists != '') {
artists += ', ';
2019-08-21 17:02:38 +12:00
}
artists += artists[uri].name;
2019-08-21 17:02:38 +12:00
}
}
}
setWindowTitle(`${album.name} by ${artists} (album)`);
2019-08-21 17:02:38 +12:00
} else {
setWindowTitle('Album');
2019-08-21 17:02:38 +12:00
}
}
handleContextMenu = (e) => {
const { album, uri, uiActions: { showContextMenu } } = this.props;
showContextMenu({
2019-09-05 15:36:11 +12:00
e,
context: 'album',
items: [album],
uris: [uri],
});
2019-08-21 17:02:38 +12:00
}
loadMore = () => {
const {
spotifyActions: {
getMore,
},
album: {
uri,
name,
tracks_more,
} = {},
} = this.props;
getMore(
tracks_more, {
2020-02-14 10:55:10 +13:00
parent_type: 'album',
parent_key: uri,
2020-02-14 10:55:10 +13:00
records_type: 'track',
},
null,
{
album: {
uri,
name,
2020-02-14 10:55:10 +13:00
},
},
);
2019-08-21 17:02:38 +12:00
}
play = () => {
const { uri, mopidyActions: { playURIs } } = this.props;
playURIs([uri], uri);
2019-08-21 17:02:38 +12:00
}
inLibrary = () => {
const { uri } = this.props;
const library = `${uriSource(uri)}_library_albums`;
return (this.props[library] && this.props[library].indexOf(this.props.uri) > -1);
2019-08-21 17:02:38 +12:00
}
render = () => {
const {
uri,
album: albumProp,
tracks,
artists,
load_queue,
} = this.props;
if (!albumProp) {
2019-08-21 17:02:38 +12:00
if (
isLoading(load_queue, [
`spotify_albums/${getFromUri('albumid', 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(uri)}"`}
2019-09-05 15:36:11 +12:00
</p>
</ErrorMessage>
);
2019-08-21 17:02:38 +12:00
}
const album = collate(albumProp, { tracks, 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} blur />
2019-08-21 17:02:38 +12:00
<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);