index.vue 17 KB

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