AddSongs.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <script setup lang="ts">
  2. import { defineAsyncComponent, ref, watch, onMounted } from "vue";
  3. import { storeToRefs } from "pinia";
  4. import { useSearchYoutube } from "@/composables/useSearchYoutube";
  5. import { useSearchMusare } from "@/composables/useSearchMusare";
  6. import { useYoutubeDirect } from "@/composables/useYoutubeDirect";
  7. import { useEditPlaylistStore } from "@/stores/editPlaylist";
  8. const SongItem = defineAsyncComponent(
  9. () => import("@/components/SongItem.vue")
  10. );
  11. const SearchQueryItem = defineAsyncComponent(
  12. () => import("@/components/SearchQueryItem.vue")
  13. );
  14. const props = defineProps({
  15. modalUuid: { type: String, required: true }
  16. });
  17. const editPlaylistStore = useEditPlaylistStore({ modalUuid: props.modalUuid });
  18. const { playlist } = storeToRefs(editPlaylistStore);
  19. const sitename = ref("Musare");
  20. const experimentalDisableYoutubeSearch = ref(false);
  21. const {
  22. youtubeSearch,
  23. searchForSongs,
  24. loadMoreSongs,
  25. addYouTubeSongToPlaylist
  26. } = useSearchYoutube();
  27. const {
  28. musareSearch,
  29. resultsLeftCount,
  30. nextPageResultsCount,
  31. searchForMusareSongs,
  32. addMusareSongToPlaylist
  33. } = useSearchMusare();
  34. const { youtubeDirect, addToPlaylist } = useYoutubeDirect();
  35. watch(
  36. () => youtubeSearch.value.songs.results,
  37. songs => {
  38. songs.forEach((searchItem, index) =>
  39. playlist.value.songs.find(song => {
  40. if (song.youtubeId === searchItem.id)
  41. youtubeSearch.value.songs.results[index].isAddedToQueue =
  42. true;
  43. return song.youtubeId === searchItem.id;
  44. })
  45. );
  46. }
  47. );
  48. watch(
  49. () => musareSearch.value.results,
  50. songs => {
  51. songs.forEach((searchItem, index) =>
  52. playlist.value.songs.find(song => {
  53. if (song._id === searchItem._id)
  54. musareSearch.value.results[index].isAddedToQueue = true;
  55. return song._id === searchItem._id;
  56. })
  57. );
  58. }
  59. );
  60. watch(
  61. () => playlist.value.songs,
  62. () => {
  63. youtubeSearch.value.songs.results.forEach((searchItem, index) =>
  64. playlist.value.songs.find(song => {
  65. youtubeSearch.value.songs.results[index].isAddedToQueue = false;
  66. if (song.youtubeId === searchItem.id)
  67. youtubeSearch.value.songs.results[index].isAddedToQueue =
  68. true;
  69. return song.youtubeId === searchItem.id;
  70. })
  71. );
  72. musareSearch.value.results.forEach((searchItem, index) =>
  73. playlist.value.songs.find(song => {
  74. musareSearch.value.results[index].isAddedToQueue = false;
  75. if (song.youtubeId === searchItem.youtubeId)
  76. musareSearch.value.results[index].isAddedToQueue = true;
  77. return song.youtubeId === searchItem.youtubeId;
  78. })
  79. );
  80. }
  81. );
  82. onMounted(async () => {
  83. sitename.value = await lofig.get("siteSettings.sitename");
  84. lofig.get("experimental").then(experimental => {
  85. if (
  86. experimental &&
  87. Object.hasOwn(experimental, "disable_youtube_search") &&
  88. experimental.disable_youtube_search
  89. ) {
  90. experimentalDisableYoutubeSearch.value = true;
  91. }
  92. });
  93. });
  94. </script>
  95. <template>
  96. <div class="youtube-tab section">
  97. <div>
  98. <label class="label"> Search for a song on {{ sitename }}</label>
  99. <div class="control is-grouped input-with-button">
  100. <p class="control is-expanded">
  101. <input
  102. class="input"
  103. type="text"
  104. placeholder="Enter your song query here..."
  105. v-model="musareSearch.query"
  106. @keyup.enter="searchForMusareSongs(1)"
  107. />
  108. </p>
  109. <p class="control">
  110. <a class="button is-info" @click="searchForMusareSongs(1)"
  111. ><i class="material-icons icon-with-button">search</i
  112. >Search</a
  113. >
  114. </p>
  115. </div>
  116. <div
  117. v-if="musareSearch.results.length > 0"
  118. class="song-query-results"
  119. >
  120. <song-item
  121. v-for="(song, index) in musareSearch.results"
  122. :key="song._id"
  123. :song="song"
  124. >
  125. <template #actions>
  126. <transition
  127. name="musare-search-query-actions"
  128. mode="out-in"
  129. >
  130. <i
  131. v-if="song.isAddedToQueue"
  132. class="material-icons added-to-playlist-icon"
  133. content="Song is already in playlist"
  134. v-tippy
  135. >done</i
  136. >
  137. <i
  138. v-else
  139. class="material-icons add-to-playlist-icon"
  140. content="Add Song to Playlist"
  141. v-tippy
  142. @click="
  143. addMusareSongToPlaylist(
  144. playlist._id,
  145. song.youtubeId,
  146. index
  147. )
  148. "
  149. >playlist_add</i
  150. >
  151. </transition>
  152. </template>
  153. </song-item>
  154. <button
  155. v-if="resultsLeftCount > 0"
  156. class="button is-primary load-more-button"
  157. @click="searchForMusareSongs(musareSearch.page + 1)"
  158. >
  159. Load {{ nextPageResultsCount }} more results
  160. </button>
  161. </div>
  162. </div>
  163. <br v-if="musareSearch.results.length > 0" />
  164. <label class="label"> Add a YouTube song from a URL </label>
  165. <div class="control is-grouped input-with-button">
  166. <p class="control is-expanded">
  167. <input
  168. class="input"
  169. type="text"
  170. placeholder="Enter your YouTube song URL here..."
  171. v-model="youtubeDirect"
  172. @keyup.enter="addToPlaylist(playlist._id)"
  173. />
  174. </p>
  175. <p class="control">
  176. <a class="button is-info" @click="addToPlaylist(playlist._id)"
  177. ><i class="material-icons icon-with-button">add</i>Add</a
  178. >
  179. </p>
  180. </div>
  181. <div v-if="!experimentalDisableYoutubeSearch">
  182. <label class="label"> Search for a song from YouTube </label>
  183. <div class="control is-grouped input-with-button">
  184. <p class="control is-expanded">
  185. <input
  186. class="input"
  187. type="text"
  188. placeholder="Enter your YouTube query here..."
  189. v-model="youtubeSearch.songs.query"
  190. autofocus
  191. @keyup.enter="searchForSongs()"
  192. />
  193. </p>
  194. <p class="control">
  195. <button
  196. class="button is-info"
  197. @click.prevent="searchForSongs()"
  198. >
  199. <i class="material-icons icon-with-button">search</i
  200. >Search
  201. </button>
  202. </p>
  203. </div>
  204. <div
  205. v-if="youtubeSearch.songs.results.length > 0"
  206. class="song-query-results"
  207. >
  208. <search-query-item
  209. v-for="(result, index) in youtubeSearch.songs.results"
  210. :key="result.id"
  211. :result="result"
  212. >
  213. <template #actions>
  214. <transition
  215. name="youtube-search-query-actions"
  216. mode="out-in"
  217. >
  218. <i
  219. v-if="result.isAddedToQueue"
  220. class="material-icons added-to-playlist-icon"
  221. content="Song is already in playlist"
  222. v-tippy
  223. >done</i
  224. >
  225. <i
  226. v-else
  227. class="material-icons add-to-playlist-icon"
  228. content="Add Song to Playlist"
  229. v-tippy
  230. @click="
  231. addYouTubeSongToPlaylist(
  232. playlist._id,
  233. result.id,
  234. index
  235. )
  236. "
  237. >playlist_add</i
  238. >
  239. </transition>
  240. </template>
  241. </search-query-item>
  242. <button
  243. class="button is-primary load-more-button"
  244. @click.prevent="loadMoreSongs()"
  245. >
  246. Load more...
  247. </button>
  248. </div>
  249. </div>
  250. </div>
  251. </template>
  252. <style lang="less" scoped>
  253. .youtube-tab {
  254. .song-query-results {
  255. margin-top: 10px;
  256. max-width: 565px;
  257. .search-query-item:not(:last-of-type) {
  258. margin-bottom: 10px;
  259. }
  260. }
  261. .load-more-button {
  262. width: 100%;
  263. margin-top: 10px;
  264. }
  265. }
  266. @media screen and (max-width: 1300px) {
  267. .youtube-tab .song-query-results,
  268. .section {
  269. max-width: 100% !important;
  270. }
  271. }
  272. </style>