1
- import { useReducer } from 'react' ;
1
+ import { useReducer , useRef } from 'react' ;
2
2
3
3
const STORAGE_KEY = 'fcc:desktop:layout:state' ;
4
4
@@ -22,7 +22,7 @@ const defaultLayoutState: DesktopLayoutState = {
22
22
[ DesktopLayoutPanels . Instructions ] : true ,
23
23
[ DesktopLayoutPanels . Notes ] : false ,
24
24
[ DesktopLayoutPanels . PreviewPane ] : true ,
25
- [ DesktopLayoutPanels . PreviewPortal ] : true ,
25
+ [ DesktopLayoutPanels . PreviewPortal ] : false ,
26
26
[ DesktopLayoutPanels . Console ] : false
27
27
} ;
28
28
@@ -38,20 +38,44 @@ const readFromStorage = (
38
38
}
39
39
} ;
40
40
41
+ type LayoutReducerAction =
42
+ | DesktopLayoutPanels
43
+ | {
44
+ panel : DesktopLayoutPanels ;
45
+ setVisible : boolean ;
46
+ } ;
47
+
41
48
const layoutReducer = (
42
49
state : DesktopLayoutState ,
43
- panel : DesktopLayoutPanels
50
+ action : LayoutReducerAction
44
51
) => {
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
+
45
61
const nextState = {
46
62
...state ,
47
- [ panel ] : ! state [ panel ]
63
+ [ panel ] : setVisible ?? ! state [ panel ]
48
64
} ;
49
65
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
+ ) {
51
71
nextState [ DesktopLayoutPanels . PreviewPortal ] = false ;
52
72
}
53
73
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
+ ) {
55
79
nextState [ DesktopLayoutPanels . PreviewPane ] = false ;
56
80
}
57
81
@@ -61,10 +85,9 @@ const layoutReducer = (
61
85
} ;
62
86
63
87
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 ) ;
68
91
69
92
return {
70
93
layoutState,
0 commit comments