"use client"; import React, { useEffect, useState } from "react"; import { usePathname } from "next/navigation"; import { Eye } from "lucide-react"; import { currentImpersonation, endImpersonation, logImpersonatedPage, type Impersonation } from "@/lib/impersonation"; /** Bright bar on every page while an admin is viewing the site as someone else. */ export function ImpersonationBanner() { const pathname = usePathname(); const [imp, setImp] = useState(null); const [left, setLeft] = useState(""); const [ending, setEnding] = useState(false); useEffect(() => { setImp(currentImpersonation()); }, []); // Every page viewed goes into the session's audit trail. useEffect(() => { if (imp && pathname) logImpersonatedPage(pathname + (typeof window !== "undefined" ? window.location.search : "")); }, [imp, pathname]); useEffect(() => { if (!imp) return; const tick = () => { const ms = new Date(imp.expiresAt).getTime() - Date.now(); if (ms <= 0) { endImpersonation(); return; } const m = Math.floor(ms / 60000), s = Math.floor((ms % 60000) / 1000); setLeft(`${m}:${String(s).padStart(2, "0")}`); }; tick(); const t = setInterval(tick, 1000); return () => clearInterval(t); }, [imp]); if (!imp) return null; return (
Viewing as {imp.name} ({imp.email}) — read-only, nothing can be changed. {left} left
); }