ReplaceSpotifySongs.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <script setup lang="ts">
  2. import { defineAsyncComponent, ref, onMounted, onBeforeUnmount } from "vue";
  3. import Toast from "toasters";
  4. import { DraggableList } from "vue-draggable-list";
  5. import { useWebsocketsStore } from "@/stores/websockets";
  6. import { useModalsStore } from "@/stores/modals";
  7. const Modal = defineAsyncComponent(() => import("@/components/Modal.vue"));
  8. const SongItem = defineAsyncComponent(
  9. () => import("@/components/SongItem.vue")
  10. );
  11. const props = defineProps({
  12. modalUuid: { type: String, required: true },
  13. spotifyAlbum: { type: Object, default: () => ({}) },
  14. spotifyTracks: { type: Array, default: () => [] },
  15. playlistId: { type: String },
  16. youtubePlaylistId: { type: String }
  17. });
  18. const { socket } = useWebsocketsStore();
  19. const { closeCurrentModal } = useModalsStore();
  20. const isImportingPlaylist = ref(false);
  21. const hasImportedPlaylist = ref(false);
  22. const trackSongs = ref([]);
  23. const playlistSongs = ref([]);
  24. const localSpotifyTracks = ref([]);
  25. const replacingAllSpotifySongs = ref(false);
  26. const replaceAllSpotifySongs = async () => {
  27. if (replacingAllSpotifySongs.value) return;
  28. replacingAllSpotifySongs.value = true;
  29. const replaceArray = [];
  30. localSpotifyTracks.value.forEach((spotifyTrack, index) => {
  31. const spotifyMediaSource = `spotify:${spotifyTrack.trackId}`;
  32. if (trackSongs.value[index].length === 1) {
  33. const replacementMediaSource =
  34. trackSongs.value[index][0].mediaSource;
  35. if (!spotifyMediaSource || !replacementMediaSource) return;
  36. replaceArray.push([spotifyMediaSource, replacementMediaSource]);
  37. }
  38. });
  39. if (replaceArray.length === 0) {
  40. new Toast(
  41. "No songs can be replaced, please make sure each track has one song dragged into the box"
  42. );
  43. return;
  44. }
  45. const promises = replaceArray.map(
  46. ([spotifyMediaSource, replacementMediaSource]) =>
  47. new Promise<void>(resolve => {
  48. socket.dispatch(
  49. "playlists.replaceSongInPlaylist",
  50. spotifyMediaSource,
  51. replacementMediaSource,
  52. props.playlistId,
  53. res => {
  54. console.log(
  55. "playlists.replaceSongInPlaylist response",
  56. res
  57. );
  58. if (res.status === "success") {
  59. const spotifyTrackId =
  60. spotifyMediaSource.split(":")[1];
  61. const trackIndex =
  62. localSpotifyTracks.value.findIndex(
  63. spotifyTrack =>
  64. spotifyTrack.trackId === spotifyTrackId
  65. );
  66. localSpotifyTracks.value.splice(trackIndex, 1);
  67. trackSongs.value.splice(trackIndex, 1);
  68. }
  69. resolve();
  70. }
  71. );
  72. })
  73. );
  74. Promise.allSettled(promises).finally(() => {
  75. replacingAllSpotifySongs.value = false;
  76. if (localSpotifyTracks.value.length === 0) closeCurrentModal();
  77. });
  78. };
  79. const replaceSpotifySong = (oldMediaSource, newMediaSource) => {
  80. socket.dispatch(
  81. "playlists.replaceSongInPlaylist",
  82. oldMediaSource,
  83. newMediaSource,
  84. props.playlistId,
  85. res => {
  86. console.log("playlists.replaceSongInPlaylist response", res);
  87. if (res.status === "success") {
  88. const spotifyTrackId = oldMediaSource.split(":")[1];
  89. const trackIndex = localSpotifyTracks.value.findIndex(
  90. spotifyTrack => spotifyTrack.trackId === spotifyTrackId
  91. );
  92. localSpotifyTracks.value.splice(trackIndex, 1);
  93. trackSongs.value.splice(trackIndex, 1);
  94. if (localSpotifyTracks.value.length === 0) closeCurrentModal();
  95. }
  96. }
  97. );
  98. };
  99. const tryToAutoMove = () => {
  100. const songs = playlistSongs.value;
  101. localSpotifyTracks.value.forEach((spotifyTrack, index) => {
  102. songs.forEach(playlistSong => {
  103. if (
  104. playlistSong.title
  105. .toLowerCase()
  106. .trim()
  107. .indexOf(spotifyTrack.name.toLowerCase().trim()) !== -1
  108. ) {
  109. songs.splice(songs.indexOf(playlistSong), 1);
  110. trackSongs.value[index].push(playlistSong);
  111. }
  112. });
  113. });
  114. };
  115. const importPlaylist = () => {
  116. if (hasImportedPlaylist.value)
  117. return new Toast("A playlist has already imported.");
  118. if (isImportingPlaylist.value)
  119. return new Toast("A playlist is already importing.");
  120. isImportingPlaylist.value = true;
  121. // don't give starting import message instantly in case of instant error
  122. setTimeout(() => {
  123. if (isImportingPlaylist.value) {
  124. new Toast(
  125. "Starting to import your playlist. This can take some time to do."
  126. );
  127. }
  128. }, 750);
  129. return socket.dispatch(
  130. "youtube.requestSet",
  131. `https://youtube.com/playlist?list=${props.youtubePlaylistId}`,
  132. false,
  133. true,
  134. res => {
  135. const mediaSources = res.videos.map(
  136. video => `youtube:${video.youtubeId}`
  137. );
  138. socket.dispatch(
  139. "songs.getSongsFromMediaSources",
  140. mediaSources,
  141. res => {
  142. if (res.status === "success") {
  143. const { songs } = res.data;
  144. playlistSongs.value = songs;
  145. songs.forEach(() => {
  146. trackSongs.value.push([]);
  147. });
  148. hasImportedPlaylist.value = true;
  149. isImportingPlaylist.value = false;
  150. tryToAutoMove();
  151. return;
  152. }
  153. new Toast("Could not get songs.");
  154. }
  155. );
  156. return new Toast({ content: res.message, timeout: 20000 });
  157. }
  158. );
  159. };
  160. onMounted(() => {
  161. localSpotifyTracks.value = props.spotifyTracks;
  162. importPlaylist();
  163. });
  164. onBeforeUnmount(() => {});
  165. </script>
  166. <template>
  167. <div>
  168. <modal
  169. title="Replace Spotify Songs"
  170. class="replace-spotify-songs-modal"
  171. size="wide"
  172. >
  173. <template #body>
  174. <div class="playlist-songs">
  175. <h4>YouTube songs</h4>
  176. <p v-if="isImportingPlaylist">Importing playlist...</p>
  177. <draggable-list
  178. v-if="playlistSongs.length > 0"
  179. v-model:list="playlistSongs"
  180. item-key="mediaSource"
  181. :group="`replace-spotify-album-${modalUuid}-songs`"
  182. >
  183. <template #item="{ element }">
  184. <song-item
  185. :key="`playlist-song-${element.mediaSource}`"
  186. :song="element"
  187. >
  188. </song-item>
  189. </template>
  190. </draggable-list>
  191. </div>
  192. <div class="track-boxes">
  193. <div
  194. class="track-box"
  195. v-for="(track, index) in localSpotifyTracks"
  196. :key="track.trackId"
  197. >
  198. <div class="track-position-title">
  199. <p>
  200. {{ track.name }} -
  201. {{ track.artists.join(", ") }}
  202. </p>
  203. </div>
  204. <!-- :data-track-index="index" -->
  205. <div class="track-box-songs-drag-area">
  206. <draggable-list
  207. v-model:list="trackSongs[index]"
  208. item-key="mediaSource"
  209. :group="`replace-spotify-album-${modalUuid}-songs`"
  210. >
  211. <template #item="{ element }">
  212. <song-item
  213. :key="`track-song-${element.mediaSource}`"
  214. :song="element"
  215. >
  216. </song-item>
  217. <button
  218. class="button is-primary is-fullwidth"
  219. @click="
  220. replaceSpotifySong(
  221. `spotify:${track.trackId}`,
  222. element.mediaSource
  223. )
  224. "
  225. >
  226. Replace Spotify song with this song
  227. </button>
  228. </template>
  229. </draggable-list>
  230. </div>
  231. </div>
  232. </div>
  233. </template>
  234. <template #footer>
  235. <button class="button is-primary" @click="tryToAutoMove()">
  236. Try to auto move
  237. </button>
  238. <button
  239. class="button is-primary"
  240. @click="replaceAllSpotifySongs()"
  241. >
  242. Replace all songs
  243. </button>
  244. </template>
  245. </modal>
  246. </div>
  247. </template>
  248. <style lang="less">
  249. .night-mode {
  250. .spotify-album-container,
  251. .playlist-songs,
  252. .track-boxes {
  253. background-color: var(--dark-grey-3) !important;
  254. border: 0 !important;
  255. .tab {
  256. border: 0 !important;
  257. }
  258. }
  259. .api-result {
  260. background-color: var(--dark-grey-3) !important;
  261. }
  262. .api-result .tracks .track:hover,
  263. .api-result .tracks .track:focus,
  264. .discogs-album .tracks .track:hover,
  265. .discogs-album .tracks .track:focus {
  266. background-color: var(--dark-grey-2) !important;
  267. }
  268. .api-result .bottom-row img,
  269. .discogs-album .bottom-row img {
  270. filter: invert(100%);
  271. }
  272. .label,
  273. p,
  274. strong {
  275. color: var(--light-grey-2);
  276. }
  277. }
  278. .replace-spotify-songs-modal {
  279. .modal-card-title {
  280. text-align: center;
  281. margin-left: 24px;
  282. }
  283. .modal-card {
  284. width: 100%;
  285. height: 100%;
  286. .modal-card-body {
  287. padding: 16px;
  288. display: flex;
  289. flex-direction: row;
  290. flex-wrap: wrap;
  291. justify-content: space-evenly;
  292. }
  293. .modal-card-foot {
  294. .button {
  295. margin: 0;
  296. &:not(:first-of-type) {
  297. margin-left: 5px;
  298. }
  299. }
  300. div div {
  301. margin-right: 5px;
  302. }
  303. .right {
  304. display: flex;
  305. margin-left: auto;
  306. margin-right: 0;
  307. }
  308. }
  309. }
  310. }
  311. </style>
  312. <style lang="less" scoped>
  313. .break {
  314. flex-basis: 100%;
  315. height: 0;
  316. border: 1px solid var(--dark-grey);
  317. margin-top: 16px;
  318. margin-bottom: 16px;
  319. }
  320. .spotify-album-container,
  321. .playlist-songs {
  322. width: 500px;
  323. background-color: var(--light-grey);
  324. border: 1px rgba(163, 224, 255, 0.75) solid;
  325. border-radius: @border-radius;
  326. padding: 16px;
  327. overflow: auto;
  328. height: 100%;
  329. h4 {
  330. margin: 0;
  331. margin-bottom: 16px;
  332. }
  333. button {
  334. margin: 5px 0;
  335. }
  336. }
  337. .track-boxes {
  338. width: 500px;
  339. background-color: var(--light-grey);
  340. border: 1px rgba(163, 224, 255, 0.75) solid;
  341. border-radius: @border-radius;
  342. padding: 16px;
  343. overflow: auto;
  344. height: 100%;
  345. .track-box:first-child {
  346. margin-top: 0;
  347. border-radius: @border-radius @border-radius 0 0;
  348. }
  349. .track-box:last-child {
  350. border-radius: 0 0 @border-radius @border-radius;
  351. }
  352. .track-box {
  353. border: 0.5px solid var(--black);
  354. margin-top: -1px;
  355. line-height: 16px;
  356. display: flex;
  357. flex-flow: column;
  358. .track-position-title {
  359. display: flex;
  360. span {
  361. font-weight: 600;
  362. display: inline-block;
  363. margin-top: 7px;
  364. margin-bottom: 7px;
  365. margin-left: 7px;
  366. }
  367. p {
  368. display: inline-block;
  369. margin: 7px;
  370. flex: 1;
  371. }
  372. }
  373. .track-box-songs-drag-area {
  374. flex: 1;
  375. min-height: 100px;
  376. display: flex;
  377. flex-direction: column;
  378. }
  379. }
  380. }
  381. </style>