NavigationSidebar.tsx 11 KB

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