From 2bf81798f82ebf9bb3d753b68e1531600626804e Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Sun, 30 Aug 2026 10:29:13 -0400 Subject: [PATCH] fix(music): give jukebox modes real paths so browser history works The browse mode moves from ?mode= to a path segment (/music/:mode?) written with router.push, so every subgroup is its own history entry and back/forward walks between them. Selections inside a mode stay in the query via router.replace so refining a pick does not spam history. Legacy ?mode= links are honored on entry and rewritten to the subpath. Co-Authored-By: Claude Fable 5 --- frontend/src/plugins/router.ts | 4 +- .../v2/components/Soundtrack/MiniPlayer.vue | 2 +- .../composables/useJukeboxUrlState/index.ts | 67 +++++++++++++------ frontend/src/v2/utils/jukebox.test.ts | 12 ++-- frontend/src/v2/utils/jukebox.ts | 4 +- 5 files changed, 62 insertions(+), 27 deletions(-) diff --git a/frontend/src/plugins/router.ts b/frontend/src/plugins/router.ts index 36543dd31..72238e308 100644 --- a/frontend/src/plugins/router.ts +++ b/frontend/src/plugins/router.ts @@ -188,7 +188,9 @@ const routes = [ }, }, { - path: "music", + // The browse mode is a path segment so every jukebox subgroup is + // its own history entry and browser back/forward walks between them. + path: "music/:mode?", name: ROUTES.MUSIC, meta: { title: i18n.global.t("common.jukebox") }, components: { diff --git a/frontend/src/v2/components/Soundtrack/MiniPlayer.vue b/frontend/src/v2/components/Soundtrack/MiniPlayer.vue index b53703640..5b9eea025 100644 --- a/frontend/src/v2/components/Soundtrack/MiniPlayer.vue +++ b/frontend/src/v2/components/Soundtrack/MiniPlayer.vue @@ -53,7 +53,7 @@ let loadToken = 0; const onFullSoundtrackPlayer = computed(() => { const onJukeboxPlayer = - route.name === ROUTES.MUSIC && isJukeboxPlayerMode(route.query.mode); + route.name === ROUTES.MUSIC && isJukeboxPlayerMode(route.params.mode); const onGameSoundtrack = route.name === "rom" && route.query.tab === "media" && diff --git a/frontend/src/v2/composables/useJukeboxUrlState/index.ts b/frontend/src/v2/composables/useJukeboxUrlState/index.ts index ad7d21239..c2a2dc452 100644 --- a/frontend/src/v2/composables/useJukeboxUrlState/index.ts +++ b/frontend/src/v2/composables/useJukeboxUrlState/index.ts @@ -1,12 +1,17 @@ // Two-way binding between the jukebox's browse state and the URL. // -// Mode and the current selection are bookmarkable session state (constitution -// §VI.D), so they live in the query string. Every param goes through the same -// `queryRef` helper rather than a hand-written ref + watcher pair per param, -// which is what let `artist` silently miss its URL->ref direction before. +// The mode is a path segment (`/music/:mode?`) written with `router.push`, +// so every subgroup is its own history entry and browser back/forward walks +// between them. The selection inside a mode (picked artist, album, ...) is +// bookmarkable session state (constitution §VI.D) and lives in the query +// string, written with `router.replace` so refining a pick does not spam +// the history. Every query param goes through the same `queryRef` helper +// rather than a hand-written ref + watcher pair per param, which is what +// let `artist` silently miss its URL->ref direction before. import { computed, ref, watch, type Ref } from "vue"; import { useRoute, useRouter } from "vue-router"; -import { modeFromQuery, type JukeboxMode } from "@/v2/utils/jukebox"; +import { ROUTES } from "@/plugins/router"; +import { parseJukeboxMode, type JukeboxMode } from "@/v2/utils/jukebox"; import { patchQuery } from "@/v2/utils/routeQuery"; function readParam(value: unknown): string { @@ -17,7 +22,12 @@ export function useJukeboxUrlState() { const route = useRoute(); const router = useRouter(); - const mode = ref(modeFromQuery(route.query.mode)); + // Old links carried the mode as `?mode=`; honor them on entry (the + // redirect below rewrites the URL to the subpath form). + const entryMode = parseJukeboxMode(route.params.mode); + const mode = ref( + entryMode === "home" ? parseJukeboxMode(route.query.mode) : entryMode, + ); /** A ref mirrored into `?key=`; writes are merged by `patchQuery`. */ function queryRef(key: string): Ref { @@ -42,28 +52,47 @@ export function useJukeboxUrlState() { const decade = queryRef("decade"); const game = queryRef("game"); + /** The location for a mode, carrying only the selection params that make + * sense inside it so a copied link never holds a stale filter. */ + function locationFor(value: JukeboxMode) { + return { + name: ROUTES.MUSIC, + params: { mode: value === "home" ? "" : value }, + query: { + game: value === "album" ? game.value || undefined : undefined, + artist: value === "artist" ? artist.value || undefined : undefined, + genre: value === "genre" ? genre.value || undefined : undefined, + platform: + value === "platform" ? platform.value || undefined : undefined, + decade: value === "decade" ? decade.value || undefined : undefined, + search: value === "album" ? search.value || undefined : undefined, + }, + }; + } + + // URL -> ref: back/forward and direct navigation drive the mode. watch( - () => route.query.mode, + () => route.params.mode, (value) => { - const next = modeFromQuery(value); + const next = parseJukeboxMode(value); if (next !== mode.value) mode.value = next; }, ); - // Leaving a mode drops the selection params that only make sense inside it, - // so a copied link never carries a stale filter from a previous screen. + // ref -> URL: in-app mode switches push a new history entry. watch(mode, (value) => { - patchQuery(router, { - mode: value === "home" ? undefined : value, - game: value === "album" ? game.value || undefined : undefined, - artist: value === "artist" ? artist.value || undefined : undefined, - genre: value === "genre" ? genre.value || undefined : undefined, - platform: value === "platform" ? platform.value || undefined : undefined, - decade: value === "decade" ? decade.value || undefined : undefined, - search: value === "album" ? search.value || undefined : undefined, - }); + if (parseJukeboxMode(route.params.mode) === value) return; + void router.push(locationFor(value)); }); + // Rewrite legacy `?mode=` links to the subpath form. + if ( + parseJukeboxMode(route.query.mode) !== "home" && + parseJukeboxMode(route.params.mode) === "home" + ) { + void router.replace(locationFor(mode.value)); + } + const selectedPlatformId = computed(() => Number(platform.value) || 0); const selectedDecade = computed(() => Number(decade.value) || 0); const selectedRomId = computed(() => Number(game.value) || 0); diff --git a/frontend/src/v2/utils/jukebox.test.ts b/frontend/src/v2/utils/jukebox.test.ts index 6eca072d8..f88017d4c 100644 --- a/frontend/src/v2/utils/jukebox.test.ts +++ b/frontend/src/v2/utils/jukebox.test.ts @@ -1,16 +1,20 @@ import { describe, expect, it } from "vitest"; -import { isJukeboxPlayerMode, JUKEBOX_MODES, modeFromQuery } from "./jukebox"; +import { + isJukeboxPlayerMode, + JUKEBOX_MODES, + parseJukeboxMode, +} from "./jukebox"; -describe("modeFromQuery", () => { +describe("parseJukeboxMode", () => { it("accepts every published mode", () => { for (const mode of JUKEBOX_MODES) { - expect(modeFromQuery(mode)).toBe(mode); + expect(parseJukeboxMode(mode)).toBe(mode); } }); it("falls back to home for anything else", () => { for (const value of [undefined, null, "", "nope", 3, ["album"]]) { - expect(modeFromQuery(value)).toBe("home"); + expect(parseJukeboxMode(value)).toBe("home"); } }); }); diff --git a/frontend/src/v2/utils/jukebox.ts b/frontend/src/v2/utils/jukebox.ts index be2f03bb3..0e9b403a0 100644 --- a/frontend/src/v2/utils/jukebox.ts +++ b/frontend/src/v2/utils/jukebox.ts @@ -18,8 +18,8 @@ export type JukeboxMode = JukeboxPlayerMode | "home"; const MODES = new Set(JUKEBOX_MODES); -/** `home` is the fallback: it is the mode with no `?mode=` param. */ -export function modeFromQuery(value: unknown): JukeboxMode { +/** `home` is the fallback: it is the mode with no path segment. */ +export function parseJukeboxMode(value: unknown): JukeboxMode { return typeof value === "string" && MODES.has(value) ? (value as JukeboxPlayerMode) : "home";