// ── Navbar injection + auth guard ───────────────────────────────────────────── (function () { const access = getAccess(); const isAuth = !!access; const path = window.location.pathname; const PROTECTED = ['/dashboard.html', '/gears.html', '/reloads.html', '/profile.html', '/sessions.html', '/admin.html', '/messages.html', '/friends.html']; const ADMIN_ONLY = ['/admin.html']; const AUTH_ONLY = ['/login.html', '/register.html']; if (PROTECTED.includes(path) && !isAuth) { window.location.href = '/login.html'; return; } if (AUTH_ONLY.includes(path) && isAuth) { window.location.href = '/dashboard.html'; return; } const LANGS = [ { code: 'en', label: 'EN', name: 'English' }, { code: 'fr', label: 'FR', name: 'Français' }, { code: 'de', label: 'DE', name: 'Deutsch' }, { code: 'es', label: 'ES', name: 'Español' }, ]; const currentLang = getLang(); const langSelector = ` `; // Built after profile load (need is_staff) function buildNav(isStaff) { const adminLink = isStaff ? `` : ''; const mainLinks = isAuth ? ` ${adminLink} ` : ` `; const authNav = isAuth ? ` ` : ` `; document.getElementById('navbar').innerHTML = ` `; // Apply data-i18n translations to page elements applyTranslations(); // Load notification badges for authenticated users if (isAuth) { apiFetch('/social/messages/unread-count/').then(r => r.json()).then(d => { const b = document.getElementById('navMsgBadge'); if (b && d.unread > 0) { b.textContent = d.unread; b.classList.remove('d-none'); } }).catch(() => {}); apiFetch('/social/friends/requests/').then(r => r.json()).then(d => { const b = document.getElementById('navFriendsBadge'); const count = Array.isArray(d) ? d.length : 0; if (b && count > 0) { b.textContent = count; b.classList.remove('d-none'); } }).catch(() => {}); } // Highlight active link document.querySelectorAll('#navbar .nav-link[href]').forEach(a => { if (a.getAttribute('href') === path) a.classList.add('active'); }); // Logout document.addEventListener('click', e => { if (e.target.closest('#logoutBtn')) { e.preventDefault(); clearTokens(); window.location.href = '/index.html'; } }); // Admin guard (page-level, after we know is_staff) if (ADMIN_ONLY.includes(path) && !isStaff) { window.location.href = '/dashboard.html'; } } // Language selector handler (attached after navbar renders via delegation) document.addEventListener('click', e => { const item = e.target.closest('[data-lang]'); if (!item) return; e.preventDefault(); const lang = item.dataset.lang; setLang(lang); const label = document.getElementById('currentLangLabel'); if (label) label.textContent = lang.toUpperCase(); // Persist to profile if logged in if (isAuth) { apiPatch('/users/profile/', { language: lang }).catch(() => {}); } // Refresh page so Django serves the right language window.location.reload(); }); if (isAuth) { apiFetch('/users/profile/') .then(r => r.json()) .then(user => { // Sync language from profile on first load if (user.language && user.language !== getLang()) { setLang(user.language); } buildNav(!!user.is_staff); const initials = ((user.first_name?.[0] || '') + (user.last_name?.[0] || '') || user.username?.[0] || '?').toUpperCase(); const el = document.getElementById('navInitials'); if (el) el.textContent = initials; }) .catch(() => buildNav(false)); } else { buildNav(false); } })();