Skip to content

Commit

Permalink
Site Editor: Only show 'Back' button when user came from an editor ca…
Browse files Browse the repository at this point in the history
…nvas (#58710)

* Site Editor: Only show 'Back' button when came from an editor canvas

* Check for presense of postType and postId

* usePrevious: Update implementation to return previous value, instead of value from previous render

See https://www.developerway.com/posts/implementing-advanced-use-previous-hook for a good
explanation of the problem.

* Revert "usePrevious: Update implementation to return previous value, instead of value from previous render"

This reverts commit 8068fe3.

* Ignore react-hooks/exhaustive-deps for now

Co-authored-by: noisysocks <[email protected]>
Co-authored-by: andrewserong <[email protected]>
Co-authored-by: youknowriad <[email protected]>
  • Loading branch information
4 people authored Feb 7, 2024
1 parent d0b9e7a commit c9568e2
Showing 1 changed file with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useMemo } from '@wordpress/element';
import { store as coreStore } from '@wordpress/core-data';
import { privateApis as editorPrivateApis } from '@wordpress/editor';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { usePrevious } from '@wordpress/compose';

/**
* Internal dependencies
Expand Down Expand Up @@ -92,13 +93,24 @@ function useArchiveLabel( templateSlug ) {

function useGoBack() {
const location = useLocation();
const previousLocation = usePrevious( location );
const history = useHistory();
const goBack = useMemo( () => {
const isFocusMode =
location.params.focusMode ||
FOCUSABLE_ENTITIES.includes( location.params.postType );
return isFocusMode ? () => history.back() : undefined;
}, [ location.params.focusMode, location.params.postType, history ] );
( location.params.postId &&
FOCUSABLE_ENTITIES.includes( location.params.postType ) );
const didComeFromEditorCanvas =
previousLocation?.params.postId &&
previousLocation?.params.postType &&
previousLocation?.params.canvas === 'edit';
const showBackButton = isFocusMode && didComeFromEditorCanvas;
return showBackButton ? () => history.back() : undefined;
// Disable reason: previousLocation changes when the component updates for any reason, not
// just when location changes. Until this is fixed we can't add it to deps. See
// https://github.com/WordPress/gutenberg/pull/58710#discussion_r1479219465.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ location, history ] );
return goBack;
}

Expand Down

0 comments on commit c9568e2

Please sign in to comment.