CommonDateTime.vue 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { computed } from 'vue'
  4. import { i18n } from '#shared/i18n.ts'
  5. import { useApplicationStore } from '#shared/stores/application.ts'
  6. import type { DateTimeType, DateTimeAbsoluteFormat } from './types.ts'
  7. import type { ComputedRef } from 'vue'
  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. v-tooltip="i18n.dateTime(dateTime)"
  37. :datetime="dateTime"
  38. data-test-id="date-time-relative"
  39. >
  40. {{ i18n.relativeDateTime(dateTime) }}
  41. </time>
  42. </template>