index.vue 16 KB

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