From df7e13f2b7165dfb3e37bc1fc9c22701d389c841 Mon Sep 17 00:00:00 2001 From: "Query&mut NinjaStyle, &Ryder" <97464694+X1Vi@users.noreply.github.com> Date: Tue, 14 Jan 2025 13:12:10 +0530 Subject: [PATCH] fix: now pane remembers state (#57368) Co-authored-by: root --- .../Challenges/classic/desktop-layout.tsx | 75 ++++++++++++++++++- 1 file changed, 72 insertions(+), 3 deletions(-) diff --git a/client/src/templates/Challenges/classic/desktop-layout.tsx b/client/src/templates/Challenges/classic/desktop-layout.tsx index 0353a51f9e9..6ba7f60a2c0 100644 --- a/client/src/templates/Challenges/classic/desktop-layout.tsx +++ b/client/src/templates/Challenges/classic/desktop-layout.tsx @@ -3,6 +3,7 @@ import React, { useState, useEffect, ReactElement } from 'react'; import { ReflexContainer, ReflexSplitter, ReflexElement } from 'react-reflex'; import { createSelector } from 'reselect'; import { connect } from 'react-redux'; +import store from 'store'; import { sortChallengeFiles } from '../../../../utils/sort-challengefiles'; import { challengeTypes } from '../../../../../shared/config/challenge-types'; import { @@ -99,9 +100,77 @@ const DesktopLayout = (props: DesktopLayoutProps): JSX.Element => { startWithConsoleShown } = props; - const [showNotes, setShowNotes] = useState(false); - const [showConsole, setShowConsole] = useState(startWithConsoleShown); - const [showInstructions, setShowInstructions] = useState(true); + const initialShowState = (key: string, defaultValue: boolean): boolean => { + const savedState: string = store.get('layoutPaneBooleans') as string; + try { + if (savedState) { + const parsedState: Record = JSON.parse( + savedState + ) as Record; + return parsedState[key] || defaultValue; + } + } catch (error) { + console.error('Error parsing layoutPaneBooleans from store', error); + } + return defaultValue; + }; + + const [showNotes, setShowNotes] = useState(() => + initialShowState('showNotes', false) + ); + const [showConsole, setShowConsole] = useState(() => + initialShowState('showConsole', startWithConsoleShown) + ); + const [showInstructions, setShowInstructions] = useState(() => + initialShowState('showInstructions', true) + ); + + /* eslint-disable react-hooks/exhaustive-deps */ + useEffect(() => { + setShowPreviewPane(initialShowState('showPreviewPane', false)); + setShowPreviewPortal(initialShowState('showPreviewPortal', false)); + }, []); + + useEffect(() => { + const layoutPaneBooleans = { + showNotes, + showConsole, + showInstructions, + showPreviewPane, + showPreviewPortal + }; + store.set('layoutPaneBooleans', JSON.stringify(layoutPaneBooleans)); + }, [ + showNotes, + showConsole, + showInstructions, + showPreviewPane, + showPreviewPortal + ]); + + useEffect(() => { + const layoutPaneBooleans: string = store.get( + 'layoutPaneBooleans' + ) as string; + if (layoutPaneBooleans) { + let parsedLayoutPaneBooleans: Record = {}; + try { + parsedLayoutPaneBooleans = JSON.parse(layoutPaneBooleans) as Record< + string, + boolean + >; + } catch (error) { + console.error('Error parsing layoutPaneBooleans from store', error); + } + setShowNotes(parsedLayoutPaneBooleans.showNotes || false); + setShowConsole( + parsedLayoutPaneBooleans.showConsole || startWithConsoleShown + ); + setShowInstructions(parsedLayoutPaneBooleans.showInstructions || true); + setShowPreviewPane(parsedLayoutPaneBooleans.showPreviewPane || false); + setShowPreviewPortal(parsedLayoutPaneBooleans.showPreviewPortal || false); + } + }, []); const togglePane = (pane: string): void => { if (pane === 'showPreviewPane') {