diff --git a/frontend/src/plugins/routeNames.ts b/frontend/src/plugins/routeNames.ts new file mode 100644 index 000000000..0a6b16895 --- /dev/null +++ b/frontend/src/plugins/routeNames.ts @@ -0,0 +1,61 @@ +export const ROUTES = { + SETUP: "setup", + LOGIN: "login", + RESET_PASSWORD: "reset-password", + REGISTER: "register", + MAIN: "main", + HOME: "home", + SEARCH: "search", + MUSIC: "music", + PLATFORM: "platform", + COLLECTION: "collection", + VIRTUAL_COLLECTION: "virtual-collection", + SMART_COLLECTION: "smart-collection", + ROM: "rom", + EMULATORJS: "emulatorjs", + JSDOS: "jsdos", + PICO8: "pico8", + RUFFLE: "ruffle", + STREAM: "stream", + STREAM_DESKTOP: "stream-desktop", + SCAN: "scan", + UPLOAD: "upload", + ACTIVITY: "activity", + USER_PROFILE: "user-profile", + USER_INTERFACE: "user-interface", + LIBRARY_MANAGEMENT: "library-management", + SCAN_SETTINGS: "scan-settings", + METADATA_SOURCES: "metadata-sources", + CLIENT_API_TOKENS: "client-api-tokens", + ADMINISTRATION: "administration", + SERVER_STATS: "server-stats", + LOGS: "logs", + PAIR: "pair", + PAIR_DEVICE: "pair-device", + APRIL_FOOLS: "april-fools", + CONSOLE_HOME: "console-home", + CONSOLE_PLATFORM: "console-platform", + CONSOLE_COLLECTION: "console-collection", + CONSOLE_SMART_COLLECTION: "console-smart-collection", + CONSOLE_VIRTUAL_COLLECTION: "console-virtual-collection", + CONSOLE_ROM: "console-rom", + CONSOLE_PLAY: "console-play", + PLATFORMS_INDEX: "platforms-index", + COLLECTIONS_INDEX: "collections-index", + CONTROLLER_DEBUG: "controller-debug", + NOT_FOUND: "404", +} as const; + +const authExemptRoutes = [ + ROUTES.LOGIN, + ROUTES.SETUP, + ROUTES.RESET_PASSWORD, + ROUTES.REGISTER, + ROUTES.PAIR, +] as const; + +type AuthExemptRoute = (typeof authExemptRoutes)[number]; + +export function isAuthExemptRoute(route: string): route is AuthExemptRoute { + return (authExemptRoutes as readonly string[]).includes(route); +} diff --git a/frontend/src/plugins/router.ts b/frontend/src/plugins/router.ts index 2a87c9f82..55cd1fa9c 100644 --- a/frontend/src/plugins/router.ts +++ b/frontend/src/plugins/router.ts @@ -7,6 +7,7 @@ import { type RouteLocationNormalized, } from "vue-router"; import i18n, { loadLocale } from "@/locales"; +import { isAuthExemptRoute, ROUTES } from "@/plugins/routeNames"; import { startViewTransition } from "@/plugins/transition"; import romApi from "@/services/api/rom"; import storeAuth from "@/stores/auth"; @@ -19,54 +20,7 @@ import { v2RouteComponents, } from "@/v2/router/routes"; -export const ROUTES = { - SETUP: "setup", - LOGIN: "login", - RESET_PASSWORD: "reset-password", - REGISTER: "register", - MAIN: "main", - HOME: "home", - SEARCH: "search", - MUSIC: "music", - PLATFORM: "platform", - COLLECTION: "collection", - VIRTUAL_COLLECTION: "virtual-collection", - SMART_COLLECTION: "smart-collection", - ROM: "rom", - EMULATORJS: "emulatorjs", - JSDOS: "jsdos", - PICO8: "pico8", - RUFFLE: "ruffle", - STREAM: "stream", - STREAM_DESKTOP: "stream-desktop", - SCAN: "scan", - UPLOAD: "upload", - ACTIVITY: "activity", - USER_PROFILE: "user-profile", - USER_INTERFACE: "user-interface", - LIBRARY_MANAGEMENT: "library-management", - SCAN_SETTINGS: "scan-settings", - METADATA_SOURCES: "metadata-sources", - CLIENT_API_TOKENS: "client-api-tokens", - ADMINISTRATION: "administration", - SERVER_STATS: "server-stats", - LOGS: "logs", - PAIR: "pair", - PAIR_DEVICE: "pair-device", - APRIL_FOOLS: "april-fools", - CONSOLE_HOME: "console-home", - CONSOLE_PLATFORM: "console-platform", - CONSOLE_COLLECTION: "console-collection", - CONSOLE_SMART_COLLECTION: "console-smart-collection", - CONSOLE_VIRTUAL_COLLECTION: "console-virtual-collection", - CONSOLE_ROM: "console-rom", - CONSOLE_PLAY: "console-play", - // V2-only routes (no v1 equivalent — v1 uses its drawer for these). - PLATFORMS_INDEX: "platforms-index", - COLLECTIONS_INDEX: "collections-index", - CONTROLLER_DEBUG: "controller-debug", - NOT_FOUND: "404", -} as const; +export { isAuthExemptRoute, ROUTES } from "@/plugins/routeNames"; // Resolve the v2 component for a given route name, falling back to the 404 // view so every route renders something when the user is on uiVersion=v2. @@ -618,20 +572,6 @@ const routePermissions: RoutePermissions[] = [ { path: ROUTES.LOGS, requiredScopes: ["logs.read"] }, ]; -const authExemptRoutes = [ - ROUTES.LOGIN, - ROUTES.SETUP, - ROUTES.RESET_PASSWORD, - ROUTES.REGISTER, - ROUTES.PAIR, -] as const; - -type AuthExemptRoute = (typeof authExemptRoutes)[number]; - -export function isAuthExemptRoute(route: string): route is AuthExemptRoute { - return (authExemptRoutes as readonly string[]).includes(route); -} - function checkRoutePermissions(route: string, user: User | null): boolean { // No checks needed for login and setup pages if (isAuthExemptRoute(route)) { diff --git a/frontend/src/services/api/index.ts b/frontend/src/services/api/index.ts index 7152593c3..7481112dd 100644 --- a/frontend/src/services/api/index.ts +++ b/frontend/src/services/api/index.ts @@ -1,8 +1,6 @@ import axios from "axios"; import { default as Cookies } from "js-cookie"; import { debounce } from "lodash"; -import router from "@/plugins/router"; -import { ROUTES, isAuthExemptRoute } from "@/plugins/router"; const api = axios.create({ // This will keep the url query params on refresh @@ -133,6 +131,14 @@ api.interceptors.response.use( const params = new URLSearchParams(search); const fullPath = pathname + search; + // Static router import created api ↔ router TDZ; dynamic load only on 401. + // @dpdm-ignore + const { + default: router, + ROUTES, + isAuthExemptRoute, + } = await import("@/plugins/router"); + // Don't redirect to login if already on an auth-exempt route. // Also resolve the route from the browser URL to handle the case where // the router hasn't been started yet (e.g., during app initialization), diff --git a/frontend/src/services/api/rom.ts b/frontend/src/services/api/rom.ts index 2cdd167b6..f78e71de9 100644 --- a/frontend/src/services/api/rom.ts +++ b/frontend/src/services/api/rom.ts @@ -23,7 +23,7 @@ import { type CustomLimitOffsetPage_SimpleRomSchema_ as GetRomsResponse } from " import api from "@/services/api"; import socket from "@/services/socket"; import storeUpload from "@/stores/upload"; -import { getDownloadPath } from "@/utils"; +import { getDownloadPath } from "@/utils/downloadPath"; import { buildFormInput, type FormInputField } from "@/utils/formData"; export const romApi = api; diff --git a/frontend/src/stores/navigation.ts b/frontend/src/stores/navigation.ts index 295ed7497..68e02bc81 100644 --- a/frontend/src/stores/navigation.ts +++ b/frontend/src/stores/navigation.ts @@ -1,6 +1,6 @@ import { useLocalStorage } from "@vueuse/core"; import { defineStore } from "pinia"; -import { ROUTES } from "@/plugins/router"; +import { ROUTES } from "@/plugins/routeNames"; const mainBarCollapsed = useLocalStorage("ui.mainBarCollapsed", false); diff --git a/frontend/src/utils/downloadPath.ts b/frontend/src/utils/downloadPath.ts new file mode 100644 index 000000000..9abe806f2 --- /dev/null +++ b/frontend/src/utils/downloadPath.ts @@ -0,0 +1,54 @@ +import type { SimpleRomSchema } from "@/__generated__"; + +type DownloadRom = Pick< + SimpleRomSchema, + "id" | "fs_name" | "has_nested_single_file" | "files" +>; + +/** + * Generate a download link for ROM content. + */ +export function getDownloadPath({ + rom, + fileIDs = [], +}: { + rom: DownloadRom; + fileIDs?: number[]; +}) { + const queryParams = new URLSearchParams(); + if (fileIDs.length > 0) { + queryParams.append("file_ids", fileIDs.join(",")); + } + const queryString = queryParams.toString(); + + const selectedFile = + fileIDs.length === 1 + ? rom.files?.find((f) => f.id === fileIDs[0]) + : undefined; + const nestedFile = + fileIDs.length === 0 && + rom.has_nested_single_file && + rom.files?.length === 1 + ? rom.files[0] + : undefined; + const contentFile = selectedFile ?? nestedFile; + const contentName = contentFile + ? encodeURIComponent(contentFile.file_name) + : rom.fs_name; + + return `/api/roms/${rom.id}/content/${contentName}${ + queryString ? `?${queryString}` : "" + }`; +} + +export function getDownloadLink({ + rom, + fileIDs = [], +}: { + rom: DownloadRom; + fileIDs?: number[]; +}) { + return `${window.location.origin}${encodeURI( + getDownloadPath({ rom, fileIDs }), + )}`; +} diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.ts index 60ec7c5ee..b1dcf73c8 100644 --- a/frontend/src/utils/index.ts +++ b/frontend/src/utils/index.ts @@ -107,58 +107,7 @@ export function convertCronExperssion(expression: string) { return convertedExpression; } -/** - * Generate a download link for ROM content. - * - * @param rom The ROM object. - * @param files Optional array of file names to include in the download. - * @returns The download link. - */ -export function getDownloadPath({ - rom, - fileIDs = [], -}: { - rom: SimpleRom; - fileIDs?: number[]; -}) { - const queryParams = new URLSearchParams(); - if (fileIDs.length > 0) { - queryParams.append("file_ids", fileIDs.join(",")); - } - const queryString = queryParams.toString(); - - // If a single file is selected, use its name for the download path - const selectedFile = - fileIDs.length === 1 - ? rom.files?.find((f) => f.id === fileIDs[0]) - : undefined; - const nestedFile = - fileIDs.length === 0 && - rom.has_nested_single_file && - rom.files?.length === 1 - ? rom.files[0] - : undefined; - const contentFile = selectedFile ?? nestedFile; - const contentName = contentFile - ? encodeURIComponent(contentFile.file_name) - : rom.fs_name; - - return `/api/roms/${rom.id}/content/${contentName}${ - queryString ? `?${queryString}` : "" - }`; -} - -export function getDownloadLink({ - rom, - fileIDs = [], -}: { - rom: SimpleRom; - fileIDs?: number[]; -}) { - return `${window.location.origin}${encodeURI( - getDownloadPath({ rom, fileIDs }), - )}`; -} +export { getDownloadLink, getDownloadPath } from "@/utils/downloadPath"; /** * Format bytes as human-readable text.