From eba998ef5436f6309ff940bcbc0ca730bab66c2f Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Thu, 10 Oct 2024 10:36:16 +0200 Subject: [PATCH] Global styles: improve navigation logic for revisions screen (#65946) * Global styles: improve navigation logic for revisions screen * Merge style book case into default, add comment * Remove unnecessary goTo calls * Remove useNavigator import * Apply feedback --------- Co-authored-by: ciampo Co-authored-by: ramonjd Co-authored-by: tyxla Co-authored-by: jameskoster --- .../components/global-styles-sidebar/index.js | 12 +------- .../global-styles/screen-revisions/index.js | 13 ++------- .../src/components/global-styles/ui.js | 28 +++++++++++-------- 3 files changed, 20 insertions(+), 33 deletions(-) diff --git a/packages/edit-site/src/components/global-styles-sidebar/index.js b/packages/edit-site/src/components/global-styles-sidebar/index.js index 966005907cda4a..90a3331d14c716 100644 --- a/packages/edit-site/src/components/global-styles-sidebar/index.js +++ b/packages/edit-site/src/components/global-styles-sidebar/index.js @@ -1,13 +1,7 @@ /** * WordPress dependencies */ -import { - FlexItem, - FlexBlock, - Flex, - Button, - useNavigator, -} from '@wordpress/components'; +import { FlexItem, FlexBlock, Flex, Button } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { styles, seen, backup } from '@wordpress/icons'; import { useSelect, useDispatch } from '@wordpress/data'; @@ -86,21 +80,17 @@ export default function GlobalStylesSidebar() { }, [ shouldClearCanvasContainerView ] ); const { setIsListViewOpened } = useDispatch( editorStore ); - const { goTo } = useNavigator(); const toggleRevisions = () => { setIsListViewOpened( false ); if ( isRevisionsStyleBookOpened ) { - goTo( '/' ); setEditorCanvasContainerView( 'style-book' ); return; } if ( isRevisionsOpened ) { - goTo( '/' ); setEditorCanvasContainerView( undefined ); return; } - goTo( '/revisions' ); if ( isStyleBookOpened ) { setEditorCanvasContainerView( diff --git a/packages/edit-site/src/components/global-styles/screen-revisions/index.js b/packages/edit-site/src/components/global-styles/screen-revisions/index.js index b980d199e7be30..a50b8f13d55cc2 100644 --- a/packages/edit-site/src/components/global-styles/screen-revisions/index.js +++ b/packages/edit-site/src/components/global-styles/screen-revisions/index.js @@ -3,7 +3,6 @@ */ import { __, sprintf } from '@wordpress/i18n'; import { - useNavigator, __experimentalConfirmDialog as ConfirmDialog, Spinner, } from '@wordpress/components'; @@ -33,7 +32,6 @@ const { GlobalStylesContext, areGlobalStyleConfigsEqual } = unlock( const PAGE_SIZE = 10; function ScreenRevisions() { - const { goTo } = useNavigator(); const { user: currentEditorGlobalStyles, setUserConfig } = useContext( GlobalStylesContext ); const { blocks, editorCanvasContainerView } = useSelect( @@ -71,6 +69,8 @@ function ScreenRevisions() { currentEditorGlobalStyles ); + // The actual code that triggers the revisions screen to navigate back + // to the home screen in in `packages/edit-site/src/components/global-styles/ui.js`. const onCloseRevisions = () => { const canvasContainerView = editorCanvasContainerView === 'global-styles-revisions:style-book' @@ -85,15 +85,6 @@ function ScreenRevisions() { onCloseRevisions(); }; - useEffect( () => { - if ( - ! editorCanvasContainerView || - ! editorCanvasContainerView.startsWith( 'global-styles-revisions' ) - ) { - goTo( '/' ); // Return to global styles main panel. - } - }, [ editorCanvasContainerView ] ); - useEffect( () => { if ( ! isLoading && revisions.length ) { setCurrentRevisions( revisions ); diff --git a/packages/edit-site/src/components/global-styles/ui.js b/packages/edit-site/src/components/global-styles/ui.js index d534da901cd2e2..6cd465e237100a 100644 --- a/packages/edit-site/src/components/global-styles/ui.js +++ b/packages/edit-site/src/components/global-styles/ui.js @@ -70,10 +70,8 @@ function GlobalStylesActionMenu() { const { setEditorCanvasContainerView } = unlock( useDispatch( editSiteStore ) ); - const { goTo } = useNavigator(); const loadCustomCSS = () => { setEditorCanvasContainerView( 'global-styles-css' ); - goTo( '/css' ); }; return ( @@ -253,21 +251,29 @@ function GlobalStylesEditorCanvasContainerLink() { switch ( editorCanvasContainerView ) { case 'global-styles-revisions': case 'global-styles-revisions:style-book': - goTo( '/revisions' ); + if ( ! isRevisionsOpen ) { + goTo( '/revisions' ); + } break; case 'global-styles-css': goTo( '/css' ); break; + // The stand-alone style book is open + // and the revisions panel is open, + // close the revisions panel. + // Otherwise keep the style book open while + // browsing global styles panel. + // + // Falling through as it matches the default scenario. case 'style-book': - /* - * The stand-alone style book is open - * and the revisions panel is open, - * close the revisions panel. - * Otherwise keep the style book open while - * browsing global styles panel. - */ + default: + // In general, if the revision screen is in view but the + // `editorCanvasContainerView` is not a revision view, close it. + // This also includes the scenario when the stand-alone style + // book is open, in which case we want the user to close the + // revisions screen and browse global styles. if ( isRevisionsOpen ) { - goTo( '/' ); + goTo( '/', { isBack: true } ); } break; }