Users.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. <script setup lang="ts">
  2. import { useRoute } from "vue-router";
  3. import {
  4. defineAsyncComponent,
  5. ref,
  6. reactive,
  7. computed,
  8. watch,
  9. onMounted
  10. } from "vue";
  11. import Toast from "toasters";
  12. import { storeToRefs } from "pinia";
  13. import { useWebsocketsStore } from "@/stores/websockets";
  14. import { useStationStore } from "@/stores/station";
  15. const ProfilePicture = defineAsyncComponent(
  16. () => import("@/components/ProfilePicture.vue")
  17. );
  18. const stationStore = useStationStore();
  19. const route = useRoute();
  20. const notesUri = ref("");
  21. const frontendDomain = ref("");
  22. const tab = ref("active");
  23. const tabs = ref([]);
  24. const search = reactive({
  25. query: "",
  26. searchedQuery: "",
  27. page: 0,
  28. count: 0,
  29. resultsLeft: 0,
  30. pageSize: 0,
  31. results: []
  32. });
  33. const { socket } = useWebsocketsStore();
  34. const { station, users, userCount } = storeToRefs(stationStore);
  35. const isOwner = userId => station.value.owner === userId;
  36. const isDj = userId => !!station.value.djs.find(dj => dj._id === userId);
  37. const sortedUsers = computed(() =>
  38. users.value && users.value.loggedIn
  39. ? users.value.loggedIn
  40. .slice()
  41. .sort(
  42. (a, b) =>
  43. Number(isOwner(b._id)) - Number(isOwner(a._id)) ||
  44. Number(!isOwner(a._id)) - Number(!isOwner(b._id))
  45. )
  46. : []
  47. );
  48. const resultsLeftCount = computed(() => search.count - search.results.length);
  49. const nextPageResultsCount = computed(() =>
  50. Math.min(search.pageSize, resultsLeftCount.value)
  51. );
  52. const { hasPermission } = stationStore;
  53. const copyToClipboard = async () => {
  54. try {
  55. await navigator.clipboard.writeText(
  56. frontendDomain.value + route.fullPath
  57. );
  58. } catch (err) {
  59. new Toast("Failed to copy to clipboard.");
  60. }
  61. };
  62. const showTab = _tab => {
  63. tabs.value[`${_tab}-tab`].scrollIntoView({ block: "nearest" });
  64. tab.value = _tab;
  65. };
  66. const addDj = userId => {
  67. socket.dispatch("stations.addDj", station.value._id, userId, res => {
  68. new Toast(res.message);
  69. });
  70. };
  71. const removeDj = userId => {
  72. socket.dispatch("stations.removeDj", station.value._id, userId, res => {
  73. new Toast(res.message);
  74. });
  75. };
  76. const searchForUser = page => {
  77. if (search.page >= page || search.searchedQuery !== search.query) {
  78. search.results = [];
  79. search.page = 0;
  80. search.count = 0;
  81. search.resultsLeft = 0;
  82. search.pageSize = 0;
  83. }
  84. search.searchedQuery = search.query;
  85. socket.dispatch("users.search", search.query, page, res => {
  86. const { data } = res;
  87. if (res.status === "success") {
  88. const { count, pageSize, users } = data;
  89. search.results = [...search.results, ...users];
  90. search.page = page;
  91. search.count = count;
  92. search.resultsLeft = count - search.results.length;
  93. search.pageSize = pageSize;
  94. } else if (res.status === "error") {
  95. search.results = [];
  96. search.page = 0;
  97. search.count = 0;
  98. search.resultsLeft = 0;
  99. search.pageSize = 0;
  100. new Toast(res.message);
  101. }
  102. });
  103. };
  104. watch(
  105. () => hasPermission("stations.update"),
  106. value => {
  107. if (!value && (tab.value === "djs" || tab.value === "add-dj"))
  108. showTab("active");
  109. }
  110. );
  111. onMounted(async () => {
  112. frontendDomain.value = await lofig.get("frontendDomain");
  113. notesUri.value = encodeURI(`${frontendDomain.value}/assets/notes.png`);
  114. });
  115. </script>
  116. <template>
  117. <div id="users">
  118. <div class="tabs-container">
  119. <div
  120. v-if="
  121. hasPermission('stations.update') &&
  122. station.type === 'community'
  123. "
  124. class="tab-selection"
  125. >
  126. <button
  127. class="button is-default"
  128. :ref="el => (tabs['active-tab'] = el)"
  129. :class="{ selected: tab === 'active' }"
  130. @click="showTab('active')"
  131. >
  132. Active
  133. </button>
  134. <button
  135. class="button is-default"
  136. :ref="el => (tabs['djs-tab'] = el)"
  137. :class="{ selected: tab === 'djs' }"
  138. @click="showTab('djs')"
  139. >
  140. DJs
  141. </button>
  142. <button
  143. class="button is-default"
  144. :ref="el => (tabs['add-dj-tab'] = el)"
  145. :class="{ selected: tab === 'add-dj' }"
  146. @click="showTab('add-dj')"
  147. >
  148. Add DJ
  149. </button>
  150. </div>
  151. <div class="tab" v-show="tab === 'active'">
  152. <h5 class="has-text-centered">Total users: {{ userCount }}</h5>
  153. <transition-group name="notification-box">
  154. <h6
  155. class="has-text-centered"
  156. v-if="
  157. users.loggedIn &&
  158. users.loggedOut &&
  159. ((users.loggedIn.length === 1 &&
  160. users.loggedOut.length === 0) ||
  161. (users.loggedIn.length === 0 &&
  162. users.loggedOut.length === 1))
  163. "
  164. key="only-me"
  165. >
  166. It's just you in the station!
  167. </h6>
  168. <h6
  169. class="has-text-centered"
  170. v-else-if="
  171. users.loggedIn &&
  172. users.loggedOut &&
  173. users.loggedOut.length > 0
  174. "
  175. key="logged-out-users"
  176. >
  177. {{ users.loggedOut.length }}
  178. {{
  179. users.loggedOut.length > 1 ? "users are" : "user is"
  180. }}
  181. logged-out.
  182. </h6>
  183. </transition-group>
  184. <aside class="menu">
  185. <ul class="menu-list scrollable-list">
  186. <li v-for="user in sortedUsers" :key="user.username">
  187. <router-link
  188. :to="{
  189. name: 'profile',
  190. params: { username: user.username }
  191. }"
  192. target="_blank"
  193. >
  194. <div class="left">
  195. <profile-picture
  196. :avatar="user.avatar"
  197. :name="user.name || user.username"
  198. />
  199. {{ user.name || user.username }}
  200. <span
  201. v-if="isOwner(user._id)"
  202. class="material-icons user-rank"
  203. content="Station Owner"
  204. v-tippy="{ theme: 'info' }"
  205. >local_police</span
  206. >
  207. <span
  208. v-else-if="isDj(user._id)"
  209. class="material-icons user-rank"
  210. content="Station DJ"
  211. v-tippy="{ theme: 'info' }"
  212. >shield</span
  213. >
  214. <button
  215. v-if="
  216. hasPermission('stations.djs.add') &&
  217. station.type === 'community' &&
  218. !isDj(user._id) &&
  219. !isOwner(user._id)
  220. "
  221. class="button is-primary material-icons"
  222. @click.prevent="addDj(user._id)"
  223. content="Promote user to DJ"
  224. v-tippy
  225. >
  226. add_moderator
  227. </button>
  228. <button
  229. v-else-if="
  230. hasPermission(
  231. 'stations.djs.remove'
  232. ) &&
  233. station.type === 'community' &&
  234. isDj(user._id)
  235. "
  236. class="button is-danger material-icons"
  237. @click.prevent="removeDj(user._id)"
  238. content="Demote user from DJ"
  239. v-tippy
  240. >
  241. remove_moderator
  242. </button>
  243. </div>
  244. <div class="user-state">
  245. <i
  246. class="material-icons"
  247. v-if="user.state === 'participate'"
  248. v-tippy
  249. content="Participating"
  250. >group</i
  251. >
  252. <i
  253. class="material-icons"
  254. v-if="user.state === 'local_paused'"
  255. v-tippy
  256. content="Paused"
  257. >pause</i
  258. >
  259. <i
  260. class="material-icons"
  261. v-if="user.state === 'muted'"
  262. v-tippy
  263. content="Muted"
  264. >volume_mute</i
  265. >
  266. <i
  267. class="material-icons"
  268. v-if="user.state === 'playing'"
  269. v-tippy
  270. content="Listening to music"
  271. >play_arrow</i
  272. >
  273. <i
  274. class="material-icons"
  275. v-if="user.state === 'buffering'"
  276. v-tippy
  277. content="Buffering"
  278. >warning</i
  279. >
  280. </div>
  281. </router-link>
  282. </li>
  283. </ul>
  284. </aside>
  285. </div>
  286. <div
  287. v-if="hasPermission('stations.update')"
  288. class="tab"
  289. v-show="tab === 'djs'"
  290. >
  291. <h5 class="has-text-centered">Station DJs</h5>
  292. <h6 v-if="station.djs.length === 0" class="has-text-centered">
  293. There are currently no DJs.
  294. </h6>
  295. <aside class="menu">
  296. <ul class="menu-list scrollable-list">
  297. <li v-for="dj in station.djs" :key="dj._id">
  298. <router-link
  299. :to="{
  300. name: 'profile',
  301. params: { username: dj.username }
  302. }"
  303. target="_blank"
  304. >
  305. <profile-picture
  306. :avatar="dj.avatar"
  307. :name="dj.name || dj.username"
  308. />
  309. {{ dj.name || dj.username }}
  310. <span
  311. class="material-icons user-rank"
  312. content="Station DJ"
  313. v-tippy="{ theme: 'info' }"
  314. >shield</span
  315. >
  316. <button
  317. v-if="hasPermission('stations.djs.remove')"
  318. class="button is-danger material-icons"
  319. @click.prevent="removeDj(dj._id)"
  320. content="Demote user from DJ"
  321. v-tippy
  322. >
  323. remove_moderator
  324. </button>
  325. </router-link>
  326. </li>
  327. </ul>
  328. </aside>
  329. </div>
  330. <div
  331. v-if="hasPermission('stations.update')"
  332. class="tab add-dj-tab"
  333. v-show="tab === 'add-dj'"
  334. >
  335. <h5 class="has-text-centered">Add Station DJ</h5>
  336. <h6 class="has-text-centered">
  337. Search for users to promote to DJ.
  338. </h6>
  339. <div class="control is-grouped input-with-button">
  340. <p class="control is-expanded">
  341. <input
  342. class="input"
  343. type="text"
  344. placeholder="Enter your user query here..."
  345. v-model="search.query"
  346. @keyup.enter="searchForUser(1)"
  347. />
  348. </p>
  349. <p class="control">
  350. <button
  351. class="button is-primary"
  352. @click="searchForUser(1)"
  353. >
  354. <i class="material-icons icon-with-button">search</i
  355. >Search
  356. </button>
  357. </p>
  358. </div>
  359. <aside class="menu">
  360. <ul class="menu-list scrollable-list">
  361. <li v-for="user in search.results" :key="user.username">
  362. <router-link
  363. :to="{
  364. name: 'profile',
  365. params: { username: user.username }
  366. }"
  367. target="_blank"
  368. >
  369. <profile-picture
  370. :avatar="user.avatar"
  371. :name="user.name || user.username"
  372. />
  373. {{ user.name || user.username }}
  374. <span
  375. v-if="isOwner(user._id)"
  376. class="material-icons user-rank"
  377. content="Station Owner"
  378. v-tippy="{ theme: 'info' }"
  379. >local_police</span
  380. >
  381. <span
  382. v-else-if="isDj(user._id)"
  383. class="material-icons user-rank"
  384. content="Station DJ"
  385. v-tippy="{ theme: 'info' }"
  386. >shield</span
  387. >
  388. <button
  389. v-if="
  390. hasPermission('stations.djs.add') &&
  391. station.type === 'community' &&
  392. !isDj(user._id) &&
  393. !isOwner(user._id)
  394. "
  395. class="button is-primary material-icons"
  396. @click.prevent="addDj(user._id)"
  397. content="Promote user to DJ"
  398. v-tippy
  399. >
  400. add_moderator
  401. </button>
  402. <button
  403. v-else-if="
  404. hasPermission('stations.djs.remove') &&
  405. station.type === 'community' &&
  406. isDj(user._id)
  407. "
  408. class="button is-danger material-icons"
  409. @click.prevent="removeDj(user._id)"
  410. content="Demote user from DJ"
  411. v-tippy
  412. >
  413. remove_moderator
  414. </button>
  415. </router-link>
  416. </li>
  417. <button
  418. v-if="resultsLeftCount > 0"
  419. class="button is-primary load-more-button"
  420. @click="searchForUser(search.page + 1)"
  421. >
  422. Load {{ nextPageResultsCount }} more results
  423. </button>
  424. </ul>
  425. </aside>
  426. </div>
  427. </div>
  428. <button
  429. class="button is-primary tab-actionable-button"
  430. @click="copyToClipboard()"
  431. >
  432. <i class="material-icons icon-with-button">share</i>
  433. <span> Share (copy to clipboard) </span>
  434. </button>
  435. </div>
  436. </template>
  437. <style lang="less" scoped>
  438. .night-mode {
  439. #users {
  440. background-color: var(--dark-grey-3) !important;
  441. border: 0 !important;
  442. }
  443. a {
  444. color: var(--light-grey-2);
  445. background-color: var(--dark-grey-2) !important;
  446. border: 0 !important;
  447. &:hover {
  448. color: var(--light-grey) !important;
  449. }
  450. }
  451. .tabs-container .tab-selection .button {
  452. background: var(--dark-grey) !important;
  453. color: var(--white) !important;
  454. }
  455. }
  456. .notification-box-enter-active,
  457. .fade-leave-active {
  458. transition: opacity 0.5s;
  459. }
  460. .notification-box-enter,
  461. .notification-box-leave-to {
  462. opacity: 0;
  463. }
  464. #users {
  465. background-color: var(--white);
  466. margin-bottom: 20px;
  467. border-radius: 0 0 @border-radius @border-radius;
  468. max-height: 100%;
  469. .tabs-container {
  470. padding: 10px;
  471. .tab-selection {
  472. display: flex;
  473. overflow-x: auto;
  474. margin-bottom: 10px;
  475. .button {
  476. border-radius: 0;
  477. border: 0;
  478. text-transform: uppercase;
  479. font-size: 14px;
  480. color: var(--dark-grey-3);
  481. background-color: var(--light-grey-2);
  482. flex-grow: 1;
  483. height: 32px;
  484. &:not(:first-of-type) {
  485. margin-left: 5px;
  486. }
  487. }
  488. .selected {
  489. background-color: var(--primary-color) !important;
  490. color: var(--white) !important;
  491. font-weight: 600;
  492. }
  493. }
  494. .tab {
  495. position: absolute;
  496. height: calc(100% - 120px);
  497. width: calc(100% - 20px);
  498. overflow-y: auto;
  499. .menu {
  500. margin-top: 20px;
  501. width: 100%;
  502. .menu-list {
  503. margin-left: 0;
  504. padding: 0;
  505. &.scrollable-list {
  506. max-height: unset;
  507. }
  508. }
  509. li {
  510. &:not(:first-of-type) {
  511. margin-top: 10px;
  512. }
  513. a {
  514. display: flex;
  515. flex-direction: row;
  516. align-items: center;
  517. padding: 5px 10px;
  518. border: 0.5px var(--light-grey-3) solid;
  519. border-radius: @border-radius;
  520. cursor: pointer;
  521. &:hover {
  522. background-color: var(--light-grey);
  523. color: var(--black);
  524. }
  525. .left {
  526. display: flex;
  527. align-items: center;
  528. flex: 1;
  529. .profile-picture {
  530. margin-right: 10px;
  531. width: 36px;
  532. height: 36px;
  533. }
  534. :deep(.profile-picture.using-initials span) {
  535. font-size: calc(
  536. 36px / 5 * 2
  537. ); // 2/5th of .profile-picture height/width
  538. }
  539. .user-rank {
  540. color: var(--primary-color);
  541. font-size: 18px;
  542. margin: 0 5px;
  543. }
  544. .button {
  545. margin-left: auto;
  546. font-size: 18px;
  547. width: 36px;
  548. }
  549. }
  550. .user-state {
  551. display: flex;
  552. }
  553. }
  554. }
  555. }
  556. h5 {
  557. font-size: 20px;
  558. }
  559. &.add-dj-tab {
  560. .control.is-grouped.input-with-button {
  561. margin: 20px 0 0 0 !important;
  562. & > .control {
  563. margin-bottom: 0 !important;
  564. }
  565. }
  566. .menu {
  567. margin-top: 10px;
  568. }
  569. .load-more-button {
  570. width: 100%;
  571. margin-top: 10px;
  572. }
  573. }
  574. }
  575. }
  576. }
  577. </style>