nav.js

4.6 KB · raw

1// The navbar when signed in: "admin" and "log out" in place of "log in". Loaded right after the2// nav, before the rest of the page, so the signed-out links never show first. The readable3// kepr_editor cookie carries the form token that log out posts back. Only the notification preview4// needs a request.5// Suppress the dense static dots before the timeline markup can paint.6document.documentElement.classList.add("timeline-pending");7// A failed deferred script never reaches the timeline's finally block.8window.addEventListener("load", () => {9	if (typeof window.timeline !== "function") {10		document.documentElement.classList.remove("timeline-pending");11	}12});1314{15	const token = /(?:^|;\s*)kepr_editor=([0-9a-f]{64})(?:;|$)/.exec(document.cookie)?.[1];16	const link = document.querySelector('.nav-links a[href^="/sign-in"]');17	if (token && link) {18		const admin = document.createElement("a");19		admin.href = "/admin";20		admin.textContent = "admin";21		const form = document.createElement("form");22		form.className = "nav-logout";23		form.method = "post";24		form.action = "/admin/sign-out";25		const field = document.createElement("input");26		field.type = "hidden";27		field.name = "form";28		field.value = token;29		const button = document.createElement("button");30		button.type = "submit";31		button.textContent = "log out";32		form.append(field, button);33		form.addEventListener("submit", async (event) => {34			event.preventDefault();35			button.disabled = true;36			try {37				// A regular form post under no-referrer gives Chromium a null Origin.38				const response = await fetch(form.action, {39					method: "POST",40					body: new FormData(form),41					redirect: "manual",42					referrer: location.origin,43					referrerPolicy: "same-origin",44				});45				if (response.type !== "opaqueredirect") throw new Error(`HTTP ${response.status}`);46				location.assign("/");47			} catch (error) {48				console.error("Could not log out", error);49				button.textContent = "retry log out";50				button.disabled = false;51			}52		});53		const bell = document.createElement("details");54		bell.className = "nav-notifications";55		const toggle = document.createElement("summary");56		toggle.setAttribute("aria-label", "notifications");57		toggle.innerHTML =58			'<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M18 8a6 6 0 0 0-12 0c0 7-3 7-3 9h18c0-2-3-2-3-9M10 21h4"/></svg>';59		const dot = document.createElement("span");60		dot.className = "notification-dot";61		dot.hidden = true;62		toggle.append(dot);63		const preview = document.createElement("div");64		preview.className = "notification-preview";65		const heading = document.createElement("strong");66		heading.textContent = "notifications";67		const list = document.createElement("div");68		list.className = "notification-list";69		list.textContent = "loading…";70		const all = document.createElement("a");71		all.href = "/admin/notifications";72		all.className = "notification-all";73		all.textContent = "view all notifications ↗";74		preview.append(heading, list, all);75		bell.append(toggle, preview);76		link.parentElement.after(bell);77		let loading = false;78		const refresh = async () => {79			if (loading) return;80			loading = true;81			try {82				const response = await fetch("/admin/notifications?format=json", { cache: "no-store" });83				if (response.status === 401) {84					bell.remove();85					return;86				}87				if (!response.ok) throw new Error(`notifications: ${response.status}`);88				const feed = await response.json();89				dot.hidden = feed.unread === 0;90				toggle.setAttribute("aria-label", `notifications, ${feed.unread} unread`);91				list.replaceChildren();92				for (const item of feed.notifications) {93					const entry = document.createElement("a");94					entry.href = `/admin/notifications/${item.id}`;95					entry.className = item.read ? "" : "notification-unread";96					const text = document.createElement("span");97					text.textContent = item.text;98					const when = document.createElement("small");99					when.textContent = `${item.read ? "" : "unread · "}${item.when}`;100					entry.append(text, when);101					list.append(entry);102				}103				if (!feed.notifications.length) list.textContent = "nothing yet";104			} catch (error) {105				list.textContent = "Could not load notifications. Open the full list or try again.";106				console.error(error);107			} finally {108				loading = false;109			}110		};111		bell.addEventListener("toggle", () => {112			if (bell.open) refresh();113		});114		document.addEventListener("click", (event) => {115			if (!bell.contains(event.target)) bell.open = false;116		});117		document.addEventListener("keydown", (event) => {118			if (event.key === "Escape" && bell.open) {119				bell.open = false;120				toggle.focus();121			}122		});123		refresh();124125		link.replaceWith(admin, form);126	}127}