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

364 lines
9.5 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 DropdownField from '../../components/Fields/DropdownField';
import FilterField from '../../components/Fields/FilterField';
import { Grid } from '../../components/Grid';
2021-03-01 21:31:22 +13:00
import { List } from '../../components/List';
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 googleActions from '../../services/google/actions';
import * as spotifyActions from '../../services/spotify/actions';
import { sortItems, applyFilter } from '../../util/arrays';
import Button from '../../components/Button';
import { i18n, I18n } from '../../locale';
import Loader from '../../components/Loader';
import {
makeLibrarySelector,
2020-10-20 21:34:43 +13:00
makeProcessProgressSelector,
} from '../../util/selectors';
const processKeys = [
'MOPIDY_GET_LIBRARY_ALBUMS',
'SPOTIFY_GET_LIBRARY_ALBUMS',
'GOOGLE_GET_LIBRARY_ALBUMS',
];
class LibraryAlbums extends React.Component {
2019-09-05 15:36:11 +12:00
constructor(props) {
super(props);
this.state = {
filter: '',
};
}
componentDidMount() {
const {
uiActions: {
setWindowTitle,
},
} = this.props;
setWindowTitle(i18n('library.albums.title'));
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
}
}
refresh = () => {
const { uiActions: { hideContextMenu } } = this.props;
hideContextMenu();
this.getMopidyLibrary(true);
this.getGoogleLibrary(true);
this.getSpotifyLibrary(true);
}
cancelRefresh = () => {
const { uiActions: { hideContextMenu, cancelProcess } } = this.props;
hideContextMenu();
cancelProcess(processKeys);
}
getMopidyLibrary = (forceRefetch = false) => {
2020-03-12 08:31:32 +13:00
const {
source,
coreActions: {
loadLibrary,
2020-08-12 08:06:24 +12:00
},
} = this.props;
if (source !== 'local' && source !== 'all') return;
loadLibrary('mopidy:library:albums', { 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
},
2020-03-12 08:31:32 +13:00
} = this.props;
2020-08-12 08:06:24 +12:00
if (!google_available) return;
if (source !== 'google' && source !== 'all') return;
2019-09-05 15:36:11 +12:00
loadLibrary('google:library:albums', { forceRefetch });
2020-08-12 08:06:24 +12:00
};
2019-09-05 15:36:11 +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;
if (!spotify_available) return;
if (source !== 'spotify' && source !== 'all') return;
loadLibrary('spotify:library:albums', { forceRefetch });
2020-08-12 08:06:24 +12:00
};
2019-09-05 15:36:11 +12:00
handleContextMenu = (e, item) => {
const {
uiActions: {
showContextMenu,
},
} = this.props;
showContextMenu({
2019-09-05 15:36:11 +12:00
e,
context: 'album',
uris: [item.uri],
items: [item],
});
2019-09-05 15:36:11 +12:00
}
setSort = (value) => {
const {
sort,
sort_reverse,
uiActions: {
set,
},
} = this.props;
2019-09-05 15:36:11 +12:00
let reverse = false;
if (sort === value) reverse = !sort_reverse;
2019-09-05 15:36:11 +12:00
set({
2019-09-05 15:36:11 +12:00
library_albums_sort_reverse: reverse,
library_albums_sort: value,
});
2019-09-05 15:36:11 +12:00
}
renderView = () => {
const {
sort,
sort_reverse,
view,
2020-10-20 21:34:43 +13:00
loading_progress,
} = this.props;
const {
filter,
} = this.state;
let { albums } = this.props;
2019-09-05 15:36:11 +12:00
2020-12-05 21:41:30 +13:00
if (loading_progress) {
return <Loader body loading progress={loading_progress} />;
}
if (sort) {
albums = sortItems(albums, sort, sort_reverse);
2019-09-05 15:36:11 +12:00
}
if (filter && filter !== '') {
albums = applyFilter('name', filter, albums);
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
2021-03-01 21:31:22 +13:00
items={albums}
2021-03-04 06:43:53 +13:00
details={['artists', 'followers', 'listeners']}
right_column={['tracks']}
2019-09-05 15:36:11 +12:00
thumbnail
/>
</section>
);
}
return (
<section className="content-wrapper">
<Grid items={albums} />
2019-09-05 15:36:11 +12:00
</section>
);
2019-09-05 15:36:11 +12:00
}
render = () => {
const {
spotify_available,
google_available,
sort,
view,
source,
sort_reverse,
uiActions,
loading_progress,
} = this.props;
const {
filter,
per_page,
} = this.state;
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: 'artist',
label: i18n('fields.filters.artist'),
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: 'tracks',
label: i18n('fields.filters.tracks'),
2019-09-05 15:36:11 +12:00
},
{
value: 'uri',
label: i18n('fields.filters.source'),
2019-09-05 15:36:11 +12:00
},
];
const options = (
<>
2019-09-05 15:36:11 +12:00
<FilterField
initialValue={filter}
handleChange={(value) => this.setState({ filter: value, limit: per_page })}
onSubmit={() => uiActions.hideContextMenu()}
2019-09-05 15:36:11 +12:00
/>
<DropdownField
icon="swap_vert"
name={i18n('fields.sort')}
value={sort}
valueAsLabel
2019-09-05 15:36:11 +12:00
options={sort_options}
selected_icon={sort ? (sort_reverse ? 'keyboard_arrow_up' : 'keyboard_arrow_down') : null}
handleChange={(val) => { this.setSort(val); uiActions.hideContextMenu(); }}
2019-09-05 15:36:11 +12:00
/>
<DropdownField
icon="visibility"
name={i18n('fields.view')}
value={view}
valueAsLabel
2019-09-05 15:36:11 +12:00
options={view_options}
handleChange={(val) => { uiActions.set({ library_albums_view: val }); uiActions.hideContextMenu(); }}
2019-09-05 15:36:11 +12:00
/>
<DropdownField
icon="cloud"
name={i18n('fields.source')}
value={source}
valueAsLabel
2019-09-05 15:36:11 +12:00
options={source_options}
handleChange={(val) => { uiActions.set({ library_albums_source: val }); uiActions.hideContextMenu(); }}
2019-09-05 15:36:11 +12:00
/>
<Button
noHover
discrete
2020-12-05 21:41:30 +13:00
onClick={loading_progress ? this.cancelRefresh : this.refresh}
tracking={{ category: 'LibraryAlbums', action: 'Refresh' }}
>
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" /> }
</Button>
</>
2019-09-05 15:36:11 +12:00
);
return (
<div className="view library-albums-view">
<Header options={options} uiActions={uiActions}>
2019-09-05 15:36:11 +12:00
<Icon name="album" type="material" />
<I18n path="library.albums.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 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);
const processProgressSelector = makeProcessProgressSelector(processKeys);
return {
2020-10-20 21:34:43 +13:00
loading_progress: processProgressSelector(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,
};
};
2019-09-05 15:36:11 +12:00
const mapDispatchToProps = (dispatch) => ({
coreActions: bindActionCreators(coreActions, dispatch),
uiActions: bindActionCreators(uiActions, dispatch),
mopidyActions: bindActionCreators(mopidyActions, dispatch),
googleActions: bindActionCreators(googleActions, dispatch),
spotifyActions: bindActionCreators(spotifyActions, dispatch),
});
export default connect(mapStateToProps, mapDispatchToProps)(LibraryAlbums);