index.vue 16 KB

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