2016-10-20 21:38:01 +13:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
import React from 'react';
|
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
|
import TrackList from '../../components/TrackList';
|
|
|
|
|
import Header from '../../components/Header';
|
2020-10-23 23:11:24 +13:00
|
|
|
import DropdownField from '../../components/Fields/DropdownField';
|
|
|
|
|
import FilterField from '../../components/Fields/FilterField';
|
2019-09-05 15:36:11 +12:00
|
|
|
import LazyLoadListener from '../../components/LazyLoadListener';
|
|
|
|
|
import Icon from '../../components/Icon';
|
2020-10-23 23:11:24 +13:00
|
|
|
import * as coreActions from '../../services/core/actions';
|
2019-09-05 15:36:11 +12:00
|
|
|
import * as uiActions from '../../services/ui/actions';
|
|
|
|
|
import * as mopidyActions from '../../services/mopidy/actions';
|
|
|
|
|
import * as spotifyActions from '../../services/spotify/actions';
|
2020-11-12 20:52:08 +13:00
|
|
|
import { sortItems, applyFilter, arrayOf } from '../../util/arrays';
|
2020-08-16 20:00:10 +12:00
|
|
|
import Button from '../../components/Button';
|
2020-10-23 23:11:24 +13:00
|
|
|
import { i18n, I18n } from '../../locale';
|
|
|
|
|
import Loader from '../../components/Loader';
|
|
|
|
|
import {
|
|
|
|
|
makeLibrarySelector,
|
|
|
|
|
makeProcessProgressSelector,
|
|
|
|
|
} from '../../util/selectors';
|
|
|
|
|
|
|
|
|
|
const processKeys = [
|
|
|
|
|
'MOPIDY_GET_LIBRARY_TRACKS',
|
|
|
|
|
'SPOTIFY_GET_LIBRARY_TRACKS',
|
|
|
|
|
];
|
2019-09-05 15:36:11 +12:00
|
|
|
|
2019-09-10 06:59:44 +12:00
|
|
|
class LibraryTracks extends React.Component {
|
2020-10-23 23:11:24 +13:00
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
filter: '',
|
|
|
|
|
limit: 50,
|
|
|
|
|
per_page: 50,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
componentDidMount() {
|
2020-10-23 23:11:24 +13:00
|
|
|
const {
|
|
|
|
|
location: {
|
|
|
|
|
state = {},
|
|
|
|
|
},
|
|
|
|
|
uiActions: {
|
|
|
|
|
setWindowTitle,
|
|
|
|
|
},
|
|
|
|
|
} = this.props;
|
2019-09-05 15:36:11 +12:00
|
|
|
|
2020-10-23 23:11:24 +13:00
|
|
|
// Restore any limit defined in our location state
|
|
|
|
|
if (state.limit) {
|
|
|
|
|
this.setState({
|
|
|
|
|
limit: state.limit,
|
2020-07-19 20:41:14 +12:00
|
|
|
});
|
2019-09-05 15:36:11 +12:00
|
|
|
}
|
2020-10-23 23:11:24 +13:00
|
|
|
|
|
|
|
|
setWindowTitle(i18n('library.tracks.title'));
|
|
|
|
|
this.getMopidyLibrary();
|
|
|
|
|
this.getSpotifyLibrary();
|
2019-09-05 15:36:11 +12:00
|
|
|
}
|
|
|
|
|
|
2020-10-23 23:11:24 +13:00
|
|
|
componentDidUpdate = ({ source: prevSource }) => {
|
|
|
|
|
const { source } = this.props;
|
|
|
|
|
|
|
|
|
|
if (source !== prevSource) {
|
|
|
|
|
this.getMopidyLibrary();
|
|
|
|
|
this.getSpotifyLibrary();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
refresh = () => {
|
|
|
|
|
const { uiActions: { hideContextMenu } } = this.props;
|
|
|
|
|
|
|
|
|
|
hideContextMenu();
|
|
|
|
|
this.getMopidyLibrary(true);
|
|
|
|
|
this.getSpotifyLibrary(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cancelRefresh = () => {
|
|
|
|
|
const { uiActions: { hideContextMenu, cancelProcess } } = this.props;
|
|
|
|
|
|
|
|
|
|
hideContextMenu();
|
|
|
|
|
cancelProcess(processKeys);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getMopidyLibrary = (forceRefetch = false) => {
|
|
|
|
|
const {
|
|
|
|
|
source,
|
|
|
|
|
coreActions: {
|
|
|
|
|
loadLibrary,
|
2019-09-05 15:36:11 +12:00
|
|
|
},
|
2020-10-23 23:11:24 +13:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
|
|
if (source !== 'local' && source !== 'all') return;
|
|
|
|
|
|
|
|
|
|
loadLibrary('mopidy:library:tracks', { forceRefetch });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getSpotifyLibrary = (forceRefetch = false) => {
|
|
|
|
|
const {
|
|
|
|
|
source,
|
|
|
|
|
spotify_available,
|
|
|
|
|
coreActions: {
|
|
|
|
|
loadLibrary,
|
|
|
|
|
},
|
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
|
|
if (!spotify_available) return;
|
|
|
|
|
if (source !== 'spotify' && source !== 'all') return;
|
|
|
|
|
|
|
|
|
|
loadLibrary('spotify:library:tracks', { forceRefetch });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handleContextMenu = (e, item) => {
|
|
|
|
|
const {
|
|
|
|
|
uiActions: {
|
|
|
|
|
showContextMenu,
|
|
|
|
|
},
|
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
|
|
showContextMenu({
|
|
|
|
|
e,
|
|
|
|
|
context: 'album',
|
|
|
|
|
uris: [item.uri],
|
|
|
|
|
items: [item],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loadMore = () => {
|
|
|
|
|
const {
|
|
|
|
|
limit,
|
|
|
|
|
per_page,
|
|
|
|
|
} = this.state;
|
|
|
|
|
const {
|
|
|
|
|
location: {
|
|
|
|
|
state,
|
|
|
|
|
},
|
|
|
|
|
history,
|
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
|
|
const new_limit = limit + per_page;
|
|
|
|
|
this.setState({ limit: new_limit });
|
|
|
|
|
history.replace({ state: { ...state, limit: new_limit } });
|
2019-09-05 15:36:11 +12:00
|
|
|
}
|
|
|
|
|
|
2020-10-23 23:11:24 +13:00
|
|
|
setSort = (value) => {
|
|
|
|
|
const {
|
|
|
|
|
sort,
|
|
|
|
|
sort_reverse,
|
|
|
|
|
uiActions: {
|
|
|
|
|
set,
|
|
|
|
|
},
|
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
|
|
let reverse = false;
|
|
|
|
|
if (sort === value) reverse = !sort_reverse;
|
|
|
|
|
|
|
|
|
|
set({
|
|
|
|
|
library_tracks_sort_reverse: reverse,
|
|
|
|
|
library_tracks_sort: value,
|
|
|
|
|
});
|
2019-09-05 15:36:11 +12:00
|
|
|
}
|
|
|
|
|
|
2020-11-12 20:52:08 +13:00
|
|
|
playAll = () => {
|
|
|
|
|
const {
|
|
|
|
|
sort,
|
|
|
|
|
sort_reverse,
|
|
|
|
|
mopidyActions: {
|
|
|
|
|
playURIs,
|
|
|
|
|
},
|
|
|
|
|
uiActions: {
|
|
|
|
|
hideContextMenu,
|
|
|
|
|
},
|
|
|
|
|
} = this.props;
|
|
|
|
|
let { tracks } = this.props;
|
|
|
|
|
const { filter } = this.state;
|
|
|
|
|
|
|
|
|
|
if (!tracks || !tracks.length) return;
|
|
|
|
|
|
|
|
|
|
if (sort) {
|
|
|
|
|
tracks = sortItems(tracks, sort, sort_reverse);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter && filter !== '') {
|
|
|
|
|
tracks = applyFilter('name', filter, tracks);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
playURIs(arrayOf('uri', tracks));
|
|
|
|
|
hideContextMenu();
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-23 23:11:24 +13:00
|
|
|
renderView = () => {
|
|
|
|
|
const {
|
|
|
|
|
sort,
|
|
|
|
|
sort_reverse,
|
|
|
|
|
loading_progress,
|
|
|
|
|
} = this.props;
|
|
|
|
|
const {
|
|
|
|
|
limit,
|
|
|
|
|
filter,
|
|
|
|
|
} = this.state;
|
|
|
|
|
let { tracks } = this.props;
|
|
|
|
|
|
2020-12-05 21:41:30 +13:00
|
|
|
if (loading_progress) {
|
2020-10-23 23:11:24 +13:00
|
|
|
return <Loader body loading progress={loading_progress} />;
|
2019-09-05 15:36:11 +12:00
|
|
|
}
|
|
|
|
|
|
2020-10-23 23:11:24 +13:00
|
|
|
if (sort) {
|
|
|
|
|
tracks = sortItems(tracks, sort, sort_reverse);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter && filter !== '') {
|
|
|
|
|
tracks = applyFilter('name', filter, tracks);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Apply our lazy-load-rendering
|
|
|
|
|
const total_tracks = tracks.length;
|
|
|
|
|
tracks = tracks.slice(0, limit);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<section className="content-wrapper">
|
2020-11-12 20:52:08 +13:00
|
|
|
<TrackList
|
|
|
|
|
tracks={tracks}
|
|
|
|
|
/>
|
2020-10-23 23:11:24 +13:00
|
|
|
<LazyLoadListener
|
|
|
|
|
loadKey={total_tracks > limit ? limit : total_tracks}
|
|
|
|
|
showLoader={limit < total_tracks}
|
|
|
|
|
loadMore={this.loadMore}
|
|
|
|
|
/>
|
|
|
|
|
</section>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render = () => {
|
|
|
|
|
const {
|
|
|
|
|
spotify_available,
|
|
|
|
|
sort,
|
|
|
|
|
source,
|
|
|
|
|
sort_reverse,
|
|
|
|
|
uiActions,
|
|
|
|
|
loading_progress,
|
|
|
|
|
} = this.props;
|
|
|
|
|
const {
|
|
|
|
|
filter,
|
|
|
|
|
per_page,
|
|
|
|
|
} = this.state;
|
|
|
|
|
|
|
|
|
|
const source_options = [
|
|
|
|
|
{
|
|
|
|
|
value: 'all',
|
|
|
|
|
label: i18n('fields.filters.all'),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: 'local',
|
|
|
|
|
label: i18n('services.mopidy.local'),
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (spotify_available) {
|
|
|
|
|
source_options.push({
|
|
|
|
|
value: 'spotify',
|
|
|
|
|
label: i18n('services.spotify.title'),
|
|
|
|
|
});
|
2019-09-05 15:36:11 +12:00
|
|
|
}
|
|
|
|
|
|
2020-10-23 23:11:24 +13:00
|
|
|
const sort_options = [
|
|
|
|
|
{
|
|
|
|
|
value: null,
|
|
|
|
|
label: i18n('fields.filters.as_loaded'),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: 'name',
|
|
|
|
|
label: i18n('fields.filters.name'),
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-11-08 20:49:48 +13:00
|
|
|
value: 'artist',
|
2020-10-23 23:11:24 +13:00
|
|
|
label: i18n('fields.filters.artist'),
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-11-08 20:49:48 +13:00
|
|
|
value: 'album',
|
2020-10-26 21:32:53 +13:00
|
|
|
label: i18n('fields.filters.album'),
|
2020-10-23 23:11:24 +13:00
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
const options = (
|
2020-11-08 20:49:48 +13:00
|
|
|
<>
|
2020-10-23 23:11:24 +13:00
|
|
|
<FilterField
|
|
|
|
|
initialValue={filter}
|
|
|
|
|
handleChange={(value) => this.setState({ filter: value, limit: per_page })}
|
|
|
|
|
onSubmit={() => uiActions.hideContextMenu()}
|
|
|
|
|
/>
|
|
|
|
|
<DropdownField
|
|
|
|
|
icon="swap_vert"
|
|
|
|
|
name={i18n('fields.sort')}
|
|
|
|
|
value={sort}
|
|
|
|
|
valueAsLabel
|
|
|
|
|
options={sort_options}
|
|
|
|
|
selected_icon={sort ? (sort_reverse ? 'keyboard_arrow_up' : 'keyboard_arrow_down') : null}
|
|
|
|
|
handleChange={(val) => { this.setSort(val); uiActions.hideContextMenu(); }}
|
|
|
|
|
/>
|
|
|
|
|
<DropdownField
|
|
|
|
|
icon="cloud"
|
|
|
|
|
name={i18n('fields.source')}
|
|
|
|
|
value={source}
|
|
|
|
|
valueAsLabel
|
|
|
|
|
options={source_options}
|
|
|
|
|
handleChange={(val) => { uiActions.set({ library_tracks_source: val }); uiActions.hideContextMenu(); }}
|
|
|
|
|
/>
|
2020-11-12 20:52:08 +13:00
|
|
|
<Button
|
|
|
|
|
onClick={this.playAll}
|
|
|
|
|
noHover
|
|
|
|
|
discrete
|
|
|
|
|
tracking={{ category: 'LibraryTracks', action: 'Play' }}
|
|
|
|
|
>
|
|
|
|
|
<Icon name="play_circle_filled" />
|
|
|
|
|
<I18n path="actions.play_all" />
|
|
|
|
|
</Button>
|
2020-08-17 19:31:28 +12:00
|
|
|
<Button
|
|
|
|
|
noHover
|
|
|
|
|
discrete
|
2020-12-05 21:41:30 +13:00
|
|
|
onClick={loading_progress ? this.cancelRefresh : this.refresh}
|
2020-10-23 23:11:24 +13:00
|
|
|
tracking={{ category: 'LibraryTracks', action: 'Refresh' }}
|
2020-08-17 19:31:28 +12:00
|
|
|
>
|
2020-12-05 21:41:30 +13:00
|
|
|
{loading_progress ? <Icon name="close" /> : <Icon name="refresh" /> }
|
|
|
|
|
{loading_progress ? <I18n path="actions.cancel" /> : <I18n path="actions.refresh" /> }
|
2020-08-17 19:31:28 +12:00
|
|
|
</Button>
|
2020-11-08 20:49:48 +13:00
|
|
|
</>
|
2019-09-05 15:36:11 +12:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="view library-tracks-view">
|
2020-10-23 23:11:24 +13:00
|
|
|
<Header options={options} uiActions={uiActions}>
|
|
|
|
|
<Icon name="album" type="material" />
|
|
|
|
|
<I18n path="library.tracks.title" />
|
2019-09-05 15:36:11 +12:00
|
|
|
</Header>
|
2020-10-23 23:11:24 +13:00
|
|
|
{this.renderView()}
|
2019-09-05 15:36:11 +12:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2016-10-20 21:38:01 +13:00
|
|
|
}
|
|
|
|
|
|
2020-10-23 23:11:24 +13:00
|
|
|
const mapStateToProps = (state) => {
|
|
|
|
|
const source = state.ui.library_tracks_source ? state.ui.library_tracks_source : 'all';
|
2016-10-20 21:38:01 +13:00
|
|
|
|
2020-10-23 23:11:24 +13:00
|
|
|
const libraryUris = [];
|
|
|
|
|
if (source === 'all' || source === 'local') libraryUris.push('mopidy:library:tracks');
|
|
|
|
|
if (source === 'all' || source === 'spotify') libraryUris.push('spotify:library:tracks');
|
|
|
|
|
const librarySelector = makeLibrarySelector(libraryUris);
|
|
|
|
|
const processProgressSelector = makeProcessProgressSelector(processKeys);
|
2019-09-05 15:36:11 +12:00
|
|
|
|
2020-10-23 23:11:24 +13:00
|
|
|
return {
|
|
|
|
|
loading_progress: processProgressSelector(state),
|
|
|
|
|
mopidy_uri_schemes: state.mopidy.uri_schemes,
|
|
|
|
|
tracks: librarySelector(state),
|
|
|
|
|
spotify_available: state.spotify.access_token,
|
|
|
|
|
view: state.ui.library_tracks_view,
|
|
|
|
|
source,
|
|
|
|
|
sort: state.ui.library_tracks_sort,
|
|
|
|
|
sort_reverse: state.ui.library_tracks_sort_reverse,
|
|
|
|
|
};
|
|
|
|
|
};
|
2019-09-05 15:36:11 +12:00
|
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => ({
|
2020-10-23 23:11:24 +13:00
|
|
|
coreActions: bindActionCreators(coreActions, dispatch),
|
2019-09-05 15:36:11 +12:00
|
|
|
uiActions: bindActionCreators(uiActions, dispatch),
|
|
|
|
|
mopidyActions: bindActionCreators(mopidyActions, dispatch),
|
|
|
|
|
spotifyActions: bindActionCreators(spotifyActions, dispatch),
|
|
|
|
|
});
|
|
|
|
|
|
2019-09-10 06:59:44 +12:00
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(LibraryTracks);
|