inhaus-plugin-gruppen/resources/js/components/Comments.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

103 lines
5.7 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script setup>
import { ref } from 'vue';
import { CornerDownRight, LoaderCircle, SendHorizontal, Trash2 } from '@lucide/vue';
import { PersonAvatar, formatRelative, http, toast } from '@inhaus/ui';
import RichText from './RichText.vue';
/** Kommentare unter einem Beitrag – mit Antworten (eine Ebene) */
const props = defineProps({
post: { type: Object, required: true },
full: { type: Boolean, default: false },
initial: { type: Array, default: null },
});
const comments = ref(props.initial ?? props.post.comments);
const loadedAll = ref(props.full);
const body = ref('');
const replyTo = ref(null);
const replyBody = ref('');
const busy = ref(false);
async function loadAll() {
comments.value = (await http.get(`/gruppen/beitraege/${props.post.id}/kommentare`)).comments;
loadedAll.value = true;
}
async function send(text, parentId = null) {
if (!text.trim()) return;
busy.value = true;
try {
const data = await http.post(`/gruppen/beitraege/${props.post.id}/kommentare`, { body: text, parent_id: parentId });
comments.value = data.comments;
loadedAll.value = true;
props.post.comments_count = data.comments.reduce((n, c) => n + 1 + c.replies.length, 0);
body.value = replyBody.value = '';
replyTo.value = null;
} catch (error) {
toast.error(error.message);
} finally {
busy.value = false;
}
}
async function remove(comment) {
if (!window.confirm('Kommentar löschen?')) return;
await http.delete(`/gruppen/kommentare/${comment.id}`);
await loadAll();
props.post.comments_count = comments.value.reduce((n, c) => n + 1 + c.replies.length, 0);
}
</script>
<template>
<div class="ih:grid ih:gap-3 ih:border-t ih:px-4 ih:pt-3 ih:pb-4 ih:sm:px-5">
<button v-if="!loadedAll && post.comments_count > comments.length" type="button" class="ih:justify-self-start ih:text-[13px] ih:font-medium ih:text-muted-foreground ih:hover:text-foreground ih:hover:underline" @click="loadAll">
Alle {{ post.comments_count }} Kommentare anzeigen
</button>
<div v-for="c in comments" :id="`kommentar-${c.id}`" :key="c.id" class="ih:grid ih:gap-2">
<div class="ih:flex ih:gap-2.5">
<PersonAvatar v-if="c.user" :person="c.user" class="ih:size-8 ih:text-[11px]" />
<div class="ih:min-w-0 ih:flex-1">
<div class="ih:inline-block ih:max-w-full ih:rounded-2xl ih:bg-muted ih:px-3 ih:py-2 ih:text-sm">
<a v-if="c.user" :href="`/personen/${c.user.id}`" class="ih:font-semibold ih:hover:underline">{{ c.user.name }}</a>
<RichText :text="c.body" :limit="400" />
</div>
<div class="ih:mt-0.5 ih:flex ih:gap-3 ih:px-3 ih:text-xs ih:text-muted-foreground">
<span>{{ formatRelative(c.created_at) }}</span>
<button v-if="post.can_comment" type="button" class="ih:font-medium ih:hover:text-foreground" @click="replyTo = replyTo === c.id ? null : c.id">Antworten</button>
<button v-if="c.can_delete" type="button" class="ih:hover:text-destructive" :aria-label="`Kommentar von ${c.user?.name} löschen`" @click="remove(c)"><Trash2 class="ih:size-3.5" /></button>
</div>
</div>
</div>
<div v-for="r in c.replies" :id="`kommentar-${r.id}`" :key="r.id" class="ih:ml-10 ih:flex ih:gap-2.5">
<PersonAvatar v-if="r.user" :person="r.user" class="ih:size-7 ih:text-[10px]" />
<div class="ih:min-w-0 ih:flex-1">
<div class="ih:inline-block ih:max-w-full ih:rounded-2xl ih:bg-muted ih:px-3 ih:py-2 ih:text-sm">
<a v-if="r.user" :href="`/personen/${r.user.id}`" class="ih:font-semibold ih:hover:underline">{{ r.user.name }}</a>
<RichText :text="r.body" :limit="400" />
</div>
<div class="ih:mt-0.5 ih:flex ih:gap-3 ih:px-3 ih:text-xs ih:text-muted-foreground">
<span>{{ formatRelative(r.created_at) }}</span>
<button v-if="r.can_delete" type="button" class="ih:hover:text-destructive" aria-label="Antwort löschen" @click="remove(r)"><Trash2 class="ih:size-3.5" /></button>
</div>
</div>
</div>
<form v-if="replyTo === c.id" class="ih:ml-10 ih:flex ih:items-center ih:gap-2" @submit.prevent="send(replyBody, c.id)">
<CornerDownRight class="ih:size-4 ih:shrink-0 ih:text-muted-foreground" />
<input v-model="replyBody" maxlength="3000" :placeholder="`Antwort an ${c.user?.name ?? ''}`" aria-label="Antwort" autofocus class="ih:h-9 ih:min-w-0 ih:flex-1 ih:rounded-full ih:border ih:border-input ih:bg-background ih:px-4 ih:text-sm ih:focus-visible:ring-3 ih:focus-visible:ring-ring/30 ih:focus-visible:outline-none" />
</form>
</div>
<form v-if="post.can_comment" class="ih:flex ih:items-center ih:gap-2" @submit.prevent="send(body)">
<input v-model="body" maxlength="3000" placeholder="Kommentieren …" aria-label="Kommentar" class="ih:h-9 ih:min-w-0 ih:flex-1 ih:rounded-full ih:border ih:border-input ih:bg-background ih:px-4 ih:text-sm ih:focus-visible:ring-3 ih:focus-visible:ring-ring/30 ih:focus-visible:outline-none" />
<button type="submit" class="ih:grid ih:size-9 ih:shrink-0 ih:place-items-center ih:rounded-full ih:text-brand-text ih:hover:bg-brand-soft ih:disabled:opacity-40" :disabled="busy || !body.trim()" aria-label="Kommentar senden">
<LoaderCircle v-if="busy" class="ih:size-4 ih:animate-spin" /><SendHorizontal v-else class="ih:size-4" />
</button>
</form>
</div>
</template>