CommonDateTime.vue 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. <slot />
  33. {{ outputAbsoluteDate }}
  34. </time>
  35. <time
  36. v-else
  37. v-tooltip="i18n.dateTime(dateTime)"
  38. :datetime="dateTime"
  39. data-test-id="date-time-relative"
  40. >
  41. <slot name="prefix" />
  42. {{ i18n.relativeDateTime(dateTime) }}
  43. </time>
  44. </template>