Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

feat: redesign User Management page (avatars, search, skeleton, confirm dialog)

Ilia Avdeev8e21c3c64425
diff --git a/web/app/(admin)/admin/users/page.tsx b/web/app/(admin)/admin/users/page.tsx
index c2dc744bb..524ce8932 100644
--- a/web/app/(admin)/admin/users/page.tsx
+++ b/web/app/(admin)/admin/users/page.tsx
@@ -11,14 +11,20 @@ import {
type UserRecord,
} from "@/lib/admin-api";
import { GrantEditor } from "@/features/multi-user/components/GrantEditor";
+import { UserAvatar } from "@/components/UserAvatar";
+import { ConfirmDialog } from "@/components/ui/ConfirmDialog";
+import { filterUsersByQuery } from "@/lib/admin-users";
import {
+ Search,
Shield,
+ ShieldCheck,
ShieldOff,
Trash2,
RefreshCw,
ArrowLeft,
SlidersHorizontal,
UserPlus,
+ Users,
X,
} from "lucide-react";
import Link from "next/link";
@@ -45,6 +51,12 @@ export default function AdminUsersPage() {
const [actionError, setActionError] = useState("");
const [expandedUserId, setExpandedUserId] = useState<string | null>(null);
const [showCreateDialog, setShowCreateDialog] = useState(false);
+ const [query, setQuery] = useState("");
+ const [confirmTarget, setConfirmTarget] = useState<{
+ kind: "delete" | "promote" | "demote";
+ user: UserRecord;
+ } | null>(null);
+ const [confirmBusy, setConfirmBusy] = useState(false);
const [createUsername, setCreateUsername] = useState("");
const [createPassword, setCreatePassword] = useState("");
const [createSubmitting, setCreateSubmitting] = useState(false);
@@ -115,35 +127,41 @@ export default function AdminUsersPage() {
}
}
- async function handleDelete(username: string) {
- if (!window.confirm(`Delete user "${username}"? This cannot be undone.`))
- return;
- setActionError("");
- try {
- await deleteUser(username);
- setUsers((prev) => prev.filter((u) => u.username !== username));
- } catch (e) {
- setActionError(e instanceof Error ? e.message : "Failed to delete user");
- }
- }
-
- async function handleToggleRole(user: UserRecord) {
- const newRole = user.role === "admin" ? "user" : "admin";
- const verb = newRole === "admin" ? "Promote" : "Demote";
- if (!window.confirm(`${verb} "${user.username}" to ${newRole}?`)) return;
+ async function handleConfirmAction() {
+ if (!confirmTarget || confirmBusy) return;
+ const { kind, user } = confirmTarget;
+ setConfirmBusy(true);
setActionError("");
try {
- await setUserRole(user.username, newRole);
- setUsers((prev) =>
- prev.map((u) =>
- u.username === user.username ? { ...u, role: newRole } : u,
- ),
- );
- if (newRole === "admin") {
- setExpandedUserId((current) => (current === user.id ? null : current));
+ if (kind === "delete") {
+ await deleteUser(user.username);
+ setUsers((prev) => prev.filter((u) => u.username !== user.username));
+ } else {
+ const newRole = kind === "promote" ? "admin" : "user";
+ await setUserRole(user.username, newRole);
+ setUsers((prev) =>
+ prev.map((u) =>
+ u.username === user.username ? { ...u, role: newRole } : u,
+ ),
+ );
+ if (newRole === "admin") {
+ setExpandedUserId((current) =>
+ current === user.id ? null : current,
+ );
+ }
}
+ setConfirmTarget(null);
} catch (e) {
- setActionError(e instanceof Error ? e.message : "Failed to update role");
+ setConfirmTarget(null);
+ setActionError(
+ e instanceof Error
+ ? e.message
+ : confirmTarget.kind === "delete"
+ ? "Failed to delete user"
+ : "Failed to update role",
+ );
+ } finally {
+ setConfirmBusy(false);
}
}
@@ -155,46 +173,56 @@ export default function AdminUsersPage() {
}
}, [expandedUserId, users]);
+ const normalizedQuery = query.trim().toLowerCase();
+ const filteredUsers = filterUsersByQuery(users, query);
+
return (
<div className="h-screen overflow-y-auto bg-[var(--background)] px-4 py-10 [scrollbar-gutter:stable]">
<div className="mx-auto max-w-3xl">
{/* Header */}
- <div className="mb-8 flex items-center gap-4">
+ <div className="mb-8">
<Link
href="/"
- className="flex items-center gap-1.5 text-sm text-[var(--muted-foreground)] hover:text-[var(--foreground)] transition-colors"
+ className="mb-4 inline-flex items-center gap-1.5 text-sm text-[var(--muted-foreground)] hover:text-[var(--foreground)] transition-colors"
>
- <ArrowLeft size={15} />
+ <ArrowLeft size={16} />
Back
</Link>
- <div className="flex-1">
- <h1 className="font-serif text-xl font-semibold text-[var(--foreground)]">
- User Management
- </h1>
- <p className="mt-0.5 text-sm text-[var(--muted-foreground)]">
- Manage registered accounts
- </p>
+ <div className="flex items-start justify-between gap-4">
+ <div>
+ <h1 className="font-serif text-xl font-semibold text-[var(--foreground)]">
+ User Management
+ </h1>
+ <p className="mt-0.5 text-sm text-[var(--muted-foreground)]">
+ Manage registered accounts
+ </p>
+ </div>
+ <div className="flex shrink-0 items-center gap-2">
+ <button
+ onClick={openCreateDialog}
+ className="flex items-center gap-1.5 rounded-lg px-3 py-1.5 text-sm
+ border border-[var(--border)] text-[var(--foreground)]
+ hover:bg-[var(--card)] transition-colors"
+ >
+ <UserPlus size={14} />
+ Add user
+ </button>
+ <button
+ onClick={load}
+ disabled={loading}
+ className="flex items-center gap-1.5 rounded-lg px-3 py-1.5 text-sm
+ border border-[var(--border)] text-[var(--muted-foreground)]
+ hover:text-[var(--foreground)] hover:bg-[var(--card)]
+ disabled:opacity-50 transition-colors"
+ >
+ <RefreshCw
+ size={14}
+ className={loading ? "animate-spin" : ""}
+ />
+ Refresh
+ </button>
+ </div>
</div>
- <button
- onClick={openCreateDialog}
- className="flex items-center gap-1.5 rounded-lg px-3 py-1.5 text-sm
- border border-[var(--border)] text-[var(--foreground)]
- hover:bg-[var(--card)] transition-colors"
- >
- <UserPlus size={14} />
- Add user
- </button>
- <button
- onClick={load}
- disabled={loading}
- className="flex items-center gap-1.5 rounded-lg px-3 py-1.5 text-sm
- border border-[var(--border)] text-[var(--muted-foreground)]
- hover:text-[var(--foreground)] hover:bg-[var(--card)]
- disabled:opacity-50 transition-colors"
- >
- <RefreshCw size={14} className={loading ? "animate-spin" : ""} />
- Refresh
- </button>
</div>
{actionError && (
@@ -203,18 +231,94 @@ export default function AdminUsersPage() {
</div>
)}
+ {!loading && !error && users.length > 0 && (
+ <div className="mb-4 flex items-center gap-3">
+ <div className="relative flex-1">
+ <Search
+ size={14}
+ className="pointer-events-none absolute left-3 top-1/2 -translate-y-1/2 text-[var(--muted-foreground)]"
+ />
+ <input
+ type="search"
+ value={query}
+ onChange={(e) => setQuery(e.target.value)}
+ placeholder="Search users…"
+ aria-label="Search users"
+ className="w-full rounded-lg border border-[var(--border)] bg-[var(--card)] py-2 pl-9 pr-3 text-sm
+ text-[var(--foreground)] placeholder:text-[var(--muted-foreground)]/70
+ outline-none focus:border-[var(--ring)] transition-colors"
+ />
+ </div>
+ <span className="shrink-0 text-xs text-[var(--muted-foreground)]">
+ {normalizedQuery
+ ? `${filteredUsers.length} of ${users.length}`
+ : `${users.length} ${users.length === 1 ? "user" : "users"}`}
+ </span>
+ </div>
+ )}
+
<div className="rounded-2xl border border-[var(--border)] bg-[var(--card)] overflow-hidden shadow-sm">
{loading ? (
- <div className="flex items-center justify-center py-16 text-[var(--muted-foreground)] text-sm">
- Loading…
+ <div className="divide-y divide-[var(--border)]" aria-hidden>
+ {[0, 1, 2].map((row) => (
+ <div
+ key={row}
+ className="flex animate-pulse items-center gap-3 px-5 py-4"
+ >
+ <div className="h-8 w-8 rounded-full bg-[var(--muted)]/60" />
+ <div className="flex-1 space-y-2">
+ <div className="h-3 w-36 rounded bg-[var(--muted)]/60" />
+ <div className="h-2.5 w-24 rounded bg-[var(--muted)]/40" />
+ </div>
+ <div className="h-5 w-16 rounded-full bg-[var(--muted)]/40" />
+ </div>
+ ))}
</div>
) : error ? (
<div className="flex items-center justify-center py-16 text-red-500 text-sm">
{error}
</div>
) : users.length === 0 ? (
- <div className="flex items-center justify-center py-16 text-[var(--muted-foreground)] text-sm">
- No users found.
+ <div className="flex flex-col items-center justify-center px-6 py-16 text-center">
+ <Users
+ size={28}
+ strokeWidth={1.5}
+ className="text-[var(--muted-foreground)]/50"
+ />
+ <p className="mt-3 text-sm font-medium text-[var(--foreground)]">
+ No users yet
+ </p>
+ <p className="mt-1 text-sm text-[var(--muted-foreground)]">
+ Accounts you create will appear here.
+ </p>
+ <button
+ onClick={openCreateDialog}
+ className="mt-4 flex items-center gap-1.5 rounded-lg px-3 py-1.5 text-sm
+ border border-[var(--border)] text-[var(--foreground)]
+ hover:bg-[var(--background)]/60 transition-colors"
+ >
+ <UserPlus size={14} />
+ Add user
+ </button>
+ </div>
+ ) : filteredUsers.length === 0 ? (
+ <div className="flex flex-col items-center justify-center px-6 py-16 text-center">
+ <Search
+ size={28}
+ strokeWidth={1.5}
+ className="text-[var(--muted-foreground)]/50"
+ />
+ <p className="mt-3 text-sm font-medium text-[var(--foreground)]">
+ No users match &ldquo;{query.trim()}&rdquo;
+ </p>
+ <button
+ onClick={() => setQuery("")}
+ className="mt-4 rounded-lg px-3 py-1.5 text-sm border border-[var(--border)]
+ text-[var(--muted-foreground)] hover:text-[var(--foreground)]
+ hover:bg-[var(--background)]/60 transition-colors"
+ >
+ Clear search
+ </button>
</div>
) : (
<table className="w-full text-sm">
@@ -227,34 +331,45 @@ export default function AdminUsersPage() {
</tr>
</thead>
<tbody className="divide-y divide-[var(--border)]">
- {users.map((user) => {
+ {filteredUsers.map((user) => {
const isSelf = user.username === currentUser;
const isAdmin = user.role === "admin";
const canManageAssignments = !isAdmin && Boolean(user.id);
return (
<Fragment key={user.username}>
<tr className="group hover:bg-[var(--background)]/50 transition-colors">
- <td className="px-5 py-3.5 font-medium text-[var(--foreground)]">
- {user.username}
- {isSelf && (
- <span className="ml-2 text-xs text-[var(--muted-foreground)]">
- (you)
+ <td className="px-5 py-3">
+ <div className="flex items-center gap-3">
+ <UserAvatar
+ username={user.username}
+ userId={user.id}
+ avatar={user.avatar}
+ role={user.role}
+ size={32}
+ />
+ <span className="min-w-0 truncate font-medium text-[var(--foreground)]">
+ {user.username}
+ {isSelf && (
+ <span className="ml-2 text-xs font-normal text-[var(--muted-foreground)]">
+ (you)
+ </span>
+ )}
</span>
- )}
+ </div>
</td>
- <td className="px-5 py-3.5">
+ <td className="px-5 py-3">
<span
className={`inline-flex items-center gap-1 rounded-full px-2.5 py-0.5 text-xs font-medium
${
- user.role === "admin"
- ? "bg-purple-500/15 text-purple-600 dark:text-purple-400"
+ isAdmin
+ ? "bg-amber-500/15 text-amber-600 dark:text-amber-400"
: "bg-[var(--muted)]/50 text-[var(--muted-foreground)]"
}`}
>
- {user.role === "admin" ? (
- <Shield size={11} />
- ) : null}
- {user.role}
+ {isAdmin && (
+ <ShieldCheck size={11} strokeWidth={2} />
+ )}
+ {isAdmin ? "Admin" : "User"}
</span>
</td>
<td className="px-5 py-3.5 text-[var(--muted-foreground)]">
@@ -278,7 +393,12 @@ export default function AdminUsersPage() {
</button>
)}
<button
- onClick={() => handleToggleRole(user)}
+ onClick={() =>
+ setConfirmTarget({
+ kind: isAdmin ? "demote" : "promote",
+ user,
+ })
+ }
disabled={isSelf}
title={
isSelf
@@ -298,7 +418,9 @@ export default function AdminUsersPage() {
)}
</button>
<button
- onClick={() => handleDelete(user.username)}
+ onClick={() =>
+ setConfirmTarget({ kind: "delete", user })
+ }
disabled={isSelf}
title={
isSelf
@@ -334,6 +456,65 @@ export default function AdminUsersPage() {
</p>
</div>
+ <ConfirmDialog
+ open={confirmTarget !== null}
+ title={
+ confirmTarget?.kind === "delete"
+ ? "Delete user"
+ : confirmTarget?.kind === "promote"
+ ? "Promote to admin"
+ : "Demote to user"
+ }
+ tone={confirmTarget?.kind === "delete" ? "danger" : "default"}
+ confirmLabel={
+ confirmTarget?.kind === "delete"
+ ? "Delete user"
+ : confirmTarget?.kind === "promote"
+ ? "Promote"
+ : "Demote"
+ }
+ busyLabel={
+ confirmTarget?.kind === "delete"
+ ? "Deleting…"
+ : confirmTarget?.kind === "promote"
+ ? "Promoting…"
+ : "Demoting…"
+ }
+ busy={confirmBusy}
+ onConfirm={handleConfirmAction}
+ onCancel={() => setConfirmTarget(null)}
+ >
+ {confirmTarget && (
+ <>
+ <div className="flex items-center gap-3 rounded-xl border border-[var(--border)] bg-[var(--background)]/50 px-3 py-2.5">
+ <UserAvatar
+ username={confirmTarget.user.username}
+ userId={confirmTarget.user.id}
+ avatar={confirmTarget.user.avatar}
+ role={confirmTarget.user.role}
+ size={32}
+ />
+ <div className="min-w-0">
+ <p className="truncate text-sm font-medium text-[var(--foreground)]">
+ {confirmTarget.user.username}
+ </p>
+ <p className="text-xs text-[var(--muted-foreground)]">
+ {confirmTarget.user.role === "admin" ? "Admin" : "User"} ·
+ joined {formatDate(confirmTarget.user.created_at)}
+ </p>
+ </div>
+ </div>
+ <p className="mt-3">
+ {confirmTarget.kind === "delete"
+ ? "This permanently removes the account and its assignments. This cannot be undone."
+ : confirmTarget.kind === "promote"
+ ? "Admins can manage users and assignments, and work in the shared main workspace."
+ : "They will lose access to the admin area and switch to their own assigned workspace."}
+ </p>
+ </>
+ )}
+ </ConfirmDialog>
+
{showCreateDialog && (
<div
className="fixed inset-0 z-50 flex items-center justify-center bg-[var(--overlay)] px-4"
diff --git a/web/components/ui/ConfirmDialog.tsx b/web/components/ui/ConfirmDialog.tsx
new file mode 100644
index 000000000..5623ceb0b
--- /dev/null
+++ b/web/components/ui/ConfirmDialog.tsx
@@ -0,0 +1,112 @@
+"use client";
+
+import { useEffect, type ReactNode } from "react";
+import { X } from "lucide-react";
+
+interface ConfirmDialogProps {
+ open: boolean;
+ title: string;
+ /** Body content — plain text or richer markup (e.g. an avatar row). */
+ children?: ReactNode;
+ confirmLabel: string;
+ cancelLabel?: string;
+ /** "danger" renders a red confirm button for destructive actions. */
+ tone?: "default" | "danger";
+ /** Disables the buttons and swaps the confirm label while pending. */
+ busy?: boolean;
+ busyLabel?: string;
+ onConfirm: () => void;
+ onCancel: () => void;
+}
+
+/**
+ * Small confirmation modal in the app's dialog style (overlay + card),
+ * replacing bare window.confirm() prompts. Closes on Escape and on
+ * overlay click; the cancel button takes initial focus so a stray Enter
+ * never triggers a destructive action.
+ */
+export function ConfirmDialog({
+ open,
+ title,
+ children,
+ confirmLabel,
+ cancelLabel = "Cancel",
+ tone = "default",
+ busy = false,
+ busyLabel,
+ onConfirm,
+ onCancel,
+}: ConfirmDialogProps) {
+ useEffect(() => {
+ if (!open) return;
+ const onKeyDown = (event: KeyboardEvent) => {
+ if (event.key === "Escape" && !busy) onCancel();
+ };
+ window.addEventListener("keydown", onKeyDown);
+ return () => window.removeEventListener("keydown", onKeyDown);
+ }, [open, busy, onCancel]);
+
+ if (!open) return null;
+
+ return (
+ <div
+ className="fixed inset-0 z-50 flex items-center justify-center bg-[var(--overlay)] px-4"
+ role="alertdialog"
+ aria-modal="true"
+ aria-label={title}
+ onClick={() => {
+ if (!busy) onCancel();
+ }}
+ >
+ <div
+ onClick={(e) => e.stopPropagation()}
+ className="w-full max-w-sm rounded-2xl border border-[var(--border)] bg-[var(--card)] p-5 shadow-xl"
+ >
+ <div className="mb-3 flex items-center justify-between">
+ <h2 className="text-base font-semibold text-[var(--foreground)]">
+ {title}
+ </h2>
+ <button
+ type="button"
+ onClick={onCancel}
+ disabled={busy}
+ className="rounded-md p-1 text-[var(--muted-foreground)] hover:bg-[var(--background)] hover:text-[var(--foreground)] disabled:opacity-40"
+ aria-label="Close"
+ >
+ <X size={16} />
+ </button>
+ </div>
+
+ {children && (
+ <div className="mb-4 text-sm text-[var(--muted-foreground)]">
+ {children}
+ </div>
+ )}
+
+ <div className="flex items-center justify-end gap-2">
+ <button
+ type="button"
+ onClick={onCancel}
+ disabled={busy}
+ autoFocus
+ className="rounded-lg px-3 py-1.5 text-sm text-[var(--muted-foreground)] hover:text-[var(--foreground)] disabled:opacity-40"
+ >
+ {cancelLabel}
+ </button>
+ <button
+ type="button"
+ onClick={onConfirm}
+ disabled={busy}
+ className={`rounded-lg px-3 py-1.5 text-sm font-medium disabled:opacity-40 transition-colors ${
+ tone === "danger"
+ ? "bg-red-600 text-white hover:bg-red-700"
+ : "bg-[var(--foreground)] text-[var(--background)] hover:opacity-90"
+ }`}
+ >
+ {busy ? (busyLabel ?? confirmLabel) : confirmLabel}
+ </button>
+ </div>
+ </div>
+ </div>
+ );
+}
diff --git a/web/lib/admin-users.ts b/web/lib/admin-users.ts
new file mode 100644
index 000000000..8f5b36a6d
--- /dev/null
+++ b/web/lib/admin-users.ts
@@ -0,0 +1,17 @@
+/**
+ * Case-insensitive username filter backing the admin Users search box.
+ * An empty / whitespace-only query returns the input list unchanged.
+ *
+ * Generic over `{ username }` (rather than importing UserRecord) so the
+ * module stays alias-free and loadable by the node unit tests.
+ */
+export function filterUsersByQuery<T extends { username: string }>(
+ users: T[],
+ query: string,
+): T[] {
+ const normalized = query.trim().toLowerCase();
+ if (!normalized) return users;
+ return users.filter((user) =>
+ user.username.toLowerCase().includes(normalized),
+ );
+}
diff --git a/web/tests/admin-users-filter.test.ts b/web/tests/admin-users-filter.test.ts
new file mode 100644
index 000000000..c89b486e7
--- /dev/null
+++ b/web/tests/admin-users-filter.test.ts
@@ -0,0 +1,45 @@
+import test from "node:test";
+import assert from "node:assert/strict";
+
+import { filterUsersByQuery } from "../lib/admin-users";
+
+function user(username: string): { id: string; username: string } {
+ return { id: `u_${username}`, username };
+}
+
+const USERS = [user("Alice"), user("bob"), user("alice.smith"), user("Карина")];
+
+test("empty or whitespace-only query returns the list unchanged", () => {
+ assert.equal(filterUsersByQuery(USERS, ""), USERS);
+ assert.equal(filterUsersByQuery(USERS, " "), USERS);
+});
+
+test("matching is case-insensitive and substring-based", () => {
+ assert.deepEqual(
+ filterUsersByQuery(USERS, "ALICE").map((u) => u.username),
+ ["Alice", "alice.smith"],
+ );
+ assert.deepEqual(
+ filterUsersByQuery(USERS, "smith").map((u) => u.username),
+ ["alice.smith"],
+ );
+});
+
+test("query is trimmed before matching", () => {
+ assert.deepEqual(
+ filterUsersByQuery(USERS, " bob ").map((u) => u.username),
+ ["bob"],
+ );
+});
+
+test("non-latin usernames are searchable", () => {
+ assert.deepEqual(
+ filterUsersByQuery(USERS, "кари").map((u) => u.username),
+ ["Карина"],
+ );
+});
+
+test("no match yields an empty list, not an error", () => {
+ assert.deepEqual(filterUsersByQuery(USERS, "zzz"), []);
+ assert.deepEqual(filterUsersByQuery([], "alice"), []);
+});