index.vue 18 KB

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