NavigationSidebar.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. "use client";
  2. import { useState, useRef, useEffect } from "react";
  3. import { useAuth } from "@/features/agent/hooks/useAuth";
  4. import { getAvatarUrl, getAvatarColor, getAvatarInitial } from "@/utils/avatar";
  5. import { Button } from "@/components/ui/button";
  6. import { websiteConfig } from "@/lib/website-config";
  7. import { Badge } from "@/components/ui/badge";
  8. import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
  9. import {
  10. AGENT_PAGES,
  11. type NavigationPage,
  12. } from "@/lib/constants/agent-pages";
  13. export type { NavigationPage };
  14. interface NavigationSidebarProps {
  15. currentPage?: NavigationPage;
  16. onNavigate?: (page: NavigationPage) => void;
  17. onProfileClick?: () => void;
  18. onLogout?: () => void;
  19. avatarUrl?: string | null;
  20. /** 顶部/左侧“对话”图标角标展示用:总未读消息数 */
  21. unreadChatCount?: number;
  22. }
  23. export function NavigationSidebar({
  24. currentPage = "dashboard",
  25. onNavigate,
  26. onProfileClick,
  27. onLogout,
  28. avatarUrl,
  29. unreadChatCount = 0,
  30. }: NavigationSidebarProps) {
  31. const { agent } = useAuth();
  32. const [profileMenuOpen, setProfileMenuOpen] = useState(false);
  33. const menuRef = useRef<HTMLDivElement>(null);
  34. const isAdmin = agent?.role === "admin";
  35. const permissions = agent?.permissions ?? [];
  36. const handleNavigate = (page: NavigationPage) => {
  37. onNavigate?.(page);
  38. };
  39. useEffect(() => {
  40. const handleClickOutside = (event: MouseEvent) => {
  41. if (menuRef.current && !menuRef.current.contains(event.target as Node)) {
  42. setProfileMenuOpen(false);
  43. }
  44. };
  45. if (profileMenuOpen) {
  46. document.addEventListener("mousedown", handleClickOutside);
  47. }
  48. return () => {
  49. document.removeEventListener("mousedown", handleClickOutside);
  50. };
  51. }, [profileMenuOpen]);
  52. const avatarColor = getAvatarColor(agent?.username || "");
  53. const displayInitial = getAvatarInitial(agent?.username || "");
  54. const fullAvatarUrl = getAvatarUrl(avatarUrl);
  55. const visiblePages = AGENT_PAGES.filter((p) => {
  56. if (isAdmin) return true;
  57. const need = p.requiredPermission;
  58. if (!need) return true;
  59. return permissions.includes(need);
  60. });
  61. return (
  62. <TooltipProvider delayDuration={0}>
  63. <div className="w-16 bg-gray-50 flex flex-col items-center py-4 border-r border-gray-200 h-full">
  64. <div className="mt-2 px-3 flex flex-col items-center gap-2">
  65. {visiblePages.map((page) => {
  66. const isActive = currentPage === page.id;
  67. const Icon = page.Icon;
  68. const showUnread = page.id === "dashboard" && unreadChatCount > 0;
  69. return (
  70. <Tooltip key={page.id}>
  71. <TooltipTrigger asChild>
  72. <button
  73. className={`w-10 h-10 rounded-lg flex items-center justify-center transition-colors ${
  74. isActive
  75. ? "bg-green-600 hover:bg-green-700 text-white"
  76. : "bg-white border border-gray-200 hover:bg-gray-100 text-gray-700"
  77. }`}
  78. onClick={() => handleNavigate(page.id as NavigationPage)}
  79. aria-label={page.title}
  80. >
  81. <div className="relative flex items-center justify-center">
  82. <Icon className={`h-5 w-5 ${isActive ? "text-white" : "text-gray-600"}`} />
  83. {showUnread && (
  84. <Badge
  85. variant="destructive"
  86. className="absolute -top-1 -right-1 px-1 py-0 h-4 min-w-4 rounded-full text-[10px] leading-none flex items-center justify-center"
  87. >
  88. {unreadChatCount > 99 ? "99+" : unreadChatCount}
  89. </Badge>
  90. )}
  91. </div>
  92. </button>
  93. </TooltipTrigger>
  94. <TooltipContent side="right">{page.title}</TooltipContent>
  95. </Tooltip>
  96. );
  97. })}
  98. </div>
  99. {/* 个人资料按钮和 GitHub 按钮(固定在底部) */}
  100. <div className="mt-auto flex flex-col items-center gap-2">
  101. <div className="relative" ref={menuRef}>
  102. <Tooltip>
  103. <TooltipTrigger asChild>
  104. <button
  105. className={`w-10 h-10 rounded-lg flex items-center justify-center transition-colors ${
  106. profileMenuOpen
  107. ? "bg-primary text-primary-foreground"
  108. : "bg-white border border-gray-200 hover:bg-gray-100"
  109. }`}
  110. onClick={() => setProfileMenuOpen(!profileMenuOpen)}
  111. aria-label="个人资料"
  112. >
  113. <div className="flex items-center justify-center">
  114. {fullAvatarUrl ? (
  115. <img
  116. src={fullAvatarUrl}
  117. alt={agent?.username || "用户"}
  118. className="w-8 h-8 rounded-full object-cover"
  119. />
  120. ) : (
  121. <div
  122. className="w-8 h-8 rounded-full flex items-center justify-center text-white text-xs font-semibold"
  123. style={{ backgroundColor: avatarColor }}
  124. >
  125. {displayInitial}
  126. </div>
  127. )}
  128. </div>
  129. </button>
  130. </TooltipTrigger>
  131. <TooltipContent side="right">个人资料</TooltipContent>
  132. </Tooltip>
  133. {profileMenuOpen && (
  134. <div className="absolute bottom-12 left-0 w-64 bg-white border border-gray-200 rounded-lg shadow-lg z-50">
  135. <div className="p-4 border-b border-gray-200">
  136. <div className="flex items-center gap-3">
  137. {fullAvatarUrl ? (
  138. <img
  139. src={fullAvatarUrl}
  140. alt={agent?.username || "用户"}
  141. className="w-12 h-12 rounded-full object-cover"
  142. />
  143. ) : (
  144. <div
  145. className="w-12 h-12 rounded-full flex items-center justify-center text-white text-sm font-semibold"
  146. style={{ backgroundColor: avatarColor }}
  147. >
  148. {displayInitial}
  149. </div>
  150. )}
  151. <div>
  152. <div className="text-sm font-semibold text-foreground">
  153. {agent?.username || "用户"}
  154. </div>
  155. <div className="text-xs text-muted-foreground">
  156. {agent?.role === "admin" ? "管理员" : "客服"}
  157. </div>
  158. </div>
  159. </div>
  160. </div>
  161. <div className="p-2">
  162. <Button
  163. variant="ghost"
  164. size="sm"
  165. className="w-full justify-start"
  166. onClick={() => {
  167. setProfileMenuOpen(false);
  168. onProfileClick?.();
  169. }}
  170. >
  171. <svg
  172. className="w-4 h-4 mr-2"
  173. fill="none"
  174. stroke="currentColor"
  175. viewBox="0 0 24 24"
  176. >
  177. <path
  178. strokeLinecap="round"
  179. strokeLinejoin="round"
  180. strokeWidth={2}
  181. d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"
  182. />
  183. </svg>
  184. 个人资料
  185. </Button>
  186. <Button
  187. variant="ghost"
  188. size="sm"
  189. className="w-full justify-start text-red-600 hover:text-red-700 hover:bg-red-50"
  190. onClick={() => {
  191. setProfileMenuOpen(false);
  192. onLogout?.();
  193. }}
  194. >
  195. <svg
  196. className="w-4 h-4 mr-2"
  197. fill="none"
  198. stroke="currentColor"
  199. viewBox="0 0 24 24"
  200. >
  201. <path
  202. strokeLinecap="round"
  203. strokeLinejoin="round"
  204. strokeWidth={2}
  205. d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1"
  206. />
  207. </svg>
  208. 退出登录
  209. </Button>
  210. </div>
  211. </div>
  212. )}
  213. </div>
  214. <Tooltip>
  215. <TooltipTrigger asChild>
  216. <Button
  217. variant="ghost"
  218. size="sm"
  219. asChild
  220. className="w-10 h-10 rounded-lg bg-white border border-gray-200 hover:bg-gray-100 transition-colors p-0"
  221. >
  222. <a
  223. href={websiteConfig.github.repo}
  224. target="_blank"
  225. rel="noopener noreferrer"
  226. aria-label="GitHub"
  227. >
  228. <svg
  229. className="w-5 h-5 text-gray-600"
  230. fill="currentColor"
  231. viewBox="0 0 24 24"
  232. >
  233. <path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z" />
  234. </svg>
  235. </a>
  236. </Button>
  237. </TooltipTrigger>
  238. <TooltipContent side="right">GitHub</TooltipContent>
  239. </Tooltip>
  240. </div>
  241. </div>
  242. </TooltipProvider>
  243. );
  244. }