From 6972c7969594019ce7ac61ba0a598fb4f0572156 Mon Sep 17 00:00:00 2001 From: Lourens Schep Date: Thu, 12 Dec 2024 15:09:49 +0100 Subject: [PATCH] Update hook to use correct ids for permissions --- .../src/components/post-actions/index.js | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/packages/editor/src/components/post-actions/index.js b/packages/editor/src/components/post-actions/index.js index f89b6bb83477b..784fd42b1b50a 100644 --- a/packages/editor/src/components/post-actions/index.js +++ b/packages/editor/src/components/post-actions/index.js @@ -21,27 +21,47 @@ import { usePostActions } from './actions'; const { Menu, kebabCase } = unlock( componentsPrivateApis ); function useEditedEntityRecordsWithPermissions( postType, postIds ) { - const { items, permissions } = useSelect( + const entityConfig = useSelect( + ( select ) => + select( coreStore ).getEntityConfig( 'postType', postType ), + [ postType ] + ); + const { items } = useSelect( ( select ) => { - const { getEditedEntityRecords, getEntityRecordsPermissions } = - unlock( select( coreStore ) ); + const { getEditedEntityRecords } = unlock( select( coreStore ) ); return { items: getEditedEntityRecords( 'postType', postType, postIds ), - permissions: getEntityRecordsPermissions( - 'postType', - postType, - postIds - ), }; }, [ postIds, postType ] ); + const ids = useMemo( + () => + items?.map( + // @ts-ignore + ( record ) => record[ entityConfig?.key ?? 'id' ] + ) ?? [], + [ items, entityConfig?.key ] + ); + + const permissions = useSelect( + ( select ) => { + const { getEntityRecordsPermissions } = unlock( + select( coreStore ) + ); + return getEntityRecordsPermissions( 'postType', postType, ids ); + }, + [ ids, postType ] + ); + return useMemo( () => { - return items.map( ( item, index ) => ( { - ...item, - permissions: permissions[ index ], - } ) ); + return ( + items?.map( ( item, index ) => ( { + ...item, + permissions: permissions[ index ], + } ) ) ?? [] + ); }, [ items, permissions ] ); }