From 7412011b3fb86150b054e5ffd9be2ca855eb039d Mon Sep 17 00:00:00 2001 From: samtcifihi <43021399+samtcifihi@users.noreply.github.com> Date: Wed, 25 Nov 2026 15:45:17 -0500 Subject: [PATCH 1/2] Fix Input Mode picker initialization --- CHANGELOG.md | 1 + src/components/InputModeOptionsPicker.js | 36 ++++++------ src/components/InputModeOptionsPicker.test.js | 56 +++++++++++++++++++ 3 files changed, 74 insertions(+), 19 deletions(-) create mode 100644 src/components/InputModeOptionsPicker.test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 9848383f..cf4b018e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ Since the `1.0.0-beta` release, the version in `package.json` has stayed at `1.0 ### Fixed +- **Input Mode picker:** opening board options keeps the selected mode stable. - **Dashboard ladders:** “Your ladders” table shows pagination and page-size controls when you belong to more than 10 ladders (full list still comes from `me_dashboard`; only the first page was visible before). ## [1.0.0-ci] - 2026-11-22 diff --git a/src/components/InputModeOptionsPicker.js b/src/components/InputModeOptionsPicker.js index 920a6705..3b804db8 100644 --- a/src/components/InputModeOptionsPicker.js +++ b/src/components/InputModeOptionsPicker.js @@ -94,17 +94,15 @@ function InputModeOptionsPicker({ const [groupVariants, groupVariantsSetter] = useState({}); const [groupData, groupDataSetter] = useState([]); const [allModes, allModesSetter] = useState([]); - const initialUidRef = useRef(initialUid); const lastEmittedRef = useRef(null); - const initDoneRef = useRef(false); + const pendingChangeRef = useRef(false); const { t } = useTranslation(); /** @type {[string | undefined, React.Dispatch>]} */ const [ungroupedChoice, ungroupedChoiceSetter] = useState(undefined); useEffect(() => { - initialUidRef.current = initialUid; - initDoneRef.current = false; + pendingChangeRef.current = false; if (metaGame === undefined || metaGame === null || metaGame === "") { groupDataSetter([]); allModesSetter([]); @@ -140,10 +138,10 @@ function InputModeOptionsPicker({ groupDataSetter(builtGroupData); const defs = info.inputModes ?? []; const incomingInit = - initialUidRef.current !== undefined && - initialUidRef.current !== null && - String(initialUidRef.current) !== "" - ? [String(initialUidRef.current)] + initialUid !== undefined && + initialUid !== null && + String(initialUid) !== "" + ? [String(initialUid)] : []; const sanitized = sanitizeInputModeSelection(defs, incomingInit); if (builtGroupData.length === 0) { @@ -155,7 +153,6 @@ function InputModeOptionsPicker({ ungroupedChoiceSetter(pick); groupVariantsSetter({}); lastEmittedRef.current = pick; - initDoneRef.current = true; } else { const applied = applySanitizedInputModeSelection( sanitized, @@ -167,17 +164,22 @@ function InputModeOptionsPicker({ const first = sanitized.find((u) => !u.startsWith("#")) ?? sanitized[0] ?? undefined; lastEmittedRef.current = first; - initDoneRef.current = true; } }, [metaGame, initialUid]); const handleGroupChange = useCallback((group, variant) => { + pendingChangeRef.current = true; groupVariantsSetter((current) => ({ ...current, [group]: variant, })); }, []); + const handleUngroupedChange = (uid) => { + pendingChangeRef.current = true; + ungroupedChoiceSetter(uid); + }; + const activeUids = useMemo(() => { if (groupData.length === 0 && ungroupedChoice !== undefined) { return [ungroupedChoice]; @@ -193,14 +195,9 @@ function InputModeOptionsPicker({ }, [activeUids, allModes, disableFields]); useEffect(() => { - if (!initDoneRef.current) { - return; - } - if (allModes.length === 0) { - if (lastEmittedRef.current !== undefined) { - lastEmittedRef.current = undefined; - onChange?.(undefined); - } + // Initializing from props can leave this render's selection out of date. + // Only send changes initiated by a click back to the modal. + if (!pendingChangeRef.current || allModes.length === 0) { return; } const defs = gameinfo.get(metaGame)?.inputModes ?? []; @@ -225,6 +222,7 @@ function InputModeOptionsPicker({ } active = sanitized; } + pendingChangeRef.current = false; const concrete = active.find((u) => !u.startsWith("#")); if (concrete === lastEmittedRef.current) { return; @@ -288,7 +286,7 @@ function InputModeOptionsPicker({ value={d.uid} name="inputMode-ungrouped" checked={ungroupedChoice === d.uid} - onChange={() => ungroupedChoiceSetter(d.uid)} + onChange={() => handleUngroupedChange(d.uid)} disabled={isOptionDisabled} /> {d.name ?? d.uid} diff --git a/src/components/InputModeOptionsPicker.test.js b/src/components/InputModeOptionsPicker.test.js new file mode 100644 index 00000000..66c711af --- /dev/null +++ b/src/components/InputModeOptionsPicker.test.js @@ -0,0 +1,56 @@ +import { fireEvent, render, screen } from "@testing-library/react"; +import { describe, expect, it, vi } from "vitest"; +import InputModeOptionsPicker from "./InputModeOptionsPicker"; + +vi.mock("@abstractplay/gameslib", async (importOriginal) => { + const flat = [ + { uid: "basic", name: "Basic" }, + { uid: "advanced", name: "Advanced" }, + ]; + const grouped = flat.map((mode) => ({ ...mode, group: "entry" })); + const gameinfo = new Map( + [ + ["flat", flat], + ["grouped", grouped], + ].map(([uid, inputModes]) => [uid, { uid, inputModes, playercounts: [2] }]), + ); + return { + ...(await importOriginal()), + gameinfo, + GameFactory: (uid) => ({ + inputModes: () => gameinfo.get(uid).inputModes, + }), + }; +}); + +vi.mock("react-i18next", async (importOriginal) => ({ + ...(await importOriginal()), + useTranslation: () => ({ t: (key) => key }), +})); + +describe("InputModeOptionsPicker", () => { + it.each([ + ["flat", 2], + ["grouped", 3], + ])("keeps the initial %s selection local until the user changes it", (metaGame, count) => { + const onChange = vi.fn(); + const { rerender } = render( + , + ); + + expect(screen.getAllByRole("radio")).toHaveLength(count); + expect(screen.getByRole("radio", { name: "Basic" })).toBeChecked(); + expect(onChange).not.toHaveBeenCalled(); + + const advanced = screen.getByRole("radio", { name: "Advanced" }); + fireEvent.click(advanced); + expect(advanced).toBeChecked(); + expect(onChange).toHaveBeenCalledExactlyOnceWith("advanced"); + + rerender( + , + ); + expect(advanced).toBeChecked(); + expect(onChange).toHaveBeenCalledTimes(1); + }); +}); From 7a62958e24ecb42fe6d0ea69c2fe643f42beefc7 Mon Sep 17 00:00:00 2001 From: samtcifihi <43021399+samtcifihi@users.noreply.github.com> Date: Wed, 25 Nov 2026 16:04:31 -0500 Subject: [PATCH 2/2] Narrow the Input Mode initialization fix --- src/components/InputModeOptionsPicker.js | 38 ++++++++++--------- src/components/InputModeOptionsPicker.test.js | 6 +++ 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/components/InputModeOptionsPicker.js b/src/components/InputModeOptionsPicker.js index 3b804db8..064fb999 100644 --- a/src/components/InputModeOptionsPicker.js +++ b/src/components/InputModeOptionsPicker.js @@ -94,15 +94,17 @@ function InputModeOptionsPicker({ const [groupVariants, groupVariantsSetter] = useState({}); const [groupData, groupDataSetter] = useState([]); const [allModes, allModesSetter] = useState([]); + const initialUidRef = useRef(initialUid); const lastEmittedRef = useRef(null); - const pendingChangeRef = useRef(false); + const initializedGroupDataRef = useRef(null); const { t } = useTranslation(); /** @type {[string | undefined, React.Dispatch>]} */ const [ungroupedChoice, ungroupedChoiceSetter] = useState(undefined); useEffect(() => { - pendingChangeRef.current = false; + initialUidRef.current = initialUid; + initializedGroupDataRef.current = null; if (metaGame === undefined || metaGame === null || metaGame === "") { groupDataSetter([]); allModesSetter([]); @@ -138,10 +140,10 @@ function InputModeOptionsPicker({ groupDataSetter(builtGroupData); const defs = info.inputModes ?? []; const incomingInit = - initialUid !== undefined && - initialUid !== null && - String(initialUid) !== "" - ? [String(initialUid)] + initialUidRef.current !== undefined && + initialUidRef.current !== null && + String(initialUidRef.current) !== "" + ? [String(initialUidRef.current)] : []; const sanitized = sanitizeInputModeSelection(defs, incomingInit); if (builtGroupData.length === 0) { @@ -165,21 +167,16 @@ function InputModeOptionsPicker({ sanitized.find((u) => !u.startsWith("#")) ?? sanitized[0] ?? undefined; lastEmittedRef.current = first; } + initializedGroupDataRef.current = builtGroupData; }, [metaGame, initialUid]); const handleGroupChange = useCallback((group, variant) => { - pendingChangeRef.current = true; groupVariantsSetter((current) => ({ ...current, [group]: variant, })); }, []); - const handleUngroupedChange = (uid) => { - pendingChangeRef.current = true; - ungroupedChoiceSetter(uid); - }; - const activeUids = useMemo(() => { if (groupData.length === 0 && ungroupedChoice !== undefined) { return [ungroupedChoice]; @@ -195,9 +192,15 @@ function InputModeOptionsPicker({ }, [activeUids, allModes, disableFields]); useEffect(() => { - // Initializing from props can leave this render's selection out of date. - // Only send changes initiated by a click back to the modal. - if (!pendingChangeRef.current || allModes.length === 0) { + // This effect can still see the render before initialization queued its state. + if (initializedGroupDataRef.current !== groupData) { + return; + } + if (allModes.length === 0) { + if (lastEmittedRef.current !== undefined) { + lastEmittedRef.current = undefined; + onChange?.(undefined); + } return; } const defs = gameinfo.get(metaGame)?.inputModes ?? []; @@ -222,7 +225,6 @@ function InputModeOptionsPicker({ } active = sanitized; } - pendingChangeRef.current = false; const concrete = active.find((u) => !u.startsWith("#")); if (concrete === lastEmittedRef.current) { return; @@ -234,7 +236,7 @@ function InputModeOptionsPicker({ allModes, disableFields, groupVariants, - groupData.length, + groupData, metaGame, onChange, ]); @@ -286,7 +288,7 @@ function InputModeOptionsPicker({ value={d.uid} name="inputMode-ungrouped" checked={ungroupedChoice === d.uid} - onChange={() => handleUngroupedChange(d.uid)} + onChange={() => ungroupedChoiceSetter(d.uid)} disabled={isOptionDisabled} /> {d.name ?? d.uid} diff --git a/src/components/InputModeOptionsPicker.test.js b/src/components/InputModeOptionsPicker.test.js index 66c711af..33d4d35b 100644 --- a/src/components/InputModeOptionsPicker.test.js +++ b/src/components/InputModeOptionsPicker.test.js @@ -52,5 +52,11 @@ describe("InputModeOptionsPicker", () => { ); expect(advanced).toBeChecked(); expect(onChange).toHaveBeenCalledTimes(1); + + rerender( + , + ); + expect(screen.getByRole("radio", { name: "Basic" })).toBeChecked(); + expect(onChange).toHaveBeenCalledTimes(1); }); });