NavigationSidebar.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 {
  9. AGENT_PAGES,
  10. type NavigationPage,
  11. } from "@/lib/constants/agent-pages";
  12. export type { NavigationPage };
  13. interface NavigationSidebarProps {
  14. currentPage?: NavigationPage;
  15. onNavigate?: (page: NavigationPage) => void;
  16. onProfileClick?: () => void;
  17. onLogout?: () => void;
  18. avatarUrl?: string | null;
  19. /** 顶部/左侧“对话”图标角标展示用:总未读消息数 */
  20. unreadChatCount?: number;
  21. }
  22. export function NavigationSidebar({
  23. currentPage = "dashboard",
  24. onNavigate,
  25. onProfileClick,
  26. onLogout,
  27. avatarUrl,
  28. unreadChatCount = 0,
  29. }: NavigationSidebarProps) {
  30. const { agent } = useAuth();
  31. const [profileMenuOpen, setProfileMenuOpen] = useState(false);
  32. const menuRef = useRef<HTMLDivElement>(null);
  33. const isAdmin = agent?.role === "admin";
  34. const handleNavigate = (page: NavigationPage) => {
  35. onNavigate?.(page);
  36. };
  37. useEffect(() => {
  38. const handleClickOutside = (event: MouseEvent) => {
  39. if (menuRef.current && !menuRef.current.contains(event.target as Node)) {
  40. setProfileMenuOpen(false);
  41. }
  42. };
  43. if (profileMenuOpen) {
  44. document.addEventListener("mousedown", handleClickOutside);
  45. }
  46. return () => {
  47. document.removeEventListener("mousedown", handleClickOutside);
  48. };
  49. }, [profileMenuOpen]);
  50. const avatarColor = getAvatarColor(agent?.username || "");
  51. const displayInitial = getAvatarInitial(agent?.username || "");
  52. const fullAvatarUrl = getAvatarUrl(avatarUrl);
  53. const visiblePages = AGENT_PAGES.filter(
  54. (p) => !p.adminOnly || (p.adminOnly && isAdmin)
  55. );
  56. return (
  57. <div className="w-16 bg-gray-50 flex flex-col items-center py-4 border-r border-gray-200 h-full">
  58. {visiblePages.map((page) => {
  59. const isActive = currentPage === page.id;
  60. const Icon = page.Icon;
  61. const showUnread = page.id === "dashboard" && unreadChatCount > 0;
  62. return (
  63. <button
  64. key={page.id}
  65. className={`w-10 h-10 rounded-lg flex items-center justify-center mb-4 transition-colors ${
  66. isActive
  67. ? "bg-green-600 hover:bg-green-700"
  68. : "bg-white border border-gray-200 hover:bg-gray-100"
  69. }`}
  70. title={page.title}
  71. onClick={() => handleNavigate(page.id as NavigationPage)}
  72. >
  73. <div className="relative w-full h-full flex items-center justify-center">
  74. <Icon
  75. className={`w-6 h-6 ${
  76. isActive ? "text-white" : "text-gray-600"
  77. }`}
  78. />
  79. {showUnread && (
  80. <Badge
  81. variant="destructive"
  82. 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"
  83. >
  84. {unreadChatCount > 99 ? "99+" : unreadChatCount}
  85. </Badge>
  86. )}
  87. </div>
  88. </button>
  89. );
  90. })}
  91. {/* 个人资料按钮和 GitHub 按钮(固定在底部) */}
  92. <div className="mt-auto flex flex-col items-center gap-2">
  93. <div className="relative" ref={menuRef}>
  94. <button
  95. className={`w-10 h-10 rounded-lg flex items-center justify-center transition-colors ${
  96. profileMenuOpen
  97. ? "bg-primary text-primary-foreground"
  98. : "bg-white border border-gray-200 hover:bg-gray-100"
  99. }`}
  100. title="个人资料"
  101. onClick={() => setProfileMenuOpen(!profileMenuOpen)}
  102. >
  103. {fullAvatarUrl ? (
  104. <img
  105. src={fullAvatarUrl}
  106. alt={agent?.username || "用户"}
  107. className="w-8 h-8 rounded-full object-cover"
  108. />
  109. ) : (
  110. <div
  111. className="w-8 h-8 rounded-full flex items-center justify-center text-white text-xs font-semibold"
  112. style={{ backgroundColor: avatarColor }}
  113. >
  114. {displayInitial}
  115. </div>
  116. )}
  117. </button>
  118. {profileMenuOpen && (
  119. <div className="absolute bottom-12 left-0 w-64 bg-white border border-gray-200 rounded-lg shadow-lg z-50">
  120. <div className="p-4 border-b border-gray-200">
  121. <div className="flex items-center gap-3">
  122. {fullAvatarUrl ? (
  123. <img
  124. src={fullAvatarUrl}
  125. alt={agent?.username || "用户"}
  126. className="w-12 h-12 rounded-full object-cover"
  127. />
  128. ) : (
  129. <div
  130. className="w-12 h-12 rounded-full flex items-center justify-center text-white text-sm font-semibold"
  131. style={{ backgroundColor: avatarColor }}
  132. >
  133. {displayInitial}
  134. </div>
  135. )}
  136. <div>
  137. <div className="text-sm font-semibold text-foreground">
  138. {agent?.username || "用户"}
  139. </div>
  140. <div className="text-xs text-muted-foreground">
  141. {agent?.role === "admin" ? "管理员" : "客服"}
  142. </div>
  143. </div>
  144. </div>
  145. </div>
  146. <div className="p-2">
  147. <Button
  148. variant="ghost"
  149. size="sm"
  150. className="w-full justify-start"
  151. onClick={() => {
  152. setProfileMenuOpen(false);
  153. onProfileClick?.();
  154. }}
  155. >
  156. <svg
  157. className="w-4 h-4 mr-2"
  158. fill="none"
  159. stroke="currentColor"
  160. viewBox="0 0 24 24"
  161. >
  162. <path
  163. strokeLinecap="round"
  164. strokeLinejoin="round"
  165. strokeWidth={2}
  166. d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"
  167. />
  168. </svg>
  169. 个人资料
  170. </Button>
  171. <Button
  172. variant="ghost"
  173. size="sm"
  174. className="w-full justify-start text-red-600 hover:text-red-700 hover:bg-red-50"
  175. onClick={() => {
  176. setProfileMenuOpen(false);
  177. onLogout?.();
  178. }}
  179. >
  180. <svg
  181. className="w-4 h-4 mr-2"
  182. fill="none"
  183. stroke="currentColor"
  184. viewBox="0 0 24 24"
  185. >
  186. <path
  187. strokeLinecap="round"
  188. strokeLinejoin="round"
  189. strokeWidth={2}
  190. 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"
  191. />
  192. </svg>
  193. 退出登录
  194. </Button>
  195. </div>
  196. </div>
  197. )}
  198. </div>
  199. <Button
  200. variant="ghost"
  201. size="sm"
  202. asChild
  203. className="w-10 h-10 rounded-lg bg-white border border-gray-200 hover:bg-gray-100 transition-colors p-0"
  204. title="GitHub"
  205. >
  206. <a
  207. href={websiteConfig.github.repo}
  208. target="_blank"
  209. rel="noopener noreferrer"
  210. >
  211. <svg
  212. className="w-5 h-5 text-gray-600"
  213. fill="currentColor"
  214. viewBox="0 0 24 24"
  215. >
  216. <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" />
  217. </svg>
  218. </a>
  219. </Button>
  220. </div>
  221. </div>
  222. );
  223. }