@@ -5,7 +5,7 @@ import { FlexItem, Flex, Button } from '@wordpress/components';
55import { __ } from '@wordpress/i18n' ;
66import { styles , seen , backup } from '@wordpress/icons' ;
77import { useSelect , useDispatch } from '@wordpress/data' ;
8- import { useEffect , useRef , useState } from '@wordpress/element' ;
8+ import { useEffect , useRef } from '@wordpress/element' ;
99import { store as preferencesStore } from '@wordpress/preferences' ;
1010import {
1111 store as editorStore ,
@@ -31,20 +31,18 @@ export default function GlobalStylesSidebar() {
3131 const { query } = useLocation ( ) ;
3232 const { canvas = 'view' , name } = query ;
3333 const {
34- shouldClearCanvasContainerView,
35- isStyleBookOpened,
34+ shouldResetNavigation,
35+ stylesPath,
36+ showStylebook,
3637 showListViewByDefault,
3738 hasRevisions,
38- isRevisionsOpened,
39- isRevisionsStyleBookOpened,
4039 activeComplementaryArea,
4140 } = useSelect (
4241 ( select ) => {
4342 const { getActiveComplementaryArea } = select ( interfaceStore ) ;
44- const { getEditorCanvasContainerView } = unlock (
43+ const { getStylesPath , getShowStylebook } = unlock (
4544 select ( editSiteStore )
4645 ) ;
47- const canvasContainerView = getEditorCanvasContainerView ( ) ;
4846 const _isVisualEditorMode =
4947 'visual' === select ( editorStore ) . getEditorMode ( ) ;
5048 const _isEditCanvasMode = 'edit' === canvas ;
@@ -61,8 +59,9 @@ export default function GlobalStylesSidebar() {
6159 : undefined ;
6260
6361 return {
64- isStyleBookOpened : 'style-book' === canvasContainerView ,
65- shouldClearCanvasContainerView :
62+ stylesPath : getStylesPath ( ) ,
63+ showStylebook : getShowStylebook ( ) ,
64+ shouldResetNavigation :
6665 'edit-site/global-styles' !==
6766 getActiveComplementaryArea ( 'core' ) ||
6867 ! _isVisualEditorMode ||
@@ -71,11 +70,6 @@ export default function GlobalStylesSidebar() {
7170 hasRevisions :
7271 ! ! globalStyles ?. _links ?. [ 'version-history' ] ?. [ 0 ]
7372 ?. count ,
74- isRevisionsStyleBookOpened :
75- 'global-styles-revisions:style-book' ===
76- canvasContainerView ,
77- isRevisionsOpened :
78- 'global-styles-revisions' === canvasContainerView ,
7973 activeComplementaryArea :
8074 select ( interfaceStore ) . getActiveComplementaryArea (
8175 'core'
@@ -84,65 +78,50 @@ export default function GlobalStylesSidebar() {
8478 } ,
8579 [ canvas ]
8680 ) ;
87- const { setEditorCanvasContainerView } = unlock (
81+ const { setStylesPath , setShowStylebook , resetStylesNavigation } = unlock (
8882 useDispatch ( editSiteStore )
8983 ) ;
9084 const isMobileViewport = useViewportMatch ( 'medium' , '<' ) ;
91- const [ globalStylesPath , setGlobalStylesPath ] = useState ( '/' ) ;
85+
86+ // Derive state from path and showStylebook
87+ const isRevisionsOpened =
88+ stylesPath . startsWith ( '/revisions' ) && ! showStylebook ;
89+ const isRevisionsStyleBookOpened =
90+ stylesPath . startsWith ( '/revisions' ) && showStylebook ;
91+
9292 const previousActiveArea = usePrevious ( activeComplementaryArea ) ;
9393
94- // Reset path when sidebar opens
94+ // Reset navigation when sidebar opens
9595 useEffect ( ( ) => {
9696 if (
9797 activeComplementaryArea === 'edit-site/global-styles' &&
9898 previousActiveArea !== 'edit-site/global-styles'
9999 ) {
100- setGlobalStylesPath ( '/' ) ;
100+ resetStylesNavigation ( ) ;
101101 }
102- } , [ activeComplementaryArea , previousActiveArea , setGlobalStylesPath ] ) ;
102+ } , [ activeComplementaryArea , previousActiveArea , resetStylesNavigation ] ) ;
103103
104104 useEffect ( ( ) => {
105- if ( shouldClearCanvasContainerView ) {
106- setEditorCanvasContainerView ( undefined ) ;
105+ if ( shouldResetNavigation ) {
106+ resetStylesNavigation ( ) ;
107107 }
108- } , [ shouldClearCanvasContainerView , setEditorCanvasContainerView ] ) ;
108+ } , [ shouldResetNavigation , resetStylesNavigation ] ) ;
109109
110110 const { setIsListViewOpened } = useDispatch ( editorStore ) ;
111111
112112 const toggleRevisions = ( ) => {
113113 setIsListViewOpened ( false ) ;
114- if ( isRevisionsStyleBookOpened ) {
115- setEditorCanvasContainerView ( 'style-book' ) ;
116- return ;
117- }
118- if ( isRevisionsOpened ) {
119- setEditorCanvasContainerView ( undefined ) ;
120- return ;
121- }
122-
123- if ( isStyleBookOpened ) {
124- setEditorCanvasContainerView (
125- 'global-styles-revisions:style-book'
126- ) ;
114+ if ( isRevisionsOpened || isRevisionsStyleBookOpened ) {
115+ // Close revisions, go back to root
116+ setStylesPath ( '/' ) ;
127117 } else {
128- setEditorCanvasContainerView ( 'global-styles-revisions' ) ;
118+ // Open revisions
119+ setStylesPath ( '/revisions' ) ;
129120 }
130121 } ;
131122 const toggleStyleBook = ( ) => {
132- if ( isRevisionsOpened ) {
133- setEditorCanvasContainerView (
134- 'global-styles-revisions:style-book'
135- ) ;
136- return ;
137- }
138- if ( isRevisionsStyleBookOpened ) {
139- setEditorCanvasContainerView ( 'global-styles-revisions' ) ;
140- return ;
141- }
142- setIsListViewOpened ( isStyleBookOpened && showListViewByDefault ) ;
143- setEditorCanvasContainerView (
144- isStyleBookOpened ? undefined : 'style-book'
145- ) ;
123+ setIsListViewOpened ( showStylebook && showListViewByDefault ) ;
124+ setShowStylebook ( ! showStylebook ) ;
146125 } ;
147126
148127 const { getActiveComplementaryArea } = useSelect ( interfaceStore ) ;
@@ -187,12 +166,9 @@ export default function GlobalStylesSidebar() {
187166 < Button
188167 icon = { seen }
189168 label = { __ ( 'Style Book' ) }
190- isPressed = {
191- isStyleBookOpened ||
192- isRevisionsStyleBookOpened
193- }
169+ isPressed = { showStylebook }
194170 accessibleWhenDisabled
195- disabled = { shouldClearCanvasContainerView }
171+ disabled = { shouldResetNavigation }
196172 onClick = { toggleStyleBook }
197173 size = "compact"
198174 />
@@ -218,8 +194,8 @@ export default function GlobalStylesSidebar() {
218194 }
219195 >
220196 < GlobalStylesUI
221- path = { globalStylesPath }
222- onPathChange = { setGlobalStylesPath }
197+ path = { stylesPath }
198+ onPathChange = { setStylesPath }
223199 />
224200 </ DefaultSidebar >
225201 ) ;
0 commit comments