UserStatus.jsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. "use client";
  2. import React from "react";
  3. import Link from "next/link";
  4. import { usePathname } from "next/navigation";
  5. import { LifeBuoy, LogOut, User } from "lucide-react";
  6. import { cn } from "@/lib/utils";
  7. import { useAuth } from "@/components/auth/authContext";
  8. import { logout } from "@/lib/frontend/apiClient";
  9. import { buildLoginUrl, LOGIN_REASONS } from "@/lib/frontend/authRedirect";
  10. import { Button } from "@/components/ui/button";
  11. import {
  12. DropdownMenu,
  13. DropdownMenuContent,
  14. DropdownMenuItem,
  15. DropdownMenuLabel,
  16. DropdownMenuSeparator,
  17. DropdownMenuTrigger,
  18. } from "@/components/ui/dropdown-menu";
  19. import {
  20. Tooltip,
  21. TooltipContent,
  22. TooltipTrigger,
  23. } from "@/components/ui/tooltip";
  24. function formatRole(role) {
  25. if (role === "branch") return "Niederlassung";
  26. if (role === "admin") return "Admin";
  27. if (role === "superadmin") return "Superadmin";
  28. if (role === "dev") return "Entwicklung";
  29. return role ? String(role) : "Unbekannt";
  30. }
  31. function buildSupportMailto({ user, currentUrl, pathname, userAgent }) {
  32. const to = "info@attus.de";
  33. const roleLabel = user ? formatRole(user.role) : "Unbekannt";
  34. const userLabel = user?.branchId
  35. ? `${roleLabel} (${user.branchId})`
  36. : roleLabel;
  37. const now = new Date();
  38. const tz =
  39. typeof Intl !== "undefined"
  40. ? Intl.DateTimeFormat().resolvedOptions().timeZone
  41. : "";
  42. const timestampLocal = now.toLocaleString("de-DE");
  43. const timestampIso = now.toISOString();
  44. const routeLine = pathname ? `Route: ${pathname}` : "Route: (unbekannt)";
  45. const urlLine = currentUrl ? `URL: ${currentUrl}` : "URL: (bitte einfügen)";
  46. const uaLine = userAgent
  47. ? `User-Agent: ${userAgent}`
  48. : "User-Agent: (unbekannt)";
  49. const timeLine = tz
  50. ? `Zeitpunkt: ${timestampLocal} (${tz})`
  51. : `Zeitpunkt: ${timestampLocal}`;
  52. const isoLine = `ISO: ${timestampIso}`;
  53. const subject = user?.branchId
  54. ? `Support – RHL Lieferscheine (${user.branchId})`
  55. : "Support – RHL Lieferscheine";
  56. const body = [
  57. "Hallo attus Support,",
  58. "",
  59. "bitte beschreibt hier kurz das Anliegen:",
  60. "",
  61. "- Was wollten Sie tun?",
  62. "- Was ist passiert?",
  63. "- (Optional) Screenshot / Zeitpunkt",
  64. "",
  65. "--- Kontext (bitte drin lassen) ---",
  66. `Benutzer: ${userLabel}`,
  67. routeLine,
  68. urlLine,
  69. timeLine,
  70. isoLine,
  71. uaLine,
  72. "----------------------------------",
  73. "",
  74. "Vielen Dank.",
  75. ].join("\r\n");
  76. return `mailto:${to}?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(
  77. body,
  78. )}`;
  79. }
  80. export default function UserStatus() {
  81. const pathname = usePathname() || "/";
  82. const { status, user } = useAuth();
  83. const isAuthenticated = status === "authenticated" && user;
  84. let text = "Nicht geladen";
  85. if (status === "loading") text = "Lädt…";
  86. if (isAuthenticated) {
  87. const roleLabel = formatRole(user.role);
  88. text = user.branchId ? `${roleLabel} (${user.branchId})` : roleLabel;
  89. }
  90. if (status === "unauthenticated") text = "Abgemeldet";
  91. if (status === "error") text = "Fehler";
  92. const currentUrl = typeof window !== "undefined" ? window.location.href : "";
  93. const userAgent = typeof navigator !== "undefined" ? navigator.userAgent : "";
  94. const supportMailto = buildSupportMailto({
  95. user,
  96. currentUrl,
  97. pathname,
  98. userAgent,
  99. });
  100. const isProfileActive =
  101. pathname === "/profile" || pathname.startsWith("/profile/");
  102. async function handleLogout() {
  103. try {
  104. await logout();
  105. } catch (err) {
  106. console.error("[UserStatus] logout failed:", err);
  107. }
  108. const loginUrl = buildLoginUrl({ reason: LOGIN_REASONS.LOGGED_OUT });
  109. window.location.replace(loginUrl);
  110. }
  111. return (
  112. <DropdownMenu>
  113. <Tooltip>
  114. <TooltipTrigger asChild>
  115. <DropdownMenuTrigger asChild>
  116. <Button
  117. type="button"
  118. variant="ghost"
  119. size="sm"
  120. aria-label="Benutzermenü öffnen"
  121. className={cn(
  122. "gap-2",
  123. "px-2 md:px-3",
  124. isProfileActive ? "bg-accent" : "",
  125. )}
  126. >
  127. <User className="h-4 w-4" aria-hidden="true" />
  128. <span className="hidden text-xs md:inline">{text}</span>
  129. </Button>
  130. </DropdownMenuTrigger>
  131. </TooltipTrigger>
  132. <TooltipContent side="bottom">Benutzermenü</TooltipContent>
  133. </Tooltip>
  134. <DropdownMenuContent align="end" className="min-w-56">
  135. <DropdownMenuLabel>Benutzer</DropdownMenuLabel>
  136. <div className="px-2 pb-2 text-xs text-muted-foreground">
  137. {isAuthenticated
  138. ? `Angemeldet als: ${text}`
  139. : "Keine aktive Sitzung."}
  140. </div>
  141. <DropdownMenuSeparator />
  142. <DropdownMenuItem asChild disabled={!isAuthenticated}>
  143. <Link href="/profile" className="flex w-full items-center gap-2">
  144. <User className="h-4 w-4" aria-hidden="true" />
  145. Profil
  146. </Link>
  147. </DropdownMenuItem>
  148. <DropdownMenuItem asChild>
  149. <a href={supportMailto} className="flex w-full items-center gap-2">
  150. <LifeBuoy className="h-4 w-4" aria-hidden="true" />
  151. Support
  152. </a>
  153. </DropdownMenuItem>
  154. <DropdownMenuSeparator />
  155. <DropdownMenuItem
  156. variant="destructive"
  157. disabled={!isAuthenticated}
  158. onSelect={(e) => {
  159. e.preventDefault();
  160. handleLogout();
  161. }}
  162. >
  163. <LogOut className="h-4 w-4" aria-hidden="true" />
  164. Abmelden
  165. </DropdownMenuItem>
  166. </DropdownMenuContent>
  167. </DropdownMenu>
  168. );
  169. }