"use client"; import React from "react"; import Link from "next/link"; import { usePathname } from "next/navigation"; import { LifeBuoy, LogOut, User, Users } from "lucide-react"; import { cn } from "@/lib/utils"; import { useAuth } from "@/components/auth/authContext"; import { logout } from "@/lib/frontend/apiClient"; import { buildLoginUrl, LOGIN_REASONS } from "@/lib/frontend/authRedirect"; import { canManageUsers as canManageUsersRole } from "@/lib/frontend/auth/roles"; import { buildSupportMailto } from "@/lib/frontend/support/supportMailto"; import { Button } from "@/components/ui/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Tooltip, TooltipContent, TooltipTrigger, } from "@/components/ui/tooltip"; function formatRole(role) { if (role === "branch") return "Niederlassung"; if (role === "admin") return "Admin"; if (role === "superadmin") return "Superadmin"; if (role === "dev") return "Entwicklung"; return role ? String(role) : "Unbekannt"; } export default function UserStatus() { const pathname = usePathname() || "/"; const { status, user } = useAuth(); const isAuthenticated = status === "authenticated" && user; const canManageUsers = isAuthenticated && canManageUsersRole(user.role); let text = "Nicht geladen"; if (status === "loading") text = "Lädt…"; if (isAuthenticated) { const roleLabel = formatRole(user.role); text = user.branchId ? `${roleLabel} (${user.branchId})` : roleLabel; } if (status === "unauthenticated") text = "Abgemeldet"; if (status === "error") text = "Fehler"; const currentUrl = typeof window !== "undefined" ? window.location.href : ""; const userAgent = typeof navigator !== "undefined" ? navigator.userAgent : ""; const supportMailto = buildSupportMailto({ user, currentUrl, pathname, userAgent, }); const isProfileActive = pathname === "/profile" || pathname.startsWith("/profile/"); async function handleLogout() { try { await logout(); } catch (err) { console.error("[UserStatus] logout failed:", err); } const loginUrl = buildLoginUrl({ reason: LOGIN_REASONS.LOGGED_OUT }); window.location.replace(loginUrl); } return ( Benutzermenü Benutzer
{isAuthenticated ? `Angemeldet als: ${text}` : "Keine aktive Sitzung."}
{canManageUsers ? ( ) : null} { e.preventDefault(); handleLogout(); }} >
); }