Merging feat/persist

This commit is contained in:
James Barnsley
2020-10-01 20:45:48 +13:00
70 changed files with 20671 additions and 6662 deletions

View File

@ -14,12 +14,14 @@ 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';
import Button from '../../components/Button';
import { i18n, I18n } from '../../locale';
import Loader from '../../components/Loader';
import {
makeLibrarySelector,
makeLoadingSelector,
} from '../../util/selectors';
class LibraryAlbums extends React.Component {
constructor(props) {
@ -65,55 +67,56 @@ class LibraryAlbums extends React.Component {
}
}
getMopidyLibrary = () => {
onRefresh = () => {
const { uiActions: { hideContextMenu } } = this.props;
hideContextMenu();
this.getMopidyLibrary(true);
this.getGoogleLibrary(true);
this.getSpotifyLibrary(true);
}
getMopidyLibrary = (forceRefetch = false) => {
const {
source,
mopidy_library_playlists,
mopidyActions: {
getLibraryAlbums,
coreActions: {
loadLibrary,
},
} = this.props;
if (source !== 'local' && source !== 'all') return;
if (mopidy_library_playlists) return;
getLibraryAlbums();
loadLibrary('mopidy:library:albums', { forceRefetch });
};
getGoogleLibrary = () => {
getGoogleLibrary = (forceRefetch = false) => {
const {
source,
google_available,
google_library_albums_status,
googleActions: {
getLibraryAlbums,
coreActions: {
loadLibrary,
},
} = this.props;
if (!google_available) return;
if (source !== 'google' && source !== 'all') return;
if (google_library_albums_status === 'finished') return;
if (google_library_albums_status === 'started') return;
getLibraryAlbums();
loadLibrary('google:library:albums', { forceRefetch });
};
getSpotifyLibrary = () => {
getSpotifyLibrary = (forceRefetch = false) => {
const {
source,
spotify_available,
spotify_library_albums_status,
spotifyActions: {
getLibraryAlbums,
coreActions: {
loadLibrary,
},
} = this.props;
if (!spotify_available) return;
if (source !== 'spotify' && source !== 'all') return;
if (spotify_library_albums_status === 'finished') return;
if (spotify_library_albums_status === 'started') return;
getLibraryAlbums();
loadLibrary('spotify:library:albums', { forceRefetch });
};
handleContextMenu = (e, item) => {
@ -131,28 +134,6 @@ class LibraryAlbums extends React.Component {
});
}
moreURIsToLoad = () => {
const {
albums,
library_albums,
} = this.props;
const uris = [];
if (albums && library_albums) {
for (let i = 0; i < library_albums.length; i++) {
const uri = library_albums[i];
if (!albums.hasOwnProperty(uri) && uriSource(uri) == 'local') {
uris.push(uri);
}
// limit each lookup to 50 URIs
if (uris.length >= 50) break;
}
}
return uris;
}
loadMore = () => {
const {
limit,
@ -189,71 +170,35 @@ class LibraryAlbums extends React.Component {
}
renderView = () => {
let albums = [];
const {
sort,
sort_reverse,
view,
loading,
} = this.props;
const {
limit,
filter,
} = this.state;
let { albums } = this.props;
// 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]);
}
}
if (loading) {
return <Loader body loading />;
}
// 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]);
}
}
if (sort) {
albums = sortItems(albums, sort, sort_reverse);
}
// 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);
if (filter && filter !== '') {
albums = applyFilter('name', filter, albums);
}
// Apply our lazy-load-rendering
const total_albums = albums.length;
albums = albums.slice(0, this.state.limit);
albums = albums.slice(0, limit);
if (this.props.view == 'list') {
if (view === 'list') {
return (
<section className="content-wrapper">
<List
@ -266,8 +211,8 @@ class LibraryAlbums extends React.Component {
link_prefix="/album/"
/>
<LazyLoadListener
loadKey={total_albums > this.state.limit ? this.state.limit : total_albums}
showLoader={this.state.limit < total_albums}
loadKey={total_albums > limit ? limit : total_albums}
showLoader={limit < total_albums}
loadMore={this.loadMore}
/>
</section>
@ -280,15 +225,29 @@ class LibraryAlbums extends React.Component {
albums={albums}
/>
<LazyLoadListener
loadKey={total_albums > this.state.limit ? this.state.limit : total_albums}
showLoader={this.state.limit < total_albums}
loadMore={() => this.loadMore()}
loadKey={total_albums > limit ? limit : total_albums}
showLoader={limit < total_albums}
loadMore={this.loadMore}
/>
</section>
);
}
render() {
render = () => {
const {
spotify_available,
google_available,
sort,
view,
source,
sort_reverse,
uiActions,
} = this.props;
const {
filter,
per_page,
} = this.state;
const source_options = [
{
value: 'all',
@ -300,14 +259,14 @@ class LibraryAlbums extends React.Component {
},
];
if (this.props.spotify_available) {
if (spotify_available) {
source_options.push({
value: 'spotify',
label: i18n('services.spotify.title'),
});
}
if (this.props.google_available) {
if (google_available) {
source_options.push({
value: 'google',
label: i18n('services.google.title'),
@ -355,41 +314,50 @@ class LibraryAlbums extends React.Component {
const options = (
<div className="header__options__wrapper">
<FilterField
initialValue={this.state.filter}
handleChange={(value) => this.setState({ filter: value, limit: this.state.per_page })}
onSubmit={e => this.props.uiActions.hideContextMenu()}
initialValue={filter}
handleChange={(value) => this.setState({ filter: value, limit: per_page })}
onSubmit={() => uiActions.hideContextMenu()}
/>
<DropdownField
icon="swap_vert"
name={i18n('fields.sort')}
value={this.props.sort}
value={sort}
valueAsLabel
options={sort_options}
selected_icon={this.props.sort ? (this.props.sort_reverse ? 'keyboard_arrow_up' : 'keyboard_arrow_down') : null}
handleChange={(val) => { this.setSort(val); this.props.uiActions.hideContextMenu(); }}
selected_icon={sort ? (sort_reverse ? 'keyboard_arrow_up' : 'keyboard_arrow_down') : null}
handleChange={(val) => { this.setSort(val); uiActions.hideContextMenu(); }}
/>
<DropdownField
icon="visibility"
name={i18n('fields.view')}
value={this.props.view}
value={view}
valueAsLabel
options={view_options}
handleChange={(val) => { this.props.uiActions.set({ library_albums_view: val }); this.props.uiActions.hideContextMenu(); }}
handleChange={(val) => { uiActions.set({ library_albums_view: val }); uiActions.hideContextMenu(); }}
/>
<DropdownField
icon="cloud"
name={i18n('fields.source')}
value={this.props.source}
value={source}
valueAsLabel
options={source_options}
handleChange={(val) => { this.props.uiActions.set({ library_albums_source: val }); this.props.uiActions.hideContextMenu(); }}
handleChange={(val) => { uiActions.set({ library_albums_source: val }); uiActions.hideContextMenu(); }}
/>
<Button
noHover
discrete
onClick={this.onRefresh}
tracking={{ category: 'LibraryAlbums', action: 'Refresh' }}
>
<Icon name="refresh" />
<I18n path="actions.refresh" />
</Button>
</div>
);
return (
<div className="view library-albums-view">
<Header options={options} uiActions={this.props.uiActions}>
<Header options={options} uiActions={uiActions}>
<Icon name="album" type="material" />
<I18n path="library.albums.title" />
</Header>
@ -399,24 +367,28 @@ class LibraryAlbums extends React.Component {
}
}
const mapStateToProps = (state, ownProps) => ({
mopidy_uri_schemes: state.mopidy.uri_schemes,
load_queue: state.ui.load_queue,
artists: state.core.artists,
albums: state.core.albums,
mopidy_library_albums: state.mopidy.library_albums,
mopidy_library_albums_status: (state.ui.processes.MOPIDY_LIBRARY_ALBUMS_PROCESSOR !== undefined ? state.ui.processes.MOPIDY_LIBRARY_ALBUMS_PROCESSOR.status : null),
google_available: (state.mopidy.uri_schemes && state.mopidy.uri_schemes.includes('gmusic:')),
google_library_albums: state.google.library_albums,
google_library_albums_status: (state.ui.processes.GOOGLE_LIBRARY_ALBUMS_PROCESSOR !== undefined ? state.ui.processes.GOOGLE_LIBRARY_ALBUMS_PROCESSOR.status : null),
spotify_available: state.spotify.access_token,
spotify_library_albums: state.spotify.library_albums,
spotify_library_albums_status: (state.ui.processes.SPOTIFY_GET_LIBRARY_ALBUMS_PROCESSOR !== undefined ? state.ui.processes.SPOTIFY_GET_LIBRARY_ALBUMS_PROCESSOR.status : null),
view: state.ui.library_albums_view,
source: (state.ui.library_albums_source ? state.ui.library_albums_source : 'all'),
sort: (state.ui.library_albums_sort ? state.ui.library_albums_sort : null),
sort_reverse: (state.ui.library_albums_sort_reverse ? state.ui.library_albums_sort_reverse : false),
});
const mapStateToProps = (state) => {
const source = state.ui.library_albums_source ? state.ui.library_albums_source : 'all';
const loadingSelector = makeLoadingSelector(['(.*):library:albums']);
const libraryUris = [];
if (source === 'all' || source === 'local') libraryUris.push('mopidy:library:albums');
if (source === 'all' || source === 'spotify') libraryUris.push('spotify:library:albums');
if (source === 'all' || source === 'google') libraryUris.push('google:library:albums');
const librarySelector = makeLibrarySelector(libraryUris);
return {
loading: loadingSelector(state),
mopidy_uri_schemes: state.mopidy.uri_schemes,
albums: librarySelector(state),
google_available: (state.mopidy.uri_schemes && state.mopidy.uri_schemes.includes('gmusic:')),
spotify_available: state.spotify.access_token,
view: state.ui.library_albums_view,
source,
sort: state.ui.library_albums_sort,
sort_reverse: state.ui.library_albums_sort_reverse,
};
};
const mapDispatchToProps = (dispatch) => ({
coreActions: bindActionCreators(coreActions, dispatch),

View File

@ -10,14 +10,19 @@ 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 mopidyActions from '../../services/mopidy/actions';
import * as spotifyActions from '../../services/spotify/actions';
import * as googleActions from '../../services/google/actions';
import * as coreActions from '../../services/core/actions';
import {
uriSource,
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 {
constructor(props) {
@ -62,67 +67,79 @@ class LibraryArtists extends React.Component {
}
}
getMopidyLibrary = () => {
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,
mopidy_library_artists,
mopidyActions: {
getLibraryArtists,
coreActions: {
loadLibrary,
},
} = this.props;
if (source !== 'local' && source !== 'all') return;
if (mopidy_library_artists) return;
getLibraryArtists();
loadLibrary('mopidy:library:artists', { forceRefetch });
};
getGoogleLibrary = () => {
getGoogleLibrary = (forceRefetch = false) => {
const {
source,
google_available,
google_library_artists,
googleActions: {
getLibraryArtists,
coreActions: {
loadLibrary,
},
} = this.props;
if (!google_available) return;
if (source !== 'google' && source !== 'all') return;
if (google_library_artists) return;
getLibraryArtists();
loadLibrary('google:library:artists', { forceRefetch });
};
getSpotifyLibrary = () => {
getSpotifyLibrary = (forceRefetch = false) => {
const {
source,
spotify_available,
spotify_library_artists_status,
spotifyActions: {
getLibraryArtists,
coreActions: {
loadLibrary,
},
} = this.props;
if (!spotify_available) return;
if (source !== 'spotify' && source !== 'all') return;
if (spotify_library_artists_status === 'finished') return;
if (spotify_library_artists_status === 'started') return;
getLibraryArtists();
loadLibrary('spotify:library:artists', { forceRefetch });
};
handleContextMenu(e, item) {
const data = {
e,
context: 'artist',
uris: [item.uri],
items: [item],
};
this.props.uiActions.showContextMenu(data);
}
loadMore() {
loadMore = () => {
const new_limit = this.state.limit + this.state.per_page;
this.setState({ limit: new_limit });
@ -133,81 +150,36 @@ class LibraryArtists extends React.Component {
this.props.history.replace({ state });
}
setSort(value) {
let reverse = false;
if (this.props.sort == value) reverse = !this.props.sort_reverse;
renderView = () => {
const {
sort,
sort_reverse,
view,
loading,
} = this.props;
const {
limit,
filter,
} = this.state;
let { artists } = this.props;
const data = {
library_artists_sort_reverse: reverse,
library_artists_sort: value,
};
this.props.uiActions.set(data);
}
renderView() {
let artists = [];
// Mopidy library items
if (this.props.mopidy_library_artists && (this.props.source == 'all' || this.props.source == 'local')) {
for (uri of this.props.mopidy_library_artists) {
// 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 artist = {
uri,
source,
};
if (this.props.artists.hasOwnProperty(uri)) {
artist = this.props.artists[uri];
}
artists.push(artist);
}
if (loading) {
return <Loader body loading />;
}
// Google library items
if (this.props.google_library_artists && (this.props.source == 'all' || this.props.source == 'google')) {
for (uri of this.props.google_library_artists) {
// 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 artist = {
uri,
source,
};
if (this.props.artists.hasOwnProperty(uri)) {
artist = this.props.artists[uri];
}
artists.push(artist);
}
if (sort) {
artists = sortItems(artists, sort, sort_reverse);
}
// Spotify library items
if (this.props.spotify_library_artists && (this.props.source == 'all' || this.props.source == 'spotify')) {
for (let i = 0; i < this.props.spotify_library_artists.length; i++) {
var uri = this.props.spotify_library_artists[i];
if (this.props.artists.hasOwnProperty(uri)) {
artists.push(this.props.artists[uri]);
}
}
}
if (this.props.sort) {
artists = sortItems(artists, this.props.sort, this.props.sort_reverse);
}
if (this.state.filter !== '') {
artists = applyFilter('name', this.state.filter, artists);
if (filter !== '') {
artists = applyFilter('name', filter, artists);
}
// Apply our lazy-load-rendering
const total_artists = artists.length;
artists = artists.slice(0, this.state.limit);
artists = artists.slice(0, limit);
if (this.props.view == 'list') {
if (view === 'list') {
return (
<section className="content-wrapper">
<List
@ -220,8 +192,8 @@ class LibraryArtists extends React.Component {
link_prefix="/artist/"
/>
<LazyLoadListener
loadKey={total_artists > this.state.limit ? this.state.limit : total_artists}
showLoader={this.state.limit < total_artists}
loadKey={total_artists > limit ? limit : total_artists}
showLoader={limit < total_artists}
loadMore={() => this.loadMore()}
/>
</section>
@ -234,15 +206,20 @@ class LibraryArtists extends React.Component {
artists={artists}
/>
<LazyLoadListener
loadKey={total_artists > this.state.limit ? this.state.limit : total_artists}
showLoader={this.state.limit < total_artists}
loadKey={total_artists > limit ? limit : total_artists}
showLoader={limit < total_artists}
loadMore={() => this.loadMore()}
/>
</section>
);
}
render() {
render = () => {
const {
spotify_available,
google_available,
} = this.props;
const source_options = [
{
value: 'all',
@ -254,14 +231,14 @@ class LibraryArtists extends React.Component {
},
];
if (this.props.spotify_available) {
if (spotify_available) {
source_options.push({
value: 'spotify',
label: i18n('services.spotify.title'),
});
}
if (this.props.google_available) {
if (google_available) {
source_options.push({
value: 'google',
label: i18n('services.google.title'),
@ -330,6 +307,15 @@ class LibraryArtists extends React.Component {
options={source_options}
handleChange={(value) => { this.props.uiActions.set({ library_artists_source: value }); this.props.uiActions.hideContextMenu(); }}
/>
<Button
noHover
discrete
onClick={this.onRefresh}
tracking={{ category: 'LibraryArtists', action: 'Refresh' }}
>
<Icon name="refresh" />
<I18n path="actions.refresh" />
</Button>
</span>
);
@ -345,26 +331,32 @@ class LibraryArtists extends React.Component {
}
}
const mapStateToProps = (state) => ({
mopidy_uri_schemes: state.mopidy.uri_schemes,
mopidy_library_artists: state.mopidy.library_artists,
google_available: (state.mopidy.uri_schemes && state.mopidy.uri_schemes.includes('gmusic:')),
google_library_artists: state.google.library_artists,
spotify_available: state.spotify.access_token,
spotify_library_artists: state.spotify.library_artists,
spotify_library_artists_status: (state.ui.processes.SPOTIFY_GET_LIBRARY_ARTISTS_PROCESSOR !== undefined ? state.ui.processes.SPOTIFY_GET_LIBRARY_ARTISTS_PROCESSOR.status : null),
artists: state.core.artists,
source: (state.ui.library_artists_source ? state.ui.library_artists_source : 'all'),
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,
});
const mapStateToProps = (state) => {
const source = state.ui.library_artists_source || 'all';
const loadingSelector = makeLoadingSelector(['(.*):library:artists']);
const libraryUris = [];
if (source === 'all' || source === 'local') libraryUris.push('mopidy:library:artists');
if (source === 'all' || source === 'spotify') libraryUris.push('spotify:library:artists');
if (source === 'all' || source === 'google') libraryUris.push('google:library:artists');
const librarySelector = makeLibrarySelector(libraryUris);
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: librarySelector(state),
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,
};
};
const mapDispatchToProps = (dispatch) => ({
uiActions: bindActionCreators(uiActions, dispatch),
mopidyActions: bindActionCreators(mopidyActions, dispatch),
spotifyActions: bindActionCreators(spotifyActions, dispatch),
googleActions: bindActionCreators(googleActions, dispatch),
coreActions: bindActionCreators(coreActions, dispatch),
});
export default connect(mapStateToProps, mapDispatchToProps)(LibraryArtists);

View File

@ -86,23 +86,31 @@ class LibraryBrowseDirectory extends React.Component {
});
}
playAll(e, tracks) {
const tracks_uris = arrayOf('uri', tracks);
this.props.mopidyActions.playURIs(tracks_uris, `iris:browse:${this.props.uri}`);
this.props.uiActions.hideContextMenu();
playAll = (tracks) => {
const {
uri,
mopidyActions: {
playURIs,
},
uiActions: {
hideContextMenu,
},
} = this.props;
playURIs(arrayOf('uri', tracks), `iris:browse:${uri}`);
hideContextMenu();
}
goBack(e) {
goBack = () => {
const { uiActions: { hideContextMenu } } = this.props;
window.history.back();
this.props.uiActions.hideContextMenu();
hideContextMenu();
}
renderBreadcrumbs() {
if (this.props.uri) {
var parent_uri = this.props.uri;
} else {
return null;
}
renderBreadcrumbs = () => {
const { uri } = this.props;
let parent_uri = uri || null;
if (parent_uri.startsWith('file://')) {
parent_uri = parent_uri.substring(0, parent_uri.lastIndexOf('/'));
@ -120,11 +128,12 @@ class LibraryBrowseDirectory extends React.Component {
return null;
}
renderSubdirectories(subdirectories) {
if (this.props.view === 'list') {
renderSubdirectories = (subdirectories) => {
const { view } = this.props;
if (view === 'list') {
return (
<List
nocontext
rows={subdirectories}
className="library-local-directory-list"
link_prefix="/library/browse/"
@ -219,7 +228,7 @@ class LibraryBrowseDirectory extends React.Component {
/>
{tracks && (
<Button
onClick={(e) => { uiActions.hideContextMenu(); this.playAll(e, all_tracks); }}
onClick={() => { uiActions.hideContextMenu(); this.playAll(all_tracks); }}
noHover
discrete
tracking={{ category: 'Directory', action: 'Play' }}

View File

@ -16,6 +16,10 @@ import * as mopidyActions from '../../services/mopidy/actions';
import * as spotifyActions from '../../services/spotify/actions';
import { applyFilter, removeDuplicates, sortItems } from '../../util/arrays';
import { I18n, i18n } from '../../locale';
import { collate } from '../../util/format';
import { isLoading } from '../../util/helpers';
import Loader from '../../components/Loader';
import { makeLibrarySelector, makeLoadingSelector } from '../../util/selectors';
class LibraryPlaylists extends React.Component {
constructor(props) {
@ -29,51 +33,62 @@ class LibraryPlaylists extends React.Component {
}
componentDidMount() {
// Restore any limit defined in our location state
const state = (this.props.location.state ? this.props.location.state : {});
if (state.limit) {
const {
location: {
state: {
limit,
} = {},
} = {},
uiActions: {
setWindowTitle,
},
} = this.props;
if (limit) {
this.setState({
limit: state.limit,
limit,
});
}
this.props.uiActions.setWindowTitle(i18n('library.playlists.title'));
setWindowTitle(i18n('library.playlists.title'));
this.getMopidyLibrary();
this.getSpotifyLibrary();
}
getMopidyLibrary = () => {
onRefresh = () => {
const { uiActions: { hideContextMenu } } = this.props;
hideContextMenu();
this.getMopidyLibrary(true);
this.getSpotifyLibrary(true);
}
getMopidyLibrary = (forceRefetch = false) => {
const {
source,
mopidy_library_playlists,
mopidyActions: {
getLibraryPlaylists,
coreActions: {
loadLibrary,
},
} = this.props;
if (source !== 'local' && source !== 'all') return;
if (mopidy_library_playlists) return;
getLibraryPlaylists();
loadLibrary('mopidy:library:playlists', { forceRefetch });
};
getSpotifyLibrary = () => {
getSpotifyLibrary = (forceRefetch = false) => {
const {
source,
spotify_available,
spotify_library_playlists_status,
spotifyActions: {
getLibraryPlaylists,
coreActions: {
loadLibrary,
},
} = this.props;
if (!spotify_available) return;
if (source !== 'spotify' && source !== 'all') return;
if (spotify_library_playlists_status === 'finished') return;
if (spotify_library_playlists_status === 'started') return;
getLibraryPlaylists();
loadLibrary('spotify:library:playlists', { forceRefetch });
};
componentDidUpdate = ({ source: prevSource }) => {
@ -85,6 +100,17 @@ class LibraryPlaylists extends React.Component {
}
}
setSort(value) {
let reverse = false;
if (this.props.sort == value) reverse = !this.props.sort_reverse;
const data = {
library_playlists_sort_reverse: reverse,
library_playlists_sort: value,
};
this.props.uiActions.set(data);
}
handleContextMenu(e, item) {
const data = {
e,
@ -106,54 +132,38 @@ class LibraryPlaylists extends React.Component {
this.props.history.replace({ state });
}
setSort(value) {
let reverse = false;
if (this.props.sort == value) reverse = !this.props.sort_reverse;
renderView = () => {
const {
sort,
sort_reverse,
view,
loading,
playlists: playlistsProp,
} = this.props;
const {
filter,
limit,
} = this.state;
const data = {
library_playlists_sort_reverse: reverse,
library_playlists_sort: value,
};
this.props.uiActions.set(data);
}
renderView() {
let playlists = [];
// Spotify library items
if (this.props.spotify_library_playlists && (this.props.source == 'all' || this.props.source == 'spotify')) {
for (var i = 0; i < this.props.spotify_library_playlists.length; i++) {
var uri = this.props.spotify_library_playlists[i];
if (this.props.playlists.hasOwnProperty(uri)) {
playlists.push(this.props.playlists[uri]);
}
}
if (loading) {
return <Loader body loading />;
}
let playlists = [...playlistsProp];
// Mopidy library items
if (this.props.mopidy_library_playlists && (this.props.source == 'all' || this.props.source == 'local')) {
for (var i = 0; i < this.props.mopidy_library_playlists.length; i++) {
var uri = this.props.mopidy_library_playlists[i];
if (this.props.playlists.hasOwnProperty(uri)) {
playlists.push(this.props.playlists[uri]);
}
}
}
if (this.props.sort) {
playlists = sortItems(playlists, this.props.sort, this.props.sort_reverse);
if (sort) {
playlists = sortItems(playlists, sort, sort_reverse);
}
playlists = removeDuplicates(playlists);
if (this.state.filter !== '') {
playlists = applyFilter('name', this.state.filter, playlists);
if (filter !== '') {
playlists = applyFilter('name', filter, playlists);
}
// Apply our lazy-load-rendering
const total_playlists = playlists.length;
playlists = playlists.slice(0, this.state.limit);
playlists = playlists.slice(0, limit);
if (this.props.view == 'list') {
if (view === 'list') {
return (
<section className="content-wrapper">
<List
@ -166,8 +176,8 @@ class LibraryPlaylists extends React.Component {
link_prefix="/playlist/"
/>
<LazyLoadListener
loadKey={total_playlists > this.state.limit ? this.state.limit : total_playlists}
loading={this.state.limit < total_playlists}
loadKey={total_playlists > limit ? limit : total_playlists}
loading={limit < total_playlists}
loadMore={() => this.loadMore()}
/>
</section>
@ -180,15 +190,27 @@ class LibraryPlaylists extends React.Component {
playlists={playlists}
/>
<LazyLoadListener
loadKey={total_playlists > this.state.limit ? this.state.limit : total_playlists}
loading={this.state.limit < total_playlists}
loadKey={total_playlists > limit ? limit : total_playlists}
loading={limit < total_playlists}
loadMore={() => this.loadMore()}
/>
</section>
);
}
render() {
render = () => {
const {
uiActions,
spotify_available,
source,
sort,
sort_reverse,
view,
} = this.props;
const {
filter,
} = this.state;
const source_options = [
{
value: 'all',
@ -200,7 +222,7 @@ class LibraryPlaylists extends React.Component {
},
];
if (this.props.spotify_available) {
if (spotify_available) {
source_options.push({
value: 'spotify',
label: i18n('services.spotify.title'),
@ -252,35 +274,44 @@ class LibraryPlaylists extends React.Component {
const options = (
<span>
<FilterField
initialValue={this.state.filter}
initialValue={filter}
handleChange={(value) => this.setState({ filter: value })}
onSubmit={e => this.props.uiActions.hideContextMenu()}
onSubmit={() => uiActions.hideContextMenu()}
/>
<DropdownField
icon="swap_vert"
name={i18n('fields.sort')}
value={this.props.sort}
value={sort}
valueAsLabel
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(); }}
selected_icon={sort ? (sort_reverse ? 'keyboard_arrow_up' : 'keyboard_arrow_down') : null}
handleChange={(value) => { this.setSort(value); uiActions.hideContextMenu(); }}
/>
<DropdownField
icon="visibility"
name={i18n('fields.view')}
valueAsLabel
value={this.props.view}
value={view}
options={view_options}
handleChange={(value) => { this.props.uiActions.set({ library_playlists_view: value }); this.props.uiActions.hideContextMenu(); }}
handleChange={(value) => { uiActions.set({ library_playlists_view: value }); uiActions.hideContextMenu(); }}
/>
<DropdownField
icon="cloud"
name={i18n('fields.source')}
valueAsLabel
value={this.props.source}
value={source}
options={source_options}
handleChange={(value) => { this.props.uiActions.set({ library_playlists_source: value }); this.props.uiActions.hideContextMenu(); }}
handleChange={(value) => { uiActions.set({ library_playlists_source: value }); uiActions.hideContextMenu(); }}
/>
<Button
noHover
discrete
onClick={this.onRefresh}
tracking={{ category: 'LibraryAlbums', action: 'Refresh' }}
>
<Icon name="refresh" />
<I18n path="actions.refresh" />
</Button>
<Button
to="/playlist/create"
noHover
@ -295,7 +326,7 @@ class LibraryPlaylists extends React.Component {
return (
<div className="view library-playlists-view">
<Header options={options} uiActions={this.props.uiActions}>
<Header options={options} uiActions={uiActions}>
<Icon name="queue_music" type="material" />
<I18n path="library.playlists.title" />
</Header>
@ -305,22 +336,29 @@ class LibraryPlaylists extends React.Component {
}
}
const mapStateToProps = (state) => ({
slim_mode: state.ui.slim_mode,
mopidy_uri_schemes: state.mopidy.uri_schemes,
mopidy_library_playlists: state.mopidy.library_playlists,
mopidy_library_playlists_status: (state.ui.processes.MOPIDY_LIBRARY_PLAYLISTS_PROCESSOR !== undefined ? state.ui.processes.MOPIDY_LIBRARY_PLAYLISTS_PROCESSOR.status : null),
spotify_available: state.spotify.access_token,
spotify_library_playlists: state.spotify.library_playlists,
spotify_library_playlists_status: (state.ui.processes.SPOTIFY_GET_LIBRARY_PLAYLISTS_PROCESSOR !== undefined ? state.ui.processes.SPOTIFY_GET_LIBRARY_PLAYLISTS_PROCESSOR.status : null),
load_queue: state.ui.load_queue,
me_id: (state.spotify.me ? state.spotify.me.id : false),
view: state.ui.library_playlists_view,
source: (state.ui.library_playlists_source ? state.ui.library_playlists_source : 'all'),
sort: (state.ui.library_playlists_sort ? state.ui.library_playlists_sort : null),
sort_reverse: (state.ui.library_playlists_sort_reverse ? state.ui.library_playlists_sort_reverse : false),
playlists: state.core.playlists,
});
const mapStateToProps = (state) => {
const source = state.ui.library_playlists_source || 'all';
const loadingSelector = makeLoadingSelector(['(.*):library:playlists']);
const libraryUris = [];
if (source === 'all' || source === 'local') libraryUris.push('mopidy:library:playlists');
if (source === 'all' || source === 'spotify') libraryUris.push('spotify:library:playlists');
if (source === 'all' || source === 'google') libraryUris.push('google:library:playlists');
const librarySelector = makeLibrarySelector(libraryUris);
return {
slim_mode: state.ui.slim_mode,
mopidy_uri_schemes: state.mopidy.uri_schemes,
spotify_available: state.spotify.access_token,
playlists: librarySelector(state),
loading: loadingSelector(state),
source,
me_id: (state.spotify.me ? state.spotify.me.id : false),
view: state.ui.library_playlists_view,
sort: (state.ui.library_playlists_sort ? state.ui.library_playlists_sort : null),
sort_reverse: (state.ui.library_playlists_sort_reverse ? state.ui.library_playlists_sort_reverse : false),
};
};
const mapDispatchToProps = (dispatch) => ({
coreActions: bindActionCreators(coreActions, dispatch),