// nav.js — lightweight navigation & magic‑link handling // fallback toast if app.js not yet loaded if (typeof window.showToast !== "function") { window.showToast = (msg) => alert(msg); } document.addEventListener("DOMContentLoaded", () => { const Router = { sections: Array.from(document.querySelectorAll("main > section")), showOnly(id) { this.sections.forEach(sec => { sec.hidden = sec.id !== id; sec.tabIndex = -1; }); localStorage.setItem("last_page", id); const target = document.getElementById(id); if (target) target.focus(); }, init() { initNavLinks(); initBackButtons(); initStreamsLoader(); initStreamLinks(); } }; const showOnly = Router.showOnly.bind(Router); // Highlight active profile link on browser back/forward navigation function highlightActiveProfileLink() { const params = new URLSearchParams(window.location.search); const profileUid = params.get('profile'); const ul = document.getElementById('stream-list'); if (!ul) return; ul.querySelectorAll('a.profile-link').forEach(link => { const url = new URL(link.href, window.location.origin); const uidParam = url.searchParams.get('profile'); link.classList.toggle('active', uidParam === profileUid); }); } window.addEventListener('popstate', highlightActiveProfileLink); /* restore last page (unless magic‑link token present) */ const params = new URLSearchParams(location.search); const token = params.get("token"); if (!token) { const last = localStorage.getItem("last_page"); if (last && document.getElementById(last)) showOnly(last); // Highlight active link on initial load highlightActiveProfileLink(); } /* token → show magic‑login page */ if (token) { document.getElementById("magic-token").value = token; showOnly("magic-login-page"); const err = params.get("error"); if (err) { const box = document.getElementById("magic-error"); box.textContent = decodeURIComponent(err); box.style.display = "block"; } } // Debounce loading and helper for streams list let loadingStreams = false; function renderStreamList(streams) { const ul = document.getElementById("stream-list"); if (!ul) return; if (streams.length) { streams.sort(); ul.innerHTML = streams.map(uid => `