Spaces:
Sleeping
Sleeping
File size: 11,396 Bytes
2db8ee1 7af9025 2db8ee1 7af9025 2db8ee1 7af9025 2db8ee1 de88b8d 2db8ee1 7af9025 2db8ee1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | import { useState } from 'react'
import './TopNav.css'
function TopNav({
user,
isAuthenticated,
onLogin,
onRegister,
onLogout,
onToggleSidebar,
sidebarOpen
}) {
const [showAuthModal, setShowAuthModal] = useState(false)
const [authMode, setAuthMode] = useState('login')
const [username, setUsername] = useState('')
const [password, setPassword] = useState('')
const [firstName, setFirstName] = useState('')
const [lastName, setLastName] = useState('')
const [email, setEmail] = useState('')
const [loading, setLoading] = useState(false)
const [error, setError] = useState('')
const handleSubmit = async (e) => {
e.preventDefault()
setLoading(true)
setError('')
try {
if (authMode === 'login') {
await onLogin(username, password)
} else {
await onRegister({ username, password, first_name: firstName, last_name: lastName, email })
}
setShowAuthModal(false)
setUsername('')
setPassword('')
setFirstName('')
setLastName('')
setEmail('')
} catch (err) {
setError(err.message)
} finally {
setLoading(false)
}
}
return (
<>
<nav className="topnav">
<div className="topnav-left">
<button
className="btn btn-ghost btn-icon sidebar-toggle"
onClick={onToggleSidebar}
aria-label="Toggle sidebar"
>
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
{sidebarOpen ? (
<path d="M11 19l-7-7 7-7M18 19l-7-7 7-7" />
) : (
<path d="M3 12h18M3 6h18M3 18h18" />
)}
</svg>
</button>
<div className="topnav-brand">
<div className="brand-icon">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none">
<path
d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5"
stroke="url(#brand-gradient)"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
/>
<defs>
<linearGradient id="brand-gradient" x1="2" y1="2" x2="22" y2="22">
<stop stopColor="#6366f1" />
<stop offset="1" stopColor="#a855f7" />
</linearGradient>
</defs>
</svg>
</div>
<span className="brand-name">VectorMind</span>
</div>
</div>
<div className="topnav-right">
{isAuthenticated ? (
<div className="user-menu">
<div className="user-avatar">
{user?.[0]?.toUpperCase() || 'U'}
</div>
<span className="user-name">{user}</span>
<button className="btn btn-ghost btn-sm" onClick={onLogout}>
Logout
</button>
</div>
) : (
<button
className="btn btn-primary btn-sm"
onClick={() => setShowAuthModal(true)}
>
Sign In
</button>
)}
</div>
</nav>
{/* Auth Modal */}
{showAuthModal && (
<div className="modal-overlay" onClick={() => setShowAuthModal(false)}>
<div className="modal card-glass" onClick={e => e.stopPropagation()}>
<div className="modal-header">
<h2>{authMode === 'login' ? 'Welcome Back' : 'Create Account'}</h2>
<button
className="btn btn-ghost btn-icon"
onClick={() => setShowAuthModal(false)}
>
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
<path d="M18 6L6 18M6 6l12 12" />
</svg>
</button>
</div>
<form onSubmit={handleSubmit} className="auth-form">
{error && (
<div className="auth-error">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
<circle cx="12" cy="12" r="10" />
<path d="M12 8v4M12 16h.01" />
</svg>
{error}
</div>
)}
<div className="form-group">
<label htmlFor="username">Username</label>
<input
id="username"
type="text"
className="input"
value={username}
onChange={e => setUsername(e.target.value)}
placeholder="Enter username"
required
autoComplete="username"
/>
</div>
<div className="form-group">
<label htmlFor="password">Password</label>
<input
id="password"
type="password"
className="input"
value={password}
onChange={e => setPassword(e.target.value)}
placeholder="Enter password"
required
autoComplete={authMode === 'login' ? 'current-password' : 'new-password'}
/>
</div>
{authMode === 'register' && (
<>
<div className="form-row">
<div className="form-group">
<label htmlFor="firstName">First Name</label>
<input
id="firstName"
type="text"
className="input"
value={firstName}
onChange={e => setFirstName(e.target.value)}
placeholder="First name"
required
/>
</div>
<div className="form-group">
<label htmlFor="lastName">Last Name</label>
<input
id="lastName"
type="text"
className="input"
value={lastName}
onChange={e => setLastName(e.target.value)}
placeholder="Last name"
required
/>
</div>
</div>
<div className="form-group">
<label htmlFor="email">Email</label>
<input
id="email"
type="email"
className="input"
value={email}
onChange={e => setEmail(e.target.value)}
placeholder="you@example.com"
required
/>
</div>
</>
)}
<button
type="submit"
className="btn btn-primary btn-lg"
disabled={loading}
style={{ width: '100%' }}
>
{loading ? (
<span className="loading-dots">
<span></span><span></span><span></span>
</span>
) : authMode === 'login' ? 'Sign In' : 'Create Account'}
</button>
<div className="auth-switch">
{authMode === 'login' ? (
<>
Don't have an account?{' '}
<button type="button" onClick={() => setAuthMode('register')}>
Sign up
</button>
</>
) : (
<>
Already have an account?{' '}
<button type="button" onClick={() => setAuthMode('login')}>
Sign in
</button>
</>
)}
</div>
</form>
</div>
</div>
)}
</>
)
}
export default TopNav
|