Files
Docwell/Web-App/frontend/src/views/workspace/WorkspaceSettingsView.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

245 lines
9.8 KiB
Vue

<!-- SPDX-License-Identifier: AGPL-3.0-or-later -->
<script setup lang="ts">
import type { Invite, Workspace, WorkspaceMember, WorkspaceRole } from "@docwell/api-client";
import { DwButton, DwCallout } from "@docwell/ui";
import { computed, onMounted, ref } from "vue";
import { useI18n } from "vue-i18n";
import { useRouter } from "vue-router";
import { useApi } from "../../api";
import PageShell from "../../components/PageShell.vue";
import WorkspaceGroups from "../../components/WorkspaceGroups.vue";
import { useFormErrors } from "../../composables/useFormErrors";
import { useAuthStore } from "../../stores/auth";
import { atLeast, grantableRoles, useWorkspacesStore } from "../../stores/workspaces";
const props = defineProps<{ id: string }>();
const api = useApi();
const auth = useAuthStore();
const workspaces = useWorkspacesStore();
const router = useRouter();
const { t } = useI18n();
const errors = useFormErrors();
const ws = ref<Workspace | null>(null);
const members = ref<WorkspaceMember[]>([]);
const invites = ref<Invite[]>([]);
// The outcome of the last invite: who, and the link when it could not be emailed.
const invited = ref<Invite | null>(null);
const name = ref("");
const email = ref("");
const newRole = ref<WorkspaceRole>("member");
const busy = ref(false);
const notFound = ref(false);
// The caller's effective role after the cascade (org admins and superadmins
// are owners of every workspace in their scope).
const myRole = computed(() => ws.value?.role);
const canManage = computed(() => atLeast(myRole.value, "admin"));
const roles = computed(() => (myRole.value ? grantableRoles(myRole.value) : []));
const rank: Record<WorkspaceRole, number> = { guest: 1, member: 2, admin: 3, owner: 4 };
// A member can be changed only by someone ranked at least as high, never by themselves.
function canChange(m: WorkspaceMember) {
return canManage.value && m.user.id !== auth.user?.id && rank[m.role] <= rank[myRole.value!];
}
async function load() {
if (!workspaces.loaded) await workspaces.load();
ws.value = workspaces.items.find((w) => w.id === props.id) ?? null;
notFound.value = !ws.value;
if (!ws.value) return;
name.value = ws.value.name;
members.value = await api.workspaces.members(props.id);
invites.value = canManage.value ? await api.invites.list({ workspace: props.id }) : [];
}
onMounted(() => load().catch(errors.show));
async function run(action: () => Promise<unknown>) {
busy.value = true;
errors.reset();
try {
await action();
await workspaces.load();
await load();
} catch (e) {
errors.show(e);
} finally {
busy.value = false;
}
}
const rename = () => run(() => api.workspaces.rename(props.id, name.value));
const setRole = (m: WorkspaceMember, role: WorkspaceRole) =>
run(() => api.workspaces.setMember(props.id, m.user.email, role));
const removeMember = (m: WorkspaceMember) => run(() => api.workspaces.removeMember(props.id, m.user.id));
// Someone already in the organization is added at once; anyone else gets an invite email.
async function addMember() {
invited.value = null;
await run(async () => {
invited.value = await api.invites.create({ email: email.value, workspace: props.id, workspaceRole: newRole.value });
});
if (invited.value) email.value = "";
}
const resend = (inv: Invite) =>
run(async () => {
invited.value = await api.invites.resend(inv.id);
});
const revoke = (inv: Invite) => run(() => api.invites.revoke(inv.id));
const copied = ref(false);
async function copyLink(link: string) {
await navigator.clipboard.writeText(link);
copied.value = true;
setTimeout(() => (copied.value = false), 2000);
}
async function removeWorkspace() {
if (!ws.value || !confirm(t("workspace.confirmDelete", { name: ws.value.name }))) return;
busy.value = true;
errors.reset();
try {
await api.workspaces.remove(props.id);
await workspaces.load();
router.replace("/");
} catch (e) {
errors.show(e);
} finally {
busy.value = false;
}
}
</script>
<template>
<PageShell
:title="ws?.name ?? $t('workspace.settings')"
:subtitle="
ws ? $t('workspace.yourRole', { role: $t(`workspace.roles.${ws.role}`), org: ws.organizationName }) : undefined
"
>
<p v-if="notFound" class="text-ui-md text-ink-muted">{{ $t("workspace.notFound") }}</p>
<DwCallout v-if="errors.message.value" tone="danger" size="ui">{{ errors.message.value }}</DwCallout>
<template v-if="ws">
<form v-if="canManage" class="flex flex-wrap items-end gap-2 dw-card" @submit.prevent="rename">
<label class="flex min-w-48 flex-1 flex-col gap-1.5">
<span class="text-ui-sm font-semibold">{{ $t("workspace.name") }}</span>
<input v-model="name" required maxlength="120" class="dw-input w-full" />
</label>
<DwButton type="submit" variant="primary" :disabled="busy || !name.trim() || name.trim() === ws.name">{{
$t("common.save")
}}</DwButton>
</form>
<section class="dw-card">
<h2 class="text-ui-lg">{{ $t("workspace.members") }}</h2>
<p class="mt-1 text-ui-sm text-ink-muted">{{ $t("workspace.cascadeHint") }}</p>
<form v-if="canManage" class="mt-4 flex flex-wrap gap-2" @submit.prevent="addMember">
<input
v-model="email"
type="email"
required
:placeholder="$t('auth.email')"
:aria-label="$t('auth.email')"
class="dw-input min-w-48 flex-1"
/>
<select v-model="newRole" :aria-label="$t('workspace.role')" class="dw-input">
<option v-for="r in roles" :key="r" :value="r">{{ $t(`workspace.roles.${r}`) }}</option>
</select>
<DwButton type="submit" :disabled="busy || !email">{{ $t("workspace.invite") }}</DwButton>
</form>
<DwCallout
v-if="invited"
:tone="invited.status === 'added' || invited.mailed ? 'success' : 'warning'"
size="ui"
class="mt-3"
>
<span v-if="invited.status === 'added'">{{ $t("workspace.added", { email: invited.email }) }}</span>
<span v-else-if="invited.mailed">{{ $t("workspace.invited", { email: invited.email }) }}</span>
<span v-else class="flex flex-wrap items-center gap-2">
{{ $t("workspace.inviteNotMailed", { email: invited.email }) }}
<button v-if="invited.link" class="dw-link" @click="copyLink(invited.link)">
{{ copied ? $t("workspace.copied") : $t("workspace.copyLink") }}
</button>
</span>
</DwCallout>
<ul class="mt-4 divide-y divide-line">
<li
v-for="m in members"
:key="m.user.id"
class="flex flex-wrap items-center justify-between gap-2 py-2"
data-testid="member"
>
<div class="min-w-0">
<p class="truncate text-ui-md font-medium">{{ m.user.name }}</p>
<p class="truncate text-ui-sm text-ink-muted">{{ m.user.email }}</p>
</div>
<div class="flex items-center gap-3 text-ui-md">
<select
v-if="canChange(m)"
:value="m.role"
:aria-label="$t('workspace.role')"
class="dw-input"
:disabled="busy"
@change="setRole(m, ($event.target as HTMLSelectElement).value as WorkspaceRole)"
>
<option v-for="r in roles" :key="r" :value="r">{{ $t(`workspace.roles.${r}`) }}</option>
</select>
<span v-else class="text-ink-muted">{{ $t(`workspace.roles.${m.role}`) }}</span>
<button
v-if="canChange(m) || m.user.id === auth.user?.id"
class="text-danger hover:underline"
:disabled="busy"
@click="removeMember(m)"
>
{{ m.user.id === auth.user?.id ? $t("workspace.leave") : $t("workspace.remove") }}
</button>
</div>
</li>
<li v-if="!members.length" class="py-2 text-ui-md text-ink-muted">{{ $t("workspace.noMembers") }}</li>
</ul>
<template v-if="invites.length">
<h3 class="mt-6 text-ui-md font-semibold">{{ $t("workspace.pendingInvites") }}</h3>
<ul class="mt-2 divide-y divide-line">
<li
v-for="inv in invites"
:key="inv.id"
class="flex flex-wrap items-center justify-between gap-2 py-2"
data-testid="invite"
>
<div class="min-w-0">
<p class="truncate text-ui-md font-medium">{{ inv.email }}</p>
<p class="truncate text-ui-sm text-ink-muted">
{{ $t(`workspace.roles.${inv.workspaceRole}`) }} ·
{{
inv.status === "expired"
? $t("workspace.inviteExpired")
: $t("workspace.invitedBy", { name: inv.invitedBy })
}}
</p>
</div>
<div class="flex items-center gap-3 text-ui-md">
<button class="dw-link" :disabled="busy" @click="resend(inv)">{{ $t("workspace.resend") }}</button>
<button class="text-danger hover:underline" :disabled="busy" @click="revoke(inv)">
{{ $t("workspace.revoke") }}
</button>
</div>
</li>
</ul>
</template>
</section>
<WorkspaceGroups v-if="atLeast(myRole, 'member')" :workspace="ws.id" :can-manage="canManage" />
<section v-if="atLeast(myRole, 'owner')" class="dw-card">
<h2 class="text-ui-lg">{{ $t("org.dangerTitle") }}</h2>
<p class="mt-1 text-ui-sm text-ink-muted">{{ $t("workspace.deleteHint") }}</p>
<DwButton variant="secondary" class="mt-3 !text-danger" :disabled="busy" @click="removeWorkspace">
{{ $t("workspace.delete") }}
</DwButton>
</section>
</template>
</PageShell>
</template>