// Shared page shell: site header, navigation, sign-in guard. import { api, getToken, redirectToLogin, setToken } from "./api.js"; import { avatarImage } from "./avatar.js"; import { h, replace } from "./dom.js"; const NAV = [ ["index.html", "Overview"], ["history.html", "History"], ["art.html", "Album art"], ]; // Account pages live in the menu under the user's name. "Sharing" joins them (after // Profile) when the server federates. const MENU = [ ["account.html", "Profile"], ["apps.html", "Apps"], ["sessions.html", "Sessions"], ["import.html", "Import"], ]; // Renders the header and returns the signed-in user (redirecting if there isn't one). export async function signedInPage(active) { if (!getToken()) { redirectToLogin(); return new Promise(() => {}); // navigation is under way } const header = document.getElementById("site-header"); replace(header, brand(), nav(active)); const user = await api("/auth/me"); const menu = accountMenu(user, active); header.append(menu.element); addSharingLink(menu.list, active); return user; } export function publicPage() { replace(document.getElementById("site-header"), brand()); } function brand() { return h( "a", { class: "brand", href: "index.html" }, h("span", { class: "brand-mark", "aria-hidden": "true" }), h("span", { class: "brand-word", "aria-hidden": "true" }), h("span", { class: "visually-hidden" }, "Scrobbled"), ); } function nav(active) { return h( "nav", { "aria-label": "Main" }, h( "ul", {}, NAV.map(([href, label]) => h("li", {}, h("a", { href, "aria-current": href === active ? "page" : false }, label)), ), ), ); } // The account menu: a button showing the user's picture and name that opens a list of // account pages and "Sign out". Returns the element and its