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 <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-08-30 10:29:13 -04:00
parent af97973733
commit 2bf81798f8
5 changed files with 62 additions and 27 deletions

View File

@@ -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: {

View File

@@ -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" &&

View File

@@ -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<JukeboxMode>(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<JukeboxMode>(
entryMode === "home" ? parseJukeboxMode(route.query.mode) : entryMode,
);
/** A ref mirrored into `?key=`; writes are merged by `patchQuery`. */
function queryRef(key: string): Ref<string> {
@@ -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);

View File

@@ -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");
}
});
});

View File

@@ -18,8 +18,8 @@ export type JukeboxMode = JukeboxPlayerMode | "home";
const MODES = new Set<string>(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";