Files
Scrobbler/frontend/js/layout.js
T
mattandClaude Opus 5.5 c0b9e85519 Sharing settings and public profile pages (phase 1, step 5)
- sharing.html: switch sharing on/off, see the handle (with the
  "permanent once followed" warning and a note that following comes
  later), choose who sees posts (followers only by default; help text
  on why approval matters), follower approval, directory and search
  opt-ins, display name and bio; save status shown by the button
- profile.html?u=<name>: the public page browsers land on from an actor
  URL: name, handle (with copy), bio and how to follow; never asks the
  visitor to sign in
- "Sharing" appears in the nav only when the server federates
  (remembered per tab)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
2026-09-25 20:32:40 +01:00

97 lines
2.5 KiB
JavaScript

// Shared page shell: site header, navigation, sign-in guard.
import { api, getToken, redirectToLogin, setToken } from "./api.js";
import { h, replace } from "./dom.js";
const NAV = [
["index.html", "Overview"],
["history.html", "History"],
["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");
addSharingLink(header.querySelector("nav ul"), active);
header.append(
h(
"div",
{ class: "account" },
h("span", { class: "account-name" }, user.username),
h("button", { type: "button", class: "link-button", onclick: signOut }, "Sign out"),
),
);
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" }), "Scrobbler");
}
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)),
),
),
);
}
async function signOut() {
try {
await api("/auth/logout", { method: "POST" });
} finally {
setToken(null);
location.href = "login.html";
}
}
// "Sharing" only appears when the server federates. Remembered per tab.
const FEDERATION_KEY = "scrobbler.federation";
async function federationAvailable() {
try {
const cached = sessionStorage.getItem(FEDERATION_KEY);
if (cached !== null) return cached === "1";
} catch {
// Storage unavailable: just ask.
}
let available = false;
try {
await api("/federation/settings");
available = true;
} catch {
available = false;
}
try {
sessionStorage.setItem(FEDERATION_KEY, available ? "1" : "0");
} catch {
// Not remembered; asked again next page.
}
return available;
}
async function addSharingLink(list, active) {
if (!list || !(await federationAvailable())) return;
const href = "sharing.html";
list.append(h("li", {}, h("a", { href, "aria-current": href === active ? "page" : false }, "Sharing")));
}