index.vue 17 KB

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