Files
Docwell/Web-App/frontend/src/components/WorkspaceGroups.vue
T
tajniak81andClaude Opus 5.5 34660c6b46 Add workspace groups with space grants
Workspace admins create groups of members, and space admins grant a role
to a whole group. A member's space role is now the highest of their own
grant and their groups' grants. Leaving a workspace drops its group
memberships, and deleting a group removes its space grants (they are not
a real relation, so PocketBase cannot cascade them).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 16:39:21 +01:00

129 lines
4.8 KiB
Vue

<!-- SPDX-License-Identifier: AGPL-3.0-or-later -->
<script setup lang="ts">
import type { Group, MemberUser } from "@docwell/api-client";
import { DwButton, DwCallout } from "@docwell/ui";
import { onMounted, ref } from "vue";
import { useI18n } from "vue-i18n";
import { useApi } from "../api";
import { useFormErrors } from "../composables/useFormErrors";
// Groups of a workspace's members; spaces can grant a role to a whole group.
const props = defineProps<{ workspace: string; canManage: boolean }>();
const api = useApi();
const { t } = useI18n();
const errors = useFormErrors();
const groups = ref<Group[]>([]);
const open = ref<string | null>(null);
const members = ref<MemberUser[]>([]);
const newName = ref("");
const email = ref("");
const busy = ref(false);
async function load() {
groups.value = await api.workspaces.groups(props.workspace);
if (open.value) members.value = await api.groups.members(open.value);
}
onMounted(() => load().catch(errors.show));
async function run(action: () => Promise<unknown>) {
busy.value = true;
errors.reset();
try {
await action();
await load();
return true;
} catch (e) {
errors.show(e);
return false;
} finally {
busy.value = false;
}
}
async function toggle(g: Group) {
open.value = open.value === g.id ? null : g.id;
email.value = "";
members.value = open.value ? await api.groups.members(g.id).catch(() => []) : [];
}
async function create() {
if (await run(() => api.workspaces.createGroup(props.workspace, { name: newName.value }))) newName.value = "";
}
async function addMember(g: Group) {
if (await run(() => api.groups.addMember(g.id, email.value))) email.value = "";
}
const removeMember = (g: Group, m: MemberUser) => run(() => api.groups.removeMember(g.id, m.id));
async function removeGroup(g: Group) {
if (!confirm(t("groups.confirmDelete", { name: g.name }))) return;
if (open.value === g.id) open.value = null;
await run(() => api.groups.remove(g.id));
}
</script>
<template>
<section class="dw-card">
<h2 class="text-ui-lg">{{ $t("groups.title") }}</h2>
<p class="mt-1 text-ui-sm text-ink-muted">{{ $t("groups.hint") }}</p>
<DwCallout v-if="errors.message.value" tone="danger" size="ui" class="mt-3">{{ errors.message.value }}</DwCallout>
<form v-if="canManage" class="mt-4 flex flex-wrap gap-2" @submit.prevent="create">
<input
v-model="newName"
required
maxlength="120"
:placeholder="$t('groups.name')"
:aria-label="$t('groups.name')"
class="dw-input min-w-48 flex-1"
/>
<DwButton type="submit" :disabled="busy || !newName.trim()">{{ $t("groups.create") }}</DwButton>
</form>
<p v-if="errors.fields.value.name" class="mt-1 text-ui-sm text-danger">{{ errors.fields.value.name }}</p>
<ul class="mt-4 divide-y divide-line">
<li v-for="g in groups" :key="g.id" class="py-2" data-testid="group">
<div class="flex flex-wrap items-center justify-between gap-2">
<button class="min-w-0 text-left" :aria-expanded="open === g.id" @click="toggle(g)">
<span class="block truncate text-ui-md font-medium">{{ g.name }}</span>
<span class="block text-ui-sm text-ink-muted">{{ $t("groups.memberCount", { n: g.members }) }}</span>
</button>
<button
v-if="canManage"
class="text-ui-md text-danger hover:underline"
:disabled="busy"
@click="removeGroup(g)"
>
{{ $t("groups.delete") }}
</button>
</div>
<div v-if="open === g.id" class="mt-2 rounded-sm bg-surface-sunken p-3">
<form v-if="canManage" class="flex flex-wrap gap-2" @submit.prevent="addMember(g)">
<input
v-model="email"
type="email"
required
:placeholder="$t('auth.email')"
:aria-label="$t('auth.email')"
class="dw-input min-w-48 flex-1"
/>
<DwButton type="submit" size="sm" :disabled="busy || !email">{{ $t("workspace.add") }}</DwButton>
</form>
<ul class="mt-2 space-y-1">
<li v-for="m in members" :key="m.id" class="flex items-center justify-between gap-2 text-ui-md">
<span class="min-w-0 truncate"
>{{ m.name }} <span class="text-ink-muted">· {{ m.email }}</span></span
>
<button v-if="canManage" class="text-danger hover:underline" :disabled="busy" @click="removeMember(g, m)">
{{ $t("workspace.remove") }}
</button>
</li>
<li v-if="!members.length" class="text-ui-sm text-ink-muted">{{ $t("groups.empty") }}</li>
</ul>
</div>
</li>
<li v-if="!groups.length" class="py-2 text-ui-md text-ink-muted">{{ $t("groups.none") }}</li>
</ul>
</section>
</template>