From b2b7904b5134bd10e463eef566c8ccc15917e7b1 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Thu, 1 Oct 2026 15:10:39 -0400 Subject: [PATCH] refactor: simplify pass (pr-ready step 3) - Validate each exclude list where it is read, so a new path cannot skip validation and the rule has one home. - Move useEscapable to composables/ and use it in RMenu and MatchRomBodyGrid, which hand-rolled the same stack wiring. - Drop the Escape branches in RSelect and RDateField that the escape stack's capture listener now preempts. - Debounce the gallery search with lodash like the rest of v2. - Drop a cached-client assertion that held by construction. Co-Authored-By: Claude Opus 5.5 (1M context) --- backend/config/config_manager.py | 39 +++++++------------ backend/tests/config/test_config_loader.py | 11 +++++- frontend/src/services/cache/api.test.ts | 4 +- .../components/MatchRom/MatchRomBodyGrid.vue | 21 +++------- .../composables/useDebouncedSearch/index.ts | 21 ++++------ .../useEscapable/index.ts} | 2 +- .../forms/RComboboxField/RComboboxField.vue | 2 +- .../v2/lib/forms/RDateField/RDateField.vue | 14 ++----- frontend/src/v2/lib/forms/RSelect/RSelect.vue | 13 +------ frontend/src/v2/lib/menus/RMenu/RMenu.vue | 30 ++------------ 10 files changed, 48 insertions(+), 109 deletions(-) rename frontend/src/v2/{lib/overlays/RDialog/useEscapable.ts => composables/useEscapable/index.ts} (93%) diff --git a/backend/config/config_manager.py b/backend/config/config_manager.py index 7b6d847bb..a00d5d207 100644 --- a/backend/config/config_manager.py +++ b/backend/config/config_manager.py @@ -303,14 +303,6 @@ PLATFORM_MEDIA_DIRS: Final = { DEFAULT_EXCLUDED_MULTI_FILE_DIRS: Final = sorted( {*DEFAULT_EXCLUDED_PLATFORM_DIRS, *PLATFORM_MEDIA_DIRS.values()} ) -EXCLUDE_LIST_PATHS: Final = ( - "exclude.platforms", - "exclude.roms.single_file.extensions", - "exclude.roms.single_file.names", - "exclude.roms.multi_file.names", - "exclude.roms.multi_file.parts.extensions", - "exclude.roms.multi_file.parts.names", -) class ExclusionType(enum.StrEnum): @@ -688,11 +680,21 @@ class ConfigManager: ) def _raw_exclude_list(self, path: str) -> list[str]: - """Read a user exclude list; `_validate_config` rejects non-strings.""" + """Read a user exclude list, exiting on anything but a list of strings.""" value = pydash.get(self._raw_config, path) - if not isinstance(value, list): + if value is None: return [] - return [item for item in value if isinstance(item, str)] + if not isinstance(value, list): + log.critical(f"Invalid config.yml: {path} must be a list") + sys.exit(3) + # YAML reads an unquoted 001 or 1942 as a number. + if not all(isinstance(item, str) for item in value): + log.critical( + f"Invalid config.yml: {path} must only hold strings, " + "quote numeric entries such as '001'" + ) + sys.exit(3) + return value def _parse_config(self) -> None: """Parses each entry in the config.yml""" @@ -1003,21 +1005,6 @@ class ConfigManager: """Validates the config.yml file""" self._check_retired_filesystem_keys() - for path in EXCLUDE_LIST_PATHS: - value = pydash.get(self._raw_config, path) - if value is None: - continue - if not isinstance(value, list): - log.critical(f"Invalid config.yml: {path} must be a list") - sys.exit(3) - # YAML reads an unquoted 001 or 1942 as a number. - if not all(isinstance(item, str) for item in value): - log.critical( - f"Invalid config.yml: {path} must only hold strings, " - "quote numeric entries such as '001'" - ) - sys.exit(3) - if not isinstance(self.config.GAMELIST_AUTO_EXPORT_ON_SCAN, bool): log.critical("Invalid config.yml: scan.gamelist.export must be a boolean") sys.exit(3) diff --git a/backend/tests/config/test_config_loader.py b/backend/tests/config/test_config_loader.py index 096cc7f37..6338b5fac 100644 --- a/backend/tests/config/test_config_loader.py +++ b/backend/tests/config/test_config_loader.py @@ -10,7 +10,6 @@ from config.config_manager import ( DEFAULT_EXCLUDED_FILES, DEFAULT_EXCLUDED_MULTI_FILE_DIRS, DEFAULT_EXCLUDED_PLATFORM_DIRS, - EXCLUDE_LIST_PATHS, ConfigManager, parse_firmware_template, parse_platform_templates, @@ -705,6 +704,16 @@ def critical(mocker): return lambda: str(spy.call_args[0][0]) +EXCLUDE_LIST_PATHS = ( + "exclude.platforms", + "exclude.roms.single_file.extensions", + "exclude.roms.single_file.names", + "exclude.roms.multi_file.names", + "exclude.roms.multi_file.parts.extensions", + "exclude.roms.multi_file.parts.names", +) + + def _write_exclude_config(tmp_path: Path, path: str, value: str) -> ConfigManager: """Write `value` at the dotted `path`, nesting one YAML level per segment.""" lines = [f"{' ' * depth}{key}:" for depth, key in enumerate(path.split("."))] diff --git a/frontend/src/services/cache/api.test.ts b/frontend/src/services/cache/api.test.ts index c7eae5f3c..33fe81424 100644 --- a/frontend/src/services/cache/api.test.ts +++ b/frontend/src/services/cache/api.test.ts @@ -1,7 +1,6 @@ import type { AxiosRequestConfig, AxiosResponse } from "axios"; import { beforeEach, describe, expect, it, vi } from "vitest"; import { - buildGetRomsQuery, RECENT_PLAYED_ROMS_LIMIT, RECENT_ROMS_LIMIT, } from "@/services/api/rom"; @@ -83,7 +82,7 @@ describe("home row cache invalidation", () => { }); describe("gallery request", () => { - it("sends the same query as the uncached client", async () => { + it("sends every gallery filter the uncached client maps", async () => { const params = { platformIds: [1], selectedPublishers: ["Nintendo"], @@ -98,7 +97,6 @@ describe("gallery request", () => { await cachedApiService.getRoms(params, noop); - expect(requestParams()).toEqual(buildGetRomsQuery(params)); expect(requestParams()).toMatchObject({ publishers: ["Nintendo"], has_soundtrack: true, diff --git a/frontend/src/v2/components/MatchRom/MatchRomBodyGrid.vue b/frontend/src/v2/components/MatchRom/MatchRomBodyGrid.vue index b996b01ab..c87ee0158 100644 --- a/frontend/src/v2/components/MatchRom/MatchRomBodyGrid.vue +++ b/frontend/src/v2/components/MatchRom/MatchRomBodyGrid.vue @@ -6,7 +6,7 @@ // over the body with cover-source picker + rename + confirm. Click // the backdrop or press Esc to close. import { RBtn, REmptyState, RIcon, RProgressCircular } from "@v2/lib"; -import { computed, onBeforeUnmount, ref, watch } from "vue"; +import { computed, ref, watch } from "vue"; import { useI18n } from "vue-i18n"; import type { SearchRom, SimpleRom } from "@/stores/roms"; import GameCard from "@/v2/components/GameCard/GameCard.vue"; @@ -18,11 +18,7 @@ import { matchKey, type MatchedSource, } from "@/v2/components/MatchRom/types"; -import { - type EscapableEntry, - popEscapable, - pushEscapable, -} from "@/v2/lib/overlays/RDialog/escapeStack"; +import { useEscapable } from "@/v2/composables/useEscapable"; defineOptions({ inheritAttrs: false }); @@ -88,15 +84,10 @@ function confirm() { // RDialog open (the user can press Esc again to close that). Without // the stack, the dialog's own global Esc listener would fire first // and close everything at once. -const overlayEscEntry: EscapableEntry = { - close: () => close(), - persistent: false, -}; -watch(activeKey, (value) => { - if (value !== null) pushEscapable(overlayEscEntry); - else popEscapable(overlayEscEntry); -}); -onBeforeUnmount(() => popEscapable(overlayEscEntry)); +useEscapable( + computed(() => activeKey.value !== null), + () => close(), +); watch( () => props.results, diff --git a/frontend/src/v2/composables/useDebouncedSearch/index.ts b/frontend/src/v2/composables/useDebouncedSearch/index.ts index 0e6d6496d..bd66f3350 100644 --- a/frontend/src/v2/composables/useDebouncedSearch/index.ts +++ b/frontend/src/v2/composables/useDebouncedSearch/index.ts @@ -1,34 +1,29 @@ +import { debounce } from "lodash"; import { onScopeDispose, type Ref, ref, watch } from "vue"; /** A search box bound to a store term, debounced one way and immediate the * other, so a term set by navigation shows up in the box. */ export function useDebouncedSearch(term: Ref, delayMs = 300) { const input = ref(term.value ?? ""); - let pending: ReturnType | null = null; - function cancel() { - if (pending) clearTimeout(pending); - pending = null; - } + const commit = debounce((value: string) => { + const normalized = value.trim(); + if (normalized !== (term.value ?? "")) term.value = normalized || null; + }, delayMs); function setSearch(value: string) { input.value = value; - cancel(); - pending = setTimeout(() => { - pending = null; - const normalized = value.trim(); - if (normalized !== (term.value ?? "")) term.value = normalized || null; - }, delayMs); + commit(value); } watch(term, (value) => { if (input.value.trim() === (value ?? "")) return; // A keystroke still waiting would overwrite the term just navigated to. - cancel(); + commit.cancel(); input.value = value ?? ""; }); - onScopeDispose(cancel); + onScopeDispose(commit.cancel); return { input, setSearch }; } diff --git a/frontend/src/v2/lib/overlays/RDialog/useEscapable.ts b/frontend/src/v2/composables/useEscapable/index.ts similarity index 93% rename from frontend/src/v2/lib/overlays/RDialog/useEscapable.ts rename to frontend/src/v2/composables/useEscapable/index.ts index e094e3215..0905d3738 100644 --- a/frontend/src/v2/lib/overlays/RDialog/useEscapable.ts +++ b/frontend/src/v2/composables/useEscapable/index.ts @@ -3,7 +3,7 @@ import { type EscapableEntry, popEscapable, pushEscapable, -} from "./escapeStack"; +} from "@/v2/lib/overlays/RDialog/escapeStack"; /** Keep a popover on the escape stack while `isOpen`, so Escape and pad B * close it before any overlay it sits in. Returns the stack entry. */ diff --git a/frontend/src/v2/lib/forms/RComboboxField/RComboboxField.vue b/frontend/src/v2/lib/forms/RComboboxField/RComboboxField.vue index c20979b21..65527748a 100644 --- a/frontend/src/v2/lib/forms/RComboboxField/RComboboxField.vue +++ b/frontend/src/v2/lib/forms/RComboboxField/RComboboxField.vue @@ -37,8 +37,8 @@ import { useSlots, watch, } from "vue"; +import { useEscapable } from "@/v2/composables/useEscapable"; import { useChromeLabels } from "@/v2/lib/a11y/chromeLabels"; -import { useEscapable } from "@/v2/lib/overlays/RDialog/useEscapable"; import RIcon from "@/v2/lib/primitives/RIcon/RIcon.vue"; import RTag from "@/v2/lib/primitives/RTag/RTag.vue"; import { useRFormRegistration } from "../RForm/context"; diff --git a/frontend/src/v2/lib/forms/RDateField/RDateField.vue b/frontend/src/v2/lib/forms/RDateField/RDateField.vue index e7213cd86..f9afb46ae 100644 --- a/frontend/src/v2/lib/forms/RDateField/RDateField.vue +++ b/frontend/src/v2/lib/forms/RDateField/RDateField.vue @@ -32,8 +32,8 @@ import { useId, watch, } from "vue"; +import { useEscapable } from "@/v2/composables/useEscapable"; import { useChromeLabels } from "@/v2/lib/a11y/chromeLabels"; -import { useEscapable } from "@/v2/lib/overlays/RDialog/useEscapable"; import RBtn from "../../primitives/RBtn/RBtn.vue"; import RIcon from "../../primitives/RIcon/RIcon.vue"; import RTextField from "../RTextField/RTextField.vue"; @@ -364,11 +364,6 @@ function onPanelKeydown(evt: KeyboardEvent) { evt.preventDefault(); selectDay(new Date(focusedDay.value)); break; - case "Escape": - evt.preventDefault(); - evt.stopPropagation(); - dismiss(); - break; default: break; } @@ -414,9 +409,8 @@ function focusDayCell() { // ── Field-level keyboard wiring ──────────────────────────────── // On the closed field: Space / Enter / ArrowDown opens the popup. Tab -// behaves natively (moves to next focusable). When open, Escape closes -// (handled inside the panel: the field doesn't see keydown when focus -// has moved into the calendar). +// behaves natively (moves to next focusable). When open, the escape stack +// owns Escape. function onFieldKeydown(evt: KeyboardEvent) { if (props.disabled) return; if (isOpen.value) return; @@ -456,7 +450,7 @@ useEscapable(isOpen, dismiss, () => panelRef.value);