From ae24c4a00e9d637a451790200416aa89adefa462 Mon Sep 17 00:00:00 2001 From: Matthew Daubney Date: Mon, 5 Oct 2026 15:36:34 +0100 Subject: [PATCH] Redesign the song page around the dashboard's front-panel look Previously the song page was a plain heading, a bare metadata list and a history log. Now it gets its own moment on the glowing amber display, reusing the dashboard's .panel/.display/.readouts/ .spectrum components verbatim: the track name lit up like a now-playing display, play/first-played/last-played readouts, and a 90-day per-track play-activity chart. Extracted the segmented spectrum chart out of dashboard.js into a shared frontend/js/spectrum.js so both pages use the same renderer; the dashboard's own output is unchanged. Verified manually in-browser: the panel renders correctly, an artist name with a slash round-trips, the not-found and no-query-params states render into the panel, and the dashboard's spectrum chart is pixel-for-pixel unchanged after the extraction. Co-Authored-By: Claude Sonnet 5 --- frontend/js/dashboard.js | 53 ++------------------------------------ frontend/js/song.js | 45 ++++++++++++++++++++++---------- frontend/js/spectrum.js | 55 ++++++++++++++++++++++++++++++++++++++++ frontend/song.html | 4 +-- 4 files changed, 90 insertions(+), 67 deletions(-) create mode 100644 frontend/js/spectrum.js diff --git a/frontend/js/dashboard.js b/frontend/js/dashboard.js index ee207dd..7c08145 100644 --- a/frontend/js/dashboard.js +++ b/frontend/js/dashboard.js @@ -1,6 +1,7 @@ import { api } from "./api.js"; -import { errorBox, formatDate, formatDuration, h, num, replace, timeAgo } from "./dom.js"; +import { errorBox, formatDuration, h, num, replace, timeAgo } from "./dom.js"; import { signedInPage } from "./layout.js"; +import { spectrum } from "./spectrum.js"; const PERIODS = [ ["7day", "7 days"], @@ -9,7 +10,6 @@ const PERIODS = [ ["overall", "All time"], ]; const PERIOD_KEY = "scrobbler.period"; -const SEGMENTS = 10; // lit segments per spectrum column at the busiest day const NOW_PLAYING_POLL_MS = 30_000; const panel = document.getElementById("panel"); @@ -118,55 +118,6 @@ function readouts(summary) { ); } -// Daily plays for the last 30 days, drawn as a segmented level meter. -function spectrum(counts) { - const NS = "http://www.w3.org/2000/svg"; - const width = 1000; - const height = 96; - const gap = 4; - const max = Math.max(1, ...counts.map((c) => c.count)); - const columnWidth = width / counts.length; - const segmentHeight = height / SEGMENTS; - const total = counts.reduce((sum, c) => sum + c.count, 0); - - const svg = document.createElementNS(NS, "svg"); - svg.setAttribute("viewBox", `0 0 ${width} ${height}`); - svg.setAttribute("preserveAspectRatio", "none"); - svg.setAttribute("role", "img"); - svg.setAttribute("aria-label", `Plays per day over the last ${counts.length} days: ${num(total)} in total`); - - counts.forEach((day, i) => { - const lit = day.count ? Math.max(1, Math.round((day.count / max) * SEGMENTS)) : 0; - const group = document.createElementNS(NS, "g"); - const title = document.createElementNS(NS, "title"); - title.textContent = `${formatDate(day.start)}: ${num(day.count)} ${day.count === 1 ? "play" : "plays"}`; - group.append(title); - for (let s = 0; s < SEGMENTS; s++) { - const rect = document.createElementNS(NS, "rect"); - rect.setAttribute("x", i * columnWidth + gap / 2); - rect.setAttribute("y", height - (s + 1) * segmentHeight + 1.5); - rect.setAttribute("width", columnWidth - gap); - rect.setAttribute("height", segmentHeight - 3); - rect.setAttribute("class", s < lit ? "seg-on" : "seg-off"); - group.append(rect); - } - svg.append(group); - }); - - return h( - "div", - { class: "spectrum" }, - svg, - h( - "div", - { class: "spectrum-axis" }, - h("span", {}, formatDate(counts[0].start)), - h("span", {}, `${num(total)} plays in 30 days`), - h("span", {}, "Today"), - ), - ); -} - // ---- Most played ------------------------------------------------------------ function currentPeriod() { diff --git a/frontend/js/song.js b/frontend/js/song.js index ac3fdcd..ad4d5d2 100644 --- a/frontend/js/song.js +++ b/frontend/js/song.js @@ -1,10 +1,10 @@ import { api } from "./api.js"; import { errorBox, formatDate, formatDay, formatDuration, formatTime, h, num, replace } from "./dom.js"; import { signedInPage } from "./layout.js"; +import { spectrum } from "./spectrum.js"; const PER_PAGE = 50; -const title = document.getElementById("song-title"); -const subtitle = document.getElementById("song-subtitle"); +const panel = document.getElementById("panel"); const meta = document.getElementById("song-meta"); const history = document.getElementById("song-history"); const pager = document.getElementById("pager"); @@ -17,7 +17,7 @@ const track = params.get("track") ?? ""; const page = Math.max(1, Number.parseInt(params.get("page"), 10) || 1); if (!artist || !track) { - replace(history, h("p", { class: "notice" }, "Use the search box on the dashboard to find a song.")); + replace(panel, h("p", { class: "notice" }, "Use the search box on the dashboard to find a song.")); } else { try { const result = await api( @@ -25,12 +25,10 @@ if (!artist || !track) { ); render(result); } catch (error) { - title.textContent = track; - subtitle.textContent = `by ${artist}`; replace( - history, + panel, error.status === 404 - ? h("p", { class: "notice" }, "You've never scrobbled this song.") + ? h("p", { class: "notice" }, `You've never scrobbled "${track}" by ${artist}.`) : errorBox(error.message), ); } @@ -38,18 +36,26 @@ if (!artist || !track) { function render(result) { document.title = `${result.track} ยท Scrobbler`; - title.textContent = result.track; - subtitle.textContent = `by ${result.artist}`; - const m = result.metadata; + + const display = h( + "div", + { class: "display" }, + h("p", { class: "display-status" }, "Now viewing"), + h("p", { class: "display-track" }, result.track), + h( + "p", + { class: "display-meta" }, + h("strong", {}, result.artist), + m.album ? [" from ", h("strong", {}, m.album)] : null, + ), + ); + replace(panel, display, readouts(m), spectrum(result.counts, `Plays of ${result.track}`, "90 days")); + const row = (label, value) => value === null || value === undefined ? null : h("div", {}, h("dt", {}, label), h("dd", {}, value)); replace( meta, - row("Plays", num(m.playcount)), - row("First played", formatDate(m.first_played_at)), - row("Last played", formatDate(m.last_played_at)), - row("Album", m.album), row("Album artist", m.album_artist), row("Track number", m.track_number), row("Duration", m.duration ? formatDuration(m.duration) : null), @@ -95,3 +101,14 @@ function render(result) { : h("span"), ); } + +function readouts(m) { + const item = (value, label) => h("div", {}, h("dt", {}, label), h("dd", {}, value)); + return h( + "dl", + { class: "readouts" }, + item(num(m.playcount), "plays"), + item(formatDate(m.first_played_at), "first played"), + item(formatDate(m.last_played_at), "last played"), + ); +} diff --git a/frontend/js/spectrum.js b/frontend/js/spectrum.js new file mode 100644 index 0000000..6e47d06 --- /dev/null +++ b/frontend/js/spectrum.js @@ -0,0 +1,55 @@ +// A segmented level meter: daily play counts drawn as columns of lit/unlit segments, +// like a hi-fi spectrum analyser. Shared by the dashboard (30 days, all plays) and the +// song page (90 days, one track). + +import { formatDate, h, num } from "./dom.js"; + +const SEGMENTS = 10; // lit segments per column at the busiest day + +export function spectrum(counts, subject = "Plays", windowLabel = "30 days") { + const NS = "http://www.w3.org/2000/svg"; + const width = 1000; + const height = 96; + const gap = 4; + const max = Math.max(1, ...counts.map((c) => c.count)); + const columnWidth = width / counts.length; + const segmentHeight = height / SEGMENTS; + const total = counts.reduce((sum, c) => sum + c.count, 0); + + const svg = document.createElementNS(NS, "svg"); + svg.setAttribute("viewBox", `0 0 ${width} ${height}`); + svg.setAttribute("preserveAspectRatio", "none"); + svg.setAttribute("role", "img"); + svg.setAttribute("aria-label", `${subject} per day over the last ${counts.length} days: ${num(total)} in total`); + + counts.forEach((day, i) => { + const lit = day.count ? Math.max(1, Math.round((day.count / max) * SEGMENTS)) : 0; + const group = document.createElementNS(NS, "g"); + const title = document.createElementNS(NS, "title"); + title.textContent = `${formatDate(day.start)}: ${num(day.count)} ${day.count === 1 ? "play" : "plays"}`; + group.append(title); + for (let s = 0; s < SEGMENTS; s++) { + const rect = document.createElementNS(NS, "rect"); + rect.setAttribute("x", i * columnWidth + gap / 2); + rect.setAttribute("y", height - (s + 1) * segmentHeight + 1.5); + rect.setAttribute("width", columnWidth - gap); + rect.setAttribute("height", segmentHeight - 3); + rect.setAttribute("class", s < lit ? "seg-on" : "seg-off"); + group.append(rect); + } + svg.append(group); + }); + + return h( + "div", + { class: "spectrum" }, + svg, + h( + "div", + { class: "spectrum-axis" }, + h("span", {}, formatDate(counts[0].start)), + h("span", {}, `${num(total)} ${subject.toLowerCase()} in ${windowLabel}`), + h("span", {}, "Today"), + ), + ); +} diff --git a/frontend/song.html b/frontend/song.html index a85cc10..db6fa28 100644 --- a/frontend/song.html +++ b/frontend/song.html @@ -12,8 +12,8 @@
-

Song

-

+

Song

+

History