index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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, currentUser } = 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 === currentUser.value._id)
  324. updatePermissions();
  325. addDj(res.data.user);
  326. }
  327. });
  328. socket.on("event:manageStation.djs.removed", res => {
  329. if (res.data.stationId === stationId.value) {
  330. if (res.data.user._id === currentUser.value._id)
  331. updatePermissions();
  332. removeDj(res.data.user);
  333. }
  334. });
  335. socket.on("keep.event:user.role.updated", () => {
  336. updatePermissions();
  337. });
  338. if (hasPermission("stations.view")) {
  339. socket.on(
  340. "event:playlist.song.added",
  341. res => {
  342. if (stationPlaylist.value._id === res.data.playlistId)
  343. stationPlaylist.value.songs.push(res.data.song);
  344. },
  345. {
  346. modalUuid: props.modalUuid
  347. }
  348. );
  349. socket.on(
  350. "event:playlist.song.removed",
  351. res => {
  352. if (stationPlaylist.value._id === res.data.playlistId) {
  353. // remove song from array of playlists
  354. stationPlaylist.value.songs.forEach((song, index) => {
  355. if (song.mediaSource === res.data.mediaSource)
  356. stationPlaylist.value.songs.splice(index, 1);
  357. });
  358. }
  359. },
  360. {
  361. modalUuid: props.modalUuid
  362. }
  363. );
  364. socket.on(
  365. "event:playlist.song.replaced",
  366. res => {
  367. if (stationPlaylist.value._id === res.data.playlistId)
  368. stationPlaylist.value.songs =
  369. stationPlaylist.value.songs.map(song =>
  370. song.mediaSource === res.data.oldMediaSource
  371. ? res.data.song
  372. : song
  373. );
  374. },
  375. {
  376. modalUuid: props.modalUuid
  377. }
  378. );
  379. socket.on(
  380. "event:playlist.songs.repositioned",
  381. res => {
  382. if (stationPlaylist.value._id === res.data.playlistId) {
  383. // for each song that has a new position
  384. res.data.songsBeingChanged.forEach(changedSong => {
  385. stationPlaylist.value.songs.forEach(
  386. (song, index) => {
  387. // find song locally
  388. if (
  389. song.mediaSource ===
  390. changedSong.mediaSource
  391. ) {
  392. // change song position attribute
  393. stationPlaylist.value.songs[
  394. index
  395. ].position = changedSong.position;
  396. // reposition in array if needed
  397. if (index !== changedSong.position - 1)
  398. stationPlaylist.value.songs.splice(
  399. changedSong.position - 1,
  400. 0,
  401. stationPlaylist.value.songs.splice(
  402. index,
  403. 1
  404. )[0]
  405. );
  406. }
  407. }
  408. );
  409. });
  410. }
  411. },
  412. {
  413. modalUuid: props.modalUuid
  414. }
  415. );
  416. }
  417. });
  418. });
  419. onBeforeUnmount(() => {
  420. socket.dispatch(
  421. "apis.leaveRoom",
  422. `manage-station.${stationId.value}`,
  423. () => {}
  424. );
  425. if (hasPermission("stations.update")) showTab("settings");
  426. clearStation();
  427. // Delete the Pinia store that was created for this modal, after all other cleanup tasks are performed
  428. manageStationStore.$dispose();
  429. });
  430. </script>
  431. <template>
  432. <modal
  433. v-if="station"
  434. :title="
  435. sector === 'home' && !hasPermission('stations.view.manage')
  436. ? 'View Queue'
  437. : !hasPermission('stations.view.manage')
  438. ? 'Add Song to Queue'
  439. : 'Manage Station'
  440. "
  441. :style="`--primary-color: var(--${station.theme})`"
  442. class="manage-station-modal"
  443. :size="
  444. hasPermission('stations.view.manage') || sector !== 'home'
  445. ? 'wide'
  446. : null
  447. "
  448. :split="hasPermission('stations.view.manage') || sector !== 'home'"
  449. >
  450. <template #body v-if="station && station._id">
  451. <div class="left-section">
  452. <div class="section">
  453. <div class="station-info-box-wrapper">
  454. <station-info-box
  455. :station="station"
  456. :station-paused="stationPaused"
  457. :show-go-to-station="sector !== 'station'"
  458. :sector="'manageStation'"
  459. :modal-uuid="modalUuid"
  460. />
  461. </div>
  462. <div
  463. v-if="
  464. hasPermission('stations.view.manage') ||
  465. sector !== 'home'
  466. "
  467. >
  468. <div class="tab-selection">
  469. <button
  470. v-if="hasPermission('stations.update')"
  471. class="button is-default"
  472. :class="{ selected: tab === 'settings' }"
  473. :ref="el => (tabs['settings-tab'] = el)"
  474. @click="showTab('settings')"
  475. >
  476. Settings
  477. </button>
  478. <button
  479. v-if="canRequest()"
  480. class="button is-default"
  481. :class="{ selected: tab === 'request' }"
  482. :ref="el => (tabs['request-tab'] = el)"
  483. @click="showTab('request')"
  484. >
  485. Request
  486. </button>
  487. <button
  488. v-if="
  489. hasPermission('stations.autofill') &&
  490. station.autofill.enabled
  491. "
  492. class="button is-default"
  493. :class="{ selected: tab === 'autofill' }"
  494. :ref="el => (tabs['autofill-tab'] = el)"
  495. @click="showTab('autofill')"
  496. >
  497. Autofill
  498. </button>
  499. <button
  500. v-if="hasPermission('stations.blacklist')"
  501. class="button is-default"
  502. :class="{ selected: tab === 'blacklist' }"
  503. :ref="el => (tabs['blacklist-tab'] = el)"
  504. @click="showTab('blacklist')"
  505. >
  506. Blacklist
  507. </button>
  508. </div>
  509. <settings
  510. v-if="hasPermission('stations.update')"
  511. class="tab"
  512. v-show="tab === 'settings'"
  513. :modal-uuid="modalUuid"
  514. ref="settingsTabComponent"
  515. />
  516. <request
  517. v-if="canRequest()"
  518. class="tab"
  519. v-show="tab === 'request'"
  520. :sector="'manageStation'"
  521. :disable-auto-request="sector !== 'station'"
  522. :modal-uuid="modalUuid"
  523. />
  524. <playlist-tab-base
  525. v-if="
  526. hasPermission('stations.autofill') &&
  527. station.autofill.enabled
  528. "
  529. class="tab"
  530. v-show="tab === 'autofill'"
  531. :type="'autofill'"
  532. :modal-uuid="modalUuid"
  533. >
  534. <template #info>
  535. <p>
  536. Select playlists to automatically add songs
  537. within to the queue
  538. </p>
  539. </template>
  540. </playlist-tab-base>
  541. <playlist-tab-base
  542. v-if="hasPermission('stations.blacklist')"
  543. class="tab"
  544. v-show="tab === 'blacklist'"
  545. :type="'blacklist'"
  546. :modal-uuid="modalUuid"
  547. >
  548. <template #info>
  549. <p>
  550. Blacklist a playlist to prevent all songs
  551. within from playing in this station
  552. </p>
  553. </template>
  554. </playlist-tab-base>
  555. </div>
  556. </div>
  557. </div>
  558. <div class="right-section">
  559. <div class="section">
  560. <div class="queue-title">
  561. <h4 class="section-title">Queue</h4>
  562. </div>
  563. <hr class="section-horizontal-rule" />
  564. <media-item
  565. v-if="currentSong.mediaSource"
  566. :song="currentSong"
  567. :requested-by="true"
  568. header="Currently Playing.."
  569. class="currently-playing"
  570. />
  571. <queue :modal-uuid="modalUuid" sector="manageStation" />
  572. </div>
  573. </div>
  574. </template>
  575. <template #footer>
  576. <div class="right">
  577. <quick-confirm
  578. v-if="hasPermission('stations.queue.reset')"
  579. @confirm="resetQueue()"
  580. >
  581. <a class="button is-danger">Reset queue</a>
  582. </quick-confirm>
  583. <quick-confirm
  584. v-if="hasPermission('stations.remove')"
  585. @confirm="removeStation()"
  586. >
  587. <button class="button is-danger">Delete station</button>
  588. </quick-confirm>
  589. </div>
  590. </template>
  591. </modal>
  592. </template>
  593. <style lang="less">
  594. .manage-station-modal.modal .modal-card {
  595. .tab > button {
  596. width: 100%;
  597. margin-top: 10px;
  598. }
  599. .currently-playing.song-item {
  600. height: 130px !important;
  601. .thumbnail-and-info .thumbnail {
  602. min-width: 130px;
  603. width: 130px;
  604. }
  605. }
  606. }
  607. </style>
  608. <style lang="less" scoped>
  609. .night-mode {
  610. .manage-station-modal.modal .modal-card-body {
  611. .left-section {
  612. .station-info-box-wrapper {
  613. border: 0;
  614. }
  615. .section {
  616. background-color: transparent !important;
  617. }
  618. .tab-selection .button {
  619. background: var(--dark-grey);
  620. color: var(--white);
  621. }
  622. .tab {
  623. background-color: var(--dark-grey-3);
  624. border: 0;
  625. }
  626. }
  627. .right-section .section,
  628. #queue {
  629. border-radius: @border-radius;
  630. background-color: transparent !important;
  631. }
  632. }
  633. }
  634. .manage-station-modal.modal .modal-card-body {
  635. display: flex;
  636. flex-wrap: wrap;
  637. height: 100%;
  638. .left-section {
  639. .station-info-box-wrapper {
  640. border-radius: @border-radius;
  641. border: 1px solid var(--light-grey-3);
  642. overflow: hidden;
  643. margin-bottom: 20px;
  644. }
  645. .tab-selection {
  646. display: flex;
  647. overflow-x: auto;
  648. .button {
  649. border-radius: @border-radius @border-radius 0 0;
  650. border: 0;
  651. text-transform: uppercase;
  652. font-size: 14px;
  653. color: var(--dark-grey-3);
  654. background-color: var(--light-grey-2);
  655. flex-grow: 1;
  656. height: 32px;
  657. &:not(:first-of-type) {
  658. margin-left: 5px;
  659. }
  660. }
  661. .selected {
  662. background-color: var(--primary-color) !important;
  663. color: var(--white) !important;
  664. font-weight: 600;
  665. }
  666. }
  667. .tab {
  668. border: 1px solid var(--light-grey-3);
  669. padding: 15px 10px;
  670. border-radius: 0 0 @border-radius @border-radius;
  671. }
  672. }
  673. .right-section {
  674. .section {
  675. .queue-title {
  676. display: flex;
  677. line-height: 30px;
  678. .material-icons {
  679. margin-left: 5px;
  680. margin-bottom: 5px;
  681. font-size: 28px;
  682. cursor: pointer;
  683. &:first-of-type {
  684. margin-left: auto;
  685. }
  686. &.skip-station {
  687. color: var(--dark-red);
  688. }
  689. &.resume-station,
  690. &.pause-station {
  691. color: var(--primary-color);
  692. }
  693. }
  694. }
  695. .currently-playing {
  696. margin-bottom: 10px;
  697. }
  698. }
  699. }
  700. &.modal-wide .left-section .section:first-child {
  701. padding: 0 15px 15px !important;
  702. }
  703. }
  704. </style>