index.vue 16 KB

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