import React from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import AlbumGrid from '../../components/AlbumGrid';
import List from '../../components/List';
import Header from '../../components/Header';
import DropdownField from '../../components/Fields/DropdownField';
import FilterField from '../../components/Fields/FilterField';
import LazyLoadListener from '../../components/LazyLoadListener';
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 googleActions from '../../services/google/actions';
import * as spotifyActions from '../../services/spotify/actions';
import {
uriSource,
} from '../../util/helpers';
import { sortItems, applyFilter } from '../../util/arrays';
import { collate } from '../../util/format';
class LibraryAlbums extends React.Component {
constructor(props) {
super(props);
this.state = {
filter: '',
limit: 50,
per_page: 50,
};
}
componentDidMount() {
// Restore any limit defined in our location state
const state = (this.props.location.state ? this.props.location.state : {});
if (state.limit) {
this.setState({
limit: state.limit,
});
}
this.props.uiActions.setWindowTitle('Albums');
if (this.props.mopidy_connected && this.props.mopidy_library_albums_status != 'finished' && this.props.mopidy_library_albums_status != 'started' && (this.props.source == 'all' || this.props.source == 'local')) {
this.props.mopidyActions.getLibraryAlbums();
}
if (this.props.google_enabled && this.props.google_library_albums_status != 'finished' && this.props.google_library_albums_status != 'started' && (this.props.source == 'all' || this.props.source == 'google')) {
this.props.googleActions.getLibraryAlbums();
}
if (this.props.spotify_enabled && this.props.spotify_library_albums_status != 'finished' && this.props.spotify_library_albums_status != 'started' && (this.props.source == 'all' || this.props.source == 'spotify')) {
this.props.spotifyActions.getLibraryAlbums();
}
}
componentDidUpdate = ({
mopidy_connected: prev_mopidy_connected,
}) => {
const {
mopidy_connected,
google_enabled,
spotify_enabled,
source,
mopidyActions,
googleActions,
spotifyActions,
mopidy_library_albums_status,
google_library_albums_status,
spotify_library_albums_status,
} = this.props;
if (mopidy_connected && (source == 'all' || source == 'local')) {
if (!prev_mopidy_connected) mopidyActions.getLibraryAlbums();
// Filter changed, but we haven't got this provider's library yet
if (source !== 'all' && source !== 'local' && mopidy_library_albums_status !== 'finished' && mopidy_library_albums_status !== 'started') {
mopidyActions.getLibraryAlbums();
}
}
if (google_enabled && (source == 'all' || source == 'google')) {
// Filter changed, but we haven't got this provider's library yet
if (source !== 'all' && source !== 'google' && google_library_albums_status !== 'finished' && google_library_albums_status !== 'started') {
googleActions.getLibraryAlbums();
}
}
if (spotify_enabled && (source === 'all' || source === 'spotify')) {
// Filter changed, but we haven't got this provider's library yet
if (spotify_library_albums_status !== 'finished' && spotify_library_albums_status !== 'started') {
spotifyActions.getLibraryAlbums();
}
}
}
handleContextMenu(e, item) {
const data = {
e,
context: 'album',
uris: [item.uri],
items: [item],
};
this.props.uiActions.showContextMenu(data);
}
moreURIsToLoad() {
const uris = [];
if (this.props.albums && this.props.library_albums) {
for (let i = 0; i < this.props.library_albums.length; i++) {
const uri = this.props.library_albums[i];
if (!this.props.albums.hasOwnProperty(uri) && uriSource(uri) == 'local') {
uris.push(uri);
}
// limit each lookup to 50 URIs
if (uris.length >= 50) break;
}
}
return uris;
}
loadMore() {
const new_limit = this.state.limit + this.state.per_page;
this.setState({ limit: new_limit });
// Set our pagination to location state
const state = (this.props.location && this.props.location.state ? this.props.location.state : {});
state.limit = new_limit;
this.props.history.replace({ state });
}
setSort(value) {
let reverse = false;
if (this.props.sort == value) reverse = !this.props.sort_reverse;
const data = {
library_albums_sort_reverse: reverse,
library_albums_sort: value,
};
this.props.uiActions.set(data);
}
renderView() {
let albums = [];
// Spotify library items
if (this.props.spotify_library_albums && (this.props.source == 'all' || this.props.source == 'spotify')) {
for (var uri of this.props.spotify_library_albums) {
if (this.props.albums.hasOwnProperty(uri)) {
albums.push(this.props.albums[uri]);
}
}
}
// Mopidy library items
if (this.props.mopidy_library_albums && (this.props.source == 'all' || this.props.source == 'local')) {
for (var uri of this.props.mopidy_library_albums) {
// Construct item placeholder. This is used as Mopidy needs to
// lookup ref objects to get the full object which can take some time
var source = uriSource(uri);
var album = {
uri,
source,
};
if (this.props.albums.hasOwnProperty(uri)) {
albums.push(this.props.albums[uri]);
}
}
}
// Google library items
if (this.props.google_library_albums && (this.props.source == 'all' || this.props.source == 'google')) {
for (var uri of this.props.google_library_albums) {
// Construct item placeholder. This is used as Mopidy needs to
// lookup ref objects to get the full object which can take some time
var source = uriSource(uri);
var album = {
uri,
source,
};
if (this.props.albums.hasOwnProperty(uri)) {
album = this.props.albums[uri];
}
albums.push(album);
}
}
// Collate each album into it's full object (including nested artists)
for (let i = 0; i < albums.length; i++) {
albums[i] = collate(albums[i], { artists: this.props.artists });
}
if (this.props.sort) {
albums = sortItems(albums, this.props.sort, this.props.sort_reverse);
}
if (this.state.filter && this.state.filter !== '') {
albums = applyFilter('name', this.state.filter, albums);
}
// Apply our lazy-load-rendering
const total_albums = albums.length;
albums = albums.slice(0, this.state.limit);
if (this.props.view == 'list') {
return (
this.handleContextMenu(e, item)}
rows={albums}
thumbnail
details={['artists', 'tracks_uris.length', 'last_modified']}
right_column={['added_at']}
className="albums"
link_prefix="/album/"
/>