127 lines
4.7 KiB
Vue
127 lines
4.7 KiB
Vue
<script setup>
|
|
import { computed, onBeforeUnmount, onMounted, ref } from 'vue';
|
|
import { MessagesSquare } from '@lucide/vue';
|
|
import { AppLayout } from '@inhaus/ui';
|
|
import ConversationInfo from '../components/ConversationInfo.vue';
|
|
import ConversationList from '../components/ConversationList.vue';
|
|
import NewChatDialog from '../components/NewChatDialog.vue';
|
|
import Thread from '../components/Thread.vue';
|
|
import { announceUnread, api } from '../lib/api';
|
|
import { createSync } from '../lib/sync';
|
|
|
|
/**
|
|
* Nachrichten: links die Gespräche, rechts der Verlauf. Auf dem Handy nacheinander.
|
|
*/
|
|
const props = defineProps({
|
|
me: { type: Object, required: true },
|
|
conversations: { type: Array, required: true },
|
|
activeId: { type: Number, default: null },
|
|
canCreateGroups: { type: Boolean, default: false },
|
|
realtime: { type: Object, default: null },
|
|
reactions: { type: Array, required: true },
|
|
maxUploadMb: { type: Number, default: 25 },
|
|
});
|
|
|
|
const list = ref(props.conversations);
|
|
const activeId = ref(props.activeId);
|
|
const typing = ref([]);
|
|
const thread = ref(null);
|
|
const dialog = ref(null); // 'chat' | 'group'
|
|
const infoOpen = ref(false);
|
|
|
|
const active = computed(() => list.value.find((c) => c.id === activeId.value) ?? null);
|
|
|
|
function open(id, push = true) {
|
|
activeId.value = id;
|
|
typing.value = [];
|
|
|
|
if (push && location.pathname !== `/chat/${id ?? ''}`.replace(/\/$/, '')) {
|
|
history.pushState({ chat: id }, '', id ? `/chat/${id}` : '/chat');
|
|
}
|
|
|
|
sync?.now();
|
|
}
|
|
|
|
const onPop = () => open(Number(location.pathname.split('/')[2]) || null, false);
|
|
|
|
function setList(conversations) {
|
|
if (!conversations) return;
|
|
|
|
list.value = conversations;
|
|
announceUnread(conversations.reduce((sum, c) => sum + (c.muted ? 0 : c.unread), 0));
|
|
}
|
|
|
|
let sync = null;
|
|
|
|
onMounted(() => {
|
|
window.addEventListener('popstate', onPop);
|
|
sync = createSync({
|
|
realtime: props.realtime,
|
|
userId: props.me.id,
|
|
openId: () => activeId.value,
|
|
lastId: () => thread.value?.lastId() ?? 0,
|
|
onUpdate(data) {
|
|
setList(data.conversations);
|
|
typing.value = data.typing ?? [];
|
|
thread.value?.merge(data.messages ?? []);
|
|
announceUnread(data.unread);
|
|
},
|
|
});
|
|
});
|
|
onBeforeUnmount(() => {
|
|
window.removeEventListener('popstate', onPop);
|
|
sync?.stop();
|
|
});
|
|
|
|
function created(data) {
|
|
setList(data.conversations);
|
|
open(data.id);
|
|
}
|
|
|
|
async function refresh() {
|
|
setList((await api.conversations()).conversations);
|
|
}
|
|
|
|
function onRead({ id, unread }) {
|
|
const c = list.value.find((x) => x.id === id);
|
|
|
|
if (c) c.unread = 0;
|
|
announceUnread(unread);
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<AppLayout title="Nachrichten" width="app" :header="false">
|
|
<div class="ih:-mx-4 ih:-my-6 ih:flex ih:h-[calc(100dvh-4rem)] ih:overflow-hidden ih:border-y ih:bg-card ih:lg:mx-0 ih:lg:my-0 ih:lg:h-[calc(100dvh-8.5rem)] ih:lg:rounded-xl ih:lg:border ih:lg:shadow-xs">
|
|
<aside class="ih:w-full ih:shrink-0 ih:border-r ih:lg:block ih:lg:w-80 ih:xl:w-96" :class="active ? 'ih:hidden' : 'ih:block'">
|
|
<ConversationList :conversations="list" :active-id="activeId" :can-create-groups="canCreateGroups" @open="open" @new-chat="dialog = 'chat'" @new-group="dialog = 'group'" />
|
|
</aside>
|
|
|
|
<main class="ih:min-w-0 ih:flex-1 ih:lg:block" :class="active ? 'ih:block' : 'ih:hidden'">
|
|
<Thread
|
|
v-if="active"
|
|
ref="thread"
|
|
:conversation="active"
|
|
:me="me"
|
|
:reactions="reactions"
|
|
:typing="typing"
|
|
:max-upload-mb="maxUploadMb"
|
|
@back="open(null)"
|
|
@info="infoOpen = true"
|
|
@changed="refresh"
|
|
@read="onRead"
|
|
/>
|
|
<div v-else class="ih:grid ih:h-full ih:place-items-center ih:p-8 ih:text-center">
|
|
<div>
|
|
<MessagesSquare class="ih:mx-auto ih:size-12 ih:text-muted-foreground/60" :stroke-width="1.5" />
|
|
<p class="ih:mt-3 ih:font-medium">Wählen Sie ein Gespräch</p>
|
|
<p class="ih:mt-1 ih:text-sm ih:text-muted-foreground">oder beginnen Sie eine neue Nachricht.</p>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
</div>
|
|
|
|
<NewChatDialog :open="dialog !== null" :group="dialog === 'group'" :me="me" @update:open="(v) => !v && (dialog = null)" @created="created" />
|
|
<ConversationInfo v-if="active" v-model:open="infoOpen" :conversation="active" :me="me" @updated="setList" @left="open(null)" />
|
|
</AppLayout>
|
|
</template>
|