inhaus-plugin-gruppen/resources/js/components/PostCard.vue
Friederich Loheide 5bdfb8a082 Gruppen-Plugin 1.0.0
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 17:17:10 +00:00

191 lines
12 KiB
Vue

<script setup>
import { computed, ref } from 'vue';
import { Download, Flag, MessageCircle, MoreHorizontal, Pencil, Pin, PinOff, Trash2 } from '@lucide/vue';
import { Button, DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, FileIcon, PersonAvatar, formatRelative, http, toast } from '@inhaus/ui';
import Comments from './Comments.vue';
import RichText from './RichText.vue';
import { fileSize } from '../lib/format';
const props = defineProps({
post: { type: Object, required: true },
reactions: { type: Array, required: true },
showGroup: { type: Boolean, default: false },
fullComments: { type: Array, default: null },
});
const emit = defineEmits(['deleted', 'changed']);
const p = props.post;
const showComments = ref(!!props.fullComments || p.comments.length > 0);
const editing = ref(false);
const draft = ref(p.body ?? '');
// Reaktion: ein Klick = 👍, Auswahl beim Überfahren
async function react(emoji) {
const next = p.my_reaction === emoji ? null : emoji;
const old = p.my_reaction;
const bump = (e, d) => {
if (!e) return;
const r = p.reactions.find((x) => x.emoji === e);
r ? (r.count += d) : d > 0 && p.reactions.push({ emoji: e, count: 1 });
p.reactions = p.reactions.filter((x) => x.count > 0).sort((a, b) => b.count - a.count);
p.reactions_total += d;
};
bump(old, -1);
bump(next, 1);
p.my_reaction = next;
await http.post(`/gruppen/beitraege/${p.id}/reaktion`, { emoji: next }).catch((e) => toast.error(e.message));
}
// Umfrage
const selection = ref([...(p.poll?.mine ?? [])]);
const voted = computed(() => (p.poll?.mine ?? []).length > 0);
const totalVotes = computed(() => p.poll?.options.reduce((n, o) => n + o.votes, 0) ?? 0);
async function vote(options) {
try {
p.poll = (await http.post(`/gruppen/beitraege/${p.id}/abstimmen`, { options })).poll;
selection.value = [...p.poll.mine];
} catch (e) {
toast.error(e.message);
}
}
function choose(i) {
if (!p.can_comment) return;
if (p.poll.multiple) {
selection.value = selection.value.includes(i) ? selection.value.filter((x) => x !== i) : [...selection.value, i];
} else {
vote([i]);
}
}
async function pin() {
try {
await http.post(`/gruppen/beitraege/${p.id}/anpinnen`, { pinned: !p.pinned });
p.pinned = !p.pinned;
emit('changed');
} catch (e) {
toast.error(e.message);
}
}
async function remove() {
if (!window.confirm('Beitrag löschen?')) return;
await http.delete(`/gruppen/beitraege/${p.id}`);
emit('deleted', p);
}
async function saveEdit() {
await http.put(`/gruppen/beitraege/${p.id}`, { body: draft.value });
p.body = draft.value;
p.edited = true;
editing.value = false;
}
async function report() {
const reason = window.prompt('Was stimmt mit diesem Beitrag nicht?');
if (!reason?.trim()) return;
const data = await http.post(`/gruppen/beitraege/${p.id}/melden`, { reason });
toast.success(data.message);
}
const shownImages = computed(() => p.images.slice(0, 4));
</script>
<template>
<article class="ih:overflow-hidden ih:rounded-xl ih:border ih:bg-card ih:shadow-xs" :aria-label="`Beitrag von ${p.user?.name ?? 'Gelöschtes Konto'}`">
<header class="ih:flex ih:items-start ih:gap-3 ih:px-4 ih:pt-4 ih:sm:px-5">
<PersonAvatar v-if="p.user" :person="p.user" class="ih:size-10" />
<div class="ih:min-w-0 ih:flex-1 ih:text-sm">
<p class="ih:leading-snug">
<a v-if="p.user" :href="`/personen/${p.user.id}`" class="ih:font-semibold ih:hover:underline">{{ p.user.name }}</a>
<template v-if="showGroup"><span class="ih:mx-1 ih:text-muted-foreground">in</span><a :href="`/gruppen/${p.group.slug}`" class="ih:font-semibold ih:hover:underline">{{ p.group.name }}</a></template>
</p>
<p class="ih:flex ih:items-center ih:gap-1.5 ih:text-xs ih:text-muted-foreground">
<a :href="p.url" class="ih:hover:underline">{{ formatRelative(p.created_at) }}</a>
<template v-if="p.edited"> · bearbeitet</template>
<span v-if="p.pinned" class="ih:inline-flex ih:items-center ih:gap-0.5 ih:font-medium ih:text-brand-text"><Pin class="ih:size-3" /> Angepinnt</span>
</p>
</div>
<DropdownMenu>
<DropdownMenuTrigger as-child>
<button type="button" class="ih:grid ih:size-8 ih:place-items-center ih:rounded-full ih:text-muted-foreground ih:hover:bg-accent" aria-label="Weitere Aktionen"><MoreHorizontal class="ih:size-5" /></button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem v-if="p.can_pin" @select="pin"><component :is="p.pinned ? PinOff : Pin" /> {{ p.pinned ? 'Nicht mehr anpinnen' : 'Oben anpinnen' }}</DropdownMenuItem>
<DropdownMenuItem v-if="p.can_edit && p.body !== null" @select="editing = true; draft = p.body"><Pencil /> Bearbeiten</DropdownMenuItem>
<DropdownMenuItem v-if="p.can_delete" class="ih:text-destructive" @select="remove"><Trash2 /> Löschen</DropdownMenuItem>
<DropdownMenuItem v-if="!p.can_edit" @select="report"><Flag /> Melden</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</header>
<div class="ih:px-4 ih:pt-3 ih:text-[15px] ih:leading-relaxed ih:sm:px-5">
<form v-if="editing" class="ih:grid ih:gap-2" @submit.prevent="saveEdit">
<textarea v-model="draft" rows="4" maxlength="10000" aria-label="Beitrag bearbeiten" class="ih:w-full ih:rounded-md ih:border ih:border-input ih:bg-background ih:p-3 ih:text-sm" />
<div class="ih:flex ih:gap-2"><Button size="sm" type="submit">Speichern</Button><Button size="sm" variant="ghost" type="button" @click="editing = false">Abbrechen</Button></div>
</form>
<RichText v-else-if="p.body" :text="p.body" />
</div>
<!-- Fotos -->
<div v-if="p.images.length" class="ih:mt-3 ih:grid ih:gap-0.5" :class="p.images.length === 1 ? '' : 'ih:grid-cols-2'">
<a v-for="(img, i) in shownImages" :key="img.id" :href="img.url" target="_blank" rel="noopener" class="ih:relative ih:block ih:overflow-hidden ih:bg-muted" :class="p.images.length === 3 && i === 0 ? 'ih:row-span-2' : ''">
<img :src="p.images.length === 1 ? img.url : img.thumb_url" :alt="img.name" loading="lazy" class="ih:w-full ih:object-cover" :class="p.images.length === 1 ? 'ih:max-h-[32rem]' : 'ih:aspect-square ih:h-full'" />
<span v-if="i === 3 && p.images.length > 4" class="ih:absolute ih:inset-0 ih:grid ih:place-items-center ih:bg-foreground/55 ih:text-2xl ih:font-semibold ih:text-background">+{{ p.images.length - 4 }}</span>
</a>
</div>
<!-- Dateien -->
<div v-if="p.files.length" class="ih:grid ih:gap-2 ih:px-4 ih:pt-3 ih:sm:px-5">
<a v-for="f in p.files" :key="f.id" :href="f.url" class="ih:flex ih:items-center ih:gap-3 ih:rounded-lg ih:border ih:p-2.5 ih:hover:bg-accent/60">
<FileIcon :extension="f.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">{{ f.name }}</span><span class="ih:text-xs ih:text-muted-foreground">{{ fileSize(f.size) }}</span></span>
<Download class="ih:size-4 ih:text-muted-foreground" />
</a>
</div>
<!-- Umfrage -->
<fieldset v-if="p.poll" class="ih:mx-4 ih:mt-3 ih:grid ih:gap-2 ih:rounded-lg ih:border ih:p-3 ih:sm:mx-5">
<legend class="ih:px-1 ih:text-xs ih:font-semibold ih:text-muted-foreground">Umfrage{{ p.poll.multiple ? ' · mehrere Antworten möglich' : '' }}</legend>
<button
v-for="(o, i) in p.poll.options"
:key="i"
type="button"
class="ih:relative ih:overflow-hidden ih:rounded-md ih:border ih:px-3 ih:py-2 ih:text-left ih:text-sm ih:disabled:cursor-default"
:class="(voted ? p.poll.mine : selection).includes(i) ? 'ih:border-brand' : 'ih:hover:border-brand/50'"
:aria-pressed="(voted ? p.poll.mine : selection).includes(i)"
:disabled="!p.can_comment"
@click="choose(i)"
>
<span v-if="voted" class="ih:absolute ih:inset-y-0 ih:left-0 ih:bg-brand-soft" :style="{ width: `${totalVotes ? Math.round((o.votes / totalVotes) * 100) : 0}%` }" aria-hidden="true" />
<span class="ih:relative ih:flex ih:justify-between ih:gap-3">
<span :class="{ 'ih:font-semibold': p.poll.mine.includes(i) }">{{ o.label }}</span>
<span v-if="voted" class="ih:text-muted-foreground ih:tabular-nums">{{ totalVotes ? Math.round((o.votes / totalVotes) * 100) : 0 }} % · {{ o.votes }}</span>
</span>
</button>
<div class="ih:flex ih:items-center ih:justify-between ih:text-xs ih:text-muted-foreground">
<span>{{ p.poll.voters }} {{ p.poll.voters === 1 ? 'Person hat' : 'Personen haben' }} abgestimmt</span>
<Button v-if="p.poll.multiple && p.can_comment" size="sm" variant="outline" :disabled="!selection.length && !voted" @click="vote(selection)">{{ voted ? 'Stimme ändern' : 'Abstimmen' }}</Button>
</div>
</fieldset>
<!-- Reaktionen und Kommentare -->
<div class="ih:mt-3 ih:flex ih:items-center ih:justify-between ih:px-4 ih:text-[13px] ih:text-muted-foreground ih:sm:px-5">
<span v-if="p.reactions_total" class="ih:inline-flex ih:items-center ih:gap-1"><span class="ih:tracking-tighter">{{ p.reactions.slice(0, 3).map((r) => r.emoji).join('') }}</span> {{ p.reactions_total }}</span><span v-else />
<button v-if="p.comments_count" type="button" class="ih:hover:underline" @click="showComments = !showComments">{{ p.comments_count }} {{ p.comments_count === 1 ? 'Kommentar' : 'Kommentare' }}</button>
</div>
<div v-if="p.can_comment" class="ih:mx-4 ih:mt-2 ih:grid ih:grid-cols-2 ih:border-t ih:py-1 ih:sm:mx-5">
<div class="ih:group/react ih:relative">
<button type="button" class="ih:flex ih:w-full ih:items-center ih:justify-center ih:gap-2 ih:rounded-md ih:py-2 ih:text-sm ih:font-medium ih:hover:bg-accent" :class="p.my_reaction ? 'ih:text-brand-text' : 'ih:text-muted-foreground'" :aria-pressed="!!p.my_reaction" @click="react(p.my_reaction ?? '👍')">
<span class="ih:text-base">{{ p.my_reaction ?? '👍' }}</span> {{ p.my_reaction ? 'Reagiert' : 'Gefällt mir' }}
</button>
<div class="ih:absolute ih:bottom-full ih:left-2 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">
<button v-for="emoji in reactions" :key="emoji" type="button" class="ih:grid ih:size-9 ih:place-items-center ih:rounded-full ih:text-xl ih:transition-transform ih:hover:scale-125" :aria-label="`Mit ${emoji} reagieren`" @click="react(emoji)">{{ emoji }}</button>
</div>
</div>
<button type="button" class="ih:flex ih:items-center ih:justify-center ih:gap-2 ih:rounded-md ih:py-2 ih:text-sm ih:font-medium ih:text-muted-foreground ih:hover:bg-accent" @click="showComments = true"><MessageCircle class="ih:size-4" /> Kommentieren</button>
</div>
<div v-else class="ih:h-3" />
<Comments v-if="showComments" :post="p" :full="!!fullComments" :initial="fullComments" />
</article>
</template>