Files
Docwell/Server/admin/src/views/UsersView.vue
T
tajniak81andClaude Opus 5.5 a96c6ad3ce Add spaces, the page tree and the Docwell design system
Spaces and pages:
- Spaces inside workspaces, open or private, with admin/editor/commenter/viewer
  roles that cascade from the workspace role (rules in internal/access)
- Page tree per space with fractional position keys, move with cycle checks,
  trash that hides whole subtrees, restore and permanent delete
- OpenAPI, TypeScript client, and web app sidebar tree with drag-and-drop,
  page, space, trash and space settings views (EN + PL)

Design system:
- Tokens, type scale, radii and shadows from the design kit in
  @docwell/ui/theme.css; Inter, Source Serif 4 and JetBrains Mono bundled
- Vue ports of Button, TextField, Badge, Callout, Icon, Breadcrumbs,
  SlashMenu and PageTreeItem, plus Logo; favicons and web manifest
- Web app and admin panel restyled on the new tokens, in light and dark

Also: fix the Vite dev server crashing on /config.json, and stop tracking
the local Design/ and docs/ folders.

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

224 lines
9.0 KiB
Vue

<script setup lang="ts">
import type { Organization, Role, User, UserList } from "@docwell/api-client";
import { DwBadge, DwButton, DwCallout } from "@docwell/ui";
import { computed, onMounted, reactive, ref, watch } from "vue";
import { api, currentUser, errorMessage, isSuperadmin } from "../session";
// Mirrors the server's rules so the form only offers what will be accepted:
// org admins manage their own organization and never touch superadmins;
// superadmins manage everyone and move people between organizations.
const perPage = 20;
const page = ref(1);
const search = ref("");
const orgFilter = ref("");
const list = ref<UserList | null>(null);
const orgs = ref<Organization[]>([]);
const error = ref("");
const busy = ref(false);
const roles = computed<Role[]>(() => (isSuperadmin.value ? ["user", "admin", "superadmin"] : ["user", "admin"]));
const roleLabel: Record<string, string> = { user: "User", admin: "Admin", superadmin: "Superadmin" };
async function load() {
error.value = "";
try {
list.value = await api.users.list({ page: page.value, perPage, q: search.value, organization: orgFilter.value });
} catch (e) {
error.value = errorMessage(e);
}
}
let timer: ReturnType<typeof setTimeout> | undefined;
watch(search, () => {
clearTimeout(timer);
timer = setTimeout(() => {
page.value = 1;
load();
}, 250);
});
watch([page, orgFilter], load);
onMounted(async () => {
orgs.value = await api.orgs.list().catch(() => []);
load();
});
// ---- Create / edit form ----------------------------------------------------
const editing = ref<User | "new" | null>(null);
const form = reactive({ name: "", email: "", password: "", role: "user" as Role, organization: "", status: "active" });
function startNew() {
editing.value = "new";
Object.assign(form, {
name: "",
email: "",
password: "",
role: "user",
organization: isSuperadmin.value ? orgFilter.value.replace("none", "") : "",
status: "active",
});
}
function startEdit(u: User) {
editing.value = u;
Object.assign(form, { name: u.name, email: u.email, password: "", role: u.role, organization: u.organization, status: u.status });
}
const isSelf = computed(() => editing.value !== "new" && editing.value?.id === currentUser.value?.id);
// An org admin cannot edit a superadmin at all.
const canEdit = (u: User) => isSuperadmin.value || u.role !== "superadmin";
async function save() {
busy.value = true;
error.value = "";
try {
if (editing.value === "new") {
await api.users.create({
name: form.name,
email: form.email,
password: form.password,
role: form.role,
organization: isSuperadmin.value ? form.organization : undefined,
});
} else if (editing.value) {
const u = editing.value;
await api.users.update(u.id, {
name: form.name !== u.name ? form.name : undefined,
email: form.email !== u.email ? form.email : undefined,
password: form.password || undefined,
role: form.role !== u.role ? form.role : undefined,
status: form.status !== u.status ? (form.status as User["status"]) : undefined,
organization: isSuperadmin.value && form.organization !== u.organization ? form.organization : undefined,
});
}
editing.value = null;
await load();
} catch (e) {
error.value = errorMessage(e);
} finally {
busy.value = false;
}
}
async function remove(u: User) {
if (!confirm(`Delete ${u.email}? This cannot be undone.`)) return;
busy.value = true;
error.value = "";
try {
await api.users.remove(u.id);
await load();
} catch (e) {
error.value = errorMessage(e);
} finally {
busy.value = false;
}
}
</script>
<template>
<div class="flex flex-wrap items-center justify-between gap-3">
<div>
<h1 class="text-2xl font-semibold tracking-tight">Users</h1>
<p class="mt-1 text-ink-muted">
{{ isSuperadmin ? "Everyone on this server." : `Members of ${currentUser?.organizationName}.` }}
</p>
</div>
<DwButton variant="primary" @click="startNew">New user</DwButton>
</div>
<div class="mt-4 flex flex-wrap gap-2">
<input v-model="search" type="search" placeholder="Search name or email" aria-label="Search users" class="dw-input sm:w-64" />
<select v-if="isSuperadmin" v-model="orgFilter" aria-label="Filter by organization" class="dw-input sm:w-56">
<option value="">All organizations</option>
<option value="none">No organization</option>
<option v-for="o in orgs" :key="o.id" :value="o.id">{{ o.name }}</option>
</select>
</div>
<DwCallout v-if="error" tone="danger" size="ui" class="mt-4">{{ error }}</DwCallout>
<form v-if="editing" class="mt-4 grid gap-3 dw-card !p-4 sm:grid-cols-2" @submit.prevent="save">
<h2 class="text-ui-lg sm:col-span-2">{{ editing === "new" ? "New user" : `Edit ${editing.email}` }}</h2>
<label class="flex flex-col gap-1.5">
<span class="text-ui-sm font-semibold">Name</span>
<input v-model="form.name" required class="dw-input" />
</label>
<label class="flex flex-col gap-1.5">
<span class="text-ui-sm font-semibold">Email</span>
<input v-model="form.email" type="email" required class="dw-input" />
</label>
<label class="flex flex-col gap-1.5">
<span class="text-ui-sm font-semibold">{{ editing === "new" ? "Password" : "New password (optional)" }}</span>
<input v-model="form.password" type="password" autocomplete="new-password" :required="editing === 'new'" minlength="8" class="dw-input" />
</label>
<label class="flex flex-col gap-1.5">
<span class="text-ui-sm font-semibold">Role</span>
<select v-model="form.role" :disabled="isSelf" class="dw-input">
<option v-for="r in roles" :key="r" :value="r">{{ roleLabel[r] }}</option>
</select>
<span v-if="isSelf" class="text-ui-sm text-ink-muted">You cannot change your own role.</span>
</label>
<label v-if="isSuperadmin" class="flex flex-col gap-1.5">
<span class="text-ui-sm font-semibold">Organization</span>
<select v-model="form.organization" class="dw-input">
<option value="">No organization</option>
<option v-for="o in orgs" :key="o.id" :value="o.id">{{ o.name }}</option>
</select>
</label>
<label v-if="editing !== 'new'" class="flex flex-col gap-1.5">
<span class="text-ui-sm font-semibold">Status</span>
<select v-model="form.status" :disabled="isSelf" class="dw-input">
<option value="active">Active</option>
<option value="disabled">Disabled</option>
</select>
</label>
<div class="flex gap-2 sm:col-span-2">
<DwButton type="submit" variant="primary" :disabled="busy">{{ editing === "new" ? "Create user" : "Save" }}</DwButton>
<DwButton variant="ghost" :disabled="busy" @click="editing = null">Cancel</DwButton>
</div>
</form>
<div class="mt-6 overflow-x-auto rounded-md border border-line bg-surface-raised">
<table class="w-full text-left text-ui-md">
<thead class="bg-surface-sunken text-ui-xs uppercase text-ink-muted">
<tr>
<th class="px-4 py-2 font-medium">Name</th>
<th class="px-4 py-2 font-medium">Email</th>
<th class="px-4 py-2 font-medium">Role</th>
<th v-if="isSuperadmin" class="px-4 py-2 font-medium">Organization</th>
<th class="px-4 py-2 font-medium">Status</th>
<th class="px-4 py-2"><span class="sr-only">Actions</span></th>
</tr>
</thead>
<tbody>
<tr v-for="u in list?.items" :key="u.id" class="border-t border-line">
<td class="px-4 py-2 font-medium">{{ u.name }}</td>
<td class="px-4 py-2">{{ u.email }}</td>
<td class="px-4 py-2">{{ roleLabel[u.role] }}</td>
<td v-if="isSuperadmin" class="px-4 py-2">{{ u.organizationName || "—" }}</td>
<td class="px-4 py-2">
<DwBadge :tone="u.status === 'active' ? 'success' : u.status === 'disabled' ? 'danger' : 'neutral'" class="capitalize">{{ u.status }}</DwBadge>
</td>
<td class="px-4 py-2 text-right whitespace-nowrap">
<template v-if="canEdit(u)">
<DwButton variant="ghost" size="sm" @click="startEdit(u)">Edit</DwButton>
<DwButton v-if="u.id !== currentUser?.id" variant="ghost" size="sm" class="!text-danger" :disabled="busy" @click="remove(u)">
Delete
</DwButton>
</template>
</td>
</tr>
<tr v-if="list && list.items.length === 0">
<td :colspan="isSuperadmin ? 6 : 5" class="px-4 py-6 text-center text-ink-muted">No users found.</td>
</tr>
</tbody>
</table>
</div>
<div v-if="list && list.totalItems > perPage" class="mt-4 flex items-center gap-3 text-ui-md">
<DwButton size="sm" :disabled="page <= 1" @click="page--">Previous</DwButton>
<span class="text-ink-muted">Page {{ page }} of {{ Math.ceil(list.totalItems / perPage) }}</span>
<DwButton size="sm" :disabled="page * perPage >= list.totalItems" @click="page++">Next</DwButton>
</div>
</template>