Spaces:
Running
Running
Download components/user-menu.js from Maqsadov/testmaster-pro: direct link, hf CLI and curl.
- Browser
- Download file 6.56 kB
-
https://huggingface.co/spaces/Maqsadov/testmaster-pro/resolve/main/components/user-menu.js
- Command line
-
hf download hf://spaces/Maqsadov/testmaster-pro/components/user-menu.js
-
curl -L -o user-menu.js https://huggingface.co/spaces/Maqsadov/testmaster-pro/resolve/main/components/user-menu.js
6.56 kB
| class UserMenu extends HTMLElement { | |
| connectedCallback() { | |
| this.attachShadow({ mode: 'open' }); | |
| this.render(); | |
| // Listen for auth state changes | |
| window.addEventListener('authStateChange', () => { | |
| this.render(); | |
| }); | |
| } | |
| render() { | |
| const user = this.getCurrentUser(); | |
| this.shadowRoot.innerHTML = ` | |
| <style> | |
| .user-menu { | |
| position: relative; | |
| display: inline-block; | |
| } | |
| .user-button { | |
| display: flex; | |
| align-items: center; | |
| gap: 8px; | |
| background: none; | |
| border: none; | |
| cursor: pointer; | |
| padding: 8px 12px; | |
| border-radius: 20px; | |
| transition: background-color 0.3s; | |
| } | |
| .user-button:hover { | |
| background-color: #f1f2f6; | |
| } | |
| .user-avatar { | |
| width: 32px; | |
| height: 32px; | |
| border-radius: 50%; | |
| object-fit: cover; | |
| } | |
| .user-name { | |
| font-weight: 500; | |
| color: #333; | |
| } | |
| .login-button { | |
| background-color: #0088cc; | |
| color: white; | |
| border: none; | |
| padding: 8px 16px; | |
| border-radius: 6px; | |
| cursor: pointer; | |
| font-weight: 500; | |
| transition: background-color 0.3s; | |
| } | |
| .login-button:hover { | |
| background-color: #0077b3; | |
| } | |
| .menu-dropdown { | |
| position: absolute; | |
| right: 0; | |
| top: 100%; | |
| margin-top: 8px; | |
| background: white; | |
| border-radius: 8px; | |
| box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); | |
| width: 250px; | |
| z-index: 100; | |
| padding: 16px; | |
| } | |
| .menu-header { | |
| display: flex; | |
| align-items: center; | |
| gap: 12px; | |
| padding-bottom: 12px; | |
| border-bottom: 1px solid #eee; | |
| margin-bottom: 12px; | |
| } | |
| .menu-avatar { | |
| width: 48px; | |
| height: 48px; | |
| border-radius: 50%; | |
| object-fit: cover; | |
| } | |
| .menu-user-info { | |
| flex: 1; | |
| } | |
| .menu-user-name { | |
| font-weight: 600; | |
| color: #333; | |
| margin: 0 0 4px 0; | |
| } | |
| .menu-user-username { | |
| color: #666; | |
| font-size: 14px; | |
| margin: 0; | |
| } | |
| .menu-user-id { | |
| font-size: 12px; | |
| color: #999; | |
| margin-top: 4px; | |
| } | |
| .menu-items { | |
| list-style: none; | |
| padding: 0; | |
| margin: 0; | |
| } | |
| .menu-item { | |
| padding: 10px 0; | |
| border-bottom: 1px solid #eee; | |
| } | |
| .menu-item:last-child { | |
| border-bottom: none; | |
| } | |
| .menu-link { | |
| text-decoration: none; | |
| color: #333; | |
| display: flex; | |
| align-items: center; | |
| gap: 10px; | |
| font-weight: 500; | |
| transition: color 0.2s; | |
| } | |
| .menu-link:hover { | |
| color: #0088cc; | |
| } | |
| .logout-button { | |
| background: none; | |
| border: none; | |
| color: #ff4757; | |
| cursor: pointer; | |
| font-weight: 500; | |
| padding: 10px 0; | |
| width: 100%; | |
| text-align: left; | |
| display: flex; | |
| align-items: center; | |
| gap: 10px; | |
| } | |
| .logout-button:hover { | |
| color: #e8414d; | |
| } | |
| </style> | |
| <div class="user-menu"> | |
| ${user ? this.renderUserMenu(user) : this.renderLoginButton()} | |
| </div> | |
| `; | |
| if (user) { | |
| this.setupUserMenuEvents(); | |
| } else { | |
| this.setupLoginEvent(); | |
| } | |
| } | |
| renderUserMenu(user) { | |
| return ` | |
| <button class="user-button" id="userMenuButton"> | |
| <img src="${user.photo_url || 'https://static.photos/people/320x240/3'}" alt="Avatar" class="user-avatar"> | |
| <span class="user-name">${user.first_name}</span> | |
| </button> | |
| <div class="menu-dropdown" id="userMenuDropdown" style="display: none;"> | |
| <div class="menu-header"> | |
| <img src="${user.photo_url || 'https://static.photos/people/320x240/3'}" alt="Avatar" class="menu-avatar"> | |
| <div class="menu-user-info"> | |
| <h4 class="menu-user-name">${user.first_name} ${user.last_name || ''}</h4> | |
| <p class="menu-user-username">@${user.username || 'Foydalanuvchi'}</p> | |
| <p class="menu-user-id">ID: ${user.id}</p> | |
| </div> | |
| </div> | |
| <ul class="menu-items"> | |
| <li class="menu-item"> | |
| <a href="create-test.html" class="menu-link">📝 Test Yaratish</a> | |
| </li> | |
| <li class="menu-item"> | |
| <a href="take-test.html" class="menu-link">✏️ Test Ishlash</a> | |
| </li> | |
| <li class="menu-item"> | |
| <a href="results.html" class="menu-link">📊 Natijalar</a> | |
| </li> | |
| </ul> | |
| <button class="logout-button" id="logoutButton"> | |
| <span>🚪 Chiqish</span> | |
| </button> | |
| </div> | |
| `; | |
| } | |
| renderLoginButton() { | |
| return ` | |
| <button class="login-button" id="loginButton"> | |
| Kirish | |
| </button> | |
| `; | |
| } | |
| setupUserMenuEvents() { | |
| const menuButton = this.shadowRoot.getElementById('userMenuButton'); | |
| const menuDropdown = this.shadowRoot.getElementById('userMenuDropdown'); | |
| const logoutButton = this.shadowRoot.getElementById('logoutButton'); | |
| menuButton.addEventListener('click', (e) => { | |
| e.stopPropagation(); | |
| const isVisible = menuDropdown.style.display === 'block'; | |
| menuDropdown.style.display = isVisible ? 'none' : 'block'; | |
| }); | |
| logoutButton.addEventListener('click', () => { | |
| localStorage.removeItem('telegramUser'); | |
| const event = new CustomEvent('authStateChange'); | |
| window.dispatchEvent(event); | |
| }); | |
| // Close menu when clicking outside | |
| document.addEventListener('click', () => { | |
| menuDropdown.style.display = 'none'; | |
| }); | |
| } | |
| setupLoginEvent() { | |
| const loginButton = this.shadowRoot.getElementById('loginButton'); | |
| loginButton.addEventListener('click', () => { | |
| document.querySelector('auth-modal').openModal(); | |
| }); | |
| } | |
| getCurrentUser() { | |
| return JSON.parse(localStorage.getItem('telegramUser')) || null; | |
| } | |
| } | |
| customElements.define('user-menu', UserMenu); |