Files
Iris/src/js/views/library/LibraryArtists.js

371 lines
10 KiB
JavaScript
Raw Normal View History

import React from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import Header from '../../components/Header';
import ArtistGrid from '../../components/ArtistGrid';
import List from '../../components/List';
import DropdownField from '../../components/Fields/DropdownField';
import FilterField from '../../components/Fields/FilterField';
import LazyLoadListener from '../../components/LazyLoadListener';
import Icon from '../../components/Icon';
import * as uiActions from '../../services/ui/actions';
import * as coreActions from '../../services/core/actions';
import {
uriSource, isLoading,
} from '../../util/helpers';
import { sortItems, applyFilter } from '../../util/arrays';
import { I18n, i18n } from '../../locale';
import { collate, collateLibrary } from '../../util/format';
import Button from '../../components/Button';
import Loader from '../../components/Loader';
import {
makeLibrarySelector,
makeLoadingSelector,
} from '../../util/selectors';
class LibraryArtists extends React.Component {
2019-09-05 15:36:11 +12:00
constructor(props) {
super(props);
this.state = {
filter: '',
limit: 50,
per_page: 50,
};
}
componentDidMount() {
const {
uiActions: {
setWindowTitle,
},
} = this.props;
// Restore any limit defined in our location state
2019-09-05 15:36:11 +12:00
const state = (this.props.location.state ? this.props.location.state : {});
if (state.limit) {
this.setState({
limit: state.limit,
});
}
setWindowTitle(i18n('library.artists.title'));
2019-09-05 15:36:11 +12:00
2020-08-12 08:06:24 +12:00
this.getMopidyLibrary();
this.getGoogleLibrary();
this.getSpotifyLibrary();
}
2019-09-05 15:36:11 +12:00
2020-08-12 08:06:24 +12:00
componentDidUpdate = ({ source: prevSource }) => {
const { source } = this.props;
2019-09-05 15:36:11 +12:00
2020-08-12 08:06:24 +12:00
if (source !== prevSource) {
this.getMopidyLibrary();
this.getGoogleLibrary();
this.getSpotifyLibrary();
2019-09-05 15:36:11 +12:00
}
}
onRefresh = () => {
const { uiActions: { hideContextMenu } } = this.props;
hideContextMenu();
this.getMopidyLibrary(true);
this.getGoogleLibrary(true);
this.getSpotifyLibrary(true);
}
setSort = (value) => {
const { sort, sort_reverse, uiActions: { set } } = this.props;
let reverse = false;
if (sort === value) reverse = !sort_reverse;
set({
library_artists_sort_reverse: reverse,
library_artists_sort: value,
});
}
handleContextMenu = (e, item) => {
const { uiActions: { showContextMenu } } = this.props;
showContextMenu({
e,
context: 'artist',
uris: [item.uri],
items: [item],
});
}
getMopidyLibrary = (forceRefetch = false) => {
const {
source,
coreActions: {
loadLibrary,
2020-08-12 08:06:24 +12:00
},
} = this.props;
if (source !== 'local' && source !== 'all') return;
loadLibrary('mopidy:library:artists', { forceRefetch });
2020-08-12 08:06:24 +12:00
};
getGoogleLibrary = (forceRefetch = false) => {
2020-08-12 08:06:24 +12:00
const {
source,
google_available,
coreActions: {
loadLibrary,
2020-08-12 08:06:24 +12:00
},
} = this.props;
if (!google_available) return;
if (source !== 'google' && source !== 'all') return;
loadLibrary('google:library:artists', { forceRefetch });
2020-08-12 08:06:24 +12:00
};
getSpotifyLibrary = (forceRefetch = false) => {
2020-08-12 08:06:24 +12:00
const {
source,
spotify_available,
coreActions: {
loadLibrary,
2020-08-12 08:06:24 +12:00
},
} = this.props;
2020-08-12 08:06:24 +12:00
if (!spotify_available) return;
if (source !== 'spotify' && source !== 'all') return;
loadLibrary('spotify:library:artists', { forceRefetch });
2020-08-12 08:06:24 +12:00
};
2019-09-05 15:36:11 +12:00
loadMore = () => {
2019-09-05 15:36:11 +12:00
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 });
}
renderView = () => {
const {
sort,
sort_reverse,
view,
loading,
} = this.props;
const {
limit,
filter,
} = this.state;
let { artists } = this.props;
if (loading) {
return <Loader body loading />;
}
if (sort) {
artists = sortItems(artists, sort, sort_reverse);
2019-09-05 15:36:11 +12:00
}
if (filter !== '') {
artists = applyFilter('name', filter, artists);
2019-09-05 15:36:11 +12:00
}
// Apply our lazy-load-rendering
const total_artists = artists.length;
artists = artists.slice(0, limit);
2019-09-05 15:36:11 +12:00
if (view === 'list') {
2019-09-05 15:36:11 +12:00
return (
<section className="content-wrapper">
<List
handleContextMenu={(e, item) => this.handleContextMenu(e, item)}
rows={artists}
thumbnail
details={['followers']}
middle_column={['source']}
className="artists"
link_prefix="/artist/"
2019-09-05 15:36:11 +12:00
/>
<LazyLoadListener
loadKey={total_artists > limit ? limit : total_artists}
showLoader={limit < total_artists}
loadMore={() => this.loadMore()}
2019-09-05 15:36:11 +12:00
/>
</section>
);
}
return (
<section className="content-wrapper">
<ArtistGrid
handleContextMenu={(e, item) => this.handleContextMenu(e, item)}
artists={artists}
2019-09-05 15:36:11 +12:00
/>
<LazyLoadListener
loadKey={total_artists > limit ? limit : total_artists}
showLoader={limit < total_artists}
loadMore={() => this.loadMore()}
2019-09-05 15:36:11 +12:00
/>
</section>
);
2019-09-05 15:36:11 +12:00
}
render = () => {
const {
spotify_available,
google_available,
} = this.props;
2019-09-05 15:36:11 +12:00
const source_options = [
{
value: 'all',
label: i18n('fields.filters.all'),
2019-09-05 15:36:11 +12:00
},
{
value: 'local',
label: i18n('services.mopidy.local'),
2019-09-05 15:36:11 +12:00
},
];
if (spotify_available) {
2019-09-05 15:36:11 +12:00
source_options.push({
value: 'spotify',
label: i18n('services.spotify.title'),
2019-09-05 15:36:11 +12:00
});
}
if (google_available) {
2019-09-05 15:36:11 +12:00
source_options.push({
value: 'google',
label: i18n('services.google.title'),
2019-09-05 15:36:11 +12:00
});
}
const view_options = [
{
value: 'thumbnails',
label: i18n('fields.filters.thumbnails'),
2019-09-05 15:36:11 +12:00
},
{
value: 'list',
label: i18n('fields.filters.list'),
2019-09-05 15:36:11 +12:00
},
];
const sort_options = [
{
value: null,
label: i18n('fields.filters.as_loaded'),
2019-09-05 15:36:11 +12:00
},
{
value: 'name',
label: i18n('fields.filters.name'),
2019-09-05 15:36:11 +12:00
},
{
value: 'followers',
label: i18n('fields.filters.followers'),
2019-09-05 15:36:11 +12:00
},
{
value: 'popularity',
label: i18n('fields.filters.popularity'),
2019-09-05 15:36:11 +12:00
},
];
const options = (
<span>
<FilterField
initialValue={this.state.filter}
handleChange={(value) => this.setState({ filter: value, limit: this.state.per_page })}
onSubmit={e => this.props.uiActions.hideContextMenu()}
2019-09-05 15:36:11 +12:00
/>
<DropdownField
icon="swap_vert"
name={i18n('fields.sort')}
2019-09-05 15:36:11 +12:00
value={this.props.sort}
valueAsLabel
2019-09-05 15:36:11 +12:00
options={sort_options}
selected_icon={this.props.sort ? (this.props.sort_reverse ? 'keyboard_arrow_up' : 'keyboard_arrow_down') : null}
handleChange={(value) => { this.setSort(value); this.props.uiActions.hideContextMenu(); }}
2019-09-05 15:36:11 +12:00
/>
<DropdownField
icon="visibility"
name={i18n('fields.view')}
2019-09-05 15:36:11 +12:00
value={this.props.view}
valueAsLabel
2019-09-05 15:36:11 +12:00
options={view_options}
handleChange={(value) => { this.props.uiActions.set({ library_artists_view: value }); this.props.uiActions.hideContextMenu(); }}
2019-09-05 15:36:11 +12:00
/>
<DropdownField
icon="cloud"
name={i18n('fields.source')}
2019-09-05 15:36:11 +12:00
value={this.props.source}
valueAsLabel
2019-09-05 15:36:11 +12:00
options={source_options}
handleChange={(value) => { this.props.uiActions.set({ library_artists_source: value }); this.props.uiActions.hideContextMenu(); }}
2019-09-05 15:36:11 +12:00
/>
<Button
noHover
onClick={this.onRefresh}
tracking={{ category: 'LibraryArtists', action: 'Refresh' }}
>
<Icon name="refresh" />
<I18n path="actions.refresh" />
</Button>
2019-09-05 15:36:11 +12:00
</span>
);
return (
<div className="view library-artists-view">
<Header options={options} uiActions={this.props.uiActions}>
<Icon name="recent_actors" type="material" />
<I18n path="library.artists.title" />
2019-09-05 15:36:11 +12:00
</Header>
{this.renderView()}
</div>
);
}
}
const mapStateToProps = (state) => {
const source = state.ui.library_albums_source ? state.ui.library_albums_source : 'all';
const loadingSelector = makeLoadingSelector(['(.*):library:artists']);
const spotifyLibrarySelector = makeLibrarySelector('spotify:library:artists');
const googleLibrarySelector = makeLibrarySelector('google:library:artists');
const mopidyLibrarySelector = makeLibrarySelector('mopidy:library:artists');
const artists = [
...(source === 'all' || source === 'spotify' ? spotifyLibrarySelector(state) : []),
...(source === 'all' || source === 'google' ? googleLibrarySelector(state) : []),
...(source === 'all' || source === 'local' ? mopidyLibrarySelector(state) : []),
];
/**
TODO
Apply sort, filter and source rules to the selector.
This will mean we have a universal selector for all libraries. WIN!
**/
return {
mopidy_uri_schemes: state.mopidy.uri_schemes,
google_available: (state.mopidy.uri_schemes && state.mopidy.uri_schemes.includes('gmusic:')),
spotify_available: (state.spotify.access_token),
artists,
loading: loadingSelector(state),
source,
sort: (state.ui.library_artists_sort ? state.ui.library_artists_sort : null),
sort_reverse: (state.ui.library_artists_sort_reverse ? state.ui.library_artists_sort_reverse : false),
view: state.ui.library_artists_view,
};
};
2019-09-05 15:36:11 +12:00
const mapDispatchToProps = (dispatch) => ({
uiActions: bindActionCreators(uiActions, dispatch),
coreActions: bindActionCreators(coreActions, dispatch),
2019-09-05 15:36:11 +12:00
});
export default connect(mapStateToProps, mapDispatchToProps)(LibraryArtists);