CommonDateTime.vue 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <!-- Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import type { ComputedRef } from 'vue'
  4. import { computed } from 'vue'
  5. import { i18n } from '@shared/i18n'
  6. import { useApplicationStore } from '@shared/stores/application'
  7. import type { DateTimeType, DateTimeAbsoluteFormat } from './types'
  8. export interface Props {
  9. dateTime: string
  10. type?: DateTimeType
  11. absoluteFormat?: DateTimeAbsoluteFormat
  12. }
  13. type OutputType = Exclude<DateTimeType, 'configured'>
  14. const props = withDefaults(defineProps<Props>(), {
  15. type: 'configured',
  16. absoluteFormat: 'datetime',
  17. })
  18. const application = useApplicationStore()
  19. const outputFormat: ComputedRef<OutputType> = computed(() => {
  20. if (props.type !== 'configured') return props.type
  21. return application.config.pretty_date_format === 'relative'
  22. ? 'relative'
  23. : 'absolute'
  24. })
  25. const outputAbsoluteDate = computed(() => {
  26. if (props.absoluteFormat === 'date') return i18n.date(props.dateTime)
  27. return i18n.dateTime(props.dateTime)
  28. })
  29. </script>
  30. <template>
  31. <time v-if="outputFormat === 'absolute'" data-test-id="date-time-absolute">
  32. {{ outputAbsoluteDate }}
  33. </time>
  34. <time
  35. v-else
  36. :title="i18n.dateTime(dateTime)"
  37. :datetime="dateTime"
  38. data-test-id="date-time-relative"
  39. >
  40. {{ i18n.relativeDateTime(dateTime) }}
  41. </time>
  42. </template>