index.vue 15 KB

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