Add admin impersonation button to users table

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-25 12:52:13 +05:00
parent e590f541b3
commit ce305eab58
+38 -1
View File
@@ -1,8 +1,10 @@
"use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
import Link from "next/link";
import { Badge } from "@/components/ui/badge";
import { authClient } from "@/lib/auth-client";
type Enrollment = {
courseId: string;
@@ -90,6 +92,38 @@ function UserPopup({ user }: { user: UserRow }) {
);
}
function ImpersonateButton({ userId }: { userId: string }) {
const router = useRouter();
const [loading, setLoading] = useState(false);
async function handleImpersonate() {
setLoading(true);
try {
await authClient.admin.impersonateUser({ userId });
router.push("/dashboard");
router.refresh();
} finally {
setLoading(false);
}
}
return (
<button
type="button"
onClick={handleImpersonate}
disabled={loading}
className="text-xs px-2 py-1 transition-colors"
style={{
border: "1px solid var(--border)",
color: loading ? "var(--muted-foreground)" : "var(--foreground)",
background: "transparent",
}}
>
{loading ? "..." : "Войти как"}
</button>
);
}
export function UsersTable({ users }: { users: UserRow[] }) {
const [hoveredId, setHoveredId] = useState<string | null>(null);
@@ -134,8 +168,10 @@ export function UsersTable({ users }: { users: UserRow[] }) {
<td className="px-5 py-3 text-sm text-slate-400">
{new Date(user.createdAt).toLocaleDateString("ru-RU")}
</td>
{/* Hover popup trigger */}
{/* Actions */}
<td className="px-3 py-3 relative">
<div className="flex items-center gap-2">
{user.role !== "admin" && <ImpersonateButton userId={user.id} />}
<div
className="relative inline-block"
onMouseEnter={() => setHoveredId(user.id)}
@@ -150,6 +186,7 @@ export function UsersTable({ users }: { users: UserRow[] }) {
</button>
{hoveredId === user.id && <UserPopup user={user} />}
</div>
</div>
</td>
</tr>
))}