MediaItem.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. <script setup lang="ts">
  2. import {
  3. defineAsyncComponent,
  4. ref,
  5. computed,
  6. onMounted,
  7. onUnmounted
  8. } from "vue";
  9. import { formatDistance, parseISO } from "date-fns";
  10. import { storeToRefs } from "pinia";
  11. import AddToPlaylistDropdown from "./AddToPlaylistDropdown.vue";
  12. import { useUserAuthStore } from "@/stores/userAuth";
  13. import { useModalsStore } from "@/stores/modals";
  14. import { useConfigStore } from "@/stores/config";
  15. import utils from "@/utils";
  16. const SongThumbnail = defineAsyncComponent(
  17. () => import("@/components/SongThumbnail.vue")
  18. );
  19. const UserLink = defineAsyncComponent(
  20. () => import("@/components/UserLink.vue")
  21. );
  22. const props = defineProps({
  23. song: {
  24. type: Object,
  25. default: () => {}
  26. },
  27. requestedBy: {
  28. type: Boolean,
  29. default: false
  30. },
  31. requestedType: {
  32. type: Boolean,
  33. default: false
  34. },
  35. duration: {
  36. type: Boolean,
  37. default: true
  38. },
  39. thumbnail: {
  40. type: Boolean,
  41. default: true
  42. },
  43. disabledActions: {
  44. type: Array,
  45. default: () => []
  46. },
  47. header: {
  48. type: String,
  49. default: null
  50. }
  51. });
  52. const formatedRequestedAt = ref(null);
  53. const formatRequestedAtInterval = ref();
  54. const hoveredTippy = ref(false);
  55. const songActions = ref(null);
  56. const configStore = useConfigStore();
  57. const { experimental, sitename } = storeToRefs(configStore);
  58. const userAuthStore = useUserAuthStore();
  59. const { loggedIn } = storeToRefs(userAuthStore);
  60. const { hasPermission } = userAuthStore;
  61. const { openModal } = useModalsStore();
  62. const songMediaType = computed(() => {
  63. if (
  64. !props.song ||
  65. !props.song.mediaSource ||
  66. props.song.mediaSource.indexOf(":") === -1
  67. )
  68. return "none";
  69. return props.song.mediaSource.split(":")[0];
  70. });
  71. const songMediaValue = computed(() => {
  72. if (
  73. !props.song ||
  74. !props.song.mediaSource ||
  75. props.song.mediaSource.indexOf(":") === -1
  76. )
  77. return null;
  78. return props.song.mediaSource.split(":")[1];
  79. });
  80. const formatRequestedAt = () => {
  81. if (props.requestedBy && props.song.requestedAt)
  82. formatedRequestedAt.value = formatDistance(
  83. parseISO(props.song.requestedAt),
  84. new Date()
  85. );
  86. };
  87. const formatArtists = () => {
  88. if (props.song.artists.length === 1) {
  89. return props.song.artists[0];
  90. }
  91. if (props.song.artists.length === 2) {
  92. return props.song.artists.join(" & ");
  93. }
  94. if (props.song.artists.length > 2) {
  95. return `${props.song.artists
  96. .slice(0, -1)
  97. .join(", ")} & ${props.song.artists.slice(-1)}`;
  98. }
  99. return null;
  100. };
  101. const hideTippyElements = () => {
  102. songActions.value.tippy.hide();
  103. setTimeout(
  104. () =>
  105. Array.from(document.querySelectorAll(".tippy-popper")).forEach(
  106. (popper: any) => popper._tippy.hide()
  107. ),
  108. 500
  109. );
  110. };
  111. const hoverTippy = () => {
  112. hoveredTippy.value = true;
  113. };
  114. const viewMedia = mediaSource => {
  115. hideTippyElements();
  116. openModal({
  117. modal: "viewMedia",
  118. props: {
  119. mediaSource
  120. }
  121. });
  122. };
  123. const report = song => {
  124. hideTippyElements();
  125. openModal({ modal: "report", props: { song } });
  126. };
  127. const edit = song => {
  128. hideTippyElements();
  129. openModal({
  130. modal: "editSong",
  131. props: { song }
  132. });
  133. };
  134. onMounted(() => {
  135. if (props.requestedBy) {
  136. formatRequestedAt();
  137. formatRequestedAtInterval.value = setInterval(() => {
  138. formatRequestedAt();
  139. }, 30000);
  140. }
  141. });
  142. onUnmounted(() => {
  143. clearInterval(formatRequestedAtInterval.value);
  144. });
  145. </script>
  146. <template>
  147. <div
  148. class="universal-item song-item"
  149. :class="{ 'with-duration': duration, 'with-header': header }"
  150. v-if="song"
  151. >
  152. <div class="thumbnail-and-info">
  153. <song-thumbnail :song="song" v-if="thumbnail" />
  154. <i
  155. v-if="
  156. songMediaType === 'soundcloud' && !experimental.soundcloud
  157. "
  158. class="material-icons warning-icon left-icon"
  159. :content="`SoundCloud is no longer enabled on ${sitename}`"
  160. v-tippy="{ theme: 'warning' }"
  161. >
  162. warning
  163. </i>
  164. <slot v-if="$slots.leftIcon" name="leftIcon" />
  165. <div class="song-info">
  166. <h6 v-if="header">{{ header }}</h6>
  167. <div class="song-title">
  168. <h4
  169. :class="{
  170. 'item-title': true,
  171. 'no-artists':
  172. !song.artists ||
  173. (song.artists && song.artists.length < 1)
  174. }"
  175. :title="song.title"
  176. >
  177. {{ song.title }}
  178. </h4>
  179. <i
  180. v-if="song.verified"
  181. class="material-icons verified-song"
  182. content="Verified Song"
  183. v-tippy="{ theme: 'info' }"
  184. >
  185. check_circle
  186. </i>
  187. </div>
  188. <h5
  189. class="item-description"
  190. v-if="formatArtists()"
  191. :title="formatArtists()"
  192. >
  193. {{ formatArtists() }}
  194. </h5>
  195. <p
  196. class="song-request-time"
  197. v-if="requestedBy && !requestedType"
  198. >
  199. Requested by
  200. <strong>
  201. <user-link
  202. v-if="song.requestedBy"
  203. :key="song.mediaSource"
  204. :user-id="song.requestedBy"
  205. />
  206. <span v-else>station</span>
  207. {{ formatedRequestedAt }}
  208. ago
  209. </strong>
  210. </p>
  211. <p
  212. class="song-request-time"
  213. v-if="requestedBy && requestedType"
  214. >
  215. <template v-if="song.requestedType === 'automatic'">
  216. Requested automaticaly
  217. <strong>
  218. {{ formatedRequestedAt }}
  219. ago
  220. </strong>
  221. </template>
  222. <template v-else>
  223. <span v-if="song.requestedType === 'autorequest'"
  224. >Autorequested</span
  225. ><span v-else>Requested</span> by
  226. <strong>
  227. <user-link
  228. v-if="song.requestedBy"
  229. :key="song.mediaSource"
  230. :user-id="song.requestedBy"
  231. />
  232. <span v-else>station</span>
  233. {{ formatedRequestedAt }}
  234. ago
  235. </strong>
  236. </template>
  237. </p>
  238. </div>
  239. </div>
  240. <div class="duration-and-actions">
  241. <p v-if="duration" class="song-duration">
  242. {{ utils.formatTime(song.duration) }}
  243. </p>
  244. <div
  245. class="universal-item-actions"
  246. v-if="disabledActions.indexOf('all') === -1"
  247. >
  248. <tippy
  249. v-if="loggedIn && hoveredTippy"
  250. :touch="true"
  251. :interactive="true"
  252. placement="left"
  253. theme="songActions"
  254. ref="songActions"
  255. trigger="click"
  256. >
  257. <i
  258. class="material-icons action-dropdown-icon"
  259. content="Song Options"
  260. v-tippy
  261. >more_horiz</i
  262. >
  263. <template #content>
  264. <div class="icons-group">
  265. <i
  266. v-if="
  267. disabledActions.indexOf('view-song') ===
  268. -1 &&
  269. (songMediaType === 'youtube' ||
  270. (songMediaType === 'soundcloud' &&
  271. experimental.soundcloud))
  272. "
  273. @click="viewMedia(song.mediaSource)"
  274. content="View Media"
  275. v-tippy
  276. >
  277. <div
  278. v-if="songMediaType === 'youtube'"
  279. class="youtube-icon"
  280. ></div>
  281. <div v-else class="soundcloud-icon"></div>
  282. </i>
  283. <i
  284. v-if="
  285. song._id &&
  286. disabledActions.indexOf('report') === -1
  287. "
  288. class="material-icons report-icon"
  289. @click="report(song)"
  290. content="Report Song"
  291. v-tippy
  292. >
  293. flag
  294. </i>
  295. <add-to-playlist-dropdown
  296. v-if="
  297. disabledActions.indexOf('addToPlaylist') ===
  298. -1 &&
  299. (songMediaType !== 'soundcloud' ||
  300. experimental.soundcloud)
  301. "
  302. :song="song"
  303. placement="top-end"
  304. >
  305. <template #button>
  306. <i
  307. class="material-icons add-to-playlist-icon"
  308. content="Add Song to Playlist"
  309. v-tippy
  310. >playlist_add</i
  311. >
  312. </template>
  313. </add-to-playlist-dropdown>
  314. <i
  315. v-if="
  316. loggedIn &&
  317. song._id &&
  318. hasPermission('songs.update') &&
  319. disabledActions.indexOf('edit') === -1
  320. "
  321. class="material-icons edit-icon"
  322. @click="edit(song)"
  323. content="Edit Song"
  324. v-tippy
  325. >
  326. edit
  327. </i>
  328. <slot name="tippyActions" />
  329. </div>
  330. </template>
  331. </tippy>
  332. <i
  333. class="material-icons action-dropdown-icon"
  334. v-else-if="loggedIn && !hoveredTippy"
  335. @mouseenter="hoverTippy()"
  336. >more_horiz</i
  337. >
  338. <template
  339. v-else-if="
  340. !loggedIn && disabledActions.indexOf('view-song') === -1
  341. "
  342. >
  343. <a
  344. v-if="songMediaType === 'youtube'"
  345. target="_blank"
  346. :href="`https://www.youtube.com/watch?v=${songMediaValue}`"
  347. content="View on Youtube"
  348. v-tippy
  349. >
  350. <div class="youtube-icon"></div>
  351. </a>
  352. </template>
  353. </div>
  354. <div class="universal-item-actions" v-if="$slots.actions">
  355. <slot name="actions" />
  356. </div>
  357. </div>
  358. </div>
  359. </template>
  360. <style lang="less" scoped>
  361. .night-mode {
  362. .song-item {
  363. background-color: var(--dark-grey-2) !important;
  364. border: 0 !important;
  365. }
  366. }
  367. :deep(#nav-dropdown) {
  368. margin-top: 36px;
  369. width: 0;
  370. height: 0;
  371. .nav-dropdown-items {
  372. width: 250px;
  373. max-width: 100vw;
  374. position: relative;
  375. right: 175px;
  376. }
  377. }
  378. .song-item {
  379. height: 70px;
  380. &.with-header {
  381. height: initial;
  382. min-height: 70px;
  383. }
  384. &:not(:last-of-type) {
  385. margin-bottom: 10px;
  386. }
  387. .thumbnail-and-info,
  388. .duration-and-actions {
  389. display: flex;
  390. align-items: center;
  391. }
  392. .duration-and-actions {
  393. margin-left: 5px;
  394. .universal-item-actions div i {
  395. margin-left: 5px;
  396. }
  397. }
  398. .thumbnail-and-info {
  399. position: relative;
  400. height: calc(100% + 15px);
  401. min-width: 0;
  402. .thumbnail {
  403. position: relative;
  404. height: 100%;
  405. min-width: 70px;
  406. width: 70px;
  407. margin-left: -7.5px;
  408. margin-right: 7.5px;
  409. }
  410. :deep(.left-icon) {
  411. margin-right: 7.5px;
  412. }
  413. .song-info {
  414. display: flex;
  415. flex-direction: column;
  416. justify-content: center;
  417. min-width: 0;
  418. *:not(i) {
  419. margin: 0;
  420. font-family: Nunito, Arial, sans-serif;
  421. }
  422. h6 {
  423. color: var(--primary-color) !important;
  424. font-weight: bold;
  425. font-size: 17px;
  426. margin-bottom: 5px;
  427. }
  428. .song-title {
  429. display: flex;
  430. flex-direction: row;
  431. .item-title {
  432. font-size: 18px;
  433. }
  434. .verified-song {
  435. margin-left: 5px;
  436. }
  437. .item-title.no-artists {
  438. display: -webkit-inline-box;
  439. font-size: 16px;
  440. white-space: normal;
  441. -webkit-box-orient: vertical;
  442. -webkit-line-clamp: 2;
  443. }
  444. }
  445. .item-description {
  446. line-height: 120%;
  447. }
  448. .song-request-time {
  449. font-size: 11px;
  450. overflow: hidden;
  451. text-overflow: ellipsis;
  452. white-space: nowrap;
  453. }
  454. }
  455. }
  456. .song-duration {
  457. font-size: 20px;
  458. }
  459. .edit-icon {
  460. color: var(--primary-color);
  461. }
  462. }
  463. </style>