pleroma-fe/src/components/timeago/timeago.vue

138 lines
3.7 KiB
Vue
Raw Normal View History

2019-06-18 20:28:31 +00:00
<template>
2019-07-05 10:17:44 +03:00
<time
:datetime="time"
:title="localeDateString"
>
{{ relativeOrAbsoluteTimeString }}
2019-06-18 20:28:31 +00:00
</time>
</template>
<script>
import * as DateUtils from 'src/services/date_utils/date_utils.js'
import localeService from 'src/services/locale/locale.service.js'
2019-06-18 20:28:31 +00:00
export default {
name: 'Timeago',
props: ['time', 'autoUpdate', 'longFormat', 'nowThreshold', 'templateKey'],
2026-01-06 16:22:52 +02:00
data() {
2019-06-18 20:28:31 +00:00
return {
relativeTimeMs: 0,
2019-06-18 20:28:31 +00:00
relativeTime: { key: 'time.now', num: 0 },
2026-01-06 16:22:52 +02:00
interval: null,
2019-06-18 20:28:31 +00:00
}
},
computed: {
2026-01-06 16:22:52 +02:00
shouldUseAbsoluteTimeFormat() {
if (!this.$store.getters.mergedConfig.useAbsoluteTimeFormat) {
return false
}
2026-01-06 16:22:52 +02:00
return (
DateUtils.durationStrToMs(
this.$store.getters.mergedConfig.absoluteTimeFormatMinAge,
) <= this.relativeTimeMs
)
},
2026-01-06 16:22:52 +02:00
time12hFormat() {
2025-01-27 12:00:28 +02:00
return this.$store.getters.mergedConfig.absoluteTimeFormat12h === '12h'
},
2026-01-06 16:22:52 +02:00
browserLocale() {
return localeService.internalToBrowserLocale(this.$i18n.locale)
},
2026-01-06 16:22:52 +02:00
timeAsDate() {
2019-06-18 20:28:31 +00:00
return typeof this.time === 'string'
? new Date(Date.parse(this.time))
: this.time
},
2026-01-06 16:22:52 +02:00
localeDateString() {
return this.timeAsDate.toLocaleString(this.browserLocale)
},
2026-01-06 16:22:52 +02:00
relativeTimeString() {
const timeString = this.$i18n.t(
this.relativeTime.key,
[this.relativeTime.num],
this.relativeTime.num,
)
2026-01-06 16:22:52 +02:00
if (
typeof this.templateKey === 'string' &&
this.relativeTime.key !== 'time.now'
) {
return this.$i18n.t(this.templateKey, [timeString])
}
return timeString
},
2026-01-06 16:22:52 +02:00
absoluteTimeString() {
if (this.longFormat) {
return this.localeDateString
}
const now = new Date()
const formatter = (() => {
if (DateUtils.isSameDay(this.timeAsDate, now)) {
return new Intl.DateTimeFormat(this.browserLocale, {
minute: 'numeric',
2025-01-27 12:00:28 +02:00
hour: 'numeric',
2026-01-06 16:22:52 +02:00
hour12: this.time12hFormat,
})
} else if (DateUtils.isSameMonth(this.timeAsDate, now)) {
return new Intl.DateTimeFormat(this.browserLocale, {
2024-11-13 09:19:17 +00:00
month: 'short',
2025-01-27 12:00:28 +02:00
day: 'numeric',
2026-01-06 16:22:52 +02:00
hour12: this.time12hFormat,
})
} else if (DateUtils.isSameYear(this.timeAsDate, now)) {
return new Intl.DateTimeFormat(this.browserLocale, {
month: 'short',
2025-01-27 12:00:28 +02:00
day: 'numeric',
2026-01-06 16:22:52 +02:00
hour12: this.time12hFormat,
})
} else {
return new Intl.DateTimeFormat(this.browserLocale, {
year: 'numeric',
2025-01-27 12:00:28 +02:00
month: 'short',
2026-01-06 16:22:52 +02:00
hour12: this.time12hFormat,
})
}
})()
return formatter.format(this.timeAsDate)
},
2026-01-06 16:22:52 +02:00
relativeOrAbsoluteTimeString() {
return this.shouldUseAbsoluteTimeFormat
? this.absoluteTimeString
: this.relativeTimeString
},
2019-06-18 20:28:31 +00:00
},
watch: {
2026-01-06 16:22:52 +02:00
time(newVal, oldVal) {
if (oldVal !== newVal) {
clearTimeout(this.interval)
this.refreshRelativeTimeObject()
}
2026-01-06 16:22:52 +02:00
},
},
2026-01-06 16:22:52 +02:00
created() {
this.refreshRelativeTimeObject()
},
2026-01-06 16:22:52 +02:00
unmounted() {
clearTimeout(this.interval)
},
2019-06-18 20:28:31 +00:00
methods: {
2026-01-06 16:22:52 +02:00
refreshRelativeTimeObject() {
const nowThreshold =
typeof this.nowThreshold === 'number' ? this.nowThreshold : 1
this.relativeTimeMs = DateUtils.relativeTimeMs(this.time)
2019-06-18 20:28:31 +00:00
this.relativeTime = this.longFormat
? DateUtils.relativeTime(this.time, nowThreshold)
: DateUtils.relativeTimeShort(this.time, nowThreshold)
if (this.autoUpdate) {
this.interval = setTimeout(
this.refreshRelativeTimeObject,
2026-01-06 16:22:52 +02:00
1000 * this.autoUpdate,
2019-06-18 20:28:31 +00:00
)
}
2026-01-06 16:22:52 +02:00
},
},
2019-06-18 20:28:31 +00:00
}
2019-07-05 10:17:44 +03:00
</script>