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

327 lines
9.4 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';
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';
import { applyFilter, removeDuplicates, sortItems } from '../../util/arrays';
import { I18n, i18n } from '../../locale';
import { collate } from '../../util/format';
2019-09-05 15:36:11 +12: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,
};
}
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,
});
}
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 = () => {
const {
source,
coreActions: {
loadLibrary,
2020-08-12 08:06:24 +12:00
},
} = this.props;
if (source !== 'local' && source !== 'all') return;
loadLibrary('mopidy:library:playlists');
2020-08-12 08:06:24 +12:00
};
getSpotifyLibrary = () => {
const {
source,
spotify_available,
coreActions: {
loadLibrary,
2020-08-12 08:06:24 +12:00
},
} = this.props;
if (!spotify_available) return;
2020-08-12 08:06:24 +12:00
if (source !== 'spotify' && source !== 'all') return;
2019-09-05 15:36:11 +12:00
loadLibrary('spotify:library:playlists');
2020-08-12 08:06:24 +12:00
};
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 = () => {
const {
spotify_library,
mopidy_library,
items,
sort,
sort_reverse,
view,
source,
} = this.props;
const {
filter,
limit,
} = this.state;
2019-09-05 15:36:11 +12:00
let playlists = [
...(source === 'all' || source === 'spotify' ? collate(spotify_library, { items }).items : []),
...(source === 'all' || source === 'local' ? collate(mopidy_library, { items }).items : []),
];
2019-09-05 15:36:11 +12:00
if (sort) {
playlists = sortItems(playlists, sort, sort_reverse);
2019-09-05 15:36:11 +12:00
}
playlists = removeDuplicates(playlists);
2019-09-05 15:36:11 +12:00
if (filter !== '') {
playlists = applyFilter('name', filter, playlists);
2019-09-05 15:36:11 +12:00
}
// Apply our lazy-load-rendering
const total_playlists = playlists.length;
playlists = playlists.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={playlists}
thumbnail
details={['owner', 'tracks_total', 'last_modified']}
2019-09-05 15:36:11 +12:00
right_column={['source']}
className="playlists"
link_prefix="/playlist/"
2019-09-05 15:36:11 +12:00
/>
<LazyLoadListener
loadKey={total_playlists > limit ? limit : total_playlists}
loading={limit < total_playlists}
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)}
playlists={playlists}
2019-09-05 15:36:11 +12:00
/>
<LazyLoadListener
loadKey={total_playlists > limit ? limit : total_playlists}
loading={limit < total_playlists}
loadMore={() => this.loadMore()}
2019-09-05 15:36:11 +12:00
/>
</section>
);
2019-09-05 15:36:11 +12:00
}
render() {
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 (this.props.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
});
}
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: 'last_modified',
label: i18n('fields.filters.updated'),
},
2019-09-05 15:36:11 +12:00
{
value: 'can_edit',
label: i18n('fields.filters.editable'),
2019-09-05 15:36:11 +12:00
},
{
value: 'owner.id',
label: i18n('fields.filters.owner'),
2019-09-05 15:36:11 +12:00
},
{
value: 'tracks_total',
label: i18n('fields.filters.tracks'),
2019-09-05 15:36:11 +12:00
},
{
value: 'source',
label: i18n('fields.filters.source'),
2019-09-05 15:36:11 +12:00
},
];
const options = (
<span>
<FilterField
initialValue={this.state.filter}
handleChange={(value) => this.setState({ filter: value })}
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')}
valueAsLabel
2019-09-05 15:36:11 +12:00
value={this.props.view}
options={view_options}
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"
name={i18n('fields.source')}
valueAsLabel
2019-09-05 15:36:11 +12:00
value={this.props.source}
options={source_options}
handleChange={(value) => { this.props.uiActions.set({ library_playlists_source: value }); this.props.uiActions.hideContextMenu(); }}
2019-09-05 15:36:11 +12:00
/>
<Button
to="/playlist/create"
noHover
discrete
tracking={{ category: 'Playlist', action: 'Create' }}
>
<Icon name="add_box" />
2020-07-19 21:00:00 +12:00
<I18n path="actions.add" />
</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" />
<I18n path="library.playlists.title" />
2019-09-05 15:36:11 +12:00
</Header>
{ this.renderView() }
</div>
);
}
}
const mapStateToProps = (state) => {
return {
slim_mode: state.ui.slim_mode,
mopidy_uri_schemes: state.mopidy.uri_schemes,
spotify_available: state.spotify.access_token,
items: state.core.items,
mopidy_library: state.core.items['mopidy:library:playlists'] || { items_uris: [] },
spotify_library: state.core.items['spotify:library:playlists'] || { items_uris: [] },
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,
};
};
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),
});
export default connect(mapStateToProps, mapDispatchToProps)(LibraryPlaylists);