124 lines
8.5 KiB
Vue
124 lines
8.5 KiB
Vue
<script setup>
|
|
import { computed } from 'vue';
|
|
import { CornerUpLeft, Download, Pencil, SmilePlus, Trash2 } from '@lucide/vue';
|
|
import { FileIcon, PersonAvatar } from '@inhaus/ui';
|
|
import { fileSize, linkify, messageTime, onlyEmoji } from '../lib/format';
|
|
|
|
const props = defineProps({
|
|
message: { type: Object, required: true },
|
|
showAuthor: { type: Boolean, default: true },
|
|
showAvatar: { type: Boolean, default: true },
|
|
group: { type: Boolean, default: false },
|
|
reactions: { type: Array, required: true },
|
|
read: { type: Boolean, default: false },
|
|
});
|
|
const emit = defineEmits(['react', 'reply', 'edit', 'delete', 'jump']);
|
|
|
|
const m = computed(() => props.message);
|
|
const bigEmoji = computed(() => !m.value.attachments.length && !m.value.reply_to && onlyEmoji(m.value.body));
|
|
const images = computed(() => m.value.attachments.filter((a) => a.image));
|
|
const files = computed(() => m.value.attachments.filter((a) => !a.image));
|
|
</script>
|
|
|
|
<template>
|
|
<!-- Systemmeldung (Gruppe erstellt, Person hinzugefügt …) -->
|
|
<div v-if="m.kind === 'system'" class="ih:my-3 ih:text-center">
|
|
<span class="ih:inline-block ih:rounded-full ih:bg-muted ih:px-3 ih:py-1 ih:text-xs ih:text-muted-foreground">{{ m.body }}</span>
|
|
</div>
|
|
|
|
<div v-else :id="`nachricht-${m.id}`" class="ih:group ih:flex ih:items-end ih:gap-2" :class="[m.mine ? 'ih:flex-row-reverse' : '', showAuthor ? 'ih:mt-3' : 'ih:mt-0.5']">
|
|
<div v-if="group && !m.mine" class="ih:w-8 ih:shrink-0">
|
|
<PersonAvatar v-if="showAvatar && m.user" :person="m.user" class="ih:size-8 ih:text-[11px]" />
|
|
</div>
|
|
|
|
<div class="ih:flex ih:max-w-[min(34rem,80%)] ih:min-w-0 ih:flex-col" :class="m.mine ? 'ih:items-end' : 'ih:items-start'">
|
|
<span v-if="group && !m.mine && showAuthor" class="ih:mb-0.5 ih:px-3 ih:text-xs ih:font-medium ih:text-muted-foreground">{{ m.user?.name ?? 'Gelöschtes Konto' }}</span>
|
|
|
|
<div class="ih:relative ih:flex ih:items-center ih:gap-1" :class="m.mine ? 'ih:flex-row-reverse' : ''">
|
|
<!-- Blase -->
|
|
<div
|
|
v-if="m.deleted"
|
|
class="ih:rounded-2xl ih:border ih:border-dashed ih:px-3 ih:py-2 ih:text-sm ih:text-muted-foreground ih:italic"
|
|
>Nachricht gelöscht</div>
|
|
<div
|
|
v-else
|
|
class="ih:min-w-0 ih:overflow-hidden ih:text-[15px] ih:leading-snug"
|
|
:class="bigEmoji || m.kind === 'gif' ? '' : [m.mine ? 'ih:rounded-2xl ih:rounded-br-md ih:bg-brand ih:text-brand-foreground' : 'ih:rounded-2xl ih:rounded-bl-md ih:border ih:bg-card', images.length && !m.body ? '' : 'ih:px-3 ih:py-2']"
|
|
>
|
|
<button
|
|
v-if="m.reply_to"
|
|
type="button"
|
|
class="ih:mb-1.5 ih:block ih:w-full ih:rounded-md ih:border-l-3 ih:px-2 ih:py-1 ih:text-left ih:text-[13px]"
|
|
:class="m.mine ? 'ih:border-white/60 ih:bg-white/15' : 'ih:border-brand ih:bg-muted'"
|
|
@click="emit('jump', m.reply_to.id)"
|
|
>
|
|
<span class="ih:block ih:font-semibold">{{ m.reply_to.name }}</span>
|
|
<span class="ih:line-clamp-2 ih:opacity-80">{{ m.reply_to.text }}</span>
|
|
</button>
|
|
|
|
<img v-if="m.kind === 'gif'" :src="m.gif_url" alt="GIF" loading="lazy" class="ih:max-h-60 ih:rounded-2xl" />
|
|
|
|
<div v-if="images.length" class="ih:grid ih:gap-0.5" :class="images.length > 1 ? 'ih:grid-cols-2' : ''">
|
|
<a v-for="img in images" :key="img.id" :href="img.url" target="_blank" rel="noopener" class="ih:block ih:overflow-hidden ih:bg-muted" :title="img.name">
|
|
<img :src="img.url" :alt="img.name" loading="lazy" class="ih:block ih:max-h-72 ih:w-full ih:object-cover" :style="img.width ? { aspectRatio: `${img.width} / ${img.height}` } : {}" />
|
|
</a>
|
|
</div>
|
|
|
|
<a
|
|
v-for="file in files"
|
|
:key="file.id"
|
|
:href="file.url"
|
|
class="ih:my-1 ih:flex ih:items-center ih:gap-2.5 ih:rounded-lg ih:p-2 ih:no-underline"
|
|
:class="m.mine ? 'ih:bg-white/15 ih:hover:bg-white/25' : 'ih:bg-muted ih:hover:bg-accent'"
|
|
>
|
|
<FileIcon :extension="file.name.split('.').pop()" class="ih:shrink-0" />
|
|
<span class="ih:min-w-0 ih:flex-1">
|
|
<span class="ih:block ih:truncate ih:text-sm ih:font-medium">{{ file.name }}</span>
|
|
<span class="ih:block ih:text-xs ih:opacity-75">{{ fileSize(file.size) }}</span>
|
|
</span>
|
|
<Download class="ih:size-4 ih:shrink-0 ih:opacity-75" />
|
|
</a>
|
|
|
|
<p v-if="m.body" class="ih:break-words ih:whitespace-pre-wrap" :class="[bigEmoji ? 'ih:text-4xl ih:leading-tight' : '', images.length ? 'ih:px-3 ih:py-2' : '']">
|
|
<template v-for="(part, i) in linkify(m.body)" :key="i">
|
|
<a v-if="part.href" :href="part.href" target="_blank" rel="noopener noreferrer" class="ih:underline ih:underline-offset-2">{{ part.text }}</a>
|
|
<template v-else>{{ part.text }}</template>
|
|
</template>
|
|
</p>
|
|
</div>
|
|
|
|
<!-- Aktionen beim Überfahren bzw. Fokus -->
|
|
<div v-if="!m.deleted" class="ih:flex ih:shrink-0 ih:items-center ih:opacity-0 ih:transition-opacity ih:group-hover:opacity-100 ih:focus-within:opacity-100 ih:[@media(hover:none)]:opacity-100">
|
|
<div class="ih:group/react ih:relative">
|
|
<button type="button" class="ih:grid ih:size-7 ih:place-items-center ih:rounded-full ih:text-muted-foreground ih:hover:bg-muted" aria-label="Reagieren"><SmilePlus class="ih:size-4" /></button>
|
|
<div class="ih:absolute ih:bottom-full ih:z-20 ih:hidden ih:gap-0.5 ih:rounded-full ih:border ih:bg-popover ih:p-1 ih:shadow-md ih:group-focus-within/react:flex ih:group-hover/react:flex" :class="m.mine ? 'ih:right-0' : 'ih:left-0'">
|
|
<button v-for="emoji in reactions" :key="emoji" type="button" class="ih:grid ih:size-8 ih:place-items-center ih:rounded-full ih:text-lg ih:hover:bg-accent" :aria-label="`Mit ${emoji} reagieren`" @click="emit('react', emoji)">{{ emoji }}</button>
|
|
</div>
|
|
</div>
|
|
<button type="button" class="ih:grid ih:size-7 ih:place-items-center ih:rounded-full ih:text-muted-foreground ih:hover:bg-muted" aria-label="Antworten" @click="emit('reply')"><CornerUpLeft class="ih:size-4" /></button>
|
|
<template v-if="m.mine">
|
|
<button v-if="m.body && m.kind === 'text'" type="button" class="ih:grid ih:size-7 ih:place-items-center ih:rounded-full ih:text-muted-foreground ih:hover:bg-muted" aria-label="Bearbeiten" @click="emit('edit')"><Pencil class="ih:size-4" /></button>
|
|
<button type="button" class="ih:grid ih:size-7 ih:place-items-center ih:rounded-full ih:text-muted-foreground ih:hover:bg-muted ih:hover:text-destructive" aria-label="Löschen" @click="emit('delete')"><Trash2 class="ih:size-4" /></button>
|
|
</template>
|
|
</div>
|
|
</div>
|
|
|
|
<div v-if="m.reactions.length" class="ih:relative ih:z-10 ih:-mt-1.5 ih:flex ih:flex-wrap ih:gap-1 ih:px-2">
|
|
<button
|
|
v-for="r in m.reactions"
|
|
:key="r.emoji"
|
|
type="button"
|
|
class="ih:flex ih:items-center ih:gap-1 ih:rounded-full ih:border ih:px-1.5 ih:text-xs ih:shadow-xs ih:tabular-nums"
|
|
:class="r.mine ? 'ih:border-brand ih:bg-brand-soft' : 'ih:bg-card'"
|
|
:aria-pressed="r.mine"
|
|
:aria-label="`${r.emoji} ${r.count}`"
|
|
@click="emit('react', r.emoji)"
|
|
><span class="ih:text-sm">{{ r.emoji }}</span>{{ r.count > 1 ? r.count : '' }}</button>
|
|
</div>
|
|
|
|
<span class="ih:mt-0.5 ih:px-1 ih:text-[11px] ih:text-muted-foreground ih:tabular-nums">
|
|
{{ messageTime(m.created_at) }}<template v-if="m.edited"> · bearbeitet</template><template v-if="m.mine && read"> · gelesen</template>
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</template>
|