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

321 lines
8.7 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 TrackList from '../components/TrackList';
import Thumbnail from '../components/Thumbnail';
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 { nice_number } from '../components/NiceNumber';
import { Dater } from '../components/Dater';
2019-09-05 15:36:11 +12:00
import LazyLoadListener from '../components/LazyLoadListener';
import ContextMenuTrigger from '../components/ContextMenuTrigger';
import Icon from '../components/Icon';
import { i18n, I18n } from '../locale';
2019-09-05 15:36:11 +12:00
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';
import Button from '../components/Button';
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,
}) => {
const {
uri,
album,
coreActions: {
loadAlbum,
},
lastfmActions: {
getAlbum,
},
} = this.props;
2019-08-21 17:02:38 +12:00
if (uri !== prevUri) {
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.artists && album.wiki === undefined) {
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 }, artists } = this.props;
2019-08-21 17:02:38 +12:00
if (album) {
let artistNames = [];
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)) {
artistNames.push(artists[uri].name);
2019-08-21 17:02:38 +12:00
}
}
}
setWindowTitle(i18n('album.title_window', { name: album.name, artist: artistNames.join() }));
2019-08-21 17:02:38 +12:00
} else {
setWindowTitle(i18n('album.title'));
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,
load_queue,
slim_mode,
} = this.props;
if (!album) {
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>
{i18n('errors.uri_not_found', { uri: encodeURIComponent(uri) })}
2019-09-05 15:36:11 +12:00
</p>
</ErrorMessage>
);
2019-08-21 17:02:38 +12:00
}
return (
<div className="view album-view content-wrapper preserve-3d">
<div className="thumbnail-wrapper">
<Thumbnail size="large" glow canZoom images={album.images} type="album" />
2019-08-21 17:02:38 +12:00
</div>
<div className="title">
<h1>{album.name}</h1>
<ul className="details">
{!slim_mode ? (
2019-08-21 17:02:38 +12:00
<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>
{i18n(
'specs.tracks',
{ count: album.tracks_total || album.tracks.length },
)}
</li>
2019-09-05 15:36:11 +12:00
) : null}
{!slim_mode && album.tracks ? (
2019-08-21 17:02:38 +12:00
<li>
<Dater type="total-time" data={album.tracks} />
</li>
) : null}
{!slim_mode && album.play_count ? (
2019-08-21 17:02:38 +12:00
<li>
{i18n(
'specs.plays',
{ count: nice_number(album.play_count) },
)}
2019-08-21 17:02:38 +12:00
</li>
) : null}
{!slim_mode && album.listeners ? (
2019-08-21 17:02:38 +12:00
<li>
{i18n(
'specs.plays',
{ count: nice_number(album.listeners) },
)}
2019-08-21 17:02:38 +12:00
</li>
) : null}
</ul>
</div>
<div className="actions">
<Button
type="primary"
onClick={this.play}
tracking={{ category: 'Album', action: 'Play' }}
>
<I18n path="actions.play" />
</Button>
{uriSource(uri) === 'spotify' && (
2019-08-21 17:02:38 +12:00
<FollowButton
uri={uri}
2019-08-21 17:02:38 +12:00
is_following={this.inLibrary()}
/>
)}
<ContextMenuTrigger onTrigger={this.handleContextMenu} />
2019-08-21 17:02:38 +12:00
</div>
<section className="list-wrapper">
<TrackList
className="album-track-list"
tracks={album.tracks}
uri={album.uri}
/>
</section>
{album.wiki ? (
<section className="wiki">
<h4 className="wiki__title">{i18n('album.wiki.title')}</h4>
2019-08-21 17:02:38 +12:00
<div className="wiki__text">
<p>{album.wiki}</p>
<br />
<div className="mid_grey-text">
<I18n path="album.wiki.published" params={{ date: 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
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);