From 9a75102121643f1a91852a598dd1362dc00397fc Mon Sep 17 00:00:00 2001 From: Jon Eugster Date: Tue, 18 Jul 2023 16:12:24 +0200 Subject: [PATCH] save help in store --- client/src/components/level.tsx | 24 ++++++++++++++++++++---- client/src/state/progress.ts | 10 +++++++++- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/client/src/components/level.tsx b/client/src/components/level.tsx index 4aa15e8..2259968 100644 --- a/client/src/components/level.tsx +++ b/client/src/components/level.tsx @@ -36,7 +36,7 @@ import { Button } from './button' import Markdown from './markdown'; import {Inventory, Documentation} from './inventory'; import { useGetGameInfoQuery, useLoadLevelQuery } from '../state/api'; -import { changedSelection, codeEdited, selectCode, selectSelections, progressSlice, selectCompleted } from '../state/progress'; +import { changedSelection, codeEdited, selectCode, selectSelections, progressSlice, selectCompleted, helpEdited, selectHelp } from '../state/progress'; import { DualEditor } from './infoview/main' import { DeletedHint, DeletedHints, Hints } from './hints'; import { DeletedChatContext, InputModeContext, MonacoEditorContext, ProofContext, ProofStep, SelectionContext } from './infoview/context'; @@ -72,6 +72,8 @@ function PlayableLevel({worldId, levelId}) { // a new proof has been entered. e.g. to consult messages coming from dead ends const [deletedChat, setDeletedChat] = useState>([]) + const store = useStore() + // A set of row numbers where help is displayed const [showHelp, setShowHelp] = useState>(new Set()) @@ -82,8 +84,6 @@ function PlayableLevel({worldId, levelId}) { const [commandLineInput, setCommandLineInput] = useState("") const [canUndo, setCanUndo] = useState(initialCode.trim() !== "") - const [showHiddenHints, setShowHiddenHints] = useState(false) - const [selectedStep, setSelectedStep] = useState() const theme = useTheme(); @@ -97,6 +97,11 @@ function PlayableLevel({worldId, levelId}) { setCommandLineInput("") }, [levelId]) + // Load the selected help steps from the store + useEffect(() => { + setShowHelp(new Set(selectHelp(gameId, worldId, levelId)(store.getState()))) + }, []) + useEffect(() => { // TODO: For some reason this is always called twice console.debug('scroll chat') @@ -126,7 +131,10 @@ function PlayableLevel({worldId, levelId}) { React.useEffect(() => { // Forget whether hidden hints are displayed for steps that don't exist yet - setShowHelp(new Set(Array.from(showHelp).filter(i => (i < proof.length)))) + if (proof.length) { + console.debug(Array.from(showHelp)) + setShowHelp(new Set(Array.from(showHelp).filter(i => (i < proof.length)))) + } }, [proof]) /** Unused. Was implementing an undo button, which has been replaced by `deleteProof` inside @@ -165,6 +173,14 @@ function PlayableLevel({worldId, levelId}) { setCanUndo(code.trim() !== "") } + // save showed help in store + useEffect(() => { + if (proof.length) { + console.debug(`showHelp:\n ${showHelp}`) + dispatch(helpEdited({game: gameId, world: worldId, level: levelId, help: Array.from(showHelp)})) + } + }, [showHelp]) + const onDidChangeSelection = (monacoSelections) => { const selections = monacoSelections.map( ({selectionStartLineNumber, selectionStartColumn, positionLineNumber, positionColumn}) => diff --git a/client/src/state/progress.ts b/client/src/state/progress.ts index 476d3ca..2149886 100644 --- a/client/src/state/progress.ts +++ b/client/src/state/progress.ts @@ -67,6 +67,7 @@ export const progressSlice = createSlice({ }, /** Set the list of rows where help is displayed */ helpEdited(state: ProgressState, action: PayloadAction<{game: string, world: string, level: number, help: number[]}>) { + console.debug(`!setting help to: ${action.payload.help}`) state.games[action.payload.game][action.payload.world][action.payload.level].help = action.payload.help }, /** delete all progress for this game */ @@ -103,6 +104,13 @@ export function selectCode(game: string, world: string, level: number) { } } +/** return the code of the current level */ +export function selectHelp(game: string, world: string, level: number) { + return (state) => { + return selectLevel(game, world, level)(state).help + } +} + /** return the selections made in the current level */ export function selectSelections(game: string, world: string, level: number) { return (state) => { @@ -126,4 +134,4 @@ export function selectProgress(game: string) { /** Export actions to modify the progress */ export const { changedSelection, codeEdited, levelCompleted, deleteProgress, - deleteLevelProgress, loadProgress } = progressSlice.actions + deleteLevelProgress, loadProgress, helpEdited } = progressSlice.actions