hardlounge/client/components/DateMarker.vue

58 lines
1.2 KiB
Vue
Raw Normal View History

<template>
2019-07-17 09:33:59 +00:00
<div :aria-label="localeDate" class="date-marker-container tooltipped tooltipped-s">
<div class="date-marker">
2019-11-20 22:16:58 +00:00
<span :aria-label="friendlyDate()" class="date-marker-text" />
</div>
</div>
</template>
<script>
import dayjs from "dayjs";
import calendar from "dayjs/plugin/calendar";
2020-03-16 17:58:40 +00:00
import eventbus from "../js/eventbus";
dayjs.extend(calendar);
export default {
name: "DateMarker",
props: {
message: Object,
focused: Boolean,
},
computed: {
localeDate() {
return dayjs(this.message.time).format("D MMMM YYYY");
},
},
mounted() {
if (this.hoursPassed() < 48) {
2020-03-16 17:58:40 +00:00
eventbus.on("daychange", this.dayChange);
}
},
beforeDestroy() {
2020-03-16 17:58:40 +00:00
eventbus.off("daychange", this.dayChange);
},
methods: {
hoursPassed() {
return (Date.now() - Date.parse(this.message.time)) / 3600000;
},
dayChange() {
this.$forceUpdate();
if (this.hoursPassed() >= 48) {
2020-03-16 17:58:40 +00:00
eventbus.off("daychange", this.dayChange);
}
},
friendlyDate() {
// See http://momentjs.com/docs/#/displaying/calendar-time/
return dayjs(this.message.time).calendar(null, {
sameDay: "[Today]",
lastDay: "[Yesterday]",
lastWeek: "D MMMM YYYY",
sameElse: "D MMMM YYYY",
});
},
},
};
</script>