index.jsx 17 KB

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