useEditedBy.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import { userDisplayName } from '@shared/entities/user/utils/getUserDisplayName'
  3. import { i18n } from '@shared/i18n'
  4. import { useSessionStore } from '@shared/stores/session'
  5. import type { Ref } from 'vue'
  6. import { computed } 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?: 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. }