Request.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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(
  320. `youtube:${result.id}`
  321. ) !== -1
  322. "
  323. class="material-icons added-to-playlist-icon"
  324. content="Song is already in queue"
  325. v-tippy
  326. >done</i
  327. >
  328. <i
  329. v-else
  330. class="material-icons add-to-queue-icon"
  331. @click="
  332. addSongToQueue(
  333. `youtube:${result.id}`,
  334. index
  335. )
  336. "
  337. content="Add Song to Queue"
  338. v-tippy
  339. >queue</i
  340. >
  341. </transition>
  342. </template>
  343. </search-query-item>
  344. <a
  345. class="button is-primary load-more-button"
  346. @click.prevent="loadMoreSongs()"
  347. >
  348. Load more...
  349. </a>
  350. </div>
  351. </div>
  352. <div v-if="experimental.soundcloud" class="soundcloud-direct">
  353. <label class="label">
  354. Add a SoundCloud song from a URL
  355. </label>
  356. <div class="control is-grouped input-with-button">
  357. <p class="control is-expanded">
  358. <input
  359. class="input"
  360. type="text"
  361. placeholder="Enter your SoundCloud song URL here..."
  362. v-model="soundcloudDirect"
  363. @keyup.enter="soundcloudAddToQueue(station._id)"
  364. />
  365. </p>
  366. <p class="control">
  367. <a
  368. class="button is-info"
  369. @click="soundcloudAddToQueue(station._id)"
  370. ><i class="material-icons icon-with-button"
  371. >add</i
  372. >Add</a
  373. >
  374. </p>
  375. </div>
  376. </div>
  377. </div>
  378. <playlist-tab-base
  379. v-if="!disableAutoRequest"
  380. class="tab"
  381. v-show="tab === 'autorequest'"
  382. :type="'autorequest'"
  383. :sector="sector"
  384. :modal-uuid="modalUuid"
  385. />
  386. </div>
  387. </div>
  388. </template>
  389. <style lang="less" scoped>
  390. .night-mode {
  391. .tabs-container .tab-selection .button {
  392. background: var(--dark-grey) !important;
  393. color: var(--white) !important;
  394. }
  395. }
  396. :deep(#create-new-playlist-button) {
  397. width: 100%;
  398. }
  399. .station-playlists {
  400. .top-info {
  401. font-size: 15px;
  402. margin-bottom: 15px;
  403. }
  404. .tabs-container {
  405. .tab-selection {
  406. margin-bottom: 10px;
  407. display: flex;
  408. overflow-x: auto;
  409. .button {
  410. border-radius: 0;
  411. border: 0;
  412. text-transform: uppercase;
  413. font-size: 14px;
  414. color: var(--dark-grey-3);
  415. background-color: var(--light-grey-2);
  416. flex-grow: 1;
  417. height: 32px;
  418. &:not(:first-of-type) {
  419. margin-left: 5px;
  420. }
  421. }
  422. .selected {
  423. background-color: var(--primary-color) !important;
  424. color: var(--white) !important;
  425. font-weight: 600;
  426. }
  427. }
  428. .tab {
  429. padding-bottom: 10px;
  430. border-radius: 0;
  431. .item.item-draggable:not(:last-of-type) {
  432. margin-bottom: 10px;
  433. }
  434. .load-more-button {
  435. width: 100%;
  436. margin-top: 10px;
  437. }
  438. }
  439. }
  440. }
  441. .youtube-direct {
  442. margin-top: 10px;
  443. }
  444. .youtube-search {
  445. margin-top: 10px;
  446. .search-query-item:not(:last-of-type) {
  447. margin-bottom: 10px;
  448. }
  449. }
  450. </style>