|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import cn from "classnames"; |
| 4 | +import { useState, useEffect, useRef } from "react"; |
| 5 | +import { API_BASE, checkAuthSession } from "@/lib/grapes-api"; |
| 6 | +import { useAuthContext } from "./AuthContext"; |
| 7 | + |
| 8 | +interface AuthButtonProps { |
| 9 | + readonly isMobile?: boolean; |
| 10 | + readonly onMobileMenuClose?: () => void; |
| 11 | + readonly showLogin?: boolean; |
| 12 | + readonly showUserProfile?: boolean; |
| 13 | +} |
| 14 | + |
| 15 | +export function AuthButton({ |
| 16 | + isMobile = false, |
| 17 | + onMobileMenuClose, |
| 18 | + showLogin = true, |
| 19 | + showUserProfile = true, |
| 20 | +}: AuthButtonProps) { |
| 21 | + const authContext = useAuthContext(); |
| 22 | + |
| 23 | + const [showUserMenu, setShowUserMenu] = useState(false); |
| 24 | + const userMenuRef = useRef<HTMLDivElement>(null); |
| 25 | + |
| 26 | + // Since we are now guaranteed to be inside AuthProvider, context should exist. |
| 27 | + // However, for safety (e.g. if used outside provider accidentally), we can have a safe check. |
| 28 | + const user = authContext?.authSession?.isAuthenticated ? authContext.authSession.user : null; |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + const handleClickOutside = (event: PointerEvent) => { |
| 32 | + if (userMenuRef.current && !userMenuRef.current.contains(event.target as Node)) { |
| 33 | + setShowUserMenu(false); |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + if (showUserMenu) { |
| 38 | + document.addEventListener('pointerdown', handleClickOutside); |
| 39 | + } |
| 40 | + |
| 41 | + return () => { |
| 42 | + document.removeEventListener('pointerdown', handleClickOutside); |
| 43 | + }; |
| 44 | + }, [showUserMenu]); |
| 45 | + |
| 46 | + const handleLogin = (e: React.MouseEvent) => { |
| 47 | + e.preventDefault(); |
| 48 | + onMobileMenuClose?.(); |
| 49 | + authContext?.triggerAuth(); |
| 50 | + }; |
| 51 | + |
| 52 | + const handleSignOut = async () => { |
| 53 | + setShowUserMenu(false); |
| 54 | + onMobileMenuClose?.(); |
| 55 | + authContext?.logout(); |
| 56 | + }; |
| 57 | + |
| 58 | + const handleDashboardClick = () => { |
| 59 | + onMobileMenuClose?.(); |
| 60 | + }; |
| 61 | + |
| 62 | + const userInitial = (user?.name || user?.email || 'U')[0].toUpperCase(); |
| 63 | + const userName = user?.name || 'User'; |
| 64 | + const userEmail = user?.email; |
| 65 | + const avatarSize = isMobile ? "w-10 h-10" : "w-8 h-8"; |
| 66 | + |
| 67 | + return ( |
| 68 | + <> |
| 69 | + {/* Login Button - shown when not authenticated */} |
| 70 | + {!user && showLogin && ( |
| 71 | + <button |
| 72 | + onClick={handleLogin} |
| 73 | + className={cn( |
| 74 | + "font-semibold text-gray-100 no-underline border border-gray-600 rounded-lg cursor-pointer transition-all duration-200 hover:bg-gray-800 hover:border-gray-500", |
| 75 | + isMobile |
| 76 | + ? "mt-4 w-full text-center px-6 py-3 text-base" |
| 77 | + : "inline-block px-4 py-2 text-sm leading-5 sm:px-5 sm:py-2 lg:px-6 lg:py-2 whitespace-nowrap" |
| 78 | + )} |
| 79 | + > |
| 80 | + Login |
| 81 | + </button> |
| 82 | + )} |
| 83 | + |
| 84 | + {/* Mobile Authenticated View */} |
| 85 | + {user && isMobile && showUserProfile && ( |
| 86 | + <> |
| 87 | + <div className="mt-4 px-4 py-3 bg-zinc-800 rounded-lg flex items-center gap-3"> |
| 88 | + {user.image ? ( |
| 89 | + <img src={user.image} alt={userName} className={cn("rounded-full", avatarSize)} /> |
| 90 | + ) : ( |
| 91 | + <div className={cn("rounded-full bg-violet-600 flex items-center justify-center text-white font-semibold", avatarSize)}> |
| 92 | + {userInitial} |
| 93 | + </div> |
| 94 | + )} |
| 95 | + <div className="flex-1"> |
| 96 | + <p className="text-sm font-medium text-white">{userName}</p> |
| 97 | + <p className="text-xs text-gray-400">{userEmail}</p> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + <a |
| 101 | + href={`${API_BASE}/dashboard`} |
| 102 | + className="mt-2 w-full text-center px-6 py-3 text-base font-semibold text-gray-100 no-underline border border-gray-600 rounded-lg cursor-pointer transition-all duration-200 hover:bg-gray-800 hover:border-gray-500" |
| 103 | + onClick={handleDashboardClick} |
| 104 | + > |
| 105 | + Dashboard |
| 106 | + </a> |
| 107 | + <button |
| 108 | + onClick={handleSignOut} |
| 109 | + className="mt-2 w-full text-center px-6 py-3 text-base font-semibold text-gray-100 no-underline border border-gray-600 rounded-lg cursor-pointer transition-all duration-200 hover:bg-gray-800 hover:border-gray-500" |
| 110 | + > |
| 111 | + Sign Out |
| 112 | + </button> |
| 113 | + </> |
| 114 | + )} |
| 115 | + |
| 116 | + {/* Desktop Authenticated View */} |
| 117 | + {user && !isMobile && showUserProfile && ( |
| 118 | + <div className="relative" ref={userMenuRef}> |
| 119 | + <button |
| 120 | + onClick={() => setShowUserMenu(!showUserMenu)} |
| 121 | + className="flex items-center gap-2 px-2 py-2 rounded-lg hover:bg-gray-800/50 transition-colors" |
| 122 | + aria-label="User menu" |
| 123 | + > |
| 124 | + {user.image ? ( |
| 125 | + <img src={user.image} alt={userName} className={cn("rounded-full", avatarSize)} /> |
| 126 | + ) : ( |
| 127 | + <div className={cn("rounded-full bg-violet-600 flex items-center justify-center text-white font-semibold", avatarSize)}> |
| 128 | + {userInitial} |
| 129 | + </div> |
| 130 | + )} |
| 131 | + <svg |
| 132 | + className={cn("w-4 h-4 text-gray-400 transition-transform max-lg:hidden", showUserMenu && "rotate-180")} |
| 133 | + fill="none" |
| 134 | + stroke="currentColor" |
| 135 | + viewBox="0 0 24 24" |
| 136 | + > |
| 137 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 9l-7 7-7-7" /> |
| 138 | + </svg> |
| 139 | + </button> |
| 140 | + |
| 141 | + {showUserMenu && ( |
| 142 | + <div className="absolute right-0 mt-2 w-56 bg-zinc-900 rounded-lg shadow-lg border border-zinc-700 overflow-hidden z-50"> |
| 143 | + <div className="px-4 py-3 border-b border-zinc-700"> |
| 144 | + <p className="text-sm font-medium text-white">{userName}</p> |
| 145 | + <p className="text-xs text-gray-400 truncate">{userEmail}</p> |
| 146 | + </div> |
| 147 | + <div className="py-1"> |
| 148 | + <a |
| 149 | + href={`${API_BASE}/dashboard`} |
| 150 | + className="block px-4 py-2 text-sm text-gray-300 hover:bg-zinc-800 hover:text-white transition-colors no-underline" |
| 151 | + > |
| 152 | + Dashboard |
| 153 | + </a> |
| 154 | + <button |
| 155 | + onClick={handleSignOut} |
| 156 | + className="w-full text-left px-4 py-2 text-sm text-gray-300 hover:bg-zinc-800 hover:text-white transition-colors" |
| 157 | + > |
| 158 | + Sign Out |
| 159 | + </button> |
| 160 | + </div> |
| 161 | + </div> |
| 162 | + )} |
| 163 | + </div> |
| 164 | + )} |
| 165 | + </> |
| 166 | + ); |
| 167 | +} |
0 commit comments