NavigationSidebar.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. export type NavigationPage = "dashboard" | "faqs" | "users" | "settings";
  8. interface NavigationSidebarProps {
  9. currentPage?: NavigationPage;
  10. onNavigate?: (page: NavigationPage) => void;
  11. onProfileClick?: () => void;
  12. onLogout?: () => void;
  13. avatarUrl?: string | null;
  14. }
  15. export function NavigationSidebar({
  16. currentPage = "dashboard",
  17. onNavigate,
  18. onProfileClick,
  19. onLogout,
  20. avatarUrl,
  21. }: NavigationSidebarProps) {
  22. const { agent } = useAuth();
  23. const [profileMenuOpen, setProfileMenuOpen] = useState(false);
  24. const menuRef = useRef<HTMLDivElement>(null);
  25. // 检查当前用户是否是管理员
  26. const isAdmin = agent?.role === "admin";
  27. const handleNavigate = (page: NavigationPage) => {
  28. if (onNavigate) {
  29. onNavigate(page);
  30. }
  31. };
  32. // 点击外部关闭菜单
  33. useEffect(() => {
  34. const handleClickOutside = (event: MouseEvent) => {
  35. if (menuRef.current && !menuRef.current.contains(event.target as Node)) {
  36. setProfileMenuOpen(false);
  37. }
  38. };
  39. if (profileMenuOpen) {
  40. document.addEventListener("mousedown", handleClickOutside);
  41. }
  42. return () => {
  43. document.removeEventListener("mousedown", handleClickOutside);
  44. };
  45. }, [profileMenuOpen]);
  46. // 头像相关
  47. const avatarColor = getAvatarColor(agent?.username || "");
  48. const displayInitial = getAvatarInitial(agent?.username || "");
  49. const fullAvatarUrl = getAvatarUrl(avatarUrl);
  50. return (
  51. <div className="w-16 bg-gray-50 flex flex-col items-center py-4 border-r border-gray-200 h-full">
  52. <button
  53. className={`w-10 h-10 rounded-lg flex items-center justify-center mb-4 transition-colors ${
  54. currentPage === "dashboard"
  55. ? "bg-green-600 hover:bg-green-700"
  56. : "bg-white border border-gray-200 hover:bg-gray-100"
  57. }`}
  58. title="对话"
  59. onClick={() => handleNavigate("dashboard")}
  60. >
  61. <svg
  62. className={`w-6 h-6 ${
  63. currentPage === "dashboard" ? "text-white" : "text-gray-600"
  64. }`}
  65. fill="none"
  66. stroke="currentColor"
  67. viewBox="0 0 24 24"
  68. >
  69. <path
  70. strokeLinecap="round"
  71. strokeLinejoin="round"
  72. strokeWidth={2}
  73. d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z"
  74. />
  75. </svg>
  76. </button>
  77. <button
  78. className="w-10 h-10 rounded-lg bg-white border border-gray-200 flex items-center justify-center mb-4 hover:bg-gray-100 transition-colors"
  79. title="知识库"
  80. disabled
  81. >
  82. <svg
  83. className="w-6 h-6 text-gray-600"
  84. fill="none"
  85. stroke="currentColor"
  86. viewBox="0 0 24 24"
  87. >
  88. <path
  89. strokeLinecap="round"
  90. strokeLinejoin="round"
  91. strokeWidth={2}
  92. d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5s3.332.477 4.5 1.253v13C19.832 18.477 18.246 18 16.5 18s-3.332.477-4.5 1.253"
  93. />
  94. </svg>
  95. </button>
  96. <button
  97. className={`w-10 h-10 rounded-lg flex items-center justify-center mb-4 transition-colors ${
  98. currentPage === "faqs"
  99. ? "bg-green-600 hover:bg-green-700"
  100. : "bg-white border border-gray-200 hover:bg-gray-100"
  101. }`}
  102. title="事件管理"
  103. onClick={() => handleNavigate("faqs")}
  104. >
  105. <svg
  106. className={`w-6 h-6 ${
  107. currentPage === "faqs" ? "text-white" : "text-gray-600"
  108. }`}
  109. fill="none"
  110. stroke="currentColor"
  111. viewBox="0 0 24 24"
  112. >
  113. <path
  114. strokeLinecap="round"
  115. strokeLinejoin="round"
  116. strokeWidth={2}
  117. d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"
  118. />
  119. </svg>
  120. </button>
  121. {isAdmin && (
  122. <button
  123. className={`w-10 h-10 rounded-lg flex items-center justify-center mb-4 transition-colors ${
  124. currentPage === "users"
  125. ? "bg-green-600 hover:bg-green-700"
  126. : "bg-white border border-gray-200 hover:bg-gray-100"
  127. }`}
  128. title="用户管理"
  129. onClick={() => handleNavigate("users")}
  130. >
  131. <svg
  132. className={`w-6 h-6 ${
  133. currentPage === "users" ? "text-white" : "text-gray-600"
  134. }`}
  135. fill="none"
  136. stroke="currentColor"
  137. viewBox="0 0 24 24"
  138. >
  139. <path
  140. strokeLinecap="round"
  141. strokeLinejoin="round"
  142. strokeWidth={2}
  143. d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z"
  144. />
  145. </svg>
  146. </button>
  147. )}
  148. <button
  149. className={`w-10 h-10 rounded-lg flex items-center justify-center mb-4 transition-colors ${
  150. currentPage === "settings"
  151. ? "bg-green-600 hover:bg-green-700"
  152. : "bg-white border border-gray-200 hover:bg-gray-100"
  153. }`}
  154. title="AI 配置"
  155. onClick={() => handleNavigate("settings")}
  156. >
  157. <svg
  158. className={`w-6 h-6 ${
  159. currentPage === "settings" ? "text-white" : "text-gray-600"
  160. }`}
  161. fill="none"
  162. stroke="currentColor"
  163. viewBox="0 0 24 24"
  164. >
  165. <path
  166. strokeLinecap="round"
  167. strokeLinejoin="round"
  168. strokeWidth={2}
  169. d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z"
  170. />
  171. </svg>
  172. </button>
  173. {/* 个人资料按钮和 GitHub 按钮(固定在底部) */}
  174. <div className="mt-auto flex flex-col items-center gap-2">
  175. {/* 个人资料按钮 */}
  176. <div className="relative" ref={menuRef}>
  177. <button
  178. className={`w-10 h-10 rounded-lg flex items-center justify-center transition-colors ${
  179. profileMenuOpen
  180. ? "bg-primary text-primary-foreground"
  181. : "bg-white border border-gray-200 hover:bg-gray-100"
  182. }`}
  183. title="个人资料"
  184. onClick={() => setProfileMenuOpen(!profileMenuOpen)}
  185. >
  186. {fullAvatarUrl ? (
  187. <img
  188. src={fullAvatarUrl}
  189. alt={agent?.username || "用户"}
  190. className="w-8 h-8 rounded-full object-cover"
  191. />
  192. ) : (
  193. <div
  194. className="w-8 h-8 rounded-full flex items-center justify-center text-white text-xs font-semibold"
  195. style={{ backgroundColor: avatarColor }}
  196. >
  197. {displayInitial}
  198. </div>
  199. )}
  200. </button>
  201. {/* 下拉菜单 */}
  202. {profileMenuOpen && (
  203. <div className="absolute bottom-12 left-0 w-64 bg-white border border-gray-200 rounded-lg shadow-lg z-50">
  204. {/* 用户信息 */}
  205. <div className="p-4 border-b border-gray-200">
  206. <div className="flex items-center gap-3">
  207. {fullAvatarUrl ? (
  208. <img
  209. src={fullAvatarUrl}
  210. alt={agent?.username || "用户"}
  211. className="w-12 h-12 rounded-full object-cover"
  212. />
  213. ) : (
  214. <div
  215. className="w-12 h-12 rounded-full flex items-center justify-center text-white text-sm font-semibold"
  216. style={{ backgroundColor: avatarColor }}
  217. >
  218. {displayInitial}
  219. </div>
  220. )}
  221. <div>
  222. <div className="text-sm font-semibold text-foreground">
  223. {agent?.username || "用户"}
  224. </div>
  225. <div className="text-xs text-muted-foreground">
  226. {agent?.role === "admin" ? "管理员" : "客服"}
  227. </div>
  228. </div>
  229. </div>
  230. </div>
  231. {/* 菜单项 */}
  232. <div className="p-2">
  233. <Button
  234. variant="ghost"
  235. size="sm"
  236. className="w-full justify-start"
  237. onClick={() => {
  238. setProfileMenuOpen(false);
  239. onProfileClick?.();
  240. }}
  241. >
  242. <svg
  243. className="w-4 h-4 mr-2"
  244. fill="none"
  245. stroke="currentColor"
  246. viewBox="0 0 24 24"
  247. >
  248. <path
  249. strokeLinecap="round"
  250. strokeLinejoin="round"
  251. strokeWidth={2}
  252. d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"
  253. />
  254. </svg>
  255. 个人资料
  256. </Button>
  257. <Button
  258. variant="ghost"
  259. size="sm"
  260. className="w-full justify-start text-red-600 hover:text-red-700 hover:bg-red-50"
  261. onClick={() => {
  262. setProfileMenuOpen(false);
  263. onLogout?.();
  264. }}
  265. >
  266. <svg
  267. className="w-4 h-4 mr-2"
  268. fill="none"
  269. stroke="currentColor"
  270. viewBox="0 0 24 24"
  271. >
  272. <path
  273. strokeLinecap="round"
  274. strokeLinejoin="round"
  275. strokeWidth={2}
  276. 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"
  277. />
  278. </svg>
  279. 退出登录
  280. </Button>
  281. </div>
  282. </div>
  283. )}
  284. </div>
  285. {/* GitHub 链接按钮(个人资料下方) */}
  286. <Button
  287. variant="ghost"
  288. size="sm"
  289. asChild
  290. className="w-10 h-10 rounded-lg bg-white border border-gray-200 hover:bg-gray-100 transition-colors p-0"
  291. title="GitHub"
  292. >
  293. <a
  294. href={websiteConfig.github.repo}
  295. target="_blank"
  296. rel="noopener noreferrer"
  297. >
  298. <svg
  299. className="w-5 h-5 text-gray-600"
  300. fill="currentColor"
  301. viewBox="0 0 24 24"
  302. >
  303. <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" />
  304. </svg>
  305. </a>
  306. </Button>
  307. </div>
  308. </div>
  309. );
  310. }