"use client"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { Button } from "@/components/ui/button"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; type Target = { id: number | string; username?: string | null; displayName?: string | null }; export function ImpersonationControl({ users }: { users: Target[] }) { const router = useRouter(); const [userId, setUserId] = useState(""); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); async function start() { if (!userId) return; setBusy(true); setError(null); try { const response = await fetch("/api/impersonation/start", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ userId }), }); const body = await response.json().catch(() => null); if (!response.ok) throw new Error(body?.error ?? "Unable to start impersonation."); router.push("/"); router.refresh(); } catch (e) { setError(e instanceof Error ? e.message : "Unable to start impersonation."); } finally { setBusy(false); } } return (

Impersonate a user

All requests will use the selected user's identity and permissions until you return to your account.

{error &&

{error}

}
); }