1
0
mirror of https://gitlab.com/bramw/baserow.git synced 2024-11-21 23:37:55 +00:00
bramw_baserow/web-frontend/modules/core/mixins/timeAgo.js
2024-10-15 08:53:16 +00:00

50 lines
1.2 KiB
JavaScript

import { getHumanPeriodAgoCount } from '@baserow/modules/core/utils/date'
export default {
props: {
lastUpdated: {
type: String,
required: true,
},
},
data() {
return {
timeAgo: '',
refreshPeriod: null,
timeoutHandler: null,
}
},
mounted() {
this.updateTimeAgo()
},
beforeDestroy() {
clearTimeout(this.timeoutHandler)
},
methods: {
updateTimeAgo() {
const { period, count } = getHumanPeriodAgoCount(this.lastUpdated)
if (period === 'seconds' && count <= 5) {
this.timeAgo = this.$t('datetime.justNow')
this.refreshPeriod = 5 * 1000
} else if (period === 'seconds') {
this.timeAgo = this.$t('datetime.lessThanMinuteAgo')
this.refreshPeriod = 60 * 1000
} else {
this.timeAgo = this.$tc(`datetime.${period}Ago`, count)
this.refreshPeriod = period === 'minutes' ? 60 * 1000 : 3600 * 1000
}
if (this.refreshPeriod) {
this.scheduleNextUpdate()
}
},
scheduleNextUpdate() {
clearTimeout(this.timeoutHandler)
this.timeoutHandler = setTimeout(() => {
this.updateTimeAgo()
}, this.refreshPeriod)
},
},
}