index.jsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. import React, { Component } from "react";
  2. import { NavLink } from "react-router-dom";
  3. import { Map, List } from "immutable";
  4. import async from "async";
  5. import PropTypes from "prop-types";
  6. import { translate, Trans } from "react-i18next";
  7. import Player from "./Player";
  8. import PlayerDebug from "./PlayerDebug";
  9. import Seekerbar from "./Seekerbar";
  10. import VolumeSlider from "./VolumeSlider";
  11. import Ratings from "./Ratings";
  12. import Time from "./Time";
  13. import Overlays from "./Views/Overlays";
  14. import { actionCreators as stationCurrentSongActionCreators } from "ducks/stationCurrentSong";
  15. import { actionCreators as stationInfoActionCreators } from "ducks/stationInfo";
  16. import { actionCreators as stationPlaylistsActionCreators } from "ducks/stationPlaylists";
  17. import { bindActionCreators } from "redux";
  18. //import { changeVolume } from "actions/volume";
  19. //import { changeSong, setTimeElapsed, timePaused, receivedRatings, receivedOwnRatings } from "actions/songPlayer";
  20. //import { pauseStation, resumeStation } from "actions/station";
  21. import { openOverlay1 } from "actions/stationOverlay";
  22. //import { addSong } from "actions/playlistQueue";
  23. //import { updateTimePaused } from "../../actions/songPlayer";
  24. import { connect } from "react-redux";
  25. import io from "io";
  26. import config from "config";
  27. @connect(state => ({
  28. user: {
  29. userId: state.session.get("userId"),
  30. role: state.session.get("role"),
  31. loggedIn: state.session.get("loggedIn"),
  32. },
  33. /*
  34. //queueLocked: state.station.get("locked"),
  35. //partyEnabled: state.station.get("partyMode"),*/
  36. song: {
  37. exists: state.station.currentSong.get("songId") !== "",
  38. songId: state.station.currentSong.get("songId"),
  39. title: state.station.currentSong.get("title"),
  40. artists: state.station.currentSong.get("artists"),
  41. },
  42. playlists: state.station.playlists,
  43. station: {
  44. stationId: state.station.info.get("stationId"),
  45. name: state.station.info.get("name"),
  46. displayName: state.station.info.get("displayName"),
  47. type: state.station.info.get("type"),
  48. songList: state.station.info.get("songList"),
  49. paused: state.station.info.get("paused"),
  50. ownerId: state.station.info.get("ownerId"),
  51. privatePlaylistQueue: state.station.info.get("privatePlaylistQueue"),
  52. },/*
  53. selectedPlaylistObject: {
  54. addedSongId: state.playlistQueue.get("addedSongId"),
  55. selectedPlaylistId: state.playlistQueue.get("playlistSelected"),
  56. },*/
  57. }),
  58. (dispatch) => ({
  59. onNextSong: bindActionCreators(stationCurrentSongActionCreators.nextSong, dispatch),
  60. onLikeUpdate: bindActionCreators(stationCurrentSongActionCreators.likeUpdate, dispatch),
  61. onDislikeUpdate: bindActionCreators(stationCurrentSongActionCreators.dislikeUpdate, dispatch),
  62. onLikedUpdate: bindActionCreators(stationCurrentSongActionCreators.likedUpdate, dispatch),
  63. onDislikedUpdate: bindActionCreators(stationCurrentSongActionCreators.dislikedUpdate, dispatch),
  64. onPauseTime: bindActionCreators(stationCurrentSongActionCreators.pauseTime, dispatch),
  65. onResumeTime: bindActionCreators(stationCurrentSongActionCreators.resumeTime, dispatch),
  66. onPause: bindActionCreators(stationInfoActionCreators.pause, dispatch),
  67. onResume: bindActionCreators(stationInfoActionCreators.resume, dispatch),
  68. onQueueIndex: bindActionCreators(stationInfoActionCreators.queueIndex, dispatch),
  69. onQueueUpdate: bindActionCreators(stationInfoActionCreators.queueUpdate, dispatch),
  70. onTimeElapsedUpdate: bindActionCreators(stationCurrentSongActionCreators.timeElapsedUpdate, dispatch),
  71. onPlaylistsUpdate: bindActionCreators(stationPlaylistsActionCreators.update, dispatch),
  72. onPlaylistsAddSong: bindActionCreators(stationPlaylistsActionCreators.addSong, dispatch),
  73. onPlaylistsUpdateDisplayName: bindActionCreators(stationPlaylistsActionCreators.updateDisplayName, dispatch),
  74. onPlaylistsMoveSongToBottom: bindActionCreators(stationPlaylistsActionCreators.moveSongToBottom, dispatch),
  75. onPlaylistsMoveSongToTop: bindActionCreators(stationPlaylistsActionCreators.moveSongToTop, dispatch),
  76. onPlaylistsRemoveSong: bindActionCreators(stationPlaylistsActionCreators.removeSong, dispatch),
  77. openOverlay1: bindActionCreators(openOverlay1, dispatch),
  78. }))
  79. @translate(["station"], { wait: true })
  80. export default class Station extends Component {
  81. static propTypes = {
  82. t: PropTypes.func,
  83. };
  84. static defaultProps = {
  85. t: () => {},
  86. };
  87. constructor(props) {
  88. super();
  89. this.state = {
  90. timeElapsedInterval: setInterval(() => {
  91. props.onTimeElapsedUpdate();
  92. }, 500),
  93. automaticallyAddedSongId: null,
  94. };
  95. /*this.state = {
  96. mode: this.getModeTemp(props.partyEnabled, props.queueLocked),
  97. };*/
  98. io.getSocket(socket => {
  99. socket.emit("stations.join", props.station.name, res => {
  100. if (res.status === "success") {
  101. if (res.data.currentSong) {
  102. let song = {
  103. songId: res.data.currentSong.songId,
  104. timings: {
  105. duration: res.data.currentSong.duration,
  106. skipDuration: res.data.currentSong.skipDuration,
  107. // timeElapsed?
  108. timePaused: res.data.timePaused,
  109. startedAt: res.data.startedAt,
  110. },
  111. title: res.data.currentSong.title,
  112. artists: res.data.currentSong.artists,
  113. thumbnail: res.data.currentSong.thumbnail,
  114. ratings: {
  115. enabled: !(res.data.currentSong.likes === -1 && res.data.currentSong.dislikes === -1),
  116. likes: res.data.currentSong.likes,
  117. dislikes: res.data.currentSong.dislikes,
  118. },
  119. };
  120. this.props.onNextSong(song);
  121. this.fetchOwnRatings();
  122. } else {
  123. // TODO This will probably need to be handled
  124. this.props.onNextSong(null);
  125. }
  126. socket.emit("playlists.indexForUser", res => {
  127. if (res.status === "success") this.props.onPlaylistsUpdate(res.data);
  128. });
  129. socket.on("event:songs.next", data => {
  130. this.addTopToQueue();
  131. if (data.currentSong) {
  132. let song = {
  133. songId: data.currentSong.songId,
  134. timings: {
  135. duration: data.currentSong.duration,
  136. skipDuration: data.currentSong.skipDuration,
  137. // timeElapsed?
  138. timePaused: data.timePaused,
  139. // pausedAt?
  140. startedAt: data.startedAt,
  141. },
  142. title: data.currentSong.title,
  143. artists: data.currentSong.artists,
  144. thumbnail: data.currentSong.thumbnail,
  145. ratings: {
  146. enabled: !(data.currentSong.likes === -1 && data.currentSong.dislikes === -1),
  147. likes: data.currentSong.likes,
  148. dislikes: data.currentSong.dislikes,
  149. },
  150. };
  151. this.props.onNextSong(song);
  152. this.fetchOwnRatings();
  153. } else {
  154. this.props.onNextSong(null);
  155. }
  156. });
  157. socket.on("event:stations.pause", pausedAt => {
  158. // TODO Dispatch to station info
  159. this.props.onPause();
  160. this.props.onPauseTime(pausedAt);
  161. });
  162. socket.on("event:stations.resume", data => {
  163. // TODO Dispatch to station info
  164. this.props.onResume();
  165. this.props.onResumeTime(data.timePaused);
  166. });
  167. socket.on("event:song.like", data => {
  168. if (data.songId === this.props.song.songId) {
  169. this.props.onLikeUpdate(data.likes);
  170. this.props.onDislikeUpdate(data.dislikes);
  171. }
  172. });
  173. socket.on("event:song.dislike", data => {
  174. if (data.songId === this.props.song.songId) {
  175. this.props.onLikeUpdate(data.likes);
  176. this.props.onDislikeUpdate(data.dislikes);
  177. }
  178. });
  179. socket.on("event:song.unlike", data => {
  180. if (data.songId === this.props.song.songId) {
  181. this.props.onLikeUpdate(data.likes);
  182. this.props.onDislikeUpdate(data.dislikes);
  183. }
  184. });
  185. socket.on("event:song.undislike", data => {
  186. if (data.songId === this.props.song.songId) {
  187. this.props.onLikeUpdate(data.likes);
  188. this.props.onDislikeUpdate(data.dislikes);
  189. }
  190. });
  191. socket.on("event:song.newRatings", data => {
  192. if (data.songId === this.props.song.songId) {
  193. this.props.onLikedUpdate(data.liked);
  194. this.props.onDislikedUpdate(data.disliked);
  195. }
  196. });
  197. socket.on("event:playlist.create", () => {
  198. socket.emit("playlists.indexForUser", res => {
  199. if (res.status === "success") this.props.onPlaylistsUpdate(res.data);
  200. });
  201. });
  202. socket.on("event:playlist.delete", () => {
  203. socket.emit("playlists.indexForUser", res => {
  204. if (res.status === "success") this.props.onPlaylistsUpdate(res.data);
  205. });
  206. });
  207. socket.on("event:playlist.addSong", data => {
  208. this.props.onPlaylistsAddSong(data.playlistId, data.song);
  209. });
  210. socket.on("event:playlist.updateDisplayName", data => {
  211. this.props.onPlaylistsUpdateDisplayName(data.playlistId, data.displayName);
  212. });
  213. socket.on("event:playlist.moveSongToBottom", data => {
  214. this.props.onPlaylistsMoveSongToBottom(data.playlistId, data.songId);
  215. });
  216. socket.on("event:playlist.moveSongToTop", data => {
  217. this.props.onPlaylistsMoveSongToTop(data.playlistId, data.songId);
  218. });
  219. socket.on("event:playlist.removeSong", data => {
  220. this.props.onPlaylistsRemoveSong(data.playlistId, data.songId);
  221. });
  222. if (this.props.station.type === "community") {
  223. socket.emit("stations.getQueue", this.props.station.stationId, data => {
  224. if (data.status === "success") {
  225. this.props.onQueueIndex(data.queue);
  226. }
  227. //TODO Handle error
  228. });
  229. socket.on("event:queue.update", queue => {
  230. this.props.onQueueUpdate(queue);
  231. });
  232. }
  233. }
  234. });
  235. });
  236. }
  237. componentWillUnmount() {
  238. clearInterval(this.state.timeElapsedInterval);
  239. }
  240. addTopToQueue = () => {
  241. const randomIdentifier = Math.floor(Math.random() * 10000000000);
  242. console.log("ADDTOPTOQUEUE", randomIdentifier);
  243. const automaticallyAddedSongId = this.state.automaticallyAddedSongId;
  244. const privatePlaylistQueue = this.props.station.privatePlaylistQueue;
  245. io.getSocket((socket) => {
  246. async.waterfall([
  247. (next) => {
  248. if (this.state.mode === "normal") return next("Mode invalid."); //
  249. if (!privatePlaylistQueue) return next("No playlist selected."); // There hasn't been any playlist that has been selected
  250. if (!automaticallyAddedSongId) return next(); // There hasn't been any song that automatically got added to the queue
  251. //if (this.props.song.exists && automaticallyAddedSongId === this.props.song.songId) return next("Previously automatically added song is still playing."); // The song that was previously automatically added is already currently playing
  252. let alreadyAdded = false;
  253. this.props.station.songList.forEach((song) => {
  254. if (automaticallyAddedSongId === song.get("songId")) alreadyAdded = true;
  255. });
  256. if (alreadyAdded) return next("Previously automatically added song is still in the queue."); // The song that was automatically added previously is already currently in the queue
  257. return next();
  258. },
  259. (next) => {
  260. const playlist = this.props.playlists.find((playlist) => {
  261. return privatePlaylistQueue === playlist.get("playlistId");
  262. });
  263. if (!playlist) return next("Selected playlist isn't found.");
  264. const song = playlist.get("songs").get(0);
  265. if (!song) return next("Top song couldn't be found.");
  266. if (song.get("duration") > 15 * 60) return next("BOTTOM");
  267. const songId = song.get("songId");
  268. this.setState({
  269. ...this.state,
  270. automaticallyAddedSongId: songId,
  271. });
  272. socket.emit("stations.addToQueue", this.props.station.stationId, songId, data => {
  273. return next(null, data, songId);
  274. });
  275. },
  276. (data, songId, next) => {
  277. if (data.status !== "success") return next("Song couldn't be added to the queue.");
  278. this.moveToBottom(privatePlaylistQueue, songId, (data) => {
  279. return next(null, data);
  280. });
  281. },
  282. (data, next) => {
  283. if (data.status !== "success") return next("Song couldn't be moved to the bottom of the playlist.");
  284. return next();
  285. },
  286. ], (err, res) => {
  287. if (err) console.log("ADDTOPTOQUEUE ERROR", randomIdentifier, err, res);
  288. else console.log("ADDTOPTOQUEUE SUCCESS", randomIdentifier);
  289. if (err === "BOTTOM") {
  290. this.moveToBottom(privatePlaylistQueue, res.songId, (data) => {
  291. if (data.status === "success" && res.status !== "success") {
  292. setTimeout(() => {
  293. this.addTopToQueue();
  294. }, 2000);
  295. }
  296. });
  297. }
  298. });
  299. });
  300. };
  301. moveToBottom = (playlistId, songId, cb) => {
  302. io.getSocket((socket) => {
  303. socket.emit("playlists.moveSongToBottom", playlistId, songId, data => {
  304. cb(data);
  305. });
  306. });
  307. }
  308. fetchOwnRatings = () => {
  309. io.getSocket((socket) => {
  310. if (!this.props.song.exists) return;
  311. socket.emit("songs.getOwnSongRatings", this.props.song.songId, (data) => {
  312. if (this.props.song.songId === data.songId) {
  313. this.props.onLikedUpdate(data.liked);
  314. this.props.onDislikedUpdate(data.disliked);
  315. }
  316. });
  317. });
  318. };
  319. isOwner = () => {
  320. if (this.props.user.loggedIn) {
  321. if (this.props.user.role === "admin") return true;
  322. if (this.props.user.userId === this.props.station.ownerId) return true;
  323. }
  324. return false;
  325. };
  326. addSongTemp = () => {
  327. io.getSocket(socket => {
  328. socket.emit('stations.addToQueue', this.props.station.stationId, '60ItHLz5WEA', data => {
  329. console.log("ATQ Res", data);
  330. });
  331. });
  332. };
  333. resumeStation = () => {
  334. io.getSocket(socket => {
  335. socket.emit("stations.resume", this.props.station.stationId, data => {
  336. // TODO Handle error/success
  337. });
  338. });
  339. };
  340. pauseStation = () => {
  341. io.getSocket(socket => {
  342. socket.emit("stations.pause", this.props.station.stationId, data => {
  343. // TODO Handle error/success
  344. });
  345. });
  346. };
  347. skipStation = () => {
  348. io.getSocket(socket => {
  349. socket.emit("stations.forceSkip", this.props.station.stationId, data => {});
  350. });
  351. }
  352. render() {
  353. const { t } = this.props;
  354. //TODO Make this not re-render a lot
  355. return (
  356. <main id="station">
  357. <Overlays t={ this.props.t } />
  358. <div id="sidebar">
  359. <button onClick={ () => { this.props.openOverlay1("users") } }><i className="material-icons">people</i></button>
  360. <button onClick={ () => { this.props.openOverlay1("queueList") } }><i className="material-icons">queue_music</i></button>
  361. <button onClick={ () => { this.props.openOverlay1("playlists") } }><i className="material-icons">library_music</i></button>
  362. <hr/>
  363. {
  364. (this.isOwner())
  365. ? (this.props.station.paused)
  366. ? <button onClick={ this.resumeStation }><i className="material-icons">play_arrow</i></button>
  367. : <button onClick={ this.pauseStation }><i className="material-icons">pause</i></button>
  368. : null
  369. }
  370. {
  371. (this.isOwner())
  372. ? <button onClick={ this.skipStation }><i className="material-icons">skip_next</i></button>
  373. : null
  374. }
  375. {
  376. (this.isOwner())
  377. ? <button onClick={ () => { this.props.openOverlay1("settings") } }><i className="material-icons">settings</i></button>
  378. : null
  379. }
  380. </div>
  381. <h1 onClick={ this.addSongTemp }>{ this.props.station.displayName }</h1>
  382. <PlayerDebug />
  383. <div className={(!this.props.song.exists) ? "player-container hidden" : "player-container"}>
  384. <div className="iframe-container">
  385. <Player onRef={ ref => (this.player = ref) }/>
  386. { (this.props.station.paused) ? <div className="paused-overlay"><span>Paused</span><i className="material-icons">pause</i></div> : null }
  387. </div>
  388. <Seekerbar/>
  389. </div>
  390. { (this.props.song.exists) ? (
  391. [
  392. <div key="content" className="content">
  393. <span className="title">{ this.props.song.title }</span>
  394. <span className="artists">{ this.props.song.artists.join(", ") }</span>
  395. <Time/>
  396. <VolumeSlider/>
  397. <Ratings/>
  398. </div>,
  399. ]) : (
  400. <h1>No song playing</h1>
  401. ) }
  402. </main>
  403. );
  404. }
  405. }