SongItem.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. <song-thumbnail :song="song" v-if="thumbnail" />
  151. <slot v-if="$slots.leftIcon" name="leftIcon" />
  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. disabledActions.indexOf('youtube') === -1 &&
  266. songMediaType === 'spotify'
  267. "
  268. @click="viewYoutubeVideo(songMediaValue)"
  269. content="View Spotify Video"
  270. v-tippy
  271. >
  272. <div class="spotify-icon"></div>
  273. </i> -->
  274. <i
  275. v-if="
  276. song._id &&
  277. disabledActions.indexOf('report') === -1
  278. "
  279. class="material-icons report-icon"
  280. @click="report(song)"
  281. content="Report Song"
  282. v-tippy
  283. >
  284. flag
  285. </i>
  286. <add-to-playlist-dropdown
  287. v-if="
  288. disabledActions.indexOf('addToPlaylist') ===
  289. -1
  290. "
  291. :song="song"
  292. placement="top-end"
  293. >
  294. <template #button>
  295. <i
  296. class="material-icons add-to-playlist-icon"
  297. content="Add Song to Playlist"
  298. v-tippy
  299. >playlist_add</i
  300. >
  301. </template>
  302. </add-to-playlist-dropdown>
  303. <i
  304. v-if="
  305. loggedIn &&
  306. song._id &&
  307. hasPermission('songs.update') &&
  308. disabledActions.indexOf('edit') === -1
  309. "
  310. class="material-icons edit-icon"
  311. @click="edit(song)"
  312. content="Edit Song"
  313. v-tippy
  314. >
  315. edit
  316. </i>
  317. <slot name="tippyActions" />
  318. </div>
  319. </template>
  320. </tippy>
  321. <i
  322. class="material-icons action-dropdown-icon"
  323. v-else-if="loggedIn && !hoveredTippy"
  324. @mouseenter="hoverTippy()"
  325. >more_horiz</i
  326. >
  327. <a
  328. v-else-if="
  329. !loggedIn && disabledActions.indexOf('youtube') === -1
  330. "
  331. target="_blank"
  332. :href="`https://www.youtube.com/watch?v=${song.youtubeId}`"
  333. content="View on Youtube"
  334. v-tippy
  335. >
  336. <div class="youtube-icon"></div>
  337. </a>
  338. </div>
  339. <div class="universal-item-actions" v-if="$slots.actions">
  340. <slot name="actions" />
  341. </div>
  342. </div>
  343. </div>
  344. </template>
  345. <style lang="less" scoped>
  346. .night-mode {
  347. .song-item {
  348. background-color: var(--dark-grey-2) !important;
  349. border: 0 !important;
  350. }
  351. }
  352. :deep(#nav-dropdown) {
  353. margin-top: 36px;
  354. width: 0;
  355. height: 0;
  356. .nav-dropdown-items {
  357. width: 250px;
  358. max-width: 100vw;
  359. position: relative;
  360. right: 175px;
  361. }
  362. }
  363. .song-item {
  364. height: 70px;
  365. &.with-header {
  366. height: initial;
  367. min-height: 70px;
  368. }
  369. &:not(:last-of-type) {
  370. margin-bottom: 10px;
  371. }
  372. .thumbnail-and-info,
  373. .duration-and-actions {
  374. display: flex;
  375. align-items: center;
  376. }
  377. .duration-and-actions {
  378. margin-left: 5px;
  379. .universal-item-actions div i {
  380. margin-left: 5px;
  381. }
  382. }
  383. .thumbnail-and-info {
  384. min-width: 0;
  385. min-height: 70px;
  386. position: relative;
  387. }
  388. .thumbnail {
  389. min-width: 70px;
  390. width: 70px;
  391. margin: -7.5px;
  392. margin-right: calc(20px - 7.5px);
  393. height: calc(100% + 15px);
  394. position: absolute;
  395. }
  396. :deep(.left-icon) {
  397. margin-left: 70px;
  398. }
  399. .song-info:not(:nth-child(3)) {
  400. margin-left: 70px;
  401. }
  402. .song-info {
  403. display: flex;
  404. flex-direction: column;
  405. justify-content: center;
  406. margin-left: 10px;
  407. min-width: 0;
  408. *:not(i) {
  409. margin: 0;
  410. font-family: Karla, Arial, sans-serif;
  411. }
  412. h6 {
  413. color: var(--primary-color) !important;
  414. font-weight: bold;
  415. font-size: 17px;
  416. margin-bottom: 5px;
  417. }
  418. .song-title {
  419. display: flex;
  420. flex-direction: row;
  421. .item-title {
  422. font-size: 18px;
  423. }
  424. .verified-song {
  425. margin-left: 5px;
  426. }
  427. .item-title.no-artists {
  428. display: -webkit-inline-box;
  429. font-size: 16px;
  430. white-space: normal;
  431. -webkit-box-orient: vertical;
  432. -webkit-line-clamp: 2;
  433. }
  434. }
  435. .item-description {
  436. line-height: 120%;
  437. }
  438. .song-request-time {
  439. font-size: 11px;
  440. overflow: hidden;
  441. text-overflow: ellipsis;
  442. white-space: nowrap;
  443. }
  444. }
  445. .song-duration {
  446. font-size: 20px;
  447. }
  448. .edit-icon {
  449. color: var(--primary-color);
  450. }
  451. }
  452. </style>