UserLink.vue 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <script setup lang="ts">
  2. import { ref, onMounted } from "vue";
  3. import { useWebsocketsStore } from "@/stores/websockets";
  4. import { useUserAuthStore } from "@/stores/userAuth";
  5. const props = defineProps({
  6. userId: { type: String, default: "" },
  7. link: { type: Boolean, default: true }
  8. });
  9. const user = ref<{ name: string; username?: string }>({
  10. name: "Unknown"
  11. });
  12. const { socket } = useWebsocketsStore();
  13. const { getBasicUser } = useUserAuthStore();
  14. onMounted(() => {
  15. socket.onConnect(() => {
  16. getBasicUser(props.userId).then(basicUser => {
  17. if (basicUser) {
  18. const { name, username } = basicUser;
  19. user.value = {
  20. name,
  21. username
  22. };
  23. }
  24. });
  25. });
  26. });
  27. </script>
  28. <template>
  29. <router-link
  30. v-if="$props.link && user.username"
  31. :to="{ path: `/u/${user.username}` }"
  32. :title="userId"
  33. >
  34. {{ user.name }}
  35. </router-link>
  36. <span v-else :title="userId">
  37. {{ user.name }}
  38. </span>
  39. </template>
  40. <style lang="less" scoped>
  41. a {
  42. color: var(--primary-color);
  43. &:hover,
  44. &:focus {
  45. filter: brightness(90%);
  46. }
  47. }
  48. </style>