commit
aaa
diff --git a/apps/server/src/routes/artists.ts b/apps/server/src/routes/artists.ts
index b44c8b8..fbc805a 100644
--- a/apps/server/src/routes/artists.ts
+++ b/apps/server/src/routes/artists.ts
@@ -180,6 +180,7 @@ artistRoutes.get("/:id", async (c) => {
id: s.id,
title: s.title,
artist: s.artist,
+ artistId: id,
album: s.album,
durationS: s.duration_s,
explicit: s.explicit,
diff --git a/apps/server/src/routes/playlists.ts b/apps/server/src/routes/playlists.ts
index 444511a..6883577 100644
--- a/apps/server/src/routes/playlists.ts
+++ b/apps/server/src/routes/playlists.ts
@@ -131,8 +131,12 @@ playlistRoutes.get("/:id", async (c) => {
return c.json({ error: "not_found" }, 404);
}
- const songs = await sql<Array<Song & { position: number }>>`
- SELECT s.*, ps.position
+ const songs = await sql<
+ Array<Song & { position: number; resolved_artist_id: string | null }>
+ >`
+ SELECT s.*, ps.position,
+ (SELECT sa.artist_id FROM song_artists sa WHERE sa.song_id = s.id
+ ORDER BY (sa.role = 'primary') DESC, sa.role LIMIT 1) AS resolved_artist_id
FROM playlist_songs ps
JOIN songs s ON s.id = ps.song_id
WHERE ps.playlist_id = ${pl.id}
@@ -151,6 +155,7 @@ playlistRoutes.get("/:id", async (c) => {
id: s.id,
title: s.title,
artist: s.artist,
+ artistId: s.resolved_artist_id,
album: s.album,
durationS: s.duration_s,
explicit: s.explicit,
diff --git a/apps/server/src/routes/songs.ts b/apps/server/src/routes/songs.ts
index 21a490b..0efb6d5 100644
--- a/apps/server/src/routes/songs.ts
+++ b/apps/server/src/routes/songs.ts
@@ -36,8 +36,14 @@ export const songRoutes = new Hono<AppEnv>();
// Everyone authenticated sees the whole shared library.
songRoutes.get("/", requireAuth, async (c) => {
const q = c.req.query("q")?.trim();
- const rows = await sql<Song[]>`SELECT * FROM songs ORDER BY created_at DESC`;
- if (!q) return c.json(rows.map(toPublicSong));
+ const rows = await sql<Array<Song & { resolved_artist_id: string | null }>>`
+ SELECT s.*,
+ (SELECT sa.artist_id FROM song_artists sa WHERE sa.song_id = s.id
+ ORDER BY (sa.role = 'primary') DESC, sa.role LIMIT 1) AS resolved_artist_id
+ FROM songs s
+ ORDER BY s.created_at DESC
+ `;
+ if (!q) return c.json(rows.map((s) => toPublicSong(s, s.resolved_artist_id)));
// Personal-scale library: score every song in-process rather than a DB-side
// fuzzy prefilter (same tradeoff as artists.ts / lib/duplicates.ts). Tight
@@ -62,13 +68,13 @@ songRoutes.get("/", requireAuth, async (c) => {
.filter((r) => r.score >= 0.35)
.sort((a, b) => b.score - a.score);
- return c.json(scored.map((r) => toPublicSong(r.song)));
+ return c.json(scored.map((r) => toPublicSong(r.song, r.song.resolved_artist_id)));
});
songRoutes.get("/:id", requireAuth, async (c) => {
const song = await getSong(c.req.param("id")!);
if (!song) return c.json({ error: "not_found" }, 404);
- return c.json(toPublicSong(song));
+ return c.json(toPublicSong(song, await getPrimaryArtistId(song.id)));
});
// Upload. title + artist required; cover + rest optional; tags auto-read.
@@ -347,7 +353,7 @@ songRoutes.patch("/:id", requireAuth, async (c) => {
// Metadata may have changed -> drop stored lyrics so they refetch for the
// corrected title/artist on next request.
await sql`DELETE FROM song_lyrics WHERE song_id = ${song.id}`.catch(() => {});
- return c.json(toPublicSong(rows[0]!));
+ return c.json(toPublicSong(rows[0]!, await getPrimaryArtistId(song.id)));
});
// Only the uploader may delete.
@@ -377,16 +383,28 @@ async function getSong(id: string): Promise<Song | undefined> {
return rows[0];
}
+// A song can be linked to several artists (song_artists is many-to-many);
+// "primary" role wins when present, otherwise whichever link comes first.
+async function getPrimaryArtistId(songId: string): Promise<string | null> {
+ const rows = await sql<Array<{ artist_id: string }>>`
+ SELECT artist_id FROM song_artists WHERE song_id = ${songId}
+ ORDER BY (role = 'primary') DESC, role
+ LIMIT 1
+ `;
+ return rows[0]?.artist_id ?? null;
+}
+
function str(v: FormDataEntryValue | null): string | null {
return typeof v === "string" && v.length > 0 ? v : null;
}
// Shape sent to the client (never expose absolute filesystem paths).
-function toPublicSong(s: Song) {
+function toPublicSong(s: Song, artistId: string | null = null) {
return {
id: s.id,
title: s.title,
artist: s.artist,
+ artistId,
album: s.album,
durationS: s.duration_s,
explicit: s.explicit,
diff --git a/apps/web/src/app.ts b/apps/web/src/app.ts
index 3d71cc9..ca0679e 100644
--- a/apps/web/src/app.ts
+++ b/apps/web/src/app.ts
@@ -1047,6 +1047,14 @@ async function renderArtistView(el: HTMLElement, id: string): Promise<void> {
});
}
+// Shared "go to this artist's page" navigation, used by clickable artist
+// names in song rows, the player bar, and the lyrics overlay.
+function openArtist(id: string): void {
+ if (lyricsState.open) toggleLyrics();
+ state.view = { kind: "artist", id };
+ render();
+}
+
// Create a brand-new artist page. Shared by the "Artists" browse page and
// the "link song to artist" flow's "can't find it? create new" fallback —
// `onCreated` fires either with the newly-created artist, or with the
@@ -1558,12 +1566,16 @@ function songTableHtml(songs: Song[], editablePlaylistId?: string): string {
? `<span class="tag-e" title="Explicit">E</span>`
: "";
+ const artist = s.artistId
+ ? `<button class="song-artist artist-link" data-open-artist="${s.artistId}">${escapeHtml(s.artist)}</button>`
+ : `<span class="song-artist">${escapeHtml(s.artist)}</span>`;
+
return `
<div class="song" data-index="${i}" data-song-id="${s.id}">
${cover}
<div class="song-meta">
<span class="song-title">${escapeHtml(s.title)}${badge}</span>
- <span class="song-artist">${escapeHtml(s.artist)}</span>
+ ${artist}
</div>
<span class="song-dur">${s.durationS ? formatTime(s.durationS) : ""}</span>
<div class="song-actions">${action}${linkArtist}${edit}${del}</div>
@@ -1586,11 +1598,18 @@ function wireSongList(editablePlaylistId?: string): void {
// Double-clicking the row also plays, Spotify-style.
document.querySelectorAll(".song").forEach((row) => {
row.addEventListener("dblclick", (e) => {
- if ((e.target as HTMLElement).closest(".song-actions")) return;
+ if ((e.target as HTMLElement).closest(".song-actions, .song-artist")) return;
playFromList(Number((row as HTMLElement).dataset.index));
});
});
+ document.querySelectorAll("[data-open-artist]").forEach((btn) => {
+ btn.addEventListener("click", (e) => {
+ e.stopPropagation();
+ openArtist((btn as HTMLElement).dataset.openArtist!);
+ });
+ });
+
document.querySelectorAll("[data-add]").forEach((btn) => {
btn.addEventListener("click", async (e) => {
e.stopPropagation();
@@ -1762,7 +1781,11 @@ function mountPlayerBar(el: HTMLElement, song: Song): void {
</div>
<div class="song-meta">
<span class="song-title">${escapeHtml(song.title)}</span>
- <span class="song-artist">${escapeHtml(song.artist)}</span>
+ ${
+ song.artistId
+ ? `<button class="song-artist artist-link" id="pb-artist">${escapeHtml(song.artist)}</button>`
+ : `<span class="song-artist">${escapeHtml(song.artist)}</span>`
+ }
</div>
</div>
@@ -1828,6 +1851,9 @@ function mountPlayerBar(el: HTMLElement, song: Song): void {
});
el.querySelector("#lyrics-btn")?.addEventListener("click", toggleLyrics);
+ if (song.artistId) {
+ el.querySelector("#pb-artist")?.addEventListener("click", () => openArtist(song.artistId!));
+ }
}
// Update only the changing pieces — never rebuild (keeps sliders draggable).
@@ -1945,13 +1971,20 @@ function renderLyricsOverlay(): void {
<div class="ly-head">
<div class="ly-song">
<span class="ly-title">${song ? escapeHtml(song.title) : "—"}</span>
- <span class="ly-artist">${song ? escapeHtml(song.artist) : ""}</span>
+ ${
+ song?.artistId
+ ? `<button class="ly-artist artist-link" id="ly-artist-btn">${escapeHtml(song.artist)}</button>`
+ : `<span class="ly-artist">${song ? escapeHtml(song.artist) : ""}</span>`
+ }
</div>
<button class="icon-btn" id="ly-close" title="Close"><i class="bi bi-x-lg"></i></button>
</div>
<div class="ly-body" id="ly-body"><p class="ly-note">Finding lyrics…</p></div>
`;
ov.querySelector("#ly-close")?.addEventListener("click", toggleLyrics);
+ if (song?.artistId) {
+ ov.querySelector("#ly-artist-btn")?.addEventListener("click", () => openArtist(song.artistId!));
+ }
}
async function loadLyricsForCurrent(): Promise<void> {
diff --git a/apps/web/src/styles.css b/apps/web/src/styles.css
index 6e137a3..d086935 100644
--- a/apps/web/src/styles.css
+++ b/apps/web/src/styles.css
@@ -413,6 +413,23 @@ button {
font-size: 0.85rem;
}
+.artist-link {
+ background: none;
+ border: none;
+ padding: 0;
+ margin: 0;
+ font: inherit;
+ text-align: left;
+ cursor: pointer;
+ width: fit-content;
+}
+
+.artist-link:hover,
+.artist-link:focus-visible {
+ color: var(--accent);
+ text-decoration: underline;
+}
+
.song-dur {
color: var(--muted);
font-variant-numeric: tabular-nums;
diff --git a/packages/shared/src/types.ts b/packages/shared/src/types.ts
index 4afbbb4..19c0240 100644
--- a/packages/shared/src/types.ts
+++ b/packages/shared/src/types.ts
@@ -6,6 +6,7 @@ export type Song = {
id: string;
title: string;
artist: string;
+ artistId: string | null;
album: string | null;
durationS: number | null;
explicit: boolean;