Add admin impersonation button to users table
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
|
import { useRouter } from "next/navigation";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { Badge } from "@/components/ui/badge";
|
import { Badge } from "@/components/ui/badge";
|
||||||
|
import { authClient } from "@/lib/auth-client";
|
||||||
|
|
||||||
type Enrollment = {
|
type Enrollment = {
|
||||||
courseId: string;
|
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[] }) {
|
export function UsersTable({ users }: { users: UserRow[] }) {
|
||||||
const [hoveredId, setHoveredId] = useState<string | null>(null);
|
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">
|
<td className="px-5 py-3 text-sm text-slate-400">
|
||||||
{new Date(user.createdAt).toLocaleDateString("ru-RU")}
|
{new Date(user.createdAt).toLocaleDateString("ru-RU")}
|
||||||
</td>
|
</td>
|
||||||
{/* Hover popup trigger */}
|
{/* Actions */}
|
||||||
<td className="px-3 py-3 relative">
|
<td className="px-3 py-3 relative">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
{user.role !== "admin" && <ImpersonateButton userId={user.id} />}
|
||||||
<div
|
<div
|
||||||
className="relative inline-block"
|
className="relative inline-block"
|
||||||
onMouseEnter={() => setHoveredId(user.id)}
|
onMouseEnter={() => setHoveredId(user.id)}
|
||||||
@@ -150,6 +186,7 @@ export function UsersTable({ users }: { users: UserRow[] }) {
|
|||||||
</button>
|
</button>
|
||||||
{hoveredId === user.id && <UserPopup user={user} />}
|
{hoveredId === user.id && <UserPopup user={user} />}
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
))}
|
))}
|
||||||
|
|||||||
Reference in New Issue
Block a user