37 lines
1.6 KiB
Vue
37 lines
1.6 KiB
Vue
<script setup>
|
|
import { onBeforeUnmount, onMounted, ref } from 'vue';
|
|
import { MessageCircle } from '@lucide/vue';
|
|
import { http } from '@inhaus/ui';
|
|
|
|
/**
|
|
* Sprechblase neben der Glocke: Zahl der ungelesenen Nachrichten. Auf der Chat-Seite meldet
|
|
* der Chat selbst, sonst wird alle 30 Sekunden nachgefragt.
|
|
*/
|
|
const props = defineProps({ unread: { type: Number, default: 0 } });
|
|
const count = ref(props.unread);
|
|
let timer;
|
|
|
|
const update = (event) => (count.value = event.detail);
|
|
|
|
async function poll() {
|
|
if (document.visibilityState === 'visible' && !location.pathname.startsWith('/chat')) {
|
|
count.value = (await http.get('/chat/api/ungelesen').catch(() => ({ unread: count.value }))).unread;
|
|
}
|
|
}
|
|
|
|
onMounted(() => {
|
|
window.addEventListener('inhaus-chat:unread', update);
|
|
timer = setInterval(poll, 30000);
|
|
});
|
|
onBeforeUnmount(() => {
|
|
window.removeEventListener('inhaus-chat:unread', update);
|
|
clearInterval(timer);
|
|
});
|
|
</script>
|
|
|
|
<template>
|
|
<a href="/chat" class="ih:relative ih:grid ih:size-9 ih:place-items-center ih:rounded-md ih:text-muted-foreground ih:transition-colors ih:hover:bg-accent ih:hover:text-foreground" :aria-label="count ? `Nachrichten, ${count} ungelesen` : 'Nachrichten'" :title="count ? `${count} ungelesene Nachrichten` : 'Nachrichten'">
|
|
<MessageCircle class="ih:size-5" />
|
|
<span v-if="count" class="ih:absolute ih:top-0.5 ih:right-0.5 ih:grid ih:h-4 ih:min-w-4 ih:place-items-center ih:rounded-full ih:bg-brand ih:px-1 ih:text-[10px] ih:font-semibold ih:text-brand-foreground ih:tabular-nums" aria-hidden="true">{{ count > 99 ? '99+' : count }}</span>
|
|
</a>
|
|
</template>
|