Skip to content

Commit 6468939

Browse files
committed
update useDesktopLayoutState
1 parent 71d3a31 commit 6468939

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

client/src/templates/Challenges/classic/desktop-layout.tsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import {
1111
} from '../../../redux/prop-types';
1212
import PreviewPortal from '../components/preview-portal';
1313
import ActionRow from './action-row';
14-
import { DesktopLayoutPanels, useDesktopLayoutState } from './use-desktop-layout-state';
14+
import {
15+
DesktopLayoutPanels,
16+
useDesktopLayoutState
17+
} from './use-desktop-layout-state';
1518

1619
type Pane = { flex: number };
1720

@@ -47,7 +50,13 @@ const reflexProps = {
4750

4851
const DesktopLayout = (props: DesktopLayoutProps): JSX.Element => {
4952
const {
50-
layoutState: { showInstructions, showNotes, showPreviewPane, showPreviewPortal, showConsole },
53+
layoutState: {
54+
showInstructions,
55+
showNotes,
56+
showPreviewPane,
57+
showPreviewPortal,
58+
showConsole
59+
},
5160
togglePane
5261
} = useDesktopLayoutState();
5362

@@ -178,7 +187,15 @@ const DesktopLayout = (props: DesktopLayoutProps): JSX.Element => {
178187
<Segment />
179188
<GoogleTagManager />
180189
{displayPreviewPortal && (
181-
<PreviewPortal togglePane={() => togglePane(DesktopLayoutPanels.PreviewPortal)} windowTitle={windowTitle}>
190+
<PreviewPortal
191+
togglePane={() => {
192+
togglePane({
193+
panel: DesktopLayoutPanels.PreviewPortal,
194+
setVisible: false
195+
});
196+
}}
197+
windowTitle={windowTitle}
198+
>
182199
{preview}
183200
</PreviewPortal>
184201
)}

client/src/templates/Challenges/classic/use-desktop-layout-state.tsx

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useReducer } from 'react';
1+
import { useReducer, useRef } from 'react';
22

33
const STORAGE_KEY = 'fcc:desktop:layout:state';
44

@@ -22,7 +22,7 @@ const defaultLayoutState: DesktopLayoutState = {
2222
[DesktopLayoutPanels.Instructions]: true,
2323
[DesktopLayoutPanels.Notes]: false,
2424
[DesktopLayoutPanels.PreviewPane]: true,
25-
[DesktopLayoutPanels.PreviewPortal]: true,
25+
[DesktopLayoutPanels.PreviewPortal]: false,
2626
[DesktopLayoutPanels.Console]: false
2727
};
2828

@@ -38,20 +38,44 @@ const readFromStorage = (
3838
}
3939
};
4040

41+
type LayoutReducerAction =
42+
| DesktopLayoutPanels
43+
| {
44+
panel: DesktopLayoutPanels;
45+
setVisible: boolean;
46+
};
47+
4148
const layoutReducer = (
4249
state: DesktopLayoutState,
43-
panel: DesktopLayoutPanels
50+
action: LayoutReducerAction
4451
) => {
52+
let panel: DesktopLayoutPanels,
53+
setVisible = undefined;
54+
if (typeof action === 'string') {
55+
panel = action;
56+
} else {
57+
panel = action.panel;
58+
setVisible = action.setVisible;
59+
}
60+
4561
const nextState = {
4662
...state,
47-
[panel]: !state[panel]
63+
[panel]: setVisible ?? !state[panel]
4864
};
4965

50-
if (panel === DesktopLayoutPanels.PreviewPane && nextState[DesktopLayoutPanels.PreviewPane] && nextState[DesktopLayoutPanels.PreviewPortal]) {
66+
if (
67+
panel === DesktopLayoutPanels.PreviewPane &&
68+
nextState[DesktopLayoutPanels.PreviewPane] &&
69+
nextState[DesktopLayoutPanels.PreviewPortal]
70+
) {
5171
nextState[DesktopLayoutPanels.PreviewPortal] = false;
5272
}
5373

54-
if (panel === DesktopLayoutPanels.PreviewPortal && nextState[DesktopLayoutPanels.PreviewPortal] && nextState[DesktopLayoutPanels.PreviewPane]) {
74+
if (
75+
panel === DesktopLayoutPanels.PreviewPortal &&
76+
nextState[DesktopLayoutPanels.PreviewPortal] &&
77+
nextState[DesktopLayoutPanels.PreviewPane]
78+
) {
5579
nextState[DesktopLayoutPanels.PreviewPane] = false;
5680
}
5781

@@ -61,10 +85,9 @@ const layoutReducer = (
6185
};
6286

6387
export const useDesktopLayoutState = () => {
64-
const [layoutState, dispatch] = useReducer(
65-
layoutReducer,
66-
readFromStorage(defaultLayoutState)
67-
);
88+
const initState = useRef(readFromStorage(defaultLayoutState));
89+
90+
const [layoutState, dispatch] = useReducer(layoutReducer, initState.current);
6891

6992
return {
7093
layoutState,

0 commit comments

Comments
 (0)