Nachrichten-Plugin 1.0.0: Direktnachrichten, Gruppen, Anhänge, GIFs, Emojis
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
commit
2844452002
55 changed files with 5448 additions and 0 deletions
308
resources/js/components/Thread.vue
Normal file
308
resources/js/components/Thread.vue
Normal file
|
|
@ -0,0 +1,308 @@
|
|||
<script setup>
|
||||
import { computed, nextTick, onMounted, ref, watch } from 'vue';
|
||||
import { ArrowDown, ArrowLeft, BellOff, Info, LoaderCircle, Upload } from '@lucide/vue';
|
||||
import { toast } from '@inhaus/ui';
|
||||
import ChatAvatar from './ChatAvatar.vue';
|
||||
import Composer from './Composer.vue';
|
||||
import MessageBubble from './MessageBubble.vue';
|
||||
import { api } from '../lib/api';
|
||||
import { dayLabel, sameDay } from '../lib/format';
|
||||
|
||||
const props = defineProps({
|
||||
conversation: { type: Object, required: true },
|
||||
me: { type: Object, required: true },
|
||||
reactions: { type: Array, required: true },
|
||||
maxUploadMb: { type: Number, default: 25 },
|
||||
typing: { type: Array, default: () => [] },
|
||||
});
|
||||
const emit = defineEmits(['back', 'info', 'changed', 'read']);
|
||||
|
||||
const messages = ref([]);
|
||||
const more = ref(false);
|
||||
const loading = ref(true);
|
||||
const loadingOlder = ref(false);
|
||||
const sending = ref(false);
|
||||
const replyTo = ref(null);
|
||||
const editing = ref(null);
|
||||
const scroller = ref(null);
|
||||
const composer = ref(null);
|
||||
const atBottom = ref(true);
|
||||
const newBelow = ref(0);
|
||||
const dragging = ref(false);
|
||||
// Erste ungelesene Nachricht beim Öffnen – dort steht der Trenner „Neu“
|
||||
const firstUnreadId = ref(null);
|
||||
|
||||
const isGroup = computed(() => props.conversation.type === 'group');
|
||||
const lastId = () => messages.value.at(-1)?.id ?? 0;
|
||||
const lastOwnId = computed(() => [...messages.value].reverse().find((m) => m.mine && !m.deleted && m.kind !== 'system')?.id);
|
||||
|
||||
async function load() {
|
||||
loading.value = true;
|
||||
messages.value = [];
|
||||
replyTo.value = editing.value = null;
|
||||
|
||||
try {
|
||||
const data = await api.messages(props.conversation.id);
|
||||
messages.value = data.messages;
|
||||
more.value = data.more;
|
||||
const readId = props.conversation.last_read_id ?? 0;
|
||||
firstUnreadId.value = props.conversation.unread ? data.messages.find((m) => m.id > readId && !m.mine && m.kind !== 'system')?.id ?? null : null;
|
||||
} finally {
|
||||
loading.value = false;
|
||||
await nextTick();
|
||||
scrollTo(firstUnreadId.value ? document.getElementById(`nachricht-${firstUnreadId.value}`) : null);
|
||||
markRead();
|
||||
composer.value?.focus();
|
||||
}
|
||||
}
|
||||
|
||||
async function loadOlder() {
|
||||
if (!more.value || loadingOlder.value || !messages.value.length) return;
|
||||
|
||||
loadingOlder.value = true;
|
||||
const el = scroller.value;
|
||||
const before = el.scrollHeight;
|
||||
|
||||
try {
|
||||
const data = await api.messages(props.conversation.id, messages.value[0].id);
|
||||
messages.value = [...data.messages, ...messages.value];
|
||||
more.value = data.more;
|
||||
await nextTick();
|
||||
el.scrollTop = el.scrollHeight - before; // Position halten
|
||||
} finally {
|
||||
loadingOlder.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function scrollTo(element) {
|
||||
const el = scroller.value;
|
||||
|
||||
if (!el) return;
|
||||
|
||||
element ? element.scrollIntoView({ block: 'center' }) : (el.scrollTop = el.scrollHeight);
|
||||
newBelow.value = 0;
|
||||
}
|
||||
|
||||
function onScroll() {
|
||||
const el = scroller.value;
|
||||
atBottom.value = el.scrollHeight - el.scrollTop - el.clientHeight < 80;
|
||||
|
||||
if (atBottom.value) {
|
||||
newBelow.value = 0;
|
||||
markRead();
|
||||
}
|
||||
|
||||
if (el.scrollTop < 120) loadOlder();
|
||||
}
|
||||
|
||||
let readTimer;
|
||||
function markRead() {
|
||||
clearTimeout(readTimer);
|
||||
readTimer = setTimeout(async () => {
|
||||
const last = [...messages.value].reverse().find((m) => !m.mine);
|
||||
|
||||
if (!last || document.visibilityState !== 'visible' || !atBottom.value || !props.conversation.unread) return;
|
||||
|
||||
const data = await api.read(props.conversation.id, lastId());
|
||||
emit('read', { id: props.conversation.id, unread: data.unread });
|
||||
}, 400);
|
||||
}
|
||||
|
||||
/** Neue bzw. geänderte Nachrichten aus der Abfrage einarbeiten */
|
||||
async function merge(incoming) {
|
||||
const mine = incoming.filter((m) => m.conversation_id === props.conversation.id);
|
||||
|
||||
if (!mine.length) return;
|
||||
|
||||
const byId = new Map(messages.value.map((m) => [m.id, m]));
|
||||
let added = 0;
|
||||
|
||||
for (const message of mine) {
|
||||
if (byId.has(message.id)) Object.assign(byId.get(message.id), message);
|
||||
else if (message.id > lastId()) {
|
||||
messages.value.push(message);
|
||||
added += message.mine ? 0 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (!added) return;
|
||||
|
||||
const stick = atBottom.value;
|
||||
await nextTick();
|
||||
|
||||
if (stick) {
|
||||
scrollTo();
|
||||
markRead();
|
||||
} else {
|
||||
newBelow.value += added;
|
||||
}
|
||||
}
|
||||
|
||||
async function send(payload) {
|
||||
sending.value = true;
|
||||
|
||||
try {
|
||||
const { message } = await api.send(props.conversation.id, { ...payload, replyTo: replyTo.value?.id });
|
||||
replyTo.value = null;
|
||||
|
||||
if (!messages.value.some((m) => m.id === message.id)) messages.value.push(message);
|
||||
await nextTick();
|
||||
scrollTo();
|
||||
emit('changed');
|
||||
} catch (error) {
|
||||
toast.error(error.message);
|
||||
} finally {
|
||||
sending.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function saveEdit(body) {
|
||||
const message = editing.value;
|
||||
|
||||
try {
|
||||
await api.edit(message.id, body);
|
||||
Object.assign(message, { body, edited: true });
|
||||
editing.value = null;
|
||||
} catch (error) {
|
||||
toast.error(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(message) {
|
||||
if (!window.confirm('Nachricht für alle löschen?')) return;
|
||||
|
||||
await api.remove(message.id);
|
||||
Object.assign(message, { deleted: true, body: null, gif_url: null, attachments: [], reactions: [] });
|
||||
emit('changed');
|
||||
}
|
||||
|
||||
async function react(message, emoji) {
|
||||
// Sofort anzeigen, der Server bestätigt mit der nächsten Abfrage
|
||||
const existing = message.reactions.find((r) => r.emoji === emoji);
|
||||
|
||||
if (existing?.mine) {
|
||||
existing.count--;
|
||||
existing.mine = false;
|
||||
message.reactions = message.reactions.filter((r) => r.count > 0);
|
||||
} else if (existing) {
|
||||
existing.count++;
|
||||
existing.mine = true;
|
||||
} else {
|
||||
message.reactions.push({ emoji, count: 1, mine: true });
|
||||
}
|
||||
|
||||
await api.react(message.id, emoji).catch((error) => toast.error(error.message));
|
||||
}
|
||||
|
||||
function jump(id) {
|
||||
const element = document.getElementById(`nachricht-${id}`);
|
||||
|
||||
if (element) {
|
||||
element.scrollIntoView({ block: 'center', behavior: 'smooth' });
|
||||
element.classList.add('ih:animate-pulse');
|
||||
setTimeout(() => element.classList.remove('ih:animate-pulse'), 1500);
|
||||
}
|
||||
}
|
||||
|
||||
function onDrop(event) {
|
||||
dragging.value = false;
|
||||
const files = [...(event.dataTransfer?.files ?? [])];
|
||||
files.length && composer.value?.addFiles(files);
|
||||
}
|
||||
|
||||
const showAuthor = (m, i) => {
|
||||
const prev = messages.value[i - 1];
|
||||
|
||||
return !prev || prev.kind === 'system' || prev.user?.id !== m.user?.id || !sameDay(prev.created_at, m.created_at) || new Date(m.created_at) - new Date(prev.created_at) > 5 * 60000;
|
||||
};
|
||||
const showAvatar = (m, i) => {
|
||||
const next = messages.value[i + 1];
|
||||
|
||||
return !next || next.kind === 'system' || next.user?.id !== m.user?.id || !sameDay(next.created_at, m.created_at);
|
||||
};
|
||||
|
||||
const subtitle = computed(() => {
|
||||
if (props.typing.length) return `${props.typing.join(', ')} ${props.typing.length === 1 ? 'schreibt' : 'schreiben'} …`;
|
||||
if (isGroup.value) return props.conversation.members.map((m) => m.first_name || m.name).join(', ');
|
||||
|
||||
return props.conversation.person?.job_title ?? '';
|
||||
});
|
||||
|
||||
watch(() => props.conversation.id, load);
|
||||
onMounted(load);
|
||||
|
||||
defineExpose({ merge, lastId });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section
|
||||
class="ih:relative ih:flex ih:h-full ih:min-h-0 ih:flex-col"
|
||||
:aria-label="`Gespräch mit ${conversation.name}`"
|
||||
@dragover.prevent="dragging = true"
|
||||
@dragleave.self="dragging = false"
|
||||
@drop.prevent="onDrop"
|
||||
>
|
||||
<header class="ih:flex ih:items-center ih:gap-3 ih:border-b ih:px-3 ih:py-2.5">
|
||||
<button type="button" class="ih:grid ih:size-9 ih:place-items-center ih:rounded-md ih:text-muted-foreground ih:hover:bg-accent ih:lg:hidden" aria-label="Zurück zur Übersicht" @click="emit('back')"><ArrowLeft class="ih:size-5" /></button>
|
||||
<ChatAvatar :conversation="conversation" />
|
||||
<div class="ih:min-w-0 ih:flex-1">
|
||||
<h2 class="ih:flex ih:items-center ih:gap-1.5 ih:truncate ih:font-semibold">
|
||||
<a v-if="conversation.person" :href="`/personen/${conversation.person.id}`" class="ih:truncate ih:hover:underline">{{ conversation.name }}</a>
|
||||
<span v-else class="ih:truncate">{{ conversation.name }}</span>
|
||||
<BellOff v-if="conversation.muted" class="ih:size-3.5 ih:shrink-0 ih:text-muted-foreground" aria-label="Stummgeschaltet" />
|
||||
</h2>
|
||||
<p class="ih:truncate ih:text-xs" :class="typing.length ? 'ih:text-brand-text' : 'ih:text-muted-foreground'" aria-live="polite">{{ subtitle }}</p>
|
||||
</div>
|
||||
<button type="button" class="ih:grid ih:size-9 ih:place-items-center ih:rounded-md ih:text-muted-foreground ih:hover:bg-accent" :aria-label="isGroup ? 'Gruppe: Mitglieder und Einstellungen' : 'Einstellungen zum Gespräch'" @click="emit('info')"><Info class="ih:size-5" /></button>
|
||||
</header>
|
||||
|
||||
<div ref="scroller" class="ih:min-h-0 ih:flex-1 ih:overflow-y-auto ih:bg-muted/30 ih:px-3 ih:py-4 ih:sm:px-5" role="log" aria-live="polite" aria-relevant="additions" @scroll.passive="onScroll">
|
||||
<div v-if="loading" class="ih:grid ih:h-full ih:place-items-center"><LoaderCircle class="ih:size-6 ih:animate-spin ih:text-muted-foreground" /></div>
|
||||
<template v-else>
|
||||
<div v-if="loadingOlder" class="ih:flex ih:justify-center ih:py-2"><LoaderCircle class="ih:size-4 ih:animate-spin ih:text-muted-foreground" /></div>
|
||||
<p v-if="!messages.length" class="ih:py-10 ih:text-center ih:text-sm ih:text-muted-foreground">Schreiben Sie die erste Nachricht.</p>
|
||||
<template v-for="(m, i) in messages" :key="m.id">
|
||||
<div v-if="i === 0 || !sameDay(messages[i - 1].created_at, m.created_at)" class="ih:my-4 ih:flex ih:justify-center">
|
||||
<span class="ih:rounded-full ih:bg-card ih:px-3 ih:py-1 ih:text-xs ih:font-medium ih:text-muted-foreground ih:shadow-xs">{{ dayLabel(m.created_at) }}</span>
|
||||
</div>
|
||||
<div v-if="m.id === firstUnreadId" class="ih:my-3 ih:flex ih:items-center ih:gap-3 ih:text-xs ih:font-semibold ih:text-brand-text">
|
||||
<span class="ih:h-px ih:flex-1 ih:bg-brand/40" />Neu<span class="ih:h-px ih:flex-1 ih:bg-brand/40" />
|
||||
</div>
|
||||
<MessageBubble
|
||||
:message="m"
|
||||
:group="isGroup"
|
||||
:show-author="showAuthor(m, i)"
|
||||
:show-avatar="showAvatar(m, i)"
|
||||
:reactions="reactions"
|
||||
:read="m.id === lastOwnId && !!conversation.others_read_id && conversation.others_read_id >= m.id"
|
||||
@react="react(m, $event)"
|
||||
@reply="replyTo = m; editing = null"
|
||||
@edit="editing = m; replyTo = null"
|
||||
@delete="remove(m)"
|
||||
@jump="jump"
|
||||
/>
|
||||
</template>
|
||||
</template>
|
||||
</div>
|
||||
|
||||
<button v-if="newBelow" type="button" class="ih:absolute ih:bottom-28 ih:left-1/2 ih:z-10 ih:flex ih:-translate-x-1/2 ih:items-center ih:gap-1.5 ih:rounded-full ih:bg-brand ih:px-3 ih:py-1.5 ih:text-xs ih:font-medium ih:text-brand-foreground ih:shadow-lg" @click="scrollTo(); markRead()">
|
||||
<ArrowDown class="ih:size-3.5" /> {{ newBelow }} {{ newBelow === 1 ? 'neue Nachricht' : 'neue Nachrichten' }}
|
||||
</button>
|
||||
|
||||
<Composer
|
||||
ref="composer"
|
||||
:reply-to="replyTo"
|
||||
:editing="editing"
|
||||
:sending="sending"
|
||||
:max-upload-mb="maxUploadMb"
|
||||
@send="send"
|
||||
@save-edit="saveEdit"
|
||||
@cancel="replyTo = null; editing = null"
|
||||
@typing="api.typing(conversation.id).catch(() => {})"
|
||||
/>
|
||||
|
||||
<div v-if="dragging" class="ih:pointer-events-none ih:absolute ih:inset-2 ih:z-20 ih:grid ih:place-items-center ih:rounded-xl ih:border-2 ih:border-dashed ih:border-brand ih:bg-brand-soft/80 ih:text-brand-text">
|
||||
<span class="ih:flex ih:items-center ih:gap-2 ih:font-medium"><Upload class="ih:size-5" /> Dateien hier ablegen</span>
|
||||
</div>
|
||||
</section>
|
||||
</template>
|
||||
Loading…
Add table
Add a link
Reference in a new issue