index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. <script setup lang="ts">
  2. import {
  3. defineAsyncComponent,
  4. ref,
  5. watch,
  6. onMounted,
  7. onBeforeUnmount
  8. } from "vue";
  9. import Toast from "toasters";
  10. import { storeToRefs } from "pinia";
  11. import { useWebsocketsStore } from "@/stores/websockets";
  12. import { useUserAuthStore } from "@/stores/userAuth";
  13. import { useModalsStore } from "@/stores/modals";
  14. import { useManageStationStore } from "@/stores/manageStation";
  15. const Modal = defineAsyncComponent(() => import("@/components/Modal.vue"));
  16. const Queue = defineAsyncComponent(() => import("@/components/Queue.vue"));
  17. const SongItem = defineAsyncComponent(
  18. () => import("@/components/SongItem.vue")
  19. );
  20. const StationInfoBox = defineAsyncComponent(
  21. () => import("@/components/StationInfoBox.vue")
  22. );
  23. const Settings = defineAsyncComponent(() => import("./Settings.vue"));
  24. const PlaylistTabBase = defineAsyncComponent(
  25. () => import("@/components/PlaylistTabBase.vue")
  26. );
  27. const Request = defineAsyncComponent(() => import("@/components/Request.vue"));
  28. const QuickConfirm = defineAsyncComponent(
  29. () => import("@/components/QuickConfirm.vue")
  30. );
  31. const props = defineProps({
  32. modalUuid: { type: String, default: "" }
  33. });
  34. const tabs = ref([]);
  35. const userAuthStore = useUserAuthStore();
  36. const { loggedIn, userId } = storeToRefs(userAuthStore);
  37. const { socket } = useWebsocketsStore();
  38. const manageStationStore = useManageStationStore(props);
  39. const {
  40. stationId,
  41. sector,
  42. tab,
  43. station,
  44. stationPlaylist,
  45. autofill,
  46. blacklist,
  47. stationPaused,
  48. currentSong
  49. } = storeToRefs(manageStationStore);
  50. const {
  51. editStation,
  52. setAutofillPlaylists,
  53. setBlacklist,
  54. clearStation,
  55. updateSongsList,
  56. updateStationPlaylist,
  57. repositionSongInList,
  58. updateStationPaused,
  59. updateCurrentSong,
  60. updateStation,
  61. updateIsFavorited,
  62. hasPermission,
  63. addDj,
  64. removeDj,
  65. updatePermissions
  66. } = manageStationStore;
  67. const { closeCurrentModal } = useModalsStore();
  68. const showTab = payload => {
  69. if (tabs.value[`${payload}-tab`])
  70. tabs.value[`${payload}-tab`].scrollIntoView({ block: "nearest" });
  71. manageStationStore.showTab(payload);
  72. };
  73. const canRequest = () =>
  74. station.value &&
  75. loggedIn.value &&
  76. station.value.requests &&
  77. station.value.requests.enabled &&
  78. (station.value.requests.access === "user" ||
  79. (station.value.requests.access === "owner" &&
  80. hasPermission("stations.request")));
  81. const removeStation = () => {
  82. socket.dispatch("stations.remove", stationId.value, res => {
  83. new Toast(res.message);
  84. });
  85. };
  86. const resetQueue = () => {
  87. socket.dispatch("stations.resetQueue", stationId.value, res => {
  88. if (res.status !== "success")
  89. new Toast({
  90. content: `Error: ${res.message}`,
  91. timeout: 8000
  92. });
  93. else new Toast({ content: res.message, timeout: 4000 });
  94. });
  95. };
  96. watch(
  97. () => station.value.requests,
  98. () => {
  99. if (tab.value === "request" && !canRequest()) {
  100. if (hasPermission("stations.update")) showTab("settings");
  101. else if (
  102. !(sector.value === "home" && !hasPermission("stations.update"))
  103. )
  104. closeCurrentModal();
  105. }
  106. }
  107. );
  108. watch(
  109. () => station.value.autofill,
  110. value => {
  111. if (tab.value === "autofill" && value && !value.enabled)
  112. showTab("settings");
  113. }
  114. );
  115. onMounted(() => {
  116. socket.dispatch(`stations.getStationById`, stationId.value, async res => {
  117. if (res.status === "success") {
  118. editStation(res.data.station);
  119. await updatePermissions();
  120. if (!hasPermission("stations.update")) showTab("request");
  121. const currentSong = res.data.station.currentSong
  122. ? res.data.station.currentSong
  123. : {};
  124. updateCurrentSong(currentSong);
  125. updateStationPaused(res.data.station.paused);
  126. socket.dispatch(
  127. "stations.getStationAutofillPlaylistsById",
  128. stationId.value,
  129. res => {
  130. if (res.status === "success")
  131. setAutofillPlaylists(res.data.playlists);
  132. }
  133. );
  134. socket.dispatch(
  135. "stations.getStationBlacklistById",
  136. stationId.value,
  137. res => {
  138. if (res.status === "success")
  139. setBlacklist(res.data.playlists);
  140. }
  141. );
  142. if (hasPermission("stations.view")) {
  143. socket.dispatch(
  144. "playlists.getPlaylistForStation",
  145. stationId.value,
  146. true,
  147. res => {
  148. if (res.status === "success") {
  149. updateStationPlaylist(res.data.playlist);
  150. }
  151. }
  152. );
  153. }
  154. socket.dispatch("stations.getQueue", stationId.value, res => {
  155. if (res.status === "success") updateSongsList(res.data.queue);
  156. });
  157. socket.dispatch(
  158. "apis.joinRoom",
  159. `manage-station.${stationId.value}`
  160. );
  161. socket.on(
  162. "event:station.updated",
  163. res => {
  164. updateStation(res.data.station);
  165. },
  166. { modalUuid: props.modalUuid }
  167. );
  168. socket.on(
  169. "event:station.autofillPlaylist",
  170. res => {
  171. const { playlist } = res.data;
  172. const playlistIndex = autofill.value
  173. .map(autofillPlaylist => autofillPlaylist._id)
  174. .indexOf(playlist._id);
  175. if (playlistIndex === -1) autofill.value.push(playlist);
  176. },
  177. { modalUuid: props.modalUuid }
  178. );
  179. socket.on(
  180. "event:station.blacklistedPlaylist",
  181. res => {
  182. const { playlist } = res.data;
  183. const playlistIndex = blacklist.value
  184. .map(blacklistedPlaylist => blacklistedPlaylist._id)
  185. .indexOf(playlist._id);
  186. if (playlistIndex === -1) blacklist.value.push(playlist);
  187. },
  188. { modalUuid: props.modalUuid }
  189. );
  190. socket.on(
  191. "event:station.removedAutofillPlaylist",
  192. res => {
  193. const { playlistId } = res.data;
  194. const playlistIndex = autofill.value
  195. .map(playlist => playlist._id)
  196. .indexOf(playlistId);
  197. if (playlistIndex >= 0)
  198. autofill.value.splice(playlistIndex, 1);
  199. },
  200. { modalUuid: props.modalUuid }
  201. );
  202. socket.on(
  203. "event:station.removedBlacklistedPlaylist",
  204. res => {
  205. const { playlistId } = res.data;
  206. const playlistIndex = blacklist.value
  207. .map(playlist => playlist._id)
  208. .indexOf(playlistId);
  209. if (playlistIndex >= 0)
  210. blacklist.value.splice(playlistIndex, 1);
  211. },
  212. { modalUuid: props.modalUuid }
  213. );
  214. socket.on(
  215. "event:station.deleted",
  216. () => {
  217. new Toast(`The station you were editing was deleted.`);
  218. closeCurrentModal();
  219. },
  220. { modalUuid: props.modalUuid }
  221. );
  222. socket.on(
  223. "event:user.station.favorited",
  224. res => {
  225. if (res.data.stationId === stationId.value)
  226. updateIsFavorited(true);
  227. },
  228. { modalUuid: props.modalUuid }
  229. );
  230. socket.on(
  231. "event:user.station.unfavorited",
  232. res => {
  233. if (res.data.stationId === stationId.value)
  234. updateIsFavorited(false);
  235. },
  236. { modalUuid: props.modalUuid }
  237. );
  238. } else {
  239. new Toast(`Station with that ID not found`);
  240. closeCurrentModal();
  241. }
  242. });
  243. socket.on(
  244. "event:manageStation.queue.updated",
  245. res => {
  246. if (res.data.stationId === stationId.value)
  247. updateSongsList(res.data.queue);
  248. },
  249. { modalUuid: props.modalUuid }
  250. );
  251. socket.on(
  252. "event:manageStation.queue.song.repositioned",
  253. res => {
  254. if (res.data.stationId === stationId.value)
  255. repositionSongInList(res.data.song);
  256. },
  257. { modalUuid: props.modalUuid }
  258. );
  259. socket.on(
  260. "event:station.pause",
  261. res => {
  262. if (res.data.stationId === stationId.value)
  263. updateStationPaused(true);
  264. },
  265. { modalUuid: props.modalUuid }
  266. );
  267. socket.on(
  268. "event:station.resume",
  269. res => {
  270. if (res.data.stationId === stationId.value)
  271. updateStationPaused(false);
  272. },
  273. { modalUuid: props.modalUuid }
  274. );
  275. socket.on(
  276. "event:station.nextSong",
  277. res => {
  278. if (res.data.stationId === stationId.value)
  279. updateCurrentSong(res.data.currentSong || {});
  280. },
  281. { modalUuid: props.modalUuid }
  282. );
  283. socket.on("event:manageStation.djs.added", res => {
  284. if (res.data.stationId === stationId.value) {
  285. if (res.data.user._id === userId.value) updatePermissions();
  286. addDj(res.data.user);
  287. }
  288. });
  289. socket.on("event:manageStation.djs.removed", res => {
  290. if (res.data.stationId === stationId.value) {
  291. if (res.data.user._id === userId.value) updatePermissions();
  292. removeDj(res.data.user);
  293. }
  294. });
  295. socket.on("keep.event:user.role.updated", () => {
  296. updatePermissions();
  297. });
  298. if (hasPermission("stations.view")) {
  299. socket.on(
  300. "event:playlist.song.added",
  301. res => {
  302. if (stationPlaylist.value._id === res.data.playlistId)
  303. stationPlaylist.value.songs.push(res.data.song);
  304. },
  305. {
  306. modalUuid: props.modalUuid
  307. }
  308. );
  309. socket.on(
  310. "event:playlist.song.removed",
  311. res => {
  312. if (stationPlaylist.value._id === res.data.playlistId) {
  313. // remove song from array of playlists
  314. stationPlaylist.value.songs.forEach((song, index) => {
  315. if (song.youtubeId === res.data.youtubeId)
  316. stationPlaylist.value.songs.splice(index, 1);
  317. });
  318. }
  319. },
  320. {
  321. modalUuid: props.modalUuid
  322. }
  323. );
  324. socket.on(
  325. "event:playlist.songs.repositioned",
  326. res => {
  327. if (stationPlaylist.value._id === res.data.playlistId) {
  328. // for each song that has a new position
  329. res.data.songsBeingChanged.forEach(changedSong => {
  330. stationPlaylist.value.songs.forEach((song, index) => {
  331. // find song locally
  332. if (song.youtubeId === changedSong.youtubeId) {
  333. // change song position attribute
  334. stationPlaylist.value.songs[index].position =
  335. changedSong.position;
  336. // reposition in array if needed
  337. if (index !== changedSong.position - 1)
  338. stationPlaylist.value.songs.splice(
  339. changedSong.position - 1,
  340. 0,
  341. stationPlaylist.value.songs.splice(
  342. index,
  343. 1
  344. )[0]
  345. );
  346. }
  347. });
  348. });
  349. }
  350. },
  351. {
  352. modalUuid: props.modalUuid
  353. }
  354. );
  355. }
  356. });
  357. onBeforeUnmount(() => {
  358. socket.dispatch(
  359. "apis.leaveRoom",
  360. `manage-station.${stationId.value}`,
  361. () => {}
  362. );
  363. if (hasPermission("stations.update")) showTab("settings");
  364. clearStation();
  365. // Delete the Pinia store that was created for this modal, after all other cleanup tasks are performed
  366. manageStationStore.$dispose();
  367. });
  368. </script>
  369. <template>
  370. <modal
  371. v-if="station"
  372. :title="
  373. sector === 'home' && !hasPermission('stations.view.manage')
  374. ? 'View Queue'
  375. : !hasPermission('stations.view.manage')
  376. ? 'Add Song to Queue'
  377. : 'Manage Station'
  378. "
  379. :style="`--primary-color: var(--${station.theme})`"
  380. class="manage-station-modal"
  381. :size="
  382. hasPermission('stations.view.manage') || sector !== 'home'
  383. ? 'wide'
  384. : null
  385. "
  386. :split="hasPermission('stations.view.manage') || sector !== 'home'"
  387. >
  388. <template #body v-if="station && station._id">
  389. <div class="left-section">
  390. <div class="section">
  391. <div class="station-info-box-wrapper">
  392. <station-info-box
  393. :station="station"
  394. :station-paused="stationPaused"
  395. :show-go-to-station="sector !== 'station'"
  396. :sector="'manageStation'"
  397. :modal-uuid="modalUuid"
  398. />
  399. </div>
  400. <div
  401. v-if="
  402. hasPermission('stations.view.manage') ||
  403. sector !== 'home'
  404. "
  405. >
  406. <div class="tab-selection">
  407. <button
  408. v-if="hasPermission('stations.update')"
  409. class="button is-default"
  410. :class="{ selected: tab === 'settings' }"
  411. :ref="el => (tabs['settings-tab'] = el)"
  412. @click="showTab('settings')"
  413. >
  414. Settings
  415. </button>
  416. <button
  417. v-if="canRequest()"
  418. class="button is-default"
  419. :class="{ selected: tab === 'request' }"
  420. :ref="el => (tabs['request-tab'] = el)"
  421. @click="showTab('request')"
  422. >
  423. Request
  424. </button>
  425. <button
  426. v-if="
  427. hasPermission('stations.autofill') &&
  428. station.autofill.enabled
  429. "
  430. class="button is-default"
  431. :class="{ selected: tab === 'autofill' }"
  432. :ref="el => (tabs['autofill-tab'] = el)"
  433. @click="showTab('autofill')"
  434. >
  435. Autofill
  436. </button>
  437. <button
  438. v-if="hasPermission('stations.blacklist')"
  439. class="button is-default"
  440. :class="{ selected: tab === 'blacklist' }"
  441. :ref="el => (tabs['blacklist-tab'] = el)"
  442. @click="showTab('blacklist')"
  443. >
  444. Blacklist
  445. </button>
  446. </div>
  447. <settings
  448. v-if="hasPermission('stations.update')"
  449. class="tab"
  450. v-show="tab === 'settings'"
  451. :modal-uuid="modalUuid"
  452. ref="settingsTabComponent"
  453. />
  454. <request
  455. v-if="canRequest()"
  456. class="tab"
  457. v-show="tab === 'request'"
  458. :sector="'manageStation'"
  459. :disable-auto-request="sector !== 'station'"
  460. :modal-uuid="modalUuid"
  461. />
  462. <playlist-tab-base
  463. v-if="
  464. hasPermission('stations.autofill') &&
  465. station.autofill.enabled
  466. "
  467. class="tab"
  468. v-show="tab === 'autofill'"
  469. :type="'autofill'"
  470. :modal-uuid="modalUuid"
  471. >
  472. <template #info>
  473. <p>
  474. Select playlists to automatically add songs
  475. within to the queue
  476. </p>
  477. </template>
  478. </playlist-tab-base>
  479. <playlist-tab-base
  480. v-if="hasPermission('stations.blacklist')"
  481. class="tab"
  482. v-show="tab === 'blacklist'"
  483. :type="'blacklist'"
  484. :modal-uuid="modalUuid"
  485. >
  486. <template #info>
  487. <p>
  488. Blacklist a playlist to prevent all songs
  489. within from playing in this station
  490. </p>
  491. </template>
  492. </playlist-tab-base>
  493. </div>
  494. </div>
  495. </div>
  496. <div class="right-section">
  497. <div class="section">
  498. <div class="queue-title">
  499. <h4 class="section-title">Queue</h4>
  500. </div>
  501. <hr class="section-horizontal-rule" />
  502. <song-item
  503. v-if="currentSong.youtubeId"
  504. :song="currentSong"
  505. :requested-by="true"
  506. header="Currently Playing.."
  507. class="currently-playing"
  508. />
  509. <queue :modal-uuid="modalUuid" sector="manageStation" />
  510. </div>
  511. </div>
  512. </template>
  513. <template #footer>
  514. <div class="right">
  515. <quick-confirm
  516. v-if="hasPermission('stations.queue.reset')"
  517. @confirm="resetQueue()"
  518. >
  519. <a class="button is-danger">Reset queue</a>
  520. </quick-confirm>
  521. <quick-confirm
  522. v-if="hasPermission('stations.remove')"
  523. @confirm="removeStation()"
  524. >
  525. <button class="button is-danger">Delete station</button>
  526. </quick-confirm>
  527. </div>
  528. </template>
  529. </modal>
  530. </template>
  531. <style lang="less">
  532. .manage-station-modal.modal .modal-card {
  533. .tab > button {
  534. width: 100%;
  535. margin-bottom: 10px;
  536. }
  537. .currently-playing.song-item {
  538. .thumbnail {
  539. min-width: 130px;
  540. width: 130px;
  541. height: 130px;
  542. }
  543. }
  544. }
  545. </style>
  546. <style lang="less" scoped>
  547. .night-mode {
  548. .manage-station-modal.modal .modal-card-body {
  549. .left-section {
  550. .station-info-box-wrapper {
  551. border: 0;
  552. }
  553. .section {
  554. background-color: transparent !important;
  555. }
  556. .tab-selection .button {
  557. background: var(--dark-grey);
  558. color: var(--white);
  559. }
  560. .tab {
  561. background-color: var(--dark-grey-3);
  562. border: 0;
  563. }
  564. }
  565. .right-section .section,
  566. #queue {
  567. border-radius: @border-radius;
  568. background-color: transparent !important;
  569. }
  570. }
  571. }
  572. .manage-station-modal.modal .modal-card-body {
  573. display: flex;
  574. flex-wrap: wrap;
  575. height: 100%;
  576. .left-section {
  577. .station-info-box-wrapper {
  578. border-radius: @border-radius;
  579. border: 1px solid var(--light-grey-3);
  580. overflow: hidden;
  581. margin-bottom: 20px;
  582. }
  583. .tab-selection {
  584. display: flex;
  585. overflow-x: auto;
  586. .button {
  587. border-radius: @border-radius @border-radius 0 0;
  588. border: 0;
  589. text-transform: uppercase;
  590. font-size: 14px;
  591. color: var(--dark-grey-3);
  592. background-color: var(--light-grey-2);
  593. flex-grow: 1;
  594. height: 32px;
  595. &:not(:first-of-type) {
  596. margin-left: 5px;
  597. }
  598. }
  599. .selected {
  600. background-color: var(--primary-color) !important;
  601. color: var(--white) !important;
  602. font-weight: 600;
  603. }
  604. }
  605. .tab {
  606. border: 1px solid var(--light-grey-3);
  607. padding: 15px 10px;
  608. border-radius: 0 0 @border-radius @border-radius;
  609. }
  610. }
  611. .right-section {
  612. .section {
  613. .queue-title {
  614. display: flex;
  615. line-height: 30px;
  616. .material-icons {
  617. margin-left: 5px;
  618. margin-bottom: 5px;
  619. font-size: 28px;
  620. cursor: pointer;
  621. &:first-of-type {
  622. margin-left: auto;
  623. }
  624. &.skip-station {
  625. color: var(--dark-red);
  626. }
  627. &.resume-station,
  628. &.pause-station {
  629. color: var(--primary-color);
  630. }
  631. }
  632. }
  633. .currently-playing {
  634. margin-bottom: 10px;
  635. }
  636. }
  637. }
  638. &.modal-wide .left-section .section:first-child {
  639. padding: 0 15px 15px !important;
  640. }
  641. }
  642. </style>