- Plain HTML + vanilla JS modules, no build step; talks to /api/v1 with a bearer token and only ever inserts server data as text - Overview: now-playing dot-matrix display with live progress, totals, 30-day plays meter, top artists/tracks by period, recent plays - History (paged, grouped by day), Apps (create/delete, reveal/copy keys, player setup details), Sessions (revoke), sign-in approval page for the Last.fm desktop auth flow, sign in and registration - Light and dark themes, responsive to phone widths, vendored fonts (SIL OFL) so the UI has no third-party requests Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
68 lines
2.3 KiB
JavaScript
68 lines
2.3 KiB
JavaScript
// DOM helpers. Server data only ever reaches the page as text nodes or attribute
|
|
// values, never as HTML, so track names can't inject markup.
|
|
|
|
export function h(tag, attrs = {}, ...children) {
|
|
const el = document.createElement(tag);
|
|
for (const [key, value] of Object.entries(attrs ?? {})) {
|
|
if (value === false || value === null || value === undefined) continue;
|
|
if (key.startsWith("on") && typeof value === "function") el.addEventListener(key.slice(2), value);
|
|
else if (key === "class") el.className = value;
|
|
else if (key === "dataset") Object.assign(el.dataset, value);
|
|
else el.setAttribute(key, value === true ? "" : String(value));
|
|
}
|
|
append(el, children);
|
|
return el;
|
|
}
|
|
|
|
function append(el, children) {
|
|
for (const child of children.flat(Infinity)) {
|
|
if (child === null || child === undefined || child === false) continue;
|
|
el.append(child instanceof Node ? child : document.createTextNode(String(child)));
|
|
}
|
|
}
|
|
|
|
export function replace(el, ...children) {
|
|
el.replaceChildren();
|
|
append(el, children);
|
|
return el;
|
|
}
|
|
|
|
const numberFormat = new Intl.NumberFormat();
|
|
export const num = (n) => numberFormat.format(n);
|
|
|
|
const relative = new Intl.RelativeTimeFormat(undefined, { numeric: "auto" });
|
|
const UNITS = [
|
|
["year", 365 * 86400],
|
|
["month", 30 * 86400],
|
|
["week", 7 * 86400],
|
|
["day", 86400],
|
|
["hour", 3600],
|
|
["minute", 60],
|
|
];
|
|
|
|
export function timeAgo(value) {
|
|
const seconds = (new Date(value).getTime() - Date.now()) / 1000;
|
|
for (const [unit, size] of UNITS) {
|
|
if (Math.abs(seconds) >= size) return relative.format(Math.round(seconds / size), unit);
|
|
}
|
|
return "just now";
|
|
}
|
|
|
|
export const formatTime = (value) =>
|
|
new Date(value).toLocaleTimeString(undefined, { hour: "2-digit", minute: "2-digit" });
|
|
|
|
export const formatDay = (value) =>
|
|
new Date(value).toLocaleDateString(undefined, { weekday: "long", day: "numeric", month: "long", year: "numeric" });
|
|
|
|
export const formatDate = (value) =>
|
|
new Date(value).toLocaleDateString(undefined, { day: "numeric", month: "short", year: "numeric" });
|
|
|
|
export function formatDuration(seconds) {
|
|
const s = Math.max(0, Math.floor(seconds));
|
|
return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`;
|
|
}
|
|
|
|
export function errorBox(message) {
|
|
return h("p", { class: "notice notice-error", role: "alert" }, message);
|
|
}
|