From 95dce4d131154573a88e17b4cc9aed7ae5b647f3 Mon Sep 17 00:00:00 2001 From: Rn86222 Date: Fri, 20 Nov 2026 21:01:07 +0900 Subject: [PATCH 1/2] fix(tutorial): name the stage components in the language shown The stage and the parts it hands over kept the name of the language the stage was first opened in, so the side panel and the title bar could disagree with the stage text after switching languages. They are now renamed on opening and on each switch, without saving, so that a stage only opened still leaves nothing behind. The title bar now follows a renamed component. Co-Authored-By: Claude Opus 5.5 --- src/pages/edit/Editor/components/TitleBar.tsx | 7 +-- src/pages/tutorial/stage.tsx | 20 ++++++-- src/store/autoSaver.test.ts | 40 ++++++++++++++++ src/store/autoSaver.ts | 19 +++++++- src/tutorial/stageStore.test.ts | 46 +++++++++++++++++++ src/tutorial/stageStore.ts | 16 +++++++ src/tutorial/stages.ts | 15 ++++++ 7 files changed, 153 insertions(+), 10 deletions(-) diff --git a/src/pages/edit/Editor/components/TitleBar.tsx b/src/pages/edit/Editor/components/TitleBar.tsx index 0e978e4..cce79b8 100644 --- a/src/pages/edit/Editor/components/TitleBar.tsx +++ b/src/pages/edit/Editor/components/TitleBar.tsx @@ -2,7 +2,7 @@ import { Close, Edit, KeyboardDoubleArrowRight } from "@mui/icons-material"; import { Box, IconButton, Paper } from "@mui/material"; import nullthrows from "nullthrows"; import { useMessages } from "../../../../i18n"; -import { useStore } from "../../../../store/react"; +import { useComponent } from "../../../../store/react/selectors"; import { useComponentEditorStore } from "../store"; export type CCComponentEditorTitleBarProps = { @@ -16,11 +16,8 @@ export default function CCComponentEditorTitleBar({ }: CCComponentEditorTitleBarProps) { const componentEditorStore = useComponentEditorStore(); const componentEditorState = componentEditorStore(); - const { store } = useStore(); const messages = useMessages(); - const component = nullthrows( - store.components.get(componentEditorState.componentId), - ); + const component = nullthrows(useComponent(componentEditorState.componentId)); return ( - isTutorialStageId(id) ? texts.stageTexts[id].title : texts.partTitles[id]; + const titleOf = useCallback( + (id) => + isTutorialStageId(id) ? texts.stageTexts[id].title : texts.partTitles[id], + [texts], + ); const goalTable = goalTableOf(stage.check); const stageTexts = texts.stageTexts[stage.id]; @@ -89,6 +99,10 @@ function StageView({ stage }: { stage: TutorialStage }) { // The editor keeps state of the store it was made with, so it starts anew on a reset const [resetCount, setResetCount] = useState(0); + useEffect(() => { + nameTutorialStage(store, stage, titleOf); + }, [store, stage, titleOf]); + // A result about a circuit that has since changed would mislead useEffect(() => { const clearResult = () => setResult(null); diff --git a/src/store/autoSaver.test.ts b/src/store/autoSaver.test.ts index d1e42a5..3e1b044 100644 --- a/src/store/autoSaver.test.ts +++ b/src/store/autoSaver.test.ts @@ -38,4 +38,44 @@ describe("CCStoreAutoSaver", () => { expect(saved.size).toBe(0); }); + + it("does not save a change made without saving", () => { + const store = createTestStore(); + store.autoSaver.watch(); + + store.autoSaver.changeWithoutSaving(() => + registerTestComponent(store, "Adder"), + ); + vi.advanceTimersByTime(2000); + + expect(saved.size).toBe(0); + }); + + it("keeps a save still waiting through a change made without saving", () => { + const store = createTestStore(); + store.autoSaver.watch(); + + registerTestComponent(store, "Adder"); + store.autoSaver.changeWithoutSaving(() => + registerTestComponent(store, "Multiplier"), + ); + vi.advanceTimersByTime(2000); + + expect(saved.get(CCStoreAutoSaver.localStorageKey)).toBe(store.toJSON()); + }); + + it("saves changes again after a change made without saving fails", () => { + const store = createTestStore(); + store.autoSaver.watch(); + + expect(() => + store.autoSaver.changeWithoutSaving(() => { + throw new Error("Failed"); + }), + ).toThrow("Failed"); + registerTestComponent(store, "Adder"); + vi.advanceTimersByTime(2000); + + expect(saved.get(CCStoreAutoSaver.localStorageKey)).toBe(store.toJSON()); + }); }); diff --git a/src/store/autoSaver.ts b/src/store/autoSaver.ts index 001caf9..5602e81 100644 --- a/src/store/autoSaver.ts +++ b/src/store/autoSaver.ts @@ -12,6 +12,8 @@ export class CCStoreAutoSaver { #unwatch: (() => void) | null = null; + #ignoresChanges = false; + static localStorageKey = "ccStoreAutoSaverState"; readonly storageKey: string; @@ -23,7 +25,10 @@ export class CCStoreAutoSaver { watch() { this.unwatch(); - const save = debounce(this.save.bind(this), 1000); + const debouncedSave = debounce(this.save.bind(this), 1000); + const save = () => { + if (!this.#ignoresChanges) debouncedSave(); + }; const store = this.#store; const unlistens = [ listenToEvents(store.components, ccComponentStoreChangeEventTypes, save), @@ -41,7 +46,7 @@ export class CCStoreAutoSaver { ), ]; this.#unwatch = () => { - save.clear(); + debouncedSave.clear(); for (const unlisten of unlistens) unlisten(); }; } @@ -52,6 +57,16 @@ export class CCStoreAutoSaver { this.#unwatch = null; } + /** For changes made again on every load, which alone are not worth saving */ + changeWithoutSaving(change: () => void): void { + this.#ignoresChanges = true; + try { + change(); + } finally { + this.#ignoresChanges = false; + } + } + save(): void { window.localStorage.setItem(this.storageKey, this.#store.toJSON()); } diff --git a/src/tutorial/stageStore.test.ts b/src/tutorial/stageStore.test.ts index 151ce7f..d8cee5a 100644 --- a/src/tutorial/stageStore.test.ts +++ b/src/tutorial/stageStore.test.ts @@ -1,5 +1,6 @@ import nullthrows from "nullthrows"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import type CCStore from "../store"; import { CCStoreAutoSaver } from "../store/autoSaver"; import { createTestStore, @@ -9,6 +10,7 @@ import { import { markStageCleared, readClearedStages } from "./progress"; import { getTutorialStageStore, + nameTutorialStage, resetTutorial, resetTutorialStage, } from "./stageStore"; @@ -25,6 +27,11 @@ const xorStage = nullthrows(findTutorialStage("xor")); const aOnlyStage = nullthrows(findTutorialStage("a-only")); const firstKey = `ccTutorialStage:${firstStage.id}`; const titleOf = (id: GivenPartId) => `Stage ${id}`; +const otherTitleOf = (id: GivenPartId) => `課題 ${id}`; +const stageNamesIn = (store: CCStore) => + [xorStage.componentId, aOnlyStage.componentId].map( + (id) => store.components.get(id)?.name, + ); let saved: Map; beforeEach(() => { @@ -110,6 +117,22 @@ describe("getTutorialStageStore", () => { expect(store.components.get(mine.id)).toBeDefined(); }); + it("names a stage saved in another language in the one now shown, as saved", async () => { + const earlier = getTutorialStageStore(xorStage, titleOf); + registerTestComponent(earlier, "Mine"); + vi.advanceTimersByTime(2000); + const json = earlier.toJSON(); + earlier.autoSaver.unwatch(); + vi.resetModules(); + + const { getTutorialStageStore: fresh } = await import("./stageStore"); + const store = fresh(xorStage, otherTitleOf); + vi.advanceTimersByTime(2000); + + expect(stageNamesIn(store)).toEqual(["課題 xor", "課題 a-only"]); + expect(saved.get(`ccTutorialStage:${xorStage.id}`)).toBe(json); + }); + it("starts a stage over when what was saved is broken", () => { saved.set(firstKey, "broken"); vi.spyOn(console, "warn").mockImplementation(() => {}); @@ -120,6 +143,29 @@ describe("getTutorialStageStore", () => { }); }); +describe("nameTutorialStage", () => { + it("renames the stage and what it hands over, saving nothing for a stage only opened", () => { + const store = getTutorialStageStore(xorStage, titleOf); + + nameTutorialStage(store, xorStage, otherTitleOf); + vi.advanceTimersByTime(2000); + + expect(stageNamesIn(store)).toEqual(["課題 xor", "課題 a-only"]); + expect(saved.size).toBe(0); + }); + + it("leaves the player's own components as they are, and their change saved", () => { + const store = getTutorialStageStore(xorStage, titleOf); + const mine = registerTestComponent(store, "Mine"); + + nameTutorialStage(store, xorStage, otherTitleOf); + vi.advanceTimersByTime(2000); + + expect(store.components.get(mine.id)?.name).toBe("Mine"); + expect(saved.get(`ccTutorialStage:${xorStage.id}`)).toBe(store.toJSON()); + }); +}); + describe("resetTutorialStage", () => { it("lays the stage out anew and stops the old store from saving", () => { const old = getTutorialStageStore(firstStage, titleOf); diff --git a/src/tutorial/stageStore.ts b/src/tutorial/stageStore.ts index f7d0dd7..97c8b9b 100644 --- a/src/tutorial/stageStore.ts +++ b/src/tutorial/stageStore.ts @@ -3,6 +3,7 @@ import { CCStoreAutoSaver } from "../store/autoSaver"; import { importStoreJson } from "../store/savedStore"; import { forgetClearedStages } from "./progress"; import { + nameStageComponents, registerGivenParts, type StageTitleOf, setUpTutorialStage, @@ -62,9 +63,24 @@ export function getTutorialStageStore( restoreStageStore(stage, titleOf) ?? createStageStore(stage, titleOf); storeByStageId.set(stage.id, store); } + nameTutorialStage(store, stage, titleOf); return store; } +/** + * Names the components the player cannot rename in the language now shown, which may + * differ from the one they were saved in. A stage only opened still leaves nothing behind. + */ +export function nameTutorialStage( + store: CCStore, + stage: TutorialStage, + titleOf: StageTitleOf, +): void { + store.autoSaver.changeWithoutSaving(() => + nameStageComponents(store, stage, titleOf), + ); +} + // Removed from the storage first, so that the store keeps saving if that fails function forgetStage(stage: TutorialStage) { window.localStorage.removeItem(storageKeyOf(stage)); diff --git a/src/tutorial/stages.ts b/src/tutorial/stages.ts index 2c7710e..15c6a0f 100644 --- a/src/tutorial/stages.ts +++ b/src/tutorial/stages.ts @@ -903,6 +903,21 @@ function registerStageComponent( ); } +/** Names the components of `stage` and of what it hands over by `titleOf` */ +export function nameStageComponents( + store: CCStore, + stage: GivenPart, + titleOf: StageTitleOf, +): void { + for (const built of stagesBuiltIn(stage)) { + const name = titleOf(built.id); + const component = store.components.get(built.componentId); + if (component && component.name !== name) { + store.components.update(component.id, { name }); + } + } +} + /** Skips the parts `store` already has, as a save from before may lack only some */ export function registerGivenParts( store: CCStore, From 9ff13376aa347712f9814efa43a9c46e2ab508b7 Mon Sep 17 00:00:00 2001 From: Rn86222 Date: Fri, 20 Nov 2026 21:06:32 +0900 Subject: [PATCH 2/2] fix(tutorial): rename a part in the check failure on a language switch The failure message reads part names from the store, but was rendered outside the store's scope and did not follow a rename, so it kept the old language's name after a switch. Co-Authored-By: Claude Opus 5.5 --- src/pages/tutorial/stage.tsx | 286 ++++++++++++++++++----------------- 1 file changed, 150 insertions(+), 136 deletions(-) diff --git a/src/pages/tutorial/stage.tsx b/src/pages/tutorial/stage.tsx index 3b8b930..4f1648a 100644 --- a/src/pages/tutorial/stage.tsx +++ b/src/pages/tutorial/stage.tsx @@ -31,7 +31,8 @@ import { import { TruthTableView } from "../../components/TruthTableView"; import { useMessages } from "../../i18n"; import { definitionByComponentId } from "../../store/intrinsics/definitions"; -import { StoreScope } from "../../store/react"; +import { StoreScope, useStore } from "../../store/react"; +import { useComponents } from "../../store/react/selectors"; import { theme } from "../../style"; import { type CheckResult, @@ -74,6 +75,20 @@ function SectionHeading({ children }: { children: ReactNode }) { ); } +function CheckFailure({ + result, +}: { + result: Exclude; +}) { + const { store } = useStore(); + const messages = useMessages(); + // The message may name a part, which is renamed when the language changes + useComponents(); + return ( + {describeFailure(result, store, messages)} + ); +} + function StageView({ stage }: { stage: TutorialStage }) { const messages = useMessages(); const texts = messages.tutorial; @@ -168,135 +183,134 @@ function StageView({ stage }: { stage: TutorialStage }) { return ( -
- - - - - {texts.stages} - -
- - {texts.stageNumber(stageIndex + 1)} - - +
+ + + - {stageTexts.title} - -
-
- {texts.goalHeading} - {stageTexts.goal} - {goalTable && ( - - - + + {texts.stages} + +
+ + {texts.stageNumber(stageIndex + 1)} + + + {stageTexts.title} + +
+
+ {texts.goalHeading} + {stageTexts.goal} + {goalTable && ( + + + + + {stage.tableNotation === "digits" && ( + + {texts.digitsLegend} + + )} + + )} +
+ {stageTexts.steps.length > 0 && ( +
+ {texts.stepsHeading} + + {stageTexts.steps.map((step) => ( + + {step} + + ))} - {stage.tableNotation === "digits" && ( - - {texts.digitsLegend} - - )} - +
)} -
- {stageTexts.steps.length > 0 && (
- {texts.stepsHeading} - - {stageTexts.steps.map((step) => ( - - {step} - + {messages.parts.ofStage} + + {stage.parts.map((partId) => ( + ))} - + {stage.givenParts.map(givenPartById).map((given) => ( + + ))} +
- )} -
- {messages.parts.ofStage} - - {stage.parts.map((partId) => ( - - ))} - {stage.givenParts.map(givenPartById).map((given) => ( - - ))} - -
- - {hasResetFailed && ( - {texts.resetStageFailed} - )} - - - - {/* Outside the scrolling, so that Check and its result stay in view */} - - - {result?.type === "passed" && ( - <> - - {stage.check.kind === "sequential" - ? texts.passedSteps(stage.check.steps.length) - : texts.passed} - - - - )} - {result && result.type !== "passed" && ( - - {describeFailure(result, store, messages)} - - )} - - - - + + {hasResetFailed && ( + {texts.resetStageFailed} + )} + + + + {/* Outside the scrolling, so that Check and its result stay in view */} + + + {result?.type === "passed" && ( + <> + + {stage.check.kind === "sequential" + ? texts.passedSteps(stage.check.steps.length) + : texts.passed} + + + + )} + {result && result.type !== "passed" && ( + + )} + + + - - {isConfirmingReset && ( - setIsConfirmingReset(false)} - > - {texts.resetStageDialog.body} - - )} -
+ {isConfirmingReset && ( + setIsConfirmingReset(false)} + > + {texts.resetStageDialog.body} + + )} +
+
); }