Files
Iris/src/js/views/discover/DiscoverRecommendations.js

582 lines
15 KiB
JavaScript
Raw Normal View History

2016-10-26 16:55:47 +13:00
import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
2018-01-11 16:46:57 +13:00
import InputRange from 'react-input-range';
2016-10-26 16:55:47 +13:00
import Header from '../../components/Header'
import ArtistSentence from '../../components/ArtistSentence'
import ArtistGrid from '../../components/ArtistGrid'
import AlbumGrid from '../../components/AlbumGrid'
2017-05-12 05:22:57 +12:00
import TrackList from '../../components/TrackList'
2018-01-29 08:44:52 +13:00
import Thumbnail from '../../components/Thumbnail'
2017-05-12 05:22:57 +12:00
import Parallax from '../../components/Parallax'
import DropdownField from '../../components/Fields/DropdownField'
import AddSeedField from '../../components/Fields/AddSeedField'
import URILink from '../../components/URILink'
2018-01-11 16:46:57 +13:00
import ContextMenuTrigger from '../../components/ContextMenuTrigger'
import RelatedArtists from '../../components/RelatedArtists'
2018-05-21 06:01:53 +12:00
import Icon from '../../components/Icon'
import * as helpers from '../../helpers'
2018-01-11 16:46:57 +13:00
import * as uiActions from '../../services/ui/actions'
import * as mopidyActions from '../../services/mopidy/actions'
2016-10-26 16:55:47 +13:00
import * as spotifyActions from '../../services/spotify/actions'
class Discover extends React.Component{
2017-10-10 13:05:28 +13:00
constructor(props){
2017-05-12 05:22:57 +12:00
super(props)
this._autocomplete_timer = false
this.state = {
2017-05-14 07:16:11 +12:00
add_seed: '',
2018-01-11 16:46:57 +13:00
adding_seed: false,
seeds: [],
tunabilities: {
acousticness: {
enabled: false,
convert_to_decimal: true,
min: 0,
max: 100,
value: {
2018-01-18 08:42:58 +13:00
min: 25,
max: 75
}
},
danceability: {
enabled: false,
convert_to_decimal: true,
min: 0,
max: 100,
value: {
2018-01-18 08:42:58 +13:00
min: 25,
max: 75
}
},
energy: {
enabled: false,
convert_to_decimal: true,
min: 0,
max: 100,
value: {
2018-01-18 08:42:58 +13:00
min: 25,
max: 75
}
},
instrumentalness: {
enabled: false,
convert_to_decimal: true,
min: 0,
max: 100,
value: {
2018-01-18 08:42:58 +13:00
min: 25,
max: 75
}
},
key: {
enabled: false,
min: 0,
max: 11,
value: {
2018-01-18 08:42:58 +13:00
min: 3,
max: 8
}
},
liveness: {
enabled: false,
convert_to_decimal: true,
min: 0,
max: 100,
value: {
2018-01-18 08:42:58 +13:00
min: 25,
max: 75
}
},
2018-01-11 16:46:57 +13:00
loudness: {
enabled: false,
convert_to_decimal: true,
2018-01-11 16:46:57 +13:00
min: 0,
max: 100,
value: {
2018-01-18 08:42:58 +13:00
min: 25,
max: 75
}
},
popularity: {
enabled: true,
min: 0,
max: 100,
value: {
2018-01-25 17:04:19 +13:00
min: 0,
max: 100
}
},
speechiness: {
enabled: false,
convert_to_decimal: true,
description: "The presence of spoken words in a track",
min: 0,
max: 100,
value: {
2018-01-18 08:42:58 +13:00
min: 25,
max: 75
}
},
tempo: {
enabled: false,
convert_to_decimal: true,
min: 0,
max: 100,
value: {
2018-01-18 08:42:58 +13:00
min: 25,
max: 75
}
},
valence: {
enabled: false,
convert_to_decimal: true,
description: "The musical positiveness conveyed by a track",
min: 0,
max: 100,
value: {
2018-01-18 08:42:58 +13:00
min: 25,
max: 75
}
2018-01-11 16:46:57 +13:00
}
}
2017-05-12 05:22:57 +12:00
}
2016-10-26 16:55:47 +13:00
}
componentDidMount(){
this.props.uiActions.setWindowTitle("Discover");
// We have seeds provided in the URL
if (this.props.params.seeds){
2018-01-11 16:46:57 +13:00
this.handleURLSeeds(this.props.params.seeds);
}
2017-05-12 05:22:57 +12:00
}
componentWillReceiveProps(newProps, newState){
2017-06-02 10:38:12 +12:00
// New seeds via URL
if (newProps.params.seeds != this.props.params.seeds){
this.handleURLSeeds(newProps.params.seeds)
}
2018-01-11 16:46:57 +13:00
}
2017-06-02 10:38:12 +12:00
2018-01-11 16:46:57 +13:00
handleContextMenu(e){
2017-05-12 05:22:57 +12:00
2018-01-11 16:46:57 +13:00
var uri = 'iris:discover';
if (this.state.seeds){
uri += ':';
for (var i = 0; i < this.state.seeds.length; i++){
if (i > 0){
uri += ',';
}
uri += this.state.seeds[i].split(':').join('_');
}
2017-05-12 05:22:57 +12:00
}
2018-01-11 16:46:57 +13:00
var tracks = [];
if (this.props.recommendations.tracks_uris && this.props.tracks){
for (var i = 0; i < this.props.recommendations.tracks_uris.length; i++){
var uri = this.props.recommendations.tracks_uris[i];
if (this.props.tracks.hasOwnProperty(uri)){
tracks.push(this.props.tracks[uri]);
}
}
}
var data = {
e: e,
context: 'track',
items: tracks,
2018-01-30 08:14:05 +13:00
uris: helpers.arrayOf('uri',tracks)
2018-01-11 16:46:57 +13:00
}
this.props.uiActions.showContextMenu(data);
}
2017-06-02 10:38:12 +12:00
handleURLSeeds(seeds_string = this.props.params.seeds){
// Rejoin if we've had to uri-encode these as strings
// We'd need to do this if our URL has been encoded so the whole URL can become
// it's own URI (eg iris:discover:spotify_artist_1234) where we can't use ":"
var seeds = seeds_string.split('_').join(':').split(',')
2017-06-02 10:38:12 +12:00
for (var i = 0; i < seeds.length; i++){
switch (helpers.uriType(seeds[i])){
case 'artist':
this.props.spotifyActions.getArtist(seeds[i])
break
case 'track':
this.props.spotifyActions.getTrack(seeds[i])
break
}
}
this.setState({seeds: seeds})
this.getRecommendations(seeds)
}
2018-01-11 16:46:57 +13:00
getRecommendations(seeds = this.state.seeds, tunabilities = this.state.tunabilities){
if (seeds.length > 0){
var digested_tunabilities = {};
for (var key in tunabilities){
if (tunabilities.hasOwnProperty(key) && tunabilities[key].enabled){
var tunability = tunabilities[key];
var max = tunability.value.max;
var min = tunability.value.min;
if (tunability.convert_to_decimal){
max = max / 100;
min = min / 100;
}
digested_tunabilities[key+"_max"] = max.toString();
digested_tunabilities[key+"_min"] = min.toString();
}
}
this.props.spotifyActions.getRecommendations(seeds, 50, digested_tunabilities)
}
2017-05-12 05:22:57 +12:00
}
2018-01-11 16:46:57 +13:00
playTracks(){
this.props.mopidyActions.playURIs(this.props.recommendations.tracks_uris);
}
2017-05-12 05:22:57 +12:00
removeSeed(index){
var seeds = this.state.seeds;
seeds.splice(index,1);
this.setState({seeds: seeds});
2017-05-12 05:22:57 +12:00
}
handleSelect(e,uri){
var seeds = this.state.seeds;
seeds.push(uri);
this.setState({seeds: seeds});
2017-05-12 05:22:57 +12:00
}
renderSeeds(){
var seeds_objects = []
if (this.state.seeds.length > 0){
for (var i = 0; i < this.state.seeds.length; i++){
var uri = this.state.seeds[i]
switch (helpers.uriType(uri)){
case 'track':
if (typeof(this.props.tracks[uri]) !== 'undefined'){
2018-01-29 08:44:52 +13:00
seeds_objects.push(this.props.tracks[uri]);
} else {
seeds_objects.push({
name: 'Loading...',
uri: uri
})
}
break
case 'artist':
if (typeof(this.props.artists[uri]) !== 'undefined'){
2018-01-29 08:44:52 +13:00
seeds_objects.push(this.props.artists[uri]);
} else {
seeds_objects.push({
name: 'Loading...',
uri: uri
})
}
break
case 'genre':
var name = helpers.getFromUri('genreid',uri)
seeds_objects.push({
name: (name.charAt(0).toUpperCase() + name.slice(1)).replace('-',' '),
uri: uri
})
break
}
}
}
return (
2017-05-12 05:22:57 +12:00
<div className="seeds">
{
seeds_objects.map((seed,index) => {
2018-10-30 19:33:21 +13:00
var type = helpers.uriType(seed.uri);
var images = null;
if (seed.images){
if (type == 'artist'){
if (seed.images.length > 0){
images = seed.images[0];
}
} else {
images = seed.images;
}
}
2017-05-12 05:22:57 +12:00
return (
2018-01-29 08:44:52 +13:00
<div className={"seed"+(seed.images ? " has-thumbnail" : "")} key={seed.uri}>
2018-10-30 19:33:21 +13:00
{images ? <URILink className="thumbnail-wrapper" type={type} uri={seed.uri}><Thumbnail images={images} circle={seed.type == "artist"} size="small" /></URILink> : null}
2018-01-29 08:44:52 +13:00
<div className="label">
{helpers.titleCase(type)}
2018-05-29 21:15:26 +12:00
<Icon name="close" className="remove" onClick={() => this.removeSeed(index)} />
2018-01-29 08:44:52 +13:00
</div>
<div className="name">{seed.name}</div>
</div>
2017-05-12 05:22:57 +12:00
)
})
}
2017-05-12 05:22:57 +12:00
</div>
)
}
2018-01-11 16:46:57 +13:00
setTunability(name, value){
var tunabilities = this.state.tunabilities;
tunabilities[name].value = value;
this.setState({ tunabilities: tunabilities });
}
toggleTunability(name){
var tunabilities = this.state.tunabilities;
tunabilities[name].enabled = !tunabilities[name].enabled;
2018-01-11 16:46:57 +13:00
this.setState({ tunabilities: tunabilities });
}
renderTunabilities(){
var addable_tunabilities = [];
var enabled_tunabilities = [];
2018-01-11 16:46:57 +13:00
for (var key in this.state.tunabilities){
if (this.state.tunabilities.hasOwnProperty(key)){
var tunability = Object.assign(
{},
this.state.tunabilities[key],
{
name: key
}
);
if (tunability.enabled){
enabled_tunabilities.push(tunability);
} else {
addable_tunabilities.push({
label: helpers.titleCase(tunability.name),
value: tunability.name
});
}
2018-01-11 16:46:57 +13:00
}
}
return (
<div className="tunabilities">
{
enabled_tunabilities.map(tunability => {
2018-01-11 16:46:57 +13:00
return (
<div className="field tunability range" key={tunability.name}>
<div className="label">
{helpers.titleCase(tunability.name)}
<span className="remove" onClick={e => this.toggleTunability(tunability.name)}>
2018-05-29 21:15:26 +12:00
<Icon name="close" />
</span>
</div>
<div className="input">
<InputRange
disabled={!tunability.enabled}
minValue={tunability.min}
maxValue={tunability.max}
value={tunability.value}
onChange={value => this.setTunability(tunability.name, value)}
/>
</div>
2018-01-11 16:46:57 +13:00
</div>
);
})
}
</div>
);
}
2017-05-22 04:19:44 +12:00
renderResults(){
2018-01-25 17:04:19 +13:00
// Results not in
2018-01-11 16:46:57 +13:00
if (!this.props.recommendations || this.props.recommendations.albums_uris === undefined || this.props.recommendations.artists_uris === undefined){
2017-11-13 16:52:53 +13:00
return null;
}
2018-01-11 16:46:57 +13:00
var tracks = [];
if (this.props.recommendations.tracks_uris && this.props.tracks){
for (var i = 0; i < this.props.recommendations.tracks_uris.length; i++){
var uri = this.props.recommendations.tracks_uris[i];
if (this.props.tracks.hasOwnProperty(uri)){
tracks.push(this.props.tracks[uri]);
}
}
}
2017-11-13 16:52:53 +13:00
var artists = [];
if (this.props.recommendations.artists_uris && this.props.artists){
for (var i = 0; i < this.props.recommendations.artists_uris.length; i++){
2017-11-13 16:52:53 +13:00
var uri = this.props.recommendations.artists_uris[i];
if (this.props.artists.hasOwnProperty(uri)){
2017-11-13 16:52:53 +13:00
artists.push(this.props.artists[uri]);
}
}
}
2018-01-11 16:46:57 +13:00
var albums = [];
if (this.props.recommendations.albums_uris && this.props.albums){
for (var i = 0; i < this.props.recommendations.albums_uris.length; i++){
var uri = this.props.recommendations.albums_uris[i];
if (this.props.albums.hasOwnProperty(uri)){
albums.push(this.props.albums[uri]);
2017-11-13 16:52:53 +13:00
}
}
}
2018-01-25 17:04:19 +13:00
// Complete records not yet in our index
if (tracks.length <= 0 && artists.length <= 0 && albums.length <= 0){
return null;
}
2017-11-13 16:52:53 +13:00
var uri = 'iris:discover';
2018-01-11 16:46:57 +13:00
if (this.state.seeds){
uri += ':';
for (var i = 0; i < this.state.seeds.length; i++){
if (i > 0){
uri += ',';
}
uri += this.state.seeds[i].split(':').join('_');
}
}
return (
<div className="content-wrapper recommendations-results">
2018-01-11 16:46:57 +13:00
<section className="col col--w70 tracks">
2018-01-11 16:46:57 +13:00
<h4>
Tracks
<div className="pull-right">
<ContextMenuTrigger onTrigger={e => this.handleContextMenu(e)} />
<button className="primary" onClick={e => this.playTracks(e)}>Play all</button>
</div>
2018-01-11 16:46:57 +13:00
</h4>
2017-11-13 16:52:53 +13:00
<TrackList className="discover-track-list" uri={uri} tracks={tracks} />
</section>
2018-01-11 16:46:57 +13:00
<div className="col col--w5"></div>
2018-01-11 16:46:57 +13:00
<div className="col col--w25 others">
2018-01-11 16:46:57 +13:00
<section>
<h4>Artists</h4>
<RelatedArtists artists={artists} />
</section>
<br />
<br />
<section>
<h4>Albums</h4>
<AlbumGrid className="mini" albums={albums} />
</section>
</div>
</div>
)
}
2016-10-26 16:55:47 +13:00
render(){
2018-01-25 17:04:19 +13:00
var is_loading = helpers.isLoading(this.props.load_queue,['spotify_recommendations']);
var addable_tunabilities = [];
for (var key in this.state.tunabilities){
if (this.state.tunabilities.hasOwnProperty(key)){
var tunability = Object.assign(
{},
this.state.tunabilities[key],
{
name: key
}
);
if (!tunability.enabled){
addable_tunabilities.push({
label: helpers.titleCase(tunability.name),
value: tunability.name
});
}
}
}
return (
<div className="view discover-view preserve-3d">
<div className="intro preserve-3d">
2017-06-29 08:08:19 +12:00
<Parallax image="assets/backgrounds/discover.jpg" />
2017-06-29 08:08:19 +12:00
2017-05-12 05:22:57 +12:00
<div className="liner">
<h1>Explore new music</h1>
<h2 className="mid_grey-text">
2018-01-11 16:46:57 +13:00
Add seeds and musical properties below to build your sound
</h2>
2018-01-25 17:04:19 +13:00
<div className="parameters">
2018-01-29 08:44:52 +13:00
2018-01-25 17:04:19 +13:00
{this.renderSeeds()}
{this.renderTunabilities()}
2018-01-30 08:14:05 +13:00
{this.state.seeds.length > 5 ? <p className="message error">Too many seeds! You can use up to a total of 5 seed tracks, artists and genres.</p> : null}
2018-01-29 08:44:52 +13:00
2018-01-25 17:04:19 +13:00
</div>
2018-01-23 11:34:03 +13:00
<div className="actions">
2018-01-30 08:14:05 +13:00
<AddSeedField onSelect={(e,uri) => this.handleSelect(e,uri)} />
<DropdownField className="add-properties" name="Properties" options={addable_tunabilities} no_status_icon button="default" handleChange={val => {this.toggleTunability(val)}} />
<span className={"submit button primary large"+(is_loading ? " working" : "")} onClick={e => this.getRecommendations()}>
2018-05-29 21:15:26 +12:00
<Icon name="explore" />&nbsp;
Find recommendations
</span>
2018-01-30 08:14:05 +13:00
2018-01-23 11:34:03 +13:00
</div>
2017-05-12 05:22:57 +12:00
</div>
2017-06-29 08:08:19 +12:00
2017-05-12 05:22:57 +12:00
</div>
2017-06-29 08:08:19 +12:00
{this.renderResults()}
2017-06-29 08:08:19 +12:00
</div>
)
2016-10-26 16:55:47 +13:00
}
}
/**
* Export our component
*
* We also integrate our global store, using connect()
**/
const mapStateToProps = (state, ownProps) => {
return {
disable_parallax: state.ui.disable_parallax,
2018-08-27 08:21:41 +12:00
theme: state.ui.theme,
2017-11-13 16:52:53 +13:00
albums: state.core.albums,
artists: state.core.artists,
tracks: state.core.tracks,
2017-08-02 21:11:22 +12:00
genres: (state.core.genres ? state.core.genres : []),
authorized: state.spotify.authorization,
2017-05-14 07:16:11 +12:00
load_queue: state.ui.load_queue,
2017-05-12 05:22:57 +12:00
quick_search_results: (state.spotify.quick_search_results ? state.spotify.quick_search_results : {artists: [], tracks: []}),
2018-01-25 17:04:19 +13:00
recommendations: (state.spotify.recommendations ? state.spotify.recommendations : {})
}
2016-10-26 16:55:47 +13:00
}
const mapDispatchToProps = (dispatch) => {
return {
2018-01-11 16:46:57 +13:00
uiActions: bindActionCreators(uiActions, dispatch),
mopidyActions: bindActionCreators(mopidyActions, dispatch),
2016-10-26 16:55:47 +13:00
spotifyActions: bindActionCreators(spotifyActions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Discover)