NavigationSidebar.tsx 8.0 KB

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