index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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="youtubeId"
  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. :key="`playlist-song-${element.youtubeId}`"
  383. >
  384. <template #tippyActions>
  385. <i
  386. class="material-icons add-to-queue-icon"
  387. v-if="
  388. station &&
  389. station.requests &&
  390. station.requests.enabled &&
  391. (station.requests.access ===
  392. 'user' ||
  393. (station.requests.access ===
  394. 'owner' &&
  395. (userRole === 'admin' ||
  396. station.owner ===
  397. userId)))
  398. "
  399. @click="
  400. addSongToQueue(
  401. element.youtubeId
  402. )
  403. "
  404. content="Add Song to Queue"
  405. v-tippy
  406. >queue</i
  407. >
  408. <quick-confirm
  409. v-if="
  410. userId === playlist.createdBy ||
  411. isEditable(
  412. 'playlists.songs.reposition'
  413. )
  414. "
  415. placement="left"
  416. @confirm="
  417. removeSongFromPlaylist(
  418. element.youtubeId
  419. )
  420. "
  421. >
  422. <i
  423. class="material-icons delete-icon"
  424. content="Remove Song from Playlist"
  425. v-tippy
  426. >delete_forever</i
  427. >
  428. </quick-confirm>
  429. <i
  430. class="material-icons"
  431. v-if="
  432. isEditable(
  433. 'playlists.songs.reposition'
  434. ) && index > 0
  435. "
  436. @click="moveSongToTop(index)"
  437. content="Move to top of Playlist"
  438. v-tippy
  439. >vertical_align_top</i
  440. >
  441. <i
  442. v-if="
  443. isEditable(
  444. 'playlists.songs.reposition'
  445. ) &&
  446. playlistSongs.length - 1 !==
  447. index
  448. "
  449. @click="moveSongToBottom(index)"
  450. class="material-icons"
  451. content="Move to bottom of Playlist"
  452. v-tippy
  453. >vertical_align_bottom</i
  454. >
  455. </template>
  456. </song-item>
  457. </template>
  458. </draggable-list>
  459. <p v-else-if="gettingSongs" class="nothing-here-text">
  460. Loading songs...
  461. </p>
  462. <p v-else class="nothing-here-text">
  463. This playlist doesn't have any songs.
  464. </p>
  465. </aside>
  466. </div>
  467. </div>
  468. </template>
  469. <template #footer>
  470. <button
  471. class="button is-default"
  472. v-if="
  473. isOwner() ||
  474. hasPermission('playlists.get') ||
  475. playlist.privacy === 'public'
  476. "
  477. @click="downloadPlaylist()"
  478. >
  479. Download Playlist
  480. </button>
  481. <div class="right">
  482. <quick-confirm
  483. v-if="
  484. hasPermission('playlists.clearAndRefill') &&
  485. playlist.type === 'station'
  486. "
  487. @confirm="clearAndRefillStationPlaylist()"
  488. >
  489. <a class="button is-danger">
  490. Clear and refill station playlist
  491. </a>
  492. </quick-confirm>
  493. <quick-confirm
  494. v-if="
  495. hasPermission('playlists.clearAndRefill') &&
  496. playlist.type === 'genre'
  497. "
  498. @confirm="clearAndRefillGenrePlaylist()"
  499. >
  500. <a class="button is-danger">
  501. Clear and refill genre playlist
  502. </a>
  503. </quick-confirm>
  504. <quick-confirm
  505. v-if="
  506. isEditable('playlists.removeAdmin') &&
  507. !(
  508. playlist.type === 'user-liked' ||
  509. playlist.type === 'user-disliked'
  510. )
  511. "
  512. @confirm="removePlaylist()"
  513. >
  514. <a class="button is-danger"> Remove Playlist </a>
  515. </quick-confirm>
  516. </div>
  517. </template>
  518. </modal>
  519. </template>
  520. <style lang="less" scoped>
  521. .night-mode {
  522. .label,
  523. p,
  524. strong {
  525. color: var(--light-grey-2);
  526. }
  527. .edit-playlist-modal.modal .modal-card-body {
  528. .left-section {
  529. #playlist-info-section {
  530. background-color: var(--dark-grey-3) !important;
  531. border: 0;
  532. }
  533. .tabs-container {
  534. background-color: transparent !important;
  535. .tab-selection .button {
  536. background: var(--dark-grey);
  537. color: var(--white);
  538. }
  539. .tab {
  540. background-color: var(--dark-grey-3) !important;
  541. border: 0 !important;
  542. }
  543. }
  544. }
  545. .right-section .section {
  546. border-radius: @border-radius;
  547. }
  548. }
  549. }
  550. .controls {
  551. display: flex;
  552. a {
  553. display: flex;
  554. align-items: center;
  555. }
  556. }
  557. .tabs-container {
  558. .tab-selection {
  559. display: flex;
  560. margin: 24px 10px 0 10px;
  561. max-width: 100%;
  562. .button {
  563. border-radius: @border-radius @border-radius 0 0;
  564. border: 0;
  565. text-transform: uppercase;
  566. font-size: 14px;
  567. color: var(--dark-grey-3);
  568. background-color: var(--light-grey-2);
  569. flex-grow: 1;
  570. height: 32px;
  571. &:not(:first-of-type) {
  572. margin-left: 5px;
  573. }
  574. }
  575. .selected {
  576. background-color: var(--primary-color) !important;
  577. color: var(--white) !important;
  578. font-weight: 600;
  579. }
  580. }
  581. .tab {
  582. border: 1px solid var(--light-grey-3);
  583. border-radius: 0 0 @border-radius @border-radius;
  584. }
  585. }
  586. .edit-playlist-modal {
  587. &.view-only {
  588. height: auto !important;
  589. .left-section {
  590. flex-basis: 100% !important;
  591. }
  592. .right-section {
  593. max-height: unset !important;
  594. }
  595. :deep(.section) {
  596. max-width: 100% !important;
  597. }
  598. }
  599. .nothing-here-text {
  600. display: flex;
  601. align-items: center;
  602. justify-content: center;
  603. }
  604. .label {
  605. font-size: 1rem;
  606. font-weight: normal;
  607. }
  608. .input-with-button .button {
  609. width: 150px;
  610. }
  611. .left-section {
  612. #playlist-info-section {
  613. border: 1px solid var(--light-grey-3);
  614. border-radius: @border-radius;
  615. padding: 15px !important;
  616. h3 {
  617. font-weight: 600;
  618. font-size: 30px;
  619. }
  620. h5 {
  621. font-size: 18px;
  622. }
  623. h3,
  624. h5 {
  625. margin: 0;
  626. }
  627. }
  628. }
  629. }
  630. </style>