useAvatarIndicator.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { useDateFormat } from '@vueuse/shared'
  3. import { computed, toValue, type MaybeRef } from 'vue'
  4. import type {
  5. AvatarUser,
  6. AvatarUserAccess,
  7. AvatarUserLive,
  8. } from '#shared/components/CommonUserAvatar/types.ts'
  9. import { EnumTaskbarApp } from '#shared/graphql/types.ts'
  10. import { i18n } from '#shared/i18n.ts'
  11. import { useAppName } from './useAppName.ts'
  12. import { useReactiveNow } from './useReactiveNow.ts'
  13. export const useAvatarIndicator = (
  14. entity: MaybeRef<AvatarUser>,
  15. personal?: MaybeRef<boolean>,
  16. live?: MaybeRef<AvatarUserLive | undefined>,
  17. access?: MaybeRef<AvatarUserAccess | undefined>,
  18. ) => {
  19. const appName = useAppName()
  20. const currentDate = useReactiveNow()
  21. const isOutOfOffice = computed(() => {
  22. const user = toValue(entity)
  23. if (user.outOfOffice && user.outOfOfficeStartAt && user.outOfOfficeEndAt) {
  24. const today = useDateFormat(currentDate.value, 'YYYY-MM-DD')
  25. const startDate = user.outOfOfficeStartAt
  26. const endDate = user.outOfOfficeEndAt
  27. return startDate <= today.value && endDate >= today.value // Today is between start and end date
  28. }
  29. return false
  30. })
  31. const isInactive = computed(() => toValue(entity).active === false)
  32. const isWithoutAccess = computed(
  33. () => toValue(access)?.agentReadAccess === false,
  34. )
  35. const isLiveUserIdle = computed(() => toValue(live)?.isIdle)
  36. const isLiveUserEditing = computed(() => toValue(live)?.editing)
  37. const isLiveUserDesktop = computed(
  38. () => toValue(live)?.app === EnumTaskbarApp.Desktop,
  39. )
  40. const isLiveUserMobile = computed(
  41. () => toValue(live)?.app === EnumTaskbarApp.Mobile,
  42. )
  43. const indicatorIcon = computed(() => {
  44. if (isInactive.value) return 'avatar-indicator-inactive'
  45. if (isWithoutAccess.value) return 'avatar-indicator-without-access'
  46. if (isOutOfOffice.value) return 'avatar-indicator-out-of-office'
  47. if (isLiveUserEditing.value && isLiveUserDesktop.value)
  48. return 'avatar-indicator-editing-desktop'
  49. if (isLiveUserEditing.value && isLiveUserMobile.value)
  50. return 'avatar-indicator-editing-mobile'
  51. if (isLiveUserDesktop.value && appName !== 'desktop')
  52. return 'avatar-indicator-desktop'
  53. if (isLiveUserMobile.value && appName !== 'mobile')
  54. return 'avatar-indicator-mobile'
  55. if (isLiveUserIdle.value) return 'avatar-indicator-idle'
  56. return null
  57. })
  58. const indicatorLabel = computed(() => {
  59. if (isInactive.value) return i18n.t('User is inactive')
  60. if (isWithoutAccess.value) return i18n.t('User has no access')
  61. if (isOutOfOffice.value && toValue(personal))
  62. return i18n.t('Out of office active')
  63. if (isOutOfOffice.value) return i18n.t('User is out of office')
  64. if (isLiveUserEditing.value && isLiveUserDesktop.value)
  65. return i18n.t('User is editing on desktop')
  66. if (isLiveUserEditing.value && isLiveUserMobile.value)
  67. return i18n.t('User is editing on mobile')
  68. if (isLiveUserEditing.value) return i18n.t('User is editing')
  69. if (isLiveUserDesktop.value && appName !== 'desktop')
  70. return i18n.t('User is on desktop')
  71. if (isLiveUserMobile.value && appName !== 'mobile')
  72. return i18n.t('User is on mobile')
  73. if (isLiveUserIdle.value) return i18n.t('User is idle')
  74. return undefined
  75. })
  76. const indicatorIsIdle = computed(
  77. () =>
  78. isInactive.value ||
  79. isOutOfOffice.value ||
  80. isWithoutAccess.value ||
  81. isLiveUserIdle.value,
  82. )
  83. return {
  84. isInactive,
  85. isLiveUserDesktop,
  86. isLiveUserEditing,
  87. isLiveUserIdle,
  88. isLiveUserMobile,
  89. isOutOfOffice,
  90. isWithoutAccess,
  91. indicatorIcon,
  92. indicatorLabel,
  93. indicatorIsIdle,
  94. }
  95. }