CommonUserAvatar.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { computed } from 'vue'
  4. import { useApplicationStore } from '#shared/stores/application.ts'
  5. import { getInitials } from '#shared/utils/formatter.ts'
  6. import { i18n } from '#shared/i18n.ts'
  7. import {
  8. SYSTEM_USER_ID,
  9. SYSTEM_USER_INTERNAL_ID,
  10. } from '#shared/utils/constants.ts'
  11. import { getIdFromGraphQLId } from '#shared/graphql/utils.ts'
  12. import { getUserAvatarClasses } from '#shared/initializer/initializeUserAvatarClasses.ts'
  13. import CommonAvatar from '../CommonAvatar/CommonAvatar.vue'
  14. import type { AvatarSize } from '../CommonAvatar/index.ts'
  15. import type { AvatarUser } from './types.ts'
  16. import logo from './assets/logo.svg'
  17. export interface Props {
  18. entity: AvatarUser
  19. size?: AvatarSize
  20. personal?: boolean
  21. decorative?: boolean
  22. initialsOnly?: boolean
  23. }
  24. const props = defineProps<Props>()
  25. const initials = computed(() => {
  26. const { lastname, firstname, email, phone, mobile } = props.entity
  27. return getInitials(firstname, lastname, email, phone, mobile)
  28. })
  29. const { backgroundColors } = getUserAvatarClasses()
  30. const fullName = computed(() => {
  31. const { lastname, firstname, fullname } = props.entity
  32. if (fullname) return fullname
  33. return [firstname, lastname].filter(Boolean).join(' ')
  34. })
  35. const colorClass = computed(() => {
  36. const { id } = props.entity
  37. const internalId = getIdFromGraphQLId(id)
  38. if (internalId === SYSTEM_USER_INTERNAL_ID) return 'bg-white'
  39. // get color based on mod of the integer ID
  40. // so it stays consistent between different interfaces and logins
  41. return backgroundColors[internalId % (backgroundColors.length - 1)]
  42. })
  43. const sources = ['facebook', 'twitter']
  44. const icon = computed(() => {
  45. const { source } = props.entity
  46. if (source && sources.includes(source)) return source
  47. return null
  48. })
  49. const application = useApplicationStore()
  50. const image = computed(() => {
  51. if (icon.value || props.initialsOnly) return null
  52. if (props.entity.id === SYSTEM_USER_ID) return logo
  53. if (!props.entity.image) return null
  54. // Support the inline data URI as an image source.
  55. if (props.entity.image.startsWith('data:')) return props.entity.image
  56. // we're using the REST api here to get the image and to also use the browser image cache
  57. // TODO: this should be re-evaluated when the desktop app is going to be implemented
  58. const apiUrl = String(application.config.api_path)
  59. return `${apiUrl}/users/image/${props.entity.image}`
  60. })
  61. const isVip = computed(() => {
  62. return !props.personal && props.entity.vip
  63. })
  64. const className = computed(() => {
  65. const classes = [colorClass.value]
  66. if (props.entity.outOfOffice) {
  67. classes.push('opacity-100 grayscale-[70%]')
  68. } else if (props.entity.active === false) {
  69. classes.push('opacity-20 grayscale')
  70. }
  71. return classes
  72. })
  73. const label = computed(() => {
  74. let label = i18n.t('Avatar')
  75. const name = fullName.value || props.entity.email
  76. if (name) label += ` (${name})`
  77. if (isVip.value) label += ` (${i18n.t('VIP')})`
  78. return label
  79. })
  80. </script>
  81. <template>
  82. <CommonAvatar
  83. :initials="initials"
  84. :size="size"
  85. :icon="icon"
  86. :class="className"
  87. :image="image"
  88. :vip-icon="isVip ? 'vip-user' : undefined"
  89. :decorative="decorative"
  90. :aria-label="label"
  91. />
  92. </template>