2022-09-05 10:37:58 +00:00
|
|
|
<template>
|
|
|
|
<span class="time-ago">
|
2022-10-27 19:35:39 +00:00
|
|
|
<span>{{ timeAgo }}</span>
|
2022-09-05 10:37:58 +00:00
|
|
|
</span>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
const MINUTE_IN_MILLI_SECONDS = 60000;
|
|
|
|
const HOUR_IN_MILLI_SECONDS = MINUTE_IN_MILLI_SECONDS * 60;
|
|
|
|
const DAY_IN_MILLI_SECONDS = HOUR_IN_MILLI_SECONDS * 24;
|
|
|
|
|
|
|
|
import timeMixin from 'dashboard/mixins/time';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'TimeAgo',
|
|
|
|
mixins: [timeMixin],
|
|
|
|
props: {
|
|
|
|
isAutoRefreshEnabled: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
timestamp: {
|
|
|
|
type: [String, Date, Number],
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
2022-10-27 19:35:39 +00:00
|
|
|
timeAgo: this.dynamicTime(this.timestamp),
|
2022-09-05 10:37:58 +00:00
|
|
|
timer: null,
|
|
|
|
};
|
|
|
|
},
|
2022-10-27 19:35:39 +00:00
|
|
|
watch: {
|
|
|
|
timestamp() {
|
|
|
|
this.timeAgo = this.dynamicTime(this.timestamp);
|
2022-09-05 10:37:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
if (this.isAutoRefreshEnabled) {
|
|
|
|
this.createTimer();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
beforeDestroy() {
|
2022-10-27 19:35:39 +00:00
|
|
|
clearTimeout(this.timer);
|
2022-09-05 10:37:58 +00:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
createTimer() {
|
2022-10-27 19:35:39 +00:00
|
|
|
this.timer = setTimeout(() => {
|
|
|
|
this.timeAgo = this.dynamicTime(this.timestamp);
|
|
|
|
this.createTimer();
|
|
|
|
}, this.refreshTime());
|
2022-09-05 10:37:58 +00:00
|
|
|
},
|
2022-10-27 19:35:39 +00:00
|
|
|
refreshTime() {
|
|
|
|
const timeDiff = Date.now() - this.timestamp * 1000;
|
|
|
|
if (timeDiff > DAY_IN_MILLI_SECONDS) {
|
|
|
|
return DAY_IN_MILLI_SECONDS;
|
2022-09-05 10:37:58 +00:00
|
|
|
}
|
2022-10-27 19:35:39 +00:00
|
|
|
if (timeDiff > HOUR_IN_MILLI_SECONDS) {
|
|
|
|
return HOUR_IN_MILLI_SECONDS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MINUTE_IN_MILLI_SECONDS;
|
2022-09-05 10:37:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.time-ago {
|
|
|
|
color: var(--b-600);
|
|
|
|
font-size: var(--font-size-micro);
|
|
|
|
font-weight: var(--font-weight-normal);
|
|
|
|
line-height: var(--space-normal);
|
|
|
|
margin-left: auto;
|
|
|
|
}
|
|
|
|
</style>
|