-
Notifications
You must be signed in to change notification settings - Fork 15
/
TrackContextMenu.js
163 lines (145 loc) · 4.02 KB
/
TrackContextMenu.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* Created by paulbarrass on 19/10/2017.
*/
import React, {Component, PropTypes} from 'react';
import {connectMenu, ContextMenu, MenuItem, SubMenu} from 'react-contextmenu';
import {spotifyAddTrackToPlaylist} from '../../redux/modules/spotify';
import {connect} from 'react-redux';
import ellipsize from 'ellipsize';
import copy from 'clipboard-copy';
import Popup from 'react-popup';
import '!!style!css!./contextMenu.css';
class TrackContextMenu extends Component {
static propTypes = {
myPlaylists: PropTypes.array,
handleClickSaveToPlaylist: PropTypes.func,
trigger: PropTypes.object,
id: PropTypes.any
};
static contextTypes = {
notify: PropTypes.object
};
render() {
const {myPlaylists, handleClickSaveToPlaylist, id, trigger} = this.props;
const {notify} = this.context;
if (!trigger) {
return (
<ContextMenu id={id}>
<MenuItem disabled={true}/>
</ContextMenu>
);
}
const {track, onClickVoteSkip} = trigger;
let album = track.album;
if (!album && track.json) {
album = track.json.album;
}
const title = `${track.name} by ${track.artists && track.artists.map(artist => artist.name).join(', ')}`;
let albumMenu = null;
const songUri = `spotify:track:${track.id}`;
const songLink = `https://open.spotify.com/track/${track.id}`;
const copyAndNotify = url => {
copy(url);
notify.show('Link copied to clipboard', {type: 'success'});
};
if (album) {
const albumUri = `spotify:album:${album.id}`;
const albumLink = `https://open.spotify.com/album/${album.id}`;
albumMenu = (
<SubMenu title='Album'>
<MenuItem disabled={true}>
{trigger && ellipsize(`${album.name}`, 40)}
</MenuItem>
<MenuItem divider/>
<MenuItem data={{album}}
onClick={() => {
console.log(track);
notify.show('Sorry, feature not ready yet');
}}>
Browse in Soundbounce
</MenuItem>
<MenuItem data={{album}}
onClick={() => {
document.location = albumUri;
}}>
View in Spotify
</MenuItem>
<MenuItem divider/>
<MenuItem data={{album}}
onClick={() => copyAndNotify(albumLink)}>
Copy Album Link
</MenuItem>
<MenuItem data={{album}}
onClick={() => copyAndNotify(albumUri)}>
Copy Spotify URI
</MenuItem>
</SubMenu>
);
}
return (
<ContextMenu id={id}>
<MenuItem disabled={true}>
<span title={title}>{trigger && ellipsize(title, 40)}</span>
</MenuItem>
<MenuItem divider/>
{albumMenu}
<MenuItem divider/>
{myPlaylists && (
<SubMenu title='Add to playlist'>
{
myPlaylists.map(playlist => (
<MenuItem data={{trigger, playlist}}
key={playlist.id}
onClick={(e, data) => {
handleClickSaveToPlaylist(e, data);
notify.show(`Track added to ${playlist.name}`, {type: 'success'});
}}>
{ellipsize(playlist.name, 50)}
</MenuItem>
))
}
</SubMenu>
)}
<MenuItem onClick={() => {
onClickVoteSkip(track.id);
}}>
Vote to skip
</MenuItem>
<MenuItem onClick={() => {
Popup.plugins().analysis({
track
});
}}>
Audio Analysis
</MenuItem>
<MenuItem data={{track}}
onClick={() => {
document.location = `spotify:track:${track.id}`;
}}>
View in Spotify
</MenuItem>
<MenuItem divider/>
<MenuItem data={{album}}
onClick={() => copyAndNotify(songLink)}>
Copy Song Link
</MenuItem>
<MenuItem data={{album}}
onClick={() => copyAndNotify(songUri)}>
Copy Spotify URI
</MenuItem>
</ContextMenu>
);
}
}
const mapStateToProps = state => ({
myPlaylists: state.spotify.myPlaylists
});
const mapDispatchToProps = (dispatch, ownProps) => ({
handleClickSaveToPlaylist: (e, {playlist, trigger}) => {
dispatch(spotifyAddTrackToPlaylist({
playlistId: playlist.id,
trackId: trigger.trackId
}));
}
});
export default connect(mapStateToProps, mapDispatchToProps)(connectMenu('track')(TrackContextMenu));