Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import buildNavigationLabel from '../sidebar-navigation-screen-navigation-menus/
export const postType = `wp_navigation`;

export default function SidebarNavigationScreenNavigationMenu() {
const {
params: { postId },
} = useNavigator();
const { params } = useNavigator();

// See https://github.com/WordPress/gutenberg/pull/52120.
const postId = Number( params?.postId );
Comment on lines +27 to +28
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As this is definitely Navigation posts we know it's a number.


const { record: navigationMenu, isResolving } = useEntityRecord(
'postType',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import SidebarNavigationScreenDetailsFooter from '../sidebar-navigation-screen-d
export default function SidebarNavigationScreenPage() {
const navigator = useNavigator();
const { setCanvasMode } = unlock( useDispatch( editSiteStore ) );
const {
params: { postId },
} = useNavigator();
const { params } = useNavigator();

const postId = Number( params?.postId );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As this is Page entities we know it's a number.

const { record } = useEntityRecord( 'postType', 'page', postId );

const { featuredMediaAltText, featuredMediaSourceUrl } = useSelect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ import useInitEditedEntityFromURL from '../sync-state-with-url/use-init-edited-e
import usePatternDetails from './use-pattern-details';
import { store as editSiteStore } from '../../store';
import { unlock } from '../../lock-unlock';
import normalizePostIdForPostType from '../../utils/normalize-post-id-for-post-type';

export default function SidebarNavigationScreenPattern() {
const { params } = useNavigator();
const { categoryType } = getQueryArgs( window.location.href );
const { postType, postId } = params;
const { setCanvasMode } = unlock( useDispatch( editSiteStore ) );

const { params } = useNavigator();
const { postType } = params;
const postId = normalizePostIdForPostType( params?.postId, postType );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Patterns can be wp_block or wp_template_part which use different types for recordKey so we need to selectively normalize.


useInitEditedEntityFromURL();

const patternDetails = usePatternDetails( postType, postId );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import {
SidebarNavigationScreenDetailsPanelLabel,
SidebarNavigationScreenDetailsPanelValue,
} from '../sidebar-navigation-screen-details-panel';
import normalizePostIdForPostType from '../../utils/normalize-post-id-for-post-type';

export default function usePatternDetails( postType, postId ) {
postId = normalizePostIdForPostType( postId, postType );

const { getDescription, getTitle, record } = useEditedEntityRecord(
postType,
postId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ import { privateApis as routerPrivateApis } from '@wordpress/router';
*/
import { store as editSiteStore } from '../../store';
import { unlock } from '../../lock-unlock';
import normalizePostIdForPostType from '../../utils/normalize-post-id-for-post-type';

const { useLocation } = unlock( routerPrivateApis );

export default function useInitEditedEntityFromURL() {
const { params: { postId, postType } = {} } = useLocation();
const { params } = useLocation();

const { postType } = params;

const postId = normalizePostIdForPostType( params?.postId, postType );

const { isRequestingSite, homepageId, url } = useSelect( ( select ) => {
const { getSite, getUnstableBase } = select( coreDataStore );
const siteData = getSite();
Expand Down Expand Up @@ -66,7 +72,7 @@ export default function useInitEditedEntityFromURL() {
// In all other cases, we need to set the home page in the site editor view.
if ( homepageId ) {
setPage( {
context: { postType: 'page', postId: homepageId },
context: { postType: 'page', postId: Number( homepageId ) },
} );
} else if ( ! isRequestingSite ) {
setPage( {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { decodeEntities } from '@wordpress/html-entities';
* Internal dependencies
*/
import { store as editSiteStore } from '../../store';
import normalizePostIdForPostType from '../../utils/normalize-post-id-for-post-type';

export default function useEditedEntityRecord( postType, postId ) {
const { record, title, description, isLoaded, icon } = useSelect(
Expand All @@ -21,7 +22,11 @@ export default function useEditedEntityRecord( postType, postId ) {
const { __experimentalGetTemplateInfo: getTemplateInfo } =
select( editorStore );
const usedPostType = postType ?? getEditedPostType();
const usedPostId = postId ?? getEditedPostId();

let usedPostId = postId ?? getEditedPostId();

usedPostId = normalizePostIdForPostType( usedPostId, usedPostType );

const _record = getEditedEntityRecord(
'postType',
usedPostType,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const POST_TYPES_THAT_USE_STRING_BASED_IDS = [
'wp_template',
'wp_template_part',
];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps this information could come from entities config? It feels like we should have one single source of truth for this information rather than have it stuff away in a const within a utility.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is interesting. Could use use Number.isNaN(Number(postId)) on any ID and if it's a number we convert if not we dont? So that we don't need to have this array of types that use string ids?

cc @jsnajdr @youknowriad

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated this to use the utility from #52120.

export default function normalizePostIdForPostType( postId, postType ) {
return ! POST_TYPES_THAT_USE_STRING_BASED_IDS?.includes( postType )
? Number( postId )
: postId;
}