2016-10-17 10:39:11 +13:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
import React from 'react';
|
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
import { bindActionCreators } from 'redux';
|
2020-08-17 19:31:28 +12:00
|
|
|
import Button from '../../components/Button';
|
2019-09-05 15:36:11 +12:00
|
|
|
import PlaylistGrid from '../../components/PlaylistGrid';
|
|
|
|
|
import List from '../../components/List';
|
|
|
|
|
import DropdownField from '../../components/Fields/DropdownField';
|
|
|
|
|
import Header from '../../components/Header';
|
|
|
|
|
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 spotifyActions from '../../services/spotify/actions';
|
2020-03-03 16:39:37 +13:00
|
|
|
import { applyFilter, removeDuplicates, sortItems } from '../../util/arrays';
|
2020-07-19 20:41:14 +12:00
|
|
|
import { I18n, i18n } from '../../locale';
|
2019-09-05 15:36:11 +12:00
|
|
|
|
2019-10-09 09:42:20 +13:00
|
|
|
class LibraryPlaylists extends React.Component {
|
2019-09-05 15:36:11 +12:00
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
filter: '',
|
|
|
|
|
limit: 50,
|
|
|
|
|
per_page: 50,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 13:50:39 +13:00
|
|
|
componentDidMount() {
|
|
|
|
|
// 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,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-19 20:41:14 +12:00
|
|
|
this.props.uiActions.setWindowTitle(i18n('library.playlists.title'));
|
2019-09-05 15:36:11 +12:00
|
|
|
|
2020-08-12 08:06:24 +12:00
|
|
|
this.getMopidyLibrary();
|
|
|
|
|
this.getSpotifyLibrary();
|
2019-09-05 15:36:11 +12:00
|
|
|
}
|
|
|
|
|
|
2020-08-12 08:06:24 +12:00
|
|
|
getMopidyLibrary = () => {
|
2020-03-12 21:32:50 +13:00
|
|
|
const {
|
|
|
|
|
source,
|
|
|
|
|
mopidy_library_playlists,
|
2020-08-12 08:06:24 +12:00
|
|
|
mopidyActions: {
|
|
|
|
|
getLibraryPlaylists,
|
|
|
|
|
},
|
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
|
|
if (source !== 'local' && source !== 'all') return;
|
|
|
|
|
if (mopidy_library_playlists) return;
|
|
|
|
|
|
|
|
|
|
getLibraryPlaylists();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getSpotifyLibrary = () => {
|
|
|
|
|
const {
|
|
|
|
|
source,
|
2020-03-12 21:32:50 +13:00
|
|
|
spotify_library_playlists_status,
|
2020-08-12 08:06:24 +12:00
|
|
|
spotifyActions: {
|
|
|
|
|
getLibraryPlaylists,
|
|
|
|
|
},
|
2020-03-12 21:32:50 +13:00
|
|
|
} = this.props;
|
|
|
|
|
|
2020-08-12 08:06:24 +12:00
|
|
|
if (source !== 'spotify' && source !== 'all') return;
|
|
|
|
|
if (spotify_library_playlists_status === 'finished') return;
|
|
|
|
|
if (spotify_library_playlists_status === 'started') return;
|
2019-09-05 15:36:11 +12:00
|
|
|
|
2020-08-12 08:06:24 +12:00
|
|
|
getLibraryPlaylists();
|
|
|
|
|
};
|
2019-09-05 15:36:11 +12:00
|
|
|
|
2020-08-12 08:06:24 +12:00
|
|
|
componentDidUpdate = ({ source: prevSource }) => {
|
|
|
|
|
const { source } = this.props;
|
|
|
|
|
|
|
|
|
|
if (source !== prevSource) {
|
|
|
|
|
this.getMopidyLibrary();
|
|
|
|
|
this.getSpotifyLibrary();
|
2019-09-05 15:36:11 +12:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleContextMenu(e, item) {
|
|
|
|
|
const data = {
|
|
|
|
|
e,
|
|
|
|
|
context: 'playlist',
|
|
|
|
|
uris: [item.uri],
|
|
|
|
|
items: [item],
|
|
|
|
|
};
|
|
|
|
|
this.props.uiActions.showContextMenu(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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_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]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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) {
|
2020-03-03 16:39:37 +13:00
|
|
|
playlists = sortItems(playlists, this.props.sort, this.props.sort_reverse);
|
2019-09-05 15:36:11 +12:00
|
|
|
}
|
2020-03-03 16:39:37 +13:00
|
|
|
playlists = removeDuplicates(playlists);
|
2019-09-05 15:36:11 +12:00
|
|
|
|
|
|
|
|
if (this.state.filter !== '') {
|
2020-03-03 16:39:37 +13:00
|
|
|
playlists = applyFilter('name', this.state.filter, playlists);
|
2019-09-05 15:36:11 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Apply our lazy-load-rendering
|
|
|
|
|
const total_playlists = playlists.length;
|
|
|
|
|
playlists = playlists.slice(0, this.state.limit);
|
|
|
|
|
|
|
|
|
|
if (this.props.view == 'list') {
|
|
|
|
|
return (
|
|
|
|
|
<section className="content-wrapper">
|
|
|
|
|
<List
|
|
|
|
|
handleContextMenu={(e, item) => this.handleContextMenu(e, item)}
|
|
|
|
|
rows={playlists}
|
|
|
|
|
thumbnail
|
2020-04-19 20:57:43 +12:00
|
|
|
details={['owner', 'tracks_total', 'last_modified']}
|
2019-09-05 15:36:11 +12:00
|
|
|
right_column={['source']}
|
|
|
|
|
className="playlists"
|
2019-10-09 09:42:20 +13:00
|
|
|
link_prefix="/playlist/"
|
2019-09-05 15:36:11 +12:00
|
|
|
/>
|
|
|
|
|
<LazyLoadListener
|
|
|
|
|
loadKey={total_playlists > this.state.limit ? this.state.limit : total_playlists}
|
|
|
|
|
loading={this.state.limit < total_playlists}
|
2019-10-09 09:42:20 +13:00
|
|
|
loadMore={() => this.loadMore()}
|
2019-09-05 15:36:11 +12:00
|
|
|
/>
|
|
|
|
|
</section>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return (
|
|
|
|
|
<section className="content-wrapper">
|
|
|
|
|
<PlaylistGrid
|
|
|
|
|
handleContextMenu={(e, item) => this.handleContextMenu(e, item)}
|
2019-10-09 09:42:20 +13:00
|
|
|
playlists={playlists}
|
2019-09-05 15:36:11 +12:00
|
|
|
/>
|
|
|
|
|
<LazyLoadListener
|
|
|
|
|
loadKey={total_playlists > this.state.limit ? this.state.limit : total_playlists}
|
|
|
|
|
loading={this.state.limit < total_playlists}
|
2019-10-09 09:42:20 +13:00
|
|
|
loadMore={() => this.loadMore()}
|
2019-09-05 15:36:11 +12:00
|
|
|
/>
|
|
|
|
|
</section>
|
2019-10-09 09:42:20 +13:00
|
|
|
);
|
2019-09-05 15:36:11 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const source_options = [
|
|
|
|
|
{
|
|
|
|
|
value: 'all',
|
2020-07-19 20:41:14 +12:00
|
|
|
label: i18n('fields.filters.all'),
|
2019-09-05 15:36:11 +12:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: 'local',
|
2020-07-19 20:41:14 +12:00
|
|
|
label: i18n('services.mopidy.local'),
|
2019-09-05 15:36:11 +12:00
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2020-05-12 20:32:48 +12:00
|
|
|
if (this.props.spotify_available) {
|
2019-09-05 15:36:11 +12:00
|
|
|
source_options.push({
|
|
|
|
|
value: 'spotify',
|
2020-07-19 20:41:14 +12:00
|
|
|
label: i18n('services.spotify.title'),
|
2019-09-05 15:36:11 +12:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const view_options = [
|
|
|
|
|
{
|
|
|
|
|
value: 'thumbnails',
|
2020-07-19 20:41:14 +12:00
|
|
|
label: i18n('fields.filters.thumbnails'),
|
2019-09-05 15:36:11 +12:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: 'list',
|
2020-07-19 20:41:14 +12:00
|
|
|
label: i18n('fields.filters.list'),
|
2019-09-05 15:36:11 +12:00
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const sort_options = [
|
|
|
|
|
{
|
|
|
|
|
value: null,
|
2020-07-19 20:41:14 +12:00
|
|
|
label: i18n('fields.filters.as_loaded'),
|
2019-09-05 15:36:11 +12:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: 'name',
|
2020-07-19 20:41:14 +12:00
|
|
|
label: i18n('fields.filters.name'),
|
2019-09-05 15:36:11 +12:00
|
|
|
},
|
2020-04-19 20:57:43 +12:00
|
|
|
{
|
|
|
|
|
value: 'last_modified',
|
2020-07-19 20:41:14 +12:00
|
|
|
label: i18n('fields.filters.updated'),
|
2020-04-19 20:57:43 +12:00
|
|
|
},
|
2019-09-05 15:36:11 +12:00
|
|
|
{
|
|
|
|
|
value: 'can_edit',
|
2020-07-19 20:41:14 +12:00
|
|
|
label: i18n('fields.filters.editable'),
|
2019-09-05 15:36:11 +12:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: 'owner.id',
|
2020-07-19 20:41:14 +12:00
|
|
|
label: i18n('fields.filters.owner'),
|
2019-09-05 15:36:11 +12:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: 'tracks_total',
|
2020-07-19 20:41:14 +12:00
|
|
|
label: i18n('fields.filters.tracks'),
|
2019-09-05 15:36:11 +12:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: 'source',
|
2020-07-19 20:41:14 +12:00
|
|
|
label: i18n('fields.filters.source'),
|
2019-09-05 15:36:11 +12:00
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const options = (
|
|
|
|
|
<span>
|
|
|
|
|
<FilterField
|
|
|
|
|
initialValue={this.state.filter}
|
2019-10-09 09:42:20 +13:00
|
|
|
handleChange={(value) => this.setState({ filter: value })}
|
|
|
|
|
onSubmit={e => this.props.uiActions.hideContextMenu()}
|
2019-09-05 15:36:11 +12:00
|
|
|
/>
|
|
|
|
|
<DropdownField
|
2019-10-13 09:29:41 +13:00
|
|
|
icon="swap_vert"
|
2020-07-19 20:41:14 +12:00
|
|
|
name={i18n('fields.sort')}
|
2019-09-05 15:36:11 +12:00
|
|
|
value={this.props.sort}
|
2019-10-13 09:29:41 +13:00
|
|
|
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}
|
2019-10-09 09:42:20 +13:00
|
|
|
handleChange={(value) => { this.setSort(value); this.props.uiActions.hideContextMenu(); }}
|
2019-09-05 15:36:11 +12:00
|
|
|
/>
|
|
|
|
|
<DropdownField
|
|
|
|
|
icon="visibility"
|
2020-07-19 20:41:14 +12:00
|
|
|
name={i18n('fields.view')}
|
2019-10-13 09:29:41 +13:00
|
|
|
valueAsLabel
|
2019-09-05 15:36:11 +12:00
|
|
|
value={this.props.view}
|
|
|
|
|
options={view_options}
|
2019-10-09 09:42:20 +13:00
|
|
|
handleChange={(value) => { this.props.uiActions.set({ library_playlists_view: value }); this.props.uiActions.hideContextMenu(); }}
|
2019-09-05 15:36:11 +12:00
|
|
|
/>
|
|
|
|
|
<DropdownField
|
|
|
|
|
icon="cloud"
|
2020-07-19 20:41:14 +12:00
|
|
|
name={i18n('fields.source')}
|
2019-10-13 09:29:41 +13:00
|
|
|
valueAsLabel
|
2019-09-05 15:36:11 +12:00
|
|
|
value={this.props.source}
|
|
|
|
|
options={source_options}
|
2019-10-09 09:42:20 +13:00
|
|
|
handleChange={(value) => { this.props.uiActions.set({ library_playlists_source: value }); this.props.uiActions.hideContextMenu(); }}
|
2019-09-05 15:36:11 +12:00
|
|
|
/>
|
2020-08-16 20:00:10 +12:00
|
|
|
<Button
|
|
|
|
|
to="/playlist/create"
|
|
|
|
|
noHover
|
2020-08-17 19:31:28 +12:00
|
|
|
discrete
|
2020-08-16 20:00:10 +12:00
|
|
|
tracking={{ category: 'Playlist', action: 'Create' }}
|
|
|
|
|
>
|
2019-10-09 09:42:20 +13:00
|
|
|
<Icon name="add_box" />
|
2020-07-19 21:00:00 +12:00
|
|
|
<I18n path="actions.add" />
|
2020-08-16 20:00:10 +12:00
|
|
|
</Button>
|
2019-09-05 15:36:11 +12:00
|
|
|
</span>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="view library-playlists-view">
|
|
|
|
|
<Header options={options} uiActions={this.props.uiActions}>
|
|
|
|
|
<Icon name="queue_music" type="material" />
|
2020-07-19 20:41:14 +12:00
|
|
|
<I18n path="library.playlists.title" />
|
2019-09-05 15:36:11 +12:00
|
|
|
</Header>
|
|
|
|
|
{ this.renderView() }
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2016-10-17 10:39:11 +13:00
|
|
|
}
|
|
|
|
|
|
2020-08-12 08:06:24 +12:00
|
|
|
const mapStateToProps = (state) => ({
|
2019-09-05 15:36:11 +12:00
|
|
|
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),
|
2020-05-12 20:32:48 +12:00
|
|
|
spotify_available: state.spotify.access_token,
|
2019-09-05 15:36:11 +12:00
|
|
|
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 mapDispatchToProps = (dispatch) => ({
|
|
|
|
|
coreActions: bindActionCreators(coreActions, dispatch),
|
|
|
|
|
uiActions: bindActionCreators(uiActions, dispatch),
|
|
|
|
|
mopidyActions: bindActionCreators(mopidyActions, dispatch),
|
|
|
|
|
spotifyActions: bindActionCreators(spotifyActions, dispatch),
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-09 09:42:20 +13:00
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(LibraryPlaylists);
|