useEditedBy.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { computed } from 'vue'
  3. import { userDisplayName } from '#shared/entities/user/utils/getUserDisplayName.ts'
  4. import { i18n } from '#shared/i18n.ts'
  5. import { useSessionStore } from '#shared/stores/session.ts'
  6. import type { Ref } from 'vue'
  7. interface Author {
  8. id: string
  9. firstname?: Maybe<string>
  10. lastname?: Maybe<string>
  11. fullname?: Maybe<string>
  12. }
  13. interface Entity {
  14. updatedAt?: string
  15. updatedBy?: Maybe<Author>
  16. }
  17. export const useEditedBy = (entity: Ref<Entity>) => {
  18. const session = useSessionStore()
  19. const author = computed(() => {
  20. const { updatedBy } = entity.value
  21. if (!updatedBy) return ''
  22. return userDisplayName(updatedBy)
  23. })
  24. const date = computed(() => {
  25. const { updatedAt } = entity.value
  26. if (!updatedAt) return ''
  27. return i18n.relativeDateTime(updatedAt)
  28. })
  29. const stringUpdated = computed(() => {
  30. if (!date.value) return ''
  31. if (entity.value.updatedBy?.id === session.user?.id) {
  32. return i18n.t('edited %s by me', date.value)
  33. }
  34. if (!author.value) return i18n.t('edited %s', date.value)
  35. return i18n.t('edited %s by %s', date.value, author.value)
  36. })
  37. return {
  38. author,
  39. date,
  40. stringUpdated,
  41. }
  42. }