Skip to content

Commit

Permalink
remove store subscription per action
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Jun 24, 2024
1 parent eda037c commit 471e144
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,18 +307,8 @@ const trashPostAction = {
},
};

function useCanUserEligibilityCheckPostType( postType, capability, action ) {
function useCanUserEligibilityCheckPostType( capability, resource, action ) {
const registry = useRegistry();
const { resource, cachedCanUserResolvers } = useSelect(
( select ) => {
const { getPostType, getCachedResolvers } = select( coreStore );
return {
resource: getPostType( postType )?.rest_base || '',
cachedCanUserResolvers: getCachedResolvers().canUser,
};
},
[ postType ]
);
return useMemo(
() => ( {
...action,
Expand All @@ -331,17 +321,14 @@ function useCanUserEligibilityCheckPostType( postType, capability, action ) {
);
},
} ),
// We are making this use memo depend on cachedCanUserResolvers as a way to make the component using this hook re-render
// when user capabilities are resolved. This makes sure the isEligible function is re-evaluated.
// eslint-disable-next-line react-hooks/exhaustive-deps
[ action, registry, capability, resource, cachedCanUserResolvers ]
[ action, registry, capability, resource ]
);
}

function useTrashPostAction( postType ) {
function useTrashPostAction( resource ) {
return useCanUserEligibilityCheckPostType(
postType,
'delete',
resource,
trashPostAction
);
}
Expand Down Expand Up @@ -436,10 +423,10 @@ const permanentlyDeletePostAction = {
},
};

function usePermanentlyDeletePostAction( postType ) {
function usePermanentlyDeletePostAction( resource ) {
return useCanUserEligibilityCheckPostType(
postType,
'delete',
resource,
permanentlyDeletePostAction
);
}
Expand Down Expand Up @@ -551,10 +538,10 @@ const restorePostAction = {
},
};

function useRestorePostAction( postType ) {
function useRestorePostAction( resource ) {
return useCanUserEligibilityCheckPostType(
postType,
'update',
resource,
restorePostAction
);
}
Expand Down Expand Up @@ -718,10 +705,10 @@ const renamePostAction = {
},
};

function useRenamePostAction( postType ) {
function useRenamePostAction( resource ) {
return useCanUserEligibilityCheckPostType(
postType,
'update',
resource,
renamePostAction
);
}
Expand Down Expand Up @@ -1070,27 +1057,36 @@ export const duplicateTemplatePartAction = {
};

export function usePostActions( { postType, onActionPerformed, context } ) {
const { defaultActions, postTypeObject, userCanCreatePostType } = useSelect(
const {
defaultActions,
postTypeObject,
userCanCreatePostType,
resource,
cachedCanUserResolvers,
} = useSelect(
( select ) => {
const { getPostType, canUser } = select( coreStore );
const { getPostType, canUser, getCachedResolvers } =
select( coreStore );
const { getEntityActions } = unlock( select( editorStore ) );
const _postTypeObject = getPostType( postType );
const resource = _postTypeObject?.rest_base || '';
const _resource = _postTypeObject?.rest_base || '';
return {
postTypeObject: _postTypeObject,
defaultActions: getEntityActions( 'postType', postType ),
userCanCreatePostType: canUser( 'create', resource ),
userCanCreatePostType: canUser( 'create', _resource ),
resource: _resource,
cachedCanUserResolvers: getCachedResolvers()?.canUser,
};
},
[ postType ]
);

const duplicatePostAction = useDuplicatePostAction( postType );
const trashPostActionForPostType = useTrashPostAction( postType );
const trashPostActionForPostType = useTrashPostAction( resource );
const permanentlyDeletePostActionForPostType =
usePermanentlyDeletePostAction( postType );
const renamePostActionForPostType = useRenamePostAction( postType );
const restorePostActionForPostType = useRestorePostAction( postType );
usePermanentlyDeletePostAction( resource );
const renamePostActionForPostType = useRenamePostAction( resource );
const restorePostActionForPostType = useRestorePostAction( resource );
const isTemplateOrTemplatePart = [
TEMPLATE_POST_TYPE,
TEMPLATE_PART_POST_TYPE,
Expand Down Expand Up @@ -1183,6 +1179,9 @@ export function usePostActions( { postType, onActionPerformed, context } ) {
}

return actions;
// We are making this use memo depend on cachedCanUserResolvers as a way to make the component using this hook re-render
// when user capabilities are resolved. This makes sure the isEligible functions of actions dependent on capabilities are re-evaluated.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
defaultActions,
userCanCreatePostType,
Expand All @@ -1199,5 +1198,6 @@ export function usePostActions( { postType, onActionPerformed, context } ) {
supportsRevisions,
supportsTitle,
context,
cachedCanUserResolvers,
] );
}

0 comments on commit 471e144

Please sign in to comment.