index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. <script setup lang="ts">
  2. import {
  3. defineAsyncComponent,
  4. ref,
  5. computed,
  6. onMounted,
  7. onBeforeUnmount
  8. } from "vue";
  9. import Toast from "toasters";
  10. import { storeToRefs } from "pinia";
  11. import { DraggableList } from "vue-draggable-list";
  12. import { useWebsocketsStore } from "@/stores/websockets";
  13. import { useEditPlaylistStore } from "@/stores/editPlaylist";
  14. import { useStationStore } from "@/stores/station";
  15. import { useUserAuthStore } from "@/stores/userAuth";
  16. import { useModalsStore } from "@/stores/modals";
  17. import utils from "@/utils";
  18. const Modal = defineAsyncComponent(() => import("@/components/Modal.vue"));
  19. const SongItem = defineAsyncComponent(
  20. () => import("@/components/SongItem.vue")
  21. );
  22. const Settings = defineAsyncComponent(() => import("./Tabs/Settings.vue"));
  23. const AddSongs = defineAsyncComponent(() => import("./Tabs/AddSongs.vue"));
  24. const ImportPlaylists = defineAsyncComponent(
  25. () => import("./Tabs/ImportPlaylists.vue")
  26. );
  27. const QuickConfirm = defineAsyncComponent(
  28. () => import("@/components/QuickConfirm.vue")
  29. );
  30. const props = defineProps({
  31. modalUuid: { type: String, required: true },
  32. playlistId: { type: String, required: true }
  33. });
  34. const { socket } = useWebsocketsStore();
  35. const editPlaylistStore = useEditPlaylistStore({ modalUuid: props.modalUuid });
  36. const stationStore = useStationStore();
  37. const userAuthStore = useUserAuthStore();
  38. const { station } = storeToRefs(stationStore);
  39. const { loggedIn, userId, role: userRole } = storeToRefs(userAuthStore);
  40. const drag = ref(false);
  41. const apiDomain = ref("");
  42. const gettingSongs = ref(false);
  43. const tabs = ref([]);
  44. const songItems = ref([]);
  45. const playlistSongs = computed({
  46. get: () => editPlaylistStore.playlist.songs,
  47. set: value => {
  48. editPlaylistStore.updatePlaylistSongs(value);
  49. }
  50. });
  51. const { tab, playlist } = storeToRefs(editPlaylistStore);
  52. const { setPlaylist, clearPlaylist, addSong, removeSong, repositionedSong } =
  53. editPlaylistStore;
  54. const { closeCurrentModal } = useModalsStore();
  55. const showTab = payload => {
  56. tabs.value[`${payload}-tab`].scrollIntoView({ block: "nearest" });
  57. editPlaylistStore.showTab(payload);
  58. };
  59. const { hasPermission } = userAuthStore;
  60. const isOwner = () =>
  61. loggedIn.value && userId.value === playlist.value.createdBy;
  62. const isEditable = permission =>
  63. ((playlist.value.type === "user" ||
  64. playlist.value.type === "user-liked" ||
  65. playlist.value.type === "user-disliked" ||
  66. playlist.value.type === "admin") &&
  67. (isOwner() || hasPermission(permission))) ||
  68. (playlist.value.type === "genre" &&
  69. permission === "playlists.update.privacy" &&
  70. hasPermission(permission));
  71. const repositionSong = ({ moved }) => {
  72. const { oldIndex, newIndex } = moved;
  73. if (oldIndex === newIndex) return; // we only need to update when song is moved
  74. const song = playlistSongs.value[newIndex];
  75. socket.dispatch(
  76. "playlists.repositionSong",
  77. playlist.value._id,
  78. {
  79. ...song,
  80. oldIndex,
  81. newIndex
  82. },
  83. res => {
  84. if (res.status !== "success")
  85. repositionedSong({
  86. ...song,
  87. newIndex: oldIndex,
  88. oldIndex: newIndex
  89. });
  90. }
  91. );
  92. };
  93. const moveSongToTop = index => {
  94. songItems.value[`song-item-${index}`].$refs.songActions.tippy.hide();
  95. playlistSongs.value.splice(0, 0, playlistSongs.value.splice(index, 1)[0]);
  96. repositionSong({
  97. moved: {
  98. oldIndex: index,
  99. newIndex: 0
  100. }
  101. });
  102. };
  103. const moveSongToBottom = index => {
  104. songItems.value[`song-item-${index}`].$refs.songActions.tippy.hide();
  105. playlistSongs.value.splice(
  106. playlistSongs.value.length - 1,
  107. 0,
  108. playlistSongs.value.splice(index, 1)[0]
  109. );
  110. repositionSong({
  111. moved: {
  112. oldIndex: index,
  113. newIndex: playlistSongs.value.length - 1
  114. }
  115. });
  116. };
  117. const totalLength = () => {
  118. let length = 0;
  119. playlist.value.songs.forEach(song => {
  120. length += song.duration;
  121. });
  122. return utils.formatTimeLong(length);
  123. };
  124. // const shuffle = () => {
  125. // socket.dispatch("playlists.shuffle", playlist.value._id, res => {
  126. // new Toast(res.message);
  127. // if (res.status === "success") {
  128. // updatePlaylistSongs(
  129. // res.data.playlist.songs.sort((a, b) => a.position - b.position)
  130. // );
  131. // }
  132. // });
  133. // };
  134. const removeSongFromPlaylist = id =>
  135. socket.dispatch(
  136. "playlists.removeSongFromPlaylist",
  137. id,
  138. playlist.value._id,
  139. res => {
  140. new Toast(res.message);
  141. }
  142. );
  143. const removePlaylist = () => {
  144. if (isOwner()) {
  145. socket.dispatch("playlists.remove", playlist.value._id, res => {
  146. new Toast(res.message);
  147. if (res.status === "success") closeCurrentModal();
  148. });
  149. } else if (hasPermission("playlists.removeAdmin")) {
  150. socket.dispatch("playlists.removeAdmin", playlist.value._id, res => {
  151. new Toast(res.message);
  152. if (res.status === "success") closeCurrentModal();
  153. });
  154. }
  155. };
  156. const downloadPlaylist = async () => {
  157. if (apiDomain.value === "")
  158. apiDomain.value = await lofig.get("backend.apiDomain");
  159. fetch(`${apiDomain.value}/export/playlist/${playlist.value._id}`, {
  160. credentials: "include"
  161. })
  162. .then(res => res.blob())
  163. .then(blob => {
  164. const url = window.URL.createObjectURL(blob);
  165. const a = document.createElement("a");
  166. a.style.display = "none";
  167. a.href = url;
  168. a.download = `musare-playlist-${
  169. playlist.value._id
  170. }-${new Date().toISOString()}.json`;
  171. document.body.appendChild(a);
  172. a.click();
  173. window.URL.revokeObjectURL(url);
  174. new Toast("Successfully downloaded playlist.");
  175. })
  176. .catch(() => new Toast("Failed to export and download playlist."));
  177. };
  178. const addSongToQueue = youtubeId => {
  179. socket.dispatch(
  180. "stations.addToQueue",
  181. station.value._id,
  182. youtubeId,
  183. data => {
  184. if (data.status !== "success")
  185. new Toast({
  186. content: `Error: ${data.message}`,
  187. timeout: 8000
  188. });
  189. else new Toast({ content: data.message, timeout: 4000 });
  190. }
  191. );
  192. };
  193. const clearAndRefillStationPlaylist = () => {
  194. socket.dispatch(
  195. "playlists.clearAndRefillStationPlaylist",
  196. playlist.value._id,
  197. data => {
  198. if (data.status !== "success")
  199. new Toast({
  200. content: `Error: ${data.message}`,
  201. timeout: 8000
  202. });
  203. else new Toast({ content: data.message, timeout: 4000 });
  204. }
  205. );
  206. };
  207. const clearAndRefillGenrePlaylist = () => {
  208. socket.dispatch(
  209. "playlists.clearAndRefillGenrePlaylist",
  210. playlist.value._id,
  211. data => {
  212. if (data.status !== "success")
  213. new Toast({
  214. content: `Error: ${data.message}`,
  215. timeout: 8000
  216. });
  217. else new Toast({ content: data.message, timeout: 4000 });
  218. }
  219. );
  220. };
  221. onMounted(() => {
  222. socket.onConnect(() => {
  223. gettingSongs.value = true;
  224. socket.dispatch("playlists.getPlaylist", props.playlistId, res => {
  225. if (res.status === "success") {
  226. setPlaylist(res.data.playlist);
  227. } else new Toast(res.message);
  228. gettingSongs.value = false;
  229. });
  230. });
  231. socket.on(
  232. "event:playlist.song.added",
  233. res => {
  234. if (playlist.value._id === res.data.playlistId)
  235. addSong(res.data.song);
  236. },
  237. { modalUuid: props.modalUuid }
  238. );
  239. socket.on(
  240. "event:playlist.song.removed",
  241. res => {
  242. if (playlist.value._id === res.data.playlistId) {
  243. // remove song from array of playlists
  244. removeSong(res.data.youtubeId);
  245. }
  246. },
  247. { modalUuid: props.modalUuid }
  248. );
  249. socket.on(
  250. "event:playlist.displayName.updated",
  251. res => {
  252. if (playlist.value._id === res.data.playlistId) {
  253. setPlaylist({
  254. displayName: res.data.displayName,
  255. ...playlist.value
  256. });
  257. }
  258. },
  259. { modalUuid: props.modalUuid }
  260. );
  261. socket.on(
  262. "event:playlist.song.repositioned",
  263. res => {
  264. if (playlist.value._id === res.data.playlistId) {
  265. const { song, playlistId } = res.data;
  266. if (playlist.value._id === playlistId) {
  267. repositionedSong(song);
  268. }
  269. }
  270. },
  271. { modalUuid: props.modalUuid }
  272. );
  273. });
  274. onBeforeUnmount(() => {
  275. clearPlaylist();
  276. // Delete the Pinia store that was created for this modal, after all other cleanup tasks are performed
  277. editPlaylistStore.$dispose();
  278. });
  279. </script>
  280. <template>
  281. <modal
  282. :title="
  283. isEditable('playlists.update.privacy')
  284. ? `Edit ${playlist.type === 'admin' ? 'Admin ' : ''}Playlist`
  285. : `View ${playlist.type === 'admin' ? 'Admin ' : ''}Playlist`
  286. "
  287. :class="{
  288. 'edit-playlist-modal': true,
  289. 'view-only': !isEditable('playlists.update.privacy')
  290. }"
  291. :size="isEditable('playlists.update.privacy') ? 'wide' : null"
  292. :split="true"
  293. >
  294. <template #body>
  295. <div class="left-section">
  296. <div id="playlist-info-section" class="section">
  297. <h3>{{ playlist.displayName }}</h3>
  298. <h5>Song Count: {{ playlist.songs.length }}</h5>
  299. <h5>Duration: {{ totalLength() }}</h5>
  300. </div>
  301. <div class="tabs-container">
  302. <div class="tab-selection">
  303. <button
  304. class="button is-default"
  305. :class="{ selected: tab === 'settings' }"
  306. :ref="el => (tabs['settings-tab'] = el)"
  307. @click="showTab('settings')"
  308. v-if="isEditable('playlists.update.privacy')"
  309. >
  310. Settings
  311. </button>
  312. <button
  313. class="button is-default"
  314. :class="{ selected: tab === 'add-songs' }"
  315. :ref="el => (tabs['add-songs-tab'] = el)"
  316. @click="showTab('add-songs')"
  317. v-if="isEditable('playlists.songs.add')"
  318. >
  319. Add Songs
  320. </button>
  321. <button
  322. class="button is-default"
  323. :class="{
  324. selected: tab === 'import-playlists'
  325. }"
  326. :ref="el => (tabs['import-playlists-tab'] = el)"
  327. @click="showTab('import-playlists')"
  328. v-if="isEditable('playlists.songs.add')"
  329. >
  330. Import Playlists
  331. </button>
  332. </div>
  333. <settings
  334. class="tab"
  335. v-show="tab === 'settings'"
  336. v-if="isEditable('playlists.update.privacy')"
  337. :modal-uuid="modalUuid"
  338. />
  339. <add-songs
  340. class="tab"
  341. v-show="tab === 'add-songs'"
  342. v-if="isEditable('playlists.songs.add')"
  343. :modal-uuid="modalUuid"
  344. />
  345. <import-playlists
  346. class="tab"
  347. v-show="tab === 'import-playlists'"
  348. v-if="isEditable('playlists.songs.add')"
  349. :modal-uuid="modalUuid"
  350. />
  351. </div>
  352. </div>
  353. <div class="right-section">
  354. <div id="rearrange-songs-section" class="section">
  355. <div v-if="isEditable('playlists.songs.reposition')">
  356. <h4 class="section-title">Rearrange Songs</h4>
  357. <p class="section-description">
  358. Drag and drop songs to change their order
  359. </p>
  360. <hr class="section-horizontal-rule" />
  361. </div>
  362. <aside class="menu">
  363. <draggable-list
  364. v-if="playlistSongs.length > 0"
  365. v-model:list="playlistSongs"
  366. item-key="_id"
  367. @start="drag = true"
  368. @end="drag = false"
  369. @update="repositionSong"
  370. :disabled="
  371. !isEditable('playlists.songs.reposition')
  372. "
  373. >
  374. <template #item="{ element, index }">
  375. <song-item
  376. :song="element"
  377. :ref="
  378. el =>
  379. (songItems[`song-item-${index}`] =
  380. el)
  381. "
  382. >
  383. <template #tippyActions>
  384. <i
  385. class="material-icons add-to-queue-icon"
  386. v-if="
  387. station &&
  388. station.requests &&
  389. station.requests.enabled &&
  390. (station.requests.access ===
  391. 'user' ||
  392. (station.requests.access ===
  393. 'owner' &&
  394. (userRole === 'admin' ||
  395. station.owner ===
  396. userId)))
  397. "
  398. @click="
  399. addSongToQueue(
  400. element.youtubeId
  401. )
  402. "
  403. content="Add Song to Queue"
  404. v-tippy
  405. >queue</i
  406. >
  407. <quick-confirm
  408. v-if="
  409. userId === playlist.createdBy ||
  410. isEditable(
  411. 'playlists.songs.reposition'
  412. )
  413. "
  414. placement="left"
  415. @confirm="
  416. removeSongFromPlaylist(
  417. element.youtubeId
  418. )
  419. "
  420. >
  421. <i
  422. class="material-icons delete-icon"
  423. content="Remove Song from Playlist"
  424. v-tippy
  425. >delete_forever</i
  426. >
  427. </quick-confirm>
  428. <i
  429. class="material-icons"
  430. v-if="
  431. isEditable(
  432. 'playlists.songs.reposition'
  433. ) && index > 0
  434. "
  435. @click="moveSongToTop(index)"
  436. content="Move to top of Playlist"
  437. v-tippy
  438. >vertical_align_top</i
  439. >
  440. <i
  441. v-if="
  442. isEditable(
  443. 'playlists.songs.reposition'
  444. ) &&
  445. playlistSongs.length - 1 !==
  446. index
  447. "
  448. @click="moveSongToBottom(index)"
  449. class="material-icons"
  450. content="Move to bottom of Playlist"
  451. v-tippy
  452. >vertical_align_bottom</i
  453. >
  454. </template>
  455. </song-item>
  456. </template>
  457. </draggable-list>
  458. <p v-else-if="gettingSongs" class="nothing-here-text">
  459. Loading songs...
  460. </p>
  461. <p v-else class="nothing-here-text">
  462. This playlist doesn't have any songs.
  463. </p>
  464. </aside>
  465. </div>
  466. </div>
  467. </template>
  468. <template #footer>
  469. <button
  470. class="button is-default"
  471. v-if="
  472. isOwner() ||
  473. hasPermission('playlists.get') ||
  474. playlist.privacy === 'public'
  475. "
  476. @click="downloadPlaylist()"
  477. >
  478. Download Playlist
  479. </button>
  480. <div class="right">
  481. <quick-confirm
  482. v-if="
  483. hasPermission('playlists.clearAndRefill') &&
  484. playlist.type === 'station'
  485. "
  486. @confirm="clearAndRefillStationPlaylist()"
  487. >
  488. <a class="button is-danger">
  489. Clear and refill station playlist
  490. </a>
  491. </quick-confirm>
  492. <quick-confirm
  493. v-if="
  494. hasPermission('playlists.clearAndRefill') &&
  495. playlist.type === 'genre'
  496. "
  497. @confirm="clearAndRefillGenrePlaylist()"
  498. >
  499. <a class="button is-danger">
  500. Clear and refill genre playlist
  501. </a>
  502. </quick-confirm>
  503. <quick-confirm
  504. v-if="
  505. isEditable('playlists.removeAdmin') &&
  506. !(
  507. playlist.type === 'user-liked' ||
  508. playlist.type === 'user-disliked'
  509. )
  510. "
  511. @confirm="removePlaylist()"
  512. >
  513. <a class="button is-danger"> Remove Playlist </a>
  514. </quick-confirm>
  515. </div>
  516. </template>
  517. </modal>
  518. </template>
  519. <style lang="less" scoped>
  520. .night-mode {
  521. .label,
  522. p,
  523. strong {
  524. color: var(--light-grey-2);
  525. }
  526. .edit-playlist-modal.modal .modal-card-body {
  527. .left-section {
  528. #playlist-info-section {
  529. background-color: var(--dark-grey-3) !important;
  530. border: 0;
  531. }
  532. .tabs-container {
  533. background-color: transparent !important;
  534. .tab-selection .button {
  535. background: var(--dark-grey);
  536. color: var(--white);
  537. }
  538. .tab {
  539. background-color: var(--dark-grey-3) !important;
  540. border: 0 !important;
  541. }
  542. }
  543. }
  544. .right-section .section {
  545. border-radius: @border-radius;
  546. }
  547. }
  548. }
  549. .controls {
  550. display: flex;
  551. a {
  552. display: flex;
  553. align-items: center;
  554. }
  555. }
  556. .tabs-container {
  557. .tab-selection {
  558. display: flex;
  559. margin: 24px 10px 0 10px;
  560. max-width: 100%;
  561. .button {
  562. border-radius: @border-radius @border-radius 0 0;
  563. border: 0;
  564. text-transform: uppercase;
  565. font-size: 14px;
  566. color: var(--dark-grey-3);
  567. background-color: var(--light-grey-2);
  568. flex-grow: 1;
  569. height: 32px;
  570. &:not(:first-of-type) {
  571. margin-left: 5px;
  572. }
  573. }
  574. .selected {
  575. background-color: var(--primary-color) !important;
  576. color: var(--white) !important;
  577. font-weight: 600;
  578. }
  579. }
  580. .tab {
  581. border: 1px solid var(--light-grey-3);
  582. border-radius: 0 0 @border-radius @border-radius;
  583. }
  584. }
  585. .edit-playlist-modal {
  586. &.view-only {
  587. height: auto !important;
  588. .left-section {
  589. flex-basis: 100% !important;
  590. }
  591. .right-section {
  592. max-height: unset !important;
  593. }
  594. :deep(.section) {
  595. max-width: 100% !important;
  596. }
  597. }
  598. .nothing-here-text {
  599. display: flex;
  600. align-items: center;
  601. justify-content: center;
  602. }
  603. .label {
  604. font-size: 1rem;
  605. font-weight: normal;
  606. }
  607. .input-with-button .button {
  608. width: 150px;
  609. }
  610. .left-section {
  611. #playlist-info-section {
  612. border: 1px solid var(--light-grey-3);
  613. border-radius: @border-radius;
  614. padding: 15px !important;
  615. h3 {
  616. font-weight: 600;
  617. font-size: 30px;
  618. }
  619. h5 {
  620. font-size: 18px;
  621. }
  622. h3,
  623. h5 {
  624. margin: 0;
  625. }
  626. }
  627. }
  628. }
  629. </style>