Add "login as user" to user profile card
Extract ImpersonateButton into a shared component and reuse it on the admin user profile page (header, hidden for admins) — same behaviour as the users list. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
|
||||
export function ImpersonateButton({
|
||||
userId,
|
||||
className,
|
||||
}: {
|
||||
userId: string;
|
||||
className?: string;
|
||||
}) {
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
async function handleImpersonate() {
|
||||
setLoading(true);
|
||||
try {
|
||||
const res = await fetch("/api/auth/admin/impersonate-user", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ userId }),
|
||||
credentials: "include",
|
||||
});
|
||||
if (!res.ok) throw new Error(await res.text());
|
||||
window.location.href = "/dashboard";
|
||||
} catch (e) {
|
||||
console.error("Impersonation failed:", e);
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleImpersonate}
|
||||
disabled={loading}
|
||||
className={`text-xs px-2 py-1 transition-colors ${className ?? ""}`}
|
||||
style={{
|
||||
border: "1px solid var(--border)",
|
||||
color: loading ? "var(--muted-foreground)" : "var(--foreground)",
|
||||
background: "transparent",
|
||||
}}
|
||||
>
|
||||
{loading ? "..." : "Войти как"}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import { useState } from "react";
|
||||
import Link from "next/link";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { QuickEnrollButton } from "@/components/admin/quick-enroll-modal";
|
||||
import { ImpersonateButton } from "@/components/admin/impersonate-button";
|
||||
|
||||
type Enrollment = {
|
||||
courseId: string;
|
||||
@@ -91,43 +92,6 @@ function UserPopup({ user }: { user: UserRow }) {
|
||||
);
|
||||
}
|
||||
|
||||
function ImpersonateButton({ userId }: { userId: string }) {
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
async function handleImpersonate() {
|
||||
setLoading(true);
|
||||
try {
|
||||
const res = await fetch("/api/auth/admin/impersonate-user", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ userId }),
|
||||
credentials: "include",
|
||||
});
|
||||
if (!res.ok) throw new Error(await res.text());
|
||||
window.location.href = "/dashboard";
|
||||
} catch (e) {
|
||||
console.error("Impersonation failed:", e);
|
||||
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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user