Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix navigate to entity crash in Post Editor for 6.7 #66709

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions packages/edit-post/src/hooks/use-navigate-to-entity-record.js
Original file line number Diff line number Diff line change
@@ -2,8 +2,10 @@
* WordPress dependencies
*/
import { useCallback, useReducer } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { useSelect, useDispatch, useRegistry } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';
import { store as coreStore } from '@wordpress/core-data';
import { sprintf, __ } from '@wordpress/i18n';

/**
* A hook that records the 'entity' history in the post editor as a user
@@ -25,6 +27,8 @@ export default function useNavigateToEntityRecord(
initialPostType,
defaultRenderingMode
) {
const registry = useRegistry();

const [ postHistory, dispatch ] = useReducer(
( historyState, { type, post, previousRenderingMode } ) => {
if ( type === 'push' ) {
@@ -52,7 +56,23 @@ export default function useNavigateToEntityRecord(
const { setRenderingMode } = useDispatch( editorStore );

const onNavigateToEntityRecord = useCallback(
( params ) => {
async ( params ) => {
try {
await registry
.resolveSelect( coreStore )
.getPostType( params.postType );
Comment on lines +61 to +63
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@getdave, can you explain why we need to pre-resolve this selector?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it would help. Let me see if I can dig this up. We should include information inline in a comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is probably fixing a symptom rather than fixing the root issue where we may be using an undefined postType object or something.

} catch ( err ) {
throw new Error(
sprintf(
// translators: %s: the name of a post type.
__( `Unable to fetch post type "%s" from API.` ),
params.postType
),
{
cause: err,
}
);
}
dispatch( {
type: 'push',
post: { postId: params.postId, postType: params.postType },
@@ -61,7 +81,7 @@ export default function useNavigateToEntityRecord(
} );
setRenderingMode( defaultRenderingMode );
},
[ getRenderingMode, setRenderingMode, defaultRenderingMode ]
[ registry, getRenderingMode, setRenderingMode, defaultRenderingMode ]
);

const onNavigateToPreviousEntityRecord = useCallback( () => {