NavigationSidebar.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. export type NavigationPage = "dashboard" | "faqs" | "users" | "settings";
  7. interface NavigationSidebarProps {
  8. currentPage?: NavigationPage;
  9. onNavigate?: (page: NavigationPage) => void;
  10. onProfileClick?: () => void;
  11. onLogout?: () => void;
  12. avatarUrl?: string | null;
  13. }
  14. export function NavigationSidebar({
  15. currentPage = "dashboard",
  16. onNavigate,
  17. onProfileClick,
  18. onLogout,
  19. avatarUrl,
  20. }: NavigationSidebarProps) {
  21. const { agent } = useAuth();
  22. const [profileMenuOpen, setProfileMenuOpen] = useState(false);
  23. const menuRef = useRef<HTMLDivElement>(null);
  24. // 检查当前用户是否是管理员
  25. const isAdmin = agent?.role === "admin";
  26. const handleNavigate = (page: NavigationPage) => {
  27. if (onNavigate) {
  28. onNavigate(page);
  29. }
  30. };
  31. // 点击外部关闭菜单
  32. useEffect(() => {
  33. const handleClickOutside = (event: MouseEvent) => {
  34. if (menuRef.current && !menuRef.current.contains(event.target as Node)) {
  35. setProfileMenuOpen(false);
  36. }
  37. };
  38. if (profileMenuOpen) {
  39. document.addEventListener("mousedown", handleClickOutside);
  40. }
  41. return () => {
  42. document.removeEventListener("mousedown", handleClickOutside);
  43. };
  44. }, [profileMenuOpen]);
  45. // 头像相关
  46. const avatarColor = getAvatarColor(agent?.username || "");
  47. const displayInitial = getAvatarInitial(agent?.username || "");
  48. const fullAvatarUrl = getAvatarUrl(avatarUrl);
  49. return (
  50. <div className="w-16 bg-gray-50 flex flex-col items-center py-4 border-r border-gray-200 h-full">
  51. <button
  52. className={`w-10 h-10 rounded-lg flex items-center justify-center mb-4 transition-colors ${
  53. currentPage === "dashboard"
  54. ? "bg-green-600 hover:bg-green-700"
  55. : "bg-white border border-gray-200 hover:bg-gray-100"
  56. }`}
  57. title="对话"
  58. onClick={() => handleNavigate("dashboard")}
  59. >
  60. <svg
  61. className={`w-6 h-6 ${
  62. currentPage === "dashboard" ? "text-white" : "text-gray-600"
  63. }`}
  64. fill="none"
  65. stroke="currentColor"
  66. viewBox="0 0 24 24"
  67. >
  68. <path
  69. strokeLinecap="round"
  70. strokeLinejoin="round"
  71. strokeWidth={2}
  72. 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"
  73. />
  74. </svg>
  75. </button>
  76. <button
  77. 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"
  78. title="知识库"
  79. disabled
  80. >
  81. <svg
  82. className="w-6 h-6 text-gray-600"
  83. fill="none"
  84. stroke="currentColor"
  85. viewBox="0 0 24 24"
  86. >
  87. <path
  88. strokeLinecap="round"
  89. strokeLinejoin="round"
  90. strokeWidth={2}
  91. 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"
  92. />
  93. </svg>
  94. </button>
  95. <button
  96. className={`w-10 h-10 rounded-lg flex items-center justify-center mb-4 transition-colors ${
  97. currentPage === "faqs"
  98. ? "bg-green-600 hover:bg-green-700"
  99. : "bg-white border border-gray-200 hover:bg-gray-100"
  100. }`}
  101. title="事件管理"
  102. onClick={() => handleNavigate("faqs")}
  103. >
  104. <svg
  105. className={`w-6 h-6 ${
  106. currentPage === "faqs" ? "text-white" : "text-gray-600"
  107. }`}
  108. fill="none"
  109. stroke="currentColor"
  110. viewBox="0 0 24 24"
  111. >
  112. <path
  113. strokeLinecap="round"
  114. strokeLinejoin="round"
  115. strokeWidth={2}
  116. 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"
  117. />
  118. </svg>
  119. </button>
  120. {isAdmin && (
  121. <button
  122. className={`w-10 h-10 rounded-lg flex items-center justify-center mb-4 transition-colors ${
  123. currentPage === "users"
  124. ? "bg-green-600 hover:bg-green-700"
  125. : "bg-white border border-gray-200 hover:bg-gray-100"
  126. }`}
  127. title="用户管理"
  128. onClick={() => handleNavigate("users")}
  129. >
  130. <svg
  131. className={`w-6 h-6 ${
  132. currentPage === "users" ? "text-white" : "text-gray-600"
  133. }`}
  134. fill="none"
  135. stroke="currentColor"
  136. viewBox="0 0 24 24"
  137. >
  138. <path
  139. strokeLinecap="round"
  140. strokeLinejoin="round"
  141. strokeWidth={2}
  142. 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"
  143. />
  144. </svg>
  145. </button>
  146. )}
  147. <button
  148. className={`w-10 h-10 rounded-lg flex items-center justify-center mb-4 transition-colors ${
  149. currentPage === "settings"
  150. ? "bg-green-600 hover:bg-green-700"
  151. : "bg-white border border-gray-200 hover:bg-gray-100"
  152. }`}
  153. title="AI 配置"
  154. onClick={() => handleNavigate("settings")}
  155. >
  156. <svg
  157. className={`w-6 h-6 ${
  158. currentPage === "settings" ? "text-white" : "text-gray-600"
  159. }`}
  160. fill="none"
  161. stroke="currentColor"
  162. viewBox="0 0 24 24"
  163. >
  164. <path
  165. strokeLinecap="round"
  166. strokeLinejoin="round"
  167. strokeWidth={2}
  168. 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"
  169. />
  170. </svg>
  171. </button>
  172. {/* 个人资料按钮(固定在底部) */}
  173. <div className="mt-auto relative" ref={menuRef}>
  174. <button
  175. className={`w-10 h-10 rounded-lg flex items-center justify-center transition-colors ${
  176. profileMenuOpen
  177. ? "bg-primary text-primary-foreground"
  178. : "bg-white border border-gray-200 hover:bg-gray-100"
  179. }`}
  180. title="个人资料"
  181. onClick={() => setProfileMenuOpen(!profileMenuOpen)}
  182. >
  183. {fullAvatarUrl ? (
  184. <img
  185. src={fullAvatarUrl}
  186. alt={agent?.username || "用户"}
  187. className="w-8 h-8 rounded-full object-cover"
  188. />
  189. ) : (
  190. <div
  191. className="w-8 h-8 rounded-full flex items-center justify-center text-white text-xs font-semibold"
  192. style={{ backgroundColor: avatarColor }}
  193. >
  194. {displayInitial}
  195. </div>
  196. )}
  197. </button>
  198. {/* 下拉菜单 */}
  199. {profileMenuOpen && (
  200. <div className="absolute bottom-12 left-0 w-64 bg-white border border-gray-200 rounded-lg shadow-lg z-50">
  201. {/* 用户信息 */}
  202. <div className="p-4 border-b border-gray-200">
  203. <div className="flex items-center gap-3">
  204. {fullAvatarUrl ? (
  205. <img
  206. src={fullAvatarUrl}
  207. alt={agent?.username || "用户"}
  208. className="w-12 h-12 rounded-full object-cover"
  209. />
  210. ) : (
  211. <div
  212. className="w-12 h-12 rounded-full flex items-center justify-center text-white text-sm font-semibold"
  213. style={{ backgroundColor: avatarColor }}
  214. >
  215. {displayInitial}
  216. </div>
  217. )}
  218. <div>
  219. <div className="text-sm font-semibold text-foreground">
  220. {agent?.username || "用户"}
  221. </div>
  222. <div className="text-xs text-muted-foreground">
  223. {agent?.role === "admin" ? "管理员" : "客服"}
  224. </div>
  225. </div>
  226. </div>
  227. </div>
  228. {/* 菜单项 */}
  229. <div className="p-2">
  230. <Button
  231. variant="ghost"
  232. size="sm"
  233. className="w-full justify-start"
  234. onClick={() => {
  235. setProfileMenuOpen(false);
  236. onProfileClick?.();
  237. }}
  238. >
  239. <svg
  240. className="w-4 h-4 mr-2"
  241. fill="none"
  242. stroke="currentColor"
  243. viewBox="0 0 24 24"
  244. >
  245. <path
  246. strokeLinecap="round"
  247. strokeLinejoin="round"
  248. strokeWidth={2}
  249. d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"
  250. />
  251. </svg>
  252. 个人资料
  253. </Button>
  254. <Button
  255. variant="ghost"
  256. size="sm"
  257. className="w-full justify-start text-red-600 hover:text-red-700 hover:bg-red-50"
  258. onClick={() => {
  259. setProfileMenuOpen(false);
  260. onLogout?.();
  261. }}
  262. >
  263. <svg
  264. className="w-4 h-4 mr-2"
  265. fill="none"
  266. stroke="currentColor"
  267. viewBox="0 0 24 24"
  268. >
  269. <path
  270. strokeLinecap="round"
  271. strokeLinejoin="round"
  272. strokeWidth={2}
  273. 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"
  274. />
  275. </svg>
  276. 退出登录
  277. </Button>
  278. </div>
  279. </div>
  280. )}
  281. </div>
  282. </div>
  283. );
  284. }