Skip to content

Commit

Permalink
usePostActions: accept optional actionIds
Browse files Browse the repository at this point in the history
  • Loading branch information
mcsf committed Mar 29, 2024
1 parent 3efd94a commit 24f2a56
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
21 changes: 18 additions & 3 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,18 +413,28 @@ 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,
permanentlyDeletePostAction,
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 ) {
Expand Down Expand Up @@ -467,5 +477,10 @@ export function usePostActions( onActionPerformed ) {
}
}
return actions;
}, [ permanentlyDeletePostAction, restorePostAction, onActionPerformed ] );
}, [
actionIds,
permanentlyDeletePostAction,
restorePostAction,
onActionPerformed,
] );
}

0 comments on commit 24f2a56

Please sign in to comment.