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..064fb999 100644 --- a/src/components/InputModeOptionsPicker.js +++ b/src/components/InputModeOptionsPicker.js @@ -96,7 +96,7 @@ function InputModeOptionsPicker({ const [allModes, allModesSetter] = useState([]); const initialUidRef = useRef(initialUid); const lastEmittedRef = useRef(null); - const initDoneRef = useRef(false); + const initializedGroupDataRef = useRef(null); const { t } = useTranslation(); /** @type {[string | undefined, React.Dispatch>]} */ @@ -104,7 +104,7 @@ function InputModeOptionsPicker({ useEffect(() => { initialUidRef.current = initialUid; - initDoneRef.current = false; + initializedGroupDataRef.current = null; if (metaGame === undefined || metaGame === null || metaGame === "") { groupDataSetter([]); allModesSetter([]); @@ -155,7 +155,6 @@ function InputModeOptionsPicker({ ungroupedChoiceSetter(pick); groupVariantsSetter({}); lastEmittedRef.current = pick; - initDoneRef.current = true; } else { const applied = applySanitizedInputModeSelection( sanitized, @@ -167,8 +166,8 @@ function InputModeOptionsPicker({ const first = sanitized.find((u) => !u.startsWith("#")) ?? sanitized[0] ?? undefined; lastEmittedRef.current = first; - initDoneRef.current = true; } + initializedGroupDataRef.current = builtGroupData; }, [metaGame, initialUid]); const handleGroupChange = useCallback((group, variant) => { @@ -193,7 +192,8 @@ function InputModeOptionsPicker({ }, [activeUids, allModes, disableFields]); useEffect(() => { - if (!initDoneRef.current) { + // This effect can still see the render before initialization queued its state. + if (initializedGroupDataRef.current !== groupData) { return; } if (allModes.length === 0) { @@ -236,7 +236,7 @@ function InputModeOptionsPicker({ allModes, disableFields, groupVariants, - groupData.length, + groupData, metaGame, onChange, ]); diff --git a/src/components/InputModeOptionsPicker.test.js b/src/components/InputModeOptionsPicker.test.js new file mode 100644 index 00000000..33d4d35b --- /dev/null +++ b/src/components/InputModeOptionsPicker.test.js @@ -0,0 +1,62 @@ +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); + + rerender( + , + ); + expect(screen.getByRole("radio", { name: "Basic" })).toBeChecked(); + expect(onChange).toHaveBeenCalledTimes(1); + }); +});