thelounge/client/components/DateMarker.vue

67 lines
1.5 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 lang="ts">
import dayjs from "dayjs";
import calendar from "dayjs/plugin/calendar";
import {computed, defineComponent, onBeforeUnmount, onMounted, PropType} from "vue";
2020-03-16 17:58:40 +00:00
import eventbus from "../js/eventbus";
import type {ClientMessage} from "../js/types";
dayjs.extend(calendar);
export default defineComponent({
name: "DateMarker",
props: {
message: {
type: Object as PropType<ClientMessage>,
required: true,
},
focused: Boolean,
},
setup(props) {
const localeDate = computed(() => dayjs(props.message.time).format("D MMMM YYYY"));
const hoursPassed = () => {
return (Date.now() - Date.parse(props.message.time.toString())) / 3600000;
};
const dayChange = () => {
if (hoursPassed() >= 48) {
eventbus.off("daychange", dayChange);
}
};
const friendlyDate = () => {
// See http://momentjs.com/docs/#/displaying/calendar-time/
return dayjs(props.message.time).calendar(null, {
sameDay: "[Today]",
lastDay: "[Yesterday]",
lastWeek: "D MMMM YYYY",
sameElse: "D MMMM YYYY",
});
};
onMounted(() => {
if (hoursPassed() < 48) {
eventbus.on("daychange", dayChange);
}
});
onBeforeUnmount(() => {
eventbus.off("daychange", dayChange);
});
return {
localeDate,
friendlyDate,
};
},
});
</script>