From 24f2a565cb7f3a739e1f8320045add776ecb1e9d Mon Sep 17 00:00:00 2001 From: Miguel Fonseca <150562+mcsf@users.noreply.github.com> Date: Fri, 29 Mar 2024 11:47:46 +0000 Subject: [PATCH] usePostActions: accept optional `actionIds` --- .../page-templates-template-parts/index.js | 13 ++++++------ .../src/components/post-actions/actions.js | 21 ++++++++++++++++--- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/packages/edit-site/src/components/page-templates-template-parts/index.js b/packages/edit-site/src/components/page-templates-template-parts/index.js index ddfd94047cd20..72b56b5e249eb 100644 --- a/packages/edit-site/src/components/page-templates-template-parts/index.js +++ b/packages/edit-site/src/components/page-templates-template-parts/index.js @@ -353,18 +353,19 @@ export default function PageTemplatesTemplateParts( { postType } ) { }, [ history ] ); - const postActions = usePostActions( onActionPerformed ); + const [ editAction, viewRevisionsAction ] = usePostActions( + onActionPerformed, + [ 'edit-post', 'view-post-revisions' ] + ); const actions = useMemo( () => [ - postActions.find( ( action ) => action.id === 'edit-post' ), + editAction, resetTemplateAction, renameTemplateAction, - postActions.find( - ( action ) => action.id === 'view-post-revisions' - ), + viewRevisionsAction, deleteTemplateAction, ], - [ postActions ] + [ editAction, viewRevisionsAction ] ); const onChangeView = useCallback( diff --git a/packages/editor/src/components/post-actions/actions.js b/packages/editor/src/components/post-actions/actions.js index 3ffc1a45ac513..f3b96d7f22ec2 100644 --- a/packages/editor/src/components/post-actions/actions.js +++ b/packages/editor/src/components/post-actions/actions.js @@ -413,11 +413,12 @@ export const postRevisionsAction = { }, }; -export function usePostActions( onActionPerformed ) { +export function usePostActions( onActionPerformed, actionIds = null ) { const permanentlyDeletePostAction = usePermanentlyDeletePostAction(); const restorePostAction = useRestorePostAction(); return useMemo( () => { - const actions = [ + // By default, return all actions... + const defaultActions = [ editPostAction, viewPostAction, restorePostAction, @@ -425,6 +426,15 @@ export function usePostActions( onActionPerformed ) { postRevisionsAction, trashPostAction, ]; + + // ... unless `actionIds` was specified, in which case we find the + // actions matching the given IDs. + const actions = actionIds + ? actionIds.map( ( actionId ) => + defaultActions.find( ( { id } ) => actionId === id ) + ) + : defaultActions; + if ( onActionPerformed ) { for ( let i = 0; i < actions.length; ++i ) { if ( actions[ i ].callback ) { @@ -467,5 +477,10 @@ export function usePostActions( onActionPerformed ) { } } return actions; - }, [ permanentlyDeletePostAction, restorePostAction, onActionPerformed ] ); + }, [ + actionIds, + permanentlyDeletePostAction, + restorePostAction, + onActionPerformed, + ] ); }