Add song lookup UI: search a song and view its history + metadata
New song.html shows a song's aggregated metadata and full play history (paginated), reached from the dashboard via a debounced search box or by clicking a track in the "Most played" list. Verified manually in-browser: search, click-through from Most played (including an artist name with a slash), the not-found and no-query-params states, and the pager. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d2f457a84d
commit
88b53df909
5 files changed
+284
-7
No files matched your search
@@ -582,6 +582,16 @@ input {
|
||||
background: var(--slate);
|
||||
}
|
||||
|
||||
.ranking a.name {
|
||||
color: inherit;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.ranking a.name:hover,
|
||||
.ranking a.name:focus-visible {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
/* ---- Listening log -------------------------------------------------------- */
|
||||
.log {
|
||||
list-style: none;
|
||||
@@ -977,3 +987,79 @@ input {
|
||||
.confirm-off .button-row {
|
||||
margin-top: 0.75rem;
|
||||
}
|
||||
|
||||
/* ---- Song lookup ------------------------------------------------------------ */
|
||||
.song-search {
|
||||
position: relative;
|
||||
max-width: 28rem;
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
.song-search .field {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.search-results {
|
||||
position: absolute;
|
||||
z-index: 10;
|
||||
inset-inline: 0;
|
||||
top: 100%;
|
||||
margin: 0.25rem 0 0;
|
||||
padding: 0.35rem;
|
||||
list-style: none;
|
||||
background: var(--paper-raised);
|
||||
border: 1px solid var(--rule);
|
||||
border-radius: var(--radius-control);
|
||||
box-shadow: 0 8px 24px rgb(0 0 0 / 0.12);
|
||||
max-height: 18rem;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.search-results li {
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.search-results a {
|
||||
display: block;
|
||||
padding: 0.5rem 0.6rem;
|
||||
border-radius: var(--radius-control);
|
||||
color: inherit;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.search-results a:hover,
|
||||
.search-results a:focus-visible {
|
||||
background: var(--panel);
|
||||
}
|
||||
|
||||
.search-results .by {
|
||||
display: block;
|
||||
color: var(--ink-soft);
|
||||
font-size: var(--step--1);
|
||||
}
|
||||
|
||||
.search-results .empty {
|
||||
padding: 0.5rem 0.6rem;
|
||||
}
|
||||
|
||||
.song-meta {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
|
||||
gap: 1rem 2rem;
|
||||
margin: 1.25rem 0 2rem;
|
||||
}
|
||||
|
||||
.song-meta div {
|
||||
display: grid;
|
||||
gap: 0.15rem;
|
||||
}
|
||||
|
||||
.song-meta dt {
|
||||
font-size: var(--step--1);
|
||||
color: var(--ink-soft);
|
||||
}
|
||||
|
||||
.song-meta dd {
|
||||
margin: 0;
|
||||
font-weight: 600;
|
||||
}
|
||||
@@ -14,6 +14,14 @@
|
||||
<main id="main">
|
||||
<h1 class="visually-hidden">Overview</h1>
|
||||
<div id="panel" class="panel" aria-live="polite"></div>
|
||||
<section aria-labelledby="song-search-title" class="song-search">
|
||||
<h2 id="song-search-title" class="visually-hidden">Find a song</h2>
|
||||
<div class="field">
|
||||
<label for="song-search">Find a song</label>
|
||||
<input id="song-search" type="search" autocomplete="off" placeholder="Search your scrobbles…">
|
||||
</div>
|
||||
<ul class="search-results" id="song-search-results" hidden></ul>
|
||||
</section>
|
||||
<section aria-labelledby="charts-title">
|
||||
<div class="section-head">
|
||||
<h2 id="charts-title">Most played</h2>
|
||||
|
||||
@@ -19,6 +19,7 @@ let progressTimer;
|
||||
|
||||
await signedInPage("index.html");
|
||||
renderPeriodSelector();
|
||||
initSongSearch();
|
||||
await Promise.all([loadPanel(), loadTop(currentPeriod()), loadRecent()]).catch(showError);
|
||||
setInterval(() => refreshDisplay().catch(() => {}), NOW_PLAYING_POLL_MS);
|
||||
|
||||
@@ -206,10 +207,10 @@ async function loadTop(period) {
|
||||
]);
|
||||
renderRanking(document.getElementById("top-artists"), artists.items, false);
|
||||
renderRanking(document.getElementById("top-albums"), albums.items, true);
|
||||
renderRanking(document.getElementById("top-tracks"), tracks.items, true);
|
||||
renderRanking(document.getElementById("top-tracks"), tracks.items, true, true);
|
||||
}
|
||||
|
||||
function renderRanking(list, items, withArtist) {
|
||||
function renderRanking(list, items, withArtist, linkToSong = false) {
|
||||
if (!items.length) {
|
||||
replace(list, h("li", { class: "empty" }, "No plays in this period."));
|
||||
return;
|
||||
@@ -217,23 +218,83 @@ function renderRanking(list, items, withArtist) {
|
||||
const max = items[0].playcount;
|
||||
replace(
|
||||
list,
|
||||
items.map((item) =>
|
||||
h(
|
||||
items.map((item) => {
|
||||
const by = withArtist ? h("span", { class: "by" }, item.artist) : null;
|
||||
const name = linkToSong
|
||||
? h("a", { class: "name", href: songHref(item.artist, item.name) }, item.name, by)
|
||||
: h("span", { class: "name" }, item.name, by);
|
||||
return h(
|
||||
"li",
|
||||
{},
|
||||
h("span", { class: "rank" }, item.rank),
|
||||
h("span", { class: "name" }, item.name, withArtist ? h("span", { class: "by" }, item.artist) : null),
|
||||
name,
|
||||
h("span", { class: "plays" }, num(item.playcount)),
|
||||
h("span", {
|
||||
class: "meter",
|
||||
"aria-hidden": "true",
|
||||
style: `width: calc((100% - 2.75rem) * ${item.playcount / max})`,
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
function songHref(artist, track) {
|
||||
return `song.html?artist=${encodeURIComponent(artist)}&track=${encodeURIComponent(track)}`;
|
||||
}
|
||||
|
||||
// ---- Song search --------------------------------------------------------------
|
||||
|
||||
function initSongSearch() {
|
||||
const input = document.getElementById("song-search");
|
||||
const results = document.getElementById("song-search-results");
|
||||
let debounce;
|
||||
let hideTimer;
|
||||
|
||||
input.addEventListener("input", () => {
|
||||
clearTimeout(debounce);
|
||||
const q = input.value.trim();
|
||||
if (q.length < 2) {
|
||||
results.hidden = true;
|
||||
return;
|
||||
}
|
||||
debounce = setTimeout(() => runSearch(q), 250);
|
||||
});
|
||||
input.addEventListener("focus", () => {
|
||||
if (results.childElementCount) results.hidden = false;
|
||||
});
|
||||
input.addEventListener("blur", () => {
|
||||
hideTimer = setTimeout(() => (results.hidden = true), 150);
|
||||
});
|
||||
// Keep focus on mousedown so the subsequent click still lands on the result.
|
||||
results.addEventListener("mousedown", (event) => event.preventDefault());
|
||||
|
||||
async function runSearch(q) {
|
||||
let matches;
|
||||
try {
|
||||
matches = await api(`/me/tracks/search?q=${encodeURIComponent(q)}&limit=10`);
|
||||
} catch {
|
||||
return; // a flaky search box isn't worth surfacing an error for
|
||||
}
|
||||
clearTimeout(hideTimer);
|
||||
if (!matches.length) {
|
||||
replace(results, h("li", { class: "empty" }, "No matches."));
|
||||
} else {
|
||||
replace(
|
||||
results,
|
||||
matches.map((m) =>
|
||||
h(
|
||||
"li",
|
||||
{},
|
||||
h("a", { href: songHref(m.artist, m.track) }, m.track, h("span", { class: "by" }, m.artist)),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
results.hidden = false;
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Recently played ----------------------------------------------------------
|
||||
|
||||
async function loadRecent() {
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
import { api } from "./api.js";
|
||||
import { errorBox, formatDate, formatDay, formatDuration, formatTime, h, num, replace } from "./dom.js";
|
||||
import { signedInPage } from "./layout.js";
|
||||
|
||||
const PER_PAGE = 50;
|
||||
const title = document.getElementById("song-title");
|
||||
const subtitle = document.getElementById("song-subtitle");
|
||||
const meta = document.getElementById("song-meta");
|
||||
const history = document.getElementById("song-history");
|
||||
const pager = document.getElementById("pager");
|
||||
|
||||
await signedInPage(null); // a permalink, not a top-level nav section
|
||||
|
||||
const params = new URLSearchParams(location.search);
|
||||
const artist = params.get("artist") ?? "";
|
||||
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."));
|
||||
} else {
|
||||
try {
|
||||
const result = await api(
|
||||
`/me/tracks?artist=${encodeURIComponent(artist)}&track=${encodeURIComponent(track)}&page=${page}&limit=${PER_PAGE}`,
|
||||
);
|
||||
render(result);
|
||||
} catch (error) {
|
||||
title.textContent = track;
|
||||
subtitle.textContent = `by ${artist}`;
|
||||
replace(
|
||||
history,
|
||||
error.status === 404
|
||||
? h("p", { class: "notice" }, "You've never scrobbled this song.")
|
||||
: errorBox(error.message),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function render(result) {
|
||||
document.title = `${result.track} · Scrobbler`;
|
||||
title.textContent = result.track;
|
||||
subtitle.textContent = `by ${result.artist}`;
|
||||
|
||||
const m = result.metadata;
|
||||
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),
|
||||
row("MusicBrainz ID", m.mbid),
|
||||
);
|
||||
|
||||
if (!result.items.length) {
|
||||
replace(history, h("p", { class: "empty" }, "Nothing here yet."));
|
||||
} else {
|
||||
// Group by local calendar day, same as the full history page.
|
||||
const days = new Map();
|
||||
for (const scrobble of result.items) {
|
||||
const key = new Date(scrobble.played_at).toDateString();
|
||||
if (!days.has(key)) days.set(key, []);
|
||||
days.get(key).push(scrobble);
|
||||
}
|
||||
replace(
|
||||
history,
|
||||
[...days.values()].map((scrobbles) => [
|
||||
h("h3", { class: "log-day" }, formatDay(scrobbles[0].played_at)),
|
||||
h(
|
||||
"ol",
|
||||
{ class: "log log-clock" },
|
||||
scrobbles.map((s) =>
|
||||
h(
|
||||
"li",
|
||||
{},
|
||||
h("time", { datetime: s.played_at }, formatTime(s.played_at)),
|
||||
s.album ? h("span", { class: "by" }, `from ${s.album}`) : h("span"),
|
||||
),
|
||||
),
|
||||
),
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
const link = (p) => `?artist=${encodeURIComponent(artist)}&track=${encodeURIComponent(track)}&page=${p}`;
|
||||
replace(
|
||||
pager,
|
||||
result.page > 1 ? h("a", { class: "button button-quiet", href: link(result.page - 1) }, "Newer") : h("span"),
|
||||
result.page < result.total_pages
|
||||
? h("a", { class: "button button-quiet", href: link(result.page + 1) }, "Older")
|
||||
: h("span"),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Song · Scrobbler</title>
|
||||
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Ccircle cx='8' cy='8' r='7' fill='%231c2330'/%3E%3Ccircle cx='8' cy='8' r='3.5' fill='%23f2a541'/%3E%3C/svg%3E">
|
||||
<link rel="stylesheet" href="css/style.css">
|
||||
<script src="config.js"></script>
|
||||
<script type="module" src="js/song.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<header class="site-header" id="site-header"></header>
|
||||
<main id="main">
|
||||
<h1 class="page-title" id="song-title">Song</h1>
|
||||
<p class="lede" id="song-subtitle"></p>
|
||||
<dl class="song-meta" id="song-meta"></dl>
|
||||
<section aria-labelledby="song-history-title">
|
||||
<h2 id="song-history-title">History</h2>
|
||||
<div id="song-history"></div>
|
||||
<nav class="pager" aria-label="Pages" id="pager"></nav>
|
||||
</section>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user