Request.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <script setup lang="ts">
  2. import { defineAsyncComponent, ref, computed, onMounted } from "vue";
  3. import Toast from "toasters";
  4. import { useWebsocketsStore } from "@/stores/websockets";
  5. import { useStationStore } from "@/stores/station";
  6. import { useManageStationStore } from "@/stores/manageStation";
  7. import { useSearchYoutube } from "@/composables/useSearchYoutube";
  8. import { useSearchMusare } from "@/composables/useSearchMusare";
  9. import { useYoutubeDirect } from "@/composables/useYoutubeDirect";
  10. const SongItem = defineAsyncComponent(
  11. () => import("@/components/SongItem.vue")
  12. );
  13. const SearchQueryItem = defineAsyncComponent(
  14. () => import("@/components/SearchQueryItem.vue")
  15. );
  16. const PlaylistTabBase = defineAsyncComponent(
  17. () => import("@/components/PlaylistTabBase.vue")
  18. );
  19. const props = defineProps({
  20. modalUuid: { type: String, default: null },
  21. sector: { type: String, default: "station" },
  22. disableAutoRequest: { type: Boolean, default: false }
  23. });
  24. const { youtubeSearch, searchForSongs, loadMoreSongs } = useSearchYoutube();
  25. const { musareSearch, searchForMusareSongs } = useSearchMusare();
  26. const { youtubeDirect, addToQueue } = useYoutubeDirect();
  27. const { socket } = useWebsocketsStore();
  28. const stationStore = useStationStore();
  29. const manageStationStore = useManageStationStore({
  30. modalUuid: props.modalUuid
  31. });
  32. const tab = ref("songs");
  33. const sitename = ref("Musare");
  34. const tabs = ref({});
  35. const experimentalDisableYoutubeSearch = ref(false);
  36. const station = computed({
  37. get() {
  38. if (props.sector === "manageStation") return manageStationStore.station;
  39. return stationStore.station;
  40. },
  41. set(value) {
  42. if (props.sector === "manageStation")
  43. manageStationStore.updateStation(value);
  44. else stationStore.updateStation(value);
  45. }
  46. });
  47. // const blacklist = computed({
  48. // get() {
  49. // if (props.sector === "manageStation") return manageStationStore.blacklist;
  50. // return stationStore.blacklist;
  51. // },
  52. // set(value) {
  53. // if (props.sector === "manageStation")
  54. // manageStationStore.setBlacklist(value);
  55. // else stationStore.setBlacklist(value);
  56. // }
  57. // });
  58. const songsList = computed({
  59. get() {
  60. if (props.sector === "manageStation")
  61. return manageStationStore.songsList;
  62. return stationStore.songsList;
  63. },
  64. set(value) {
  65. if (props.sector === "manageStation")
  66. manageStationStore.updateSongsList(value);
  67. else stationStore.updateSongsList(value);
  68. }
  69. });
  70. const musareResultsLeftCount = computed(
  71. () => musareSearch.value.count - musareSearch.value.results.length
  72. );
  73. const nextPageMusareResultsCount = computed(() =>
  74. Math.min(musareSearch.value.pageSize, musareResultsLeftCount.value)
  75. );
  76. const songsInQueue = computed(() => {
  77. if (station.value.currentSong)
  78. return songsList.value
  79. .map(song => song.youtubeId)
  80. .concat(station.value.currentSong.youtubeId);
  81. return songsList.value.map(song => song.youtubeId);
  82. });
  83. // const currentUserQueueSongs = computed(
  84. // () =>
  85. // songsList.value.filter(
  86. // queueSong => queueSong.requestedBy === userId.value
  87. // ).length
  88. // );
  89. const autorequestPlaylistCount = computed(() => {
  90. if (props.sector === "station") return stationStore.autoRequest.length;
  91. return 0;
  92. });
  93. const showTab = _tab => {
  94. const tabElement = tabs.value[`${_tab}-tab`];
  95. if (tabElement) tabElement.scrollIntoView({ block: "nearest" });
  96. tab.value = _tab;
  97. };
  98. const addSongToQueue = (youtubeId: string, index?: number) => {
  99. socket.dispatch(
  100. "stations.addToQueue",
  101. station.value._id,
  102. youtubeId,
  103. "manual",
  104. res => {
  105. if (res.status !== "success") new Toast(`Error: ${res.message}`);
  106. else {
  107. if (index)
  108. youtubeSearch.value.songs.results[index].isAddedToQueue =
  109. true;
  110. new Toast(res.message);
  111. }
  112. }
  113. );
  114. };
  115. onMounted(async () => {
  116. sitename.value = await lofig.get("siteSettings.sitename");
  117. lofig.get("experimental").then(experimental => {
  118. if (
  119. experimental &&
  120. Object.hasOwn(experimental, "disable_youtube_search") &&
  121. experimental.disable_youtube_search
  122. ) {
  123. experimentalDisableYoutubeSearch.value = true;
  124. }
  125. });
  126. showTab("songs");
  127. });
  128. </script>
  129. <template>
  130. <div class="station-playlists">
  131. <p
  132. class="top-info has-text-centered"
  133. v-if="
  134. tab !== 'songs' ||
  135. (station.requests.allowAutorequest && !disableAutoRequest)
  136. "
  137. >
  138. Add songs to the queue or automatically request songs from playlists
  139. </p>
  140. <div class="tabs-container">
  141. <div
  142. class="tab-selection"
  143. v-if="
  144. tab !== 'songs' ||
  145. (station.requests.allowAutorequest && !disableAutoRequest)
  146. "
  147. >
  148. <button
  149. class="button is-default"
  150. :ref="el => (tabs['songs-tab'] = el)"
  151. :class="{ selected: tab === 'songs' }"
  152. @click="showTab('songs')"
  153. >
  154. Songs
  155. </button>
  156. <button
  157. v-if="
  158. station.requests.allowAutorequest && !disableAutoRequest
  159. "
  160. class="button is-default"
  161. :ref="el => (tabs['autorequest-tab'] = el)"
  162. :class="{ selected: tab === 'autorequest' }"
  163. @click="showTab('autorequest')"
  164. >
  165. Autorequest
  166. <span
  167. v-tippy
  168. content="You are autorequesting playlists"
  169. class="tag has-icon"
  170. v-if="autorequestPlaylistCount > 0"
  171. ><i class="material-icons">play_arrow</i></span
  172. >
  173. </button>
  174. <button
  175. v-else-if="station.requests.allowAutorequest"
  176. class="button is-default disabled"
  177. content="Only available on station pages"
  178. v-tippy
  179. >
  180. Autorequest
  181. </button>
  182. </div>
  183. <div class="tab" v-show="tab === 'songs'">
  184. <div class="musare-songs">
  185. <label class="label">
  186. Search for a song on {{ sitename }}
  187. </label>
  188. <div class="control is-grouped input-with-button">
  189. <p class="control is-expanded">
  190. <input
  191. class="input"
  192. type="text"
  193. placeholder="Enter your song query here..."
  194. v-model="musareSearch.query"
  195. @keyup.enter="searchForMusareSongs(1)"
  196. />
  197. </p>
  198. <p class="control">
  199. <a
  200. class="button is-info"
  201. @click="searchForMusareSongs(1)"
  202. ><i class="material-icons icon-with-button"
  203. >search</i
  204. >Search</a
  205. >
  206. </p>
  207. </div>
  208. <div v-if="musareSearch.results.length > 0">
  209. <song-item
  210. v-for="song in musareSearch.results"
  211. :key="song._id"
  212. :song="song"
  213. >
  214. <template #actions>
  215. <transition
  216. name="musare-search-query-actions"
  217. mode="out-in"
  218. >
  219. <i
  220. v-if="
  221. songsInQueue.indexOf(
  222. song.youtubeId
  223. ) !== -1
  224. "
  225. class="material-icons added-to-playlist-icon"
  226. content="Song is already in queue"
  227. v-tippy
  228. >done</i
  229. >
  230. <i
  231. v-else
  232. class="material-icons add-to-queue-icon"
  233. @click="addSongToQueue(song.youtubeId)"
  234. content="Add Song to Queue"
  235. v-tippy
  236. >queue</i
  237. >
  238. </transition>
  239. </template>
  240. </song-item>
  241. <button
  242. v-if="musareResultsLeftCount > 0"
  243. class="button is-primary load-more-button"
  244. @click="searchForMusareSongs(musareSearch.page + 1)"
  245. >
  246. Load {{ nextPageMusareResultsCount }} more results
  247. </button>
  248. </div>
  249. </div>
  250. <div class="youtube-direct">
  251. <label class="label"> Add a YouTube song from a URL </label>
  252. <div class="control is-grouped input-with-button">
  253. <p class="control is-expanded">
  254. <input
  255. class="input"
  256. type="text"
  257. placeholder="Enter your YouTube song URL here..."
  258. v-model="youtubeDirect"
  259. @keyup.enter="addToQueue(station._id)"
  260. />
  261. </p>
  262. <p class="control">
  263. <a
  264. class="button is-info"
  265. @click="addToQueue(station._id)"
  266. ><i class="material-icons icon-with-button"
  267. >add</i
  268. >Add</a
  269. >
  270. </p>
  271. </div>
  272. </div>
  273. <div
  274. class="youtube-search"
  275. v-if="!experimentalDisableYoutubeSearch"
  276. >
  277. <label class="label"> Search for a song on YouTube </label>
  278. <div class="control is-grouped input-with-button">
  279. <p class="control is-expanded">
  280. <input
  281. class="input"
  282. type="text"
  283. placeholder="Enter your YouTube query here..."
  284. v-model="youtubeSearch.songs.query"
  285. autofocus
  286. @keyup.enter="searchForSongs()"
  287. />
  288. </p>
  289. <p class="control">
  290. <a
  291. class="button is-info"
  292. @click.prevent="searchForSongs()"
  293. ><i class="material-icons icon-with-button"
  294. >search</i
  295. >Search</a
  296. >
  297. </p>
  298. </div>
  299. <div
  300. v-if="youtubeSearch.songs.results.length > 0"
  301. id="song-query-results"
  302. >
  303. <search-query-item
  304. v-for="(result, index) in youtubeSearch.songs
  305. .results"
  306. :key="result.id"
  307. :result="result"
  308. >
  309. <template #actions>
  310. <transition
  311. name="youtube-search-query-actions"
  312. mode="out-in"
  313. >
  314. <i
  315. v-if="
  316. songsInQueue.indexOf(result.id) !==
  317. -1
  318. "
  319. class="material-icons added-to-playlist-icon"
  320. content="Song is already in queue"
  321. v-tippy
  322. >done</i
  323. >
  324. <i
  325. v-else
  326. class="material-icons add-to-queue-icon"
  327. @click="
  328. addSongToQueue(result.id, index)
  329. "
  330. content="Add Song to Queue"
  331. v-tippy
  332. >queue</i
  333. >
  334. </transition>
  335. </template>
  336. </search-query-item>
  337. <a
  338. class="button is-primary load-more-button"
  339. @click.prevent="loadMoreSongs()"
  340. >
  341. Load more...
  342. </a>
  343. </div>
  344. </div>
  345. </div>
  346. <playlist-tab-base
  347. v-if="!disableAutoRequest"
  348. class="tab"
  349. v-show="tab === 'autorequest'"
  350. :type="'autorequest'"
  351. :sector="sector"
  352. :modal-uuid="modalUuid"
  353. />
  354. </div>
  355. </div>
  356. </template>
  357. <style lang="less" scoped>
  358. .night-mode {
  359. .tabs-container .tab-selection .button {
  360. background: var(--dark-grey) !important;
  361. color: var(--white) !important;
  362. }
  363. }
  364. :deep(#create-new-playlist-button) {
  365. width: 100%;
  366. }
  367. .station-playlists {
  368. .top-info {
  369. font-size: 15px;
  370. margin-bottom: 15px;
  371. }
  372. .tabs-container {
  373. .tab-selection {
  374. margin-bottom: 10px;
  375. display: flex;
  376. overflow-x: auto;
  377. .button {
  378. border-radius: 0;
  379. border: 0;
  380. text-transform: uppercase;
  381. font-size: 14px;
  382. color: var(--dark-grey-3);
  383. background-color: var(--light-grey-2);
  384. flex-grow: 1;
  385. height: 32px;
  386. &:not(:first-of-type) {
  387. margin-left: 5px;
  388. }
  389. }
  390. .selected {
  391. background-color: var(--primary-color) !important;
  392. color: var(--white) !important;
  393. font-weight: 600;
  394. }
  395. }
  396. .tab {
  397. padding-bottom: 10px;
  398. border-radius: 0;
  399. .item.item-draggable:not(:last-of-type) {
  400. margin-bottom: 10px;
  401. }
  402. .load-more-button {
  403. width: 100%;
  404. margin-top: 10px;
  405. }
  406. }
  407. }
  408. }
  409. .youtube-direct {
  410. margin-top: 10px;
  411. }
  412. .youtube-search {
  413. margin-top: 10px;
  414. .search-query-item:not(:last-of-type) {
  415. margin-bottom: 10px;
  416. }
  417. }
  418. </style>