Request.vue 12 KB

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