SongItem.vue 9.2 KB

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