105 lines
6.3 KiB
Vue
105 lines
6.3 KiB
Vue
<script setup>
|
||
import { computed, ref, watch } from 'vue';
|
||
import { Crown, EyeOff, LoaderCircle, LogOut, UserMinus, UserPlus } from '@lucide/vue';
|
||
import { Button, Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, Input, PersonAvatar, Switch, toast } from '@inhaus/ui';
|
||
import PeopleSelect from './PeopleSelect.vue';
|
||
import { api } from '../lib/api';
|
||
|
||
/** Einstellungen eines Gesprächs: Stummschalten, Mitglieder (Gruppen), Verlassen bzw. Ausblenden */
|
||
const props = defineProps({
|
||
conversation: { type: Object, required: true },
|
||
me: { type: Object, required: true },
|
||
});
|
||
const open = defineModel('open', { type: Boolean, default: false });
|
||
const emit = defineEmits(['updated', 'left']);
|
||
|
||
const isGroup = computed(() => props.conversation.type === 'group');
|
||
const name = ref('');
|
||
const adding = ref([]);
|
||
const busy = ref(false);
|
||
|
||
watch(open, (value) => {
|
||
if (value) {
|
||
name.value = props.conversation.name;
|
||
adding.value = [];
|
||
}
|
||
});
|
||
|
||
async function run(action, message) {
|
||
busy.value = true;
|
||
|
||
try {
|
||
const data = await action();
|
||
emit('updated', data.conversations);
|
||
message && toast.success(message);
|
||
} catch (error) {
|
||
toast.error(error.message);
|
||
} finally {
|
||
busy.value = false;
|
||
}
|
||
}
|
||
|
||
const rename = () => run(() => api.updateConversation(props.conversation.id, { name: name.value.trim() }), 'Gruppe umbenannt');
|
||
const mute = (muted) => run(() => api.updateConversation(props.conversation.id, { muted }), muted ? 'Stummgeschaltet – keine Benachrichtigungen mehr' : 'Benachrichtigungen wieder an');
|
||
const add = () => run(() => api.addMembers(props.conversation.id, adding.value.map((p) => p.id)), 'Hinzugefügt').then(() => (adding.value = []));
|
||
const removeMember = (member) => window.confirm(`${member.name} aus der Gruppe entfernen?`) && run(() => api.removeMember(props.conversation.id, member.id));
|
||
const toggleAdmin = (member) => run(() => api.setAdmin(props.conversation.id, member.id, !member.admin));
|
||
|
||
async function leave() {
|
||
if (!window.confirm(isGroup.value ? 'Gruppe verlassen? Sie sehen die Nachrichten danach nicht mehr.' : 'Gespräch aus Ihrer Liste entfernen? Mit der nächsten Nachricht erscheint es wieder.')) return;
|
||
|
||
await run(() => api.leave(props.conversation.id));
|
||
open.value = false;
|
||
emit('left');
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<Dialog v-model:open="open">
|
||
<DialogContent class="ih:max-h-[85dvh] ih:overflow-y-auto ih:sm:max-w-lg">
|
||
<DialogHeader>
|
||
<DialogTitle>{{ conversation.name }}</DialogTitle>
|
||
<DialogDescription>{{ isGroup ? `Gruppe mit ${conversation.members.length} Mitgliedern` : 'Direktnachricht' }}</DialogDescription>
|
||
</DialogHeader>
|
||
|
||
<div class="ih:grid ih:gap-5">
|
||
<form v-if="isGroup && conversation.is_admin" class="ih:flex ih:gap-2" @submit.prevent="rename">
|
||
<Input v-model="name" maxlength="100" aria-label="Name der Gruppe" />
|
||
<Button type="submit" variant="outline" :disabled="busy || !name.trim() || name.trim() === conversation.name">Umbenennen</Button>
|
||
</form>
|
||
|
||
<label class="ih:flex ih:items-center ih:justify-between ih:gap-3 ih:text-sm">
|
||
<span><span class="ih:font-medium">Stummschalten</span><span class="ih:block ih:text-xs ih:text-muted-foreground">Keine Glocke und kein Push für dieses Gespräch.</span></span>
|
||
<Switch :model-value="conversation.muted" :disabled="busy" @update:model-value="mute" />
|
||
</label>
|
||
|
||
<section v-if="isGroup" class="ih:grid ih:gap-2">
|
||
<h3 class="ih:text-sm ih:font-semibold">Mitglieder</h3>
|
||
<ul class="ih:-mx-2 ih:grid">
|
||
<li v-for="member in conversation.members" :key="member.id" class="ih:flex ih:items-center ih:gap-3 ih:rounded-md ih:px-2 ih:py-1.5 ih:hover:bg-muted/60">
|
||
<PersonAvatar :person="member" class="ih:size-8 ih:text-[11px]" />
|
||
<span class="ih:min-w-0 ih:flex-1 ih:text-sm">
|
||
<span class="ih:font-medium">{{ member.name }}</span><span v-if="member.id === me.id" class="ih:text-muted-foreground"> (Sie)</span>
|
||
<span v-if="member.admin" class="ih:ml-1.5 ih:inline-flex ih:items-center ih:gap-0.5 ih:text-xs ih:text-brand-text"><Crown class="ih:size-3" /> Admin</span>
|
||
</span>
|
||
<template v-if="conversation.is_admin && member.id !== me.id">
|
||
<button type="button" class="ih:rounded ih:p-1.5 ih:text-muted-foreground ih:hover:bg-accent ih:hover:text-foreground" :title="member.admin ? 'Admin-Rechte entziehen' : 'Zum Admin machen'" :aria-label="member.admin ? `${member.name}: Admin-Rechte entziehen` : `${member.name} zum Admin machen`" @click="toggleAdmin(member)"><Crown class="ih:size-4" /></button>
|
||
<button type="button" class="ih:rounded ih:p-1.5 ih:text-muted-foreground ih:hover:bg-accent ih:hover:text-destructive" :aria-label="`${member.name} entfernen`" @click="removeMember(member)"><UserMinus class="ih:size-4" /></button>
|
||
</template>
|
||
</li>
|
||
</ul>
|
||
|
||
<div v-if="conversation.is_admin" class="ih:grid ih:gap-2 ih:rounded-md ih:border ih:p-3">
|
||
<span class="ih:flex ih:items-center ih:gap-1.5 ih:text-sm ih:font-medium"><UserPlus class="ih:size-4" /> Personen hinzufügen</span>
|
||
<PeopleSelect v-model="adding" multiple :exclude="conversation.members.map((m) => m.id)" />
|
||
<Button v-if="adding.length" size="sm" class="ih:justify-self-start" :disabled="busy" @click="add"><LoaderCircle v-if="busy" class="ih:animate-spin" /> {{ adding.length }} hinzufügen</Button>
|
||
</div>
|
||
</section>
|
||
|
||
<Button variant="outline" class="ih:justify-self-start ih:text-destructive ih:hover:text-destructive" :disabled="busy" @click="leave">
|
||
<component :is="isGroup ? LogOut : EyeOff" /> {{ isGroup ? 'Gruppe verlassen' : 'Aus der Liste entfernen' }}
|
||
</Button>
|
||
</div>
|
||
</DialogContent>
|
||
</Dialog>
|
||
</template>
|