Files
Iris/src/js/views/Library/Albums.js

303 lines
7.9 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 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,
getLibrarySource,
makeProvidersSelector,
getSortSelector,
} from '../../util/selectors';
2021-12-01 20:49:50 +13:00
import { formatSimpleObject } from '../../util/format';
const SORT_KEY = 'library_albums';
const processKeys = [
'MOPIDY_GET_LIBRARY_ALBUMS',
'SPOTIFY_GET_LIBRARY_ALBUMS',
];
2021-11-28 19:52:12 +13:00
class Albums 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'));
this.getLibraries();
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;
2019-09-05 15:36:11 +12:00
2020-08-12 08:06:24 +12:00
if (source !== prevSource) {
this.getLibraries();
2019-09-05 15:36:11 +12:00
}
}
refresh = () => {
const { uiActions: { hideContextMenu } } = this.props;
hideContextMenu();
this.getLibraries(true);
}
cancelRefresh = () => {
const { uiActions: { hideContextMenu, cancelProcess } } = this.props;
hideContextMenu();
cancelProcess(processKeys);
}
getLibraries = (forceRefetch = false) => {
2020-03-12 08:31:32 +13:00
const {
source,
providers,
coreActions: {
loadLibrary,
2020-08-12 08:06:24 +12:00
},
} = this.props;
let uris = [];
if (source === 'all') {
uris = providers.map((p) => p.uri);
} else {
uris.push(source);
}
uris.forEach((uri) => loadLibrary(uri, 'albums', { forceRefetch }));
2020-08-12 08:06:24 +12:00
};
2019-09-05 15:36:11 +12:00
onSortChange = (field) => {
const {
sortField,
sortReverse,
uiActions: {
setSort,
hideContextMenu,
},
} = this.props;
2019-09-05 15:36:11 +12:00
let reverse = false;
if (field !== null && sortField === field) {
reverse = !sortReverse;
}
2019-09-05 15:36:11 +12:00
setSort(SORT_KEY, field, reverse);
hideContextMenu();
2019-09-05 15:36:11 +12:00
}
renderView = () => {
const {
sortField,
sortReverse,
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 (sortField) {
albums = sortItems(albums, sortField, sortReverse);
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}
details={['artists', 'tracks', 'followers']}
right_column={['source']}
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 {
view,
source,
providers,
sortField,
sortReverse,
uiActions,
loading_progress,
} = this.props;
const {
filter,
per_page,
} = this.state;
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
},
2026-06-29 12:23:10 -04:00
{
value: 'release_date',
label: i18n('fields.filters.date'),
},
{
value: 'added_at',
label: i18n('fields.filters.added'),
},
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={sortField}
valueAsLabel
2019-09-05 15:36:11 +12:00
options={sort_options}
selected_icon={sortField ? (sortReverse ? 'keyboard_arrow_up' : 'keyboard_arrow_down') : null}
handleChange={this.onSortChange}
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
options={[
{
value: 'all',
label: i18n('fields.filters.all'),
},
...providers.map((p) => ({ value: p.uri, label: p.title })),
]}
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 librarySelector = makeLibrarySelector('albums');
const processProgressSelector = makeProcessProgressSelector(processKeys);
const providersSelector = makeProvidersSelector('albums');
const mapStateToProps = (state) => {
const [sortField, sortReverse] = getSortSelector(state, SORT_KEY, null);
return {
loading_progress: processProgressSelector(state),
uri_schemes: state.mopidy.uri_schemes,
albums: librarySelector(state, 'albums'),
providers: providersSelector(state),
view: state.ui.library_albums_view,
source: getLibrarySource(state, 'albums'),
sortField,
sortReverse,
};
};
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),
});
2021-11-28 19:52:12 +13:00
export default connect(mapStateToProps, mapDispatchToProps)(Albums);