-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Core Data: Support entity queries in the 'useResourcePermissions' hook #63653
Conversation
Size Change: +19 B (0%) Total Size: 1.76 MB
ℹ️ View Unchanged
|
925efaf
to
ec2abb7
Compare
const permissions = useResourcePermissions( { | ||
kind: 'postType', | ||
name: postType, | ||
} ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removes unnecessary checks for both entities, which later might have been disregarded. Reduces the possible OPTIONS
request to one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice one 👍 I appreciate the resulting cleanup!
packages/edit-widgets/src/components/widget-areas-block-editor-provider/index.js
Show resolved
Hide resolved
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
This is ready for the review. cc @WordPress/gutenberg-core |
return useQuerySelect( | ||
( resolve ) => { | ||
const isEntity = typeof resource === 'object'; | ||
const hasId = isEntity ? !! resource.id : !! id; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels confusing that we're allowing an ID to be provided as the second argument when working with a resource
. This would allow for a hybrid use, like this:
useResourcePermissions( { kind: 'postType', name: 'post' }, 123 )
which feels confusing, since we also allow:
useResourcePermissions( { kind: 'postType', name: 'post', id: 123 } )
since we're defining this behavior right here and right now, does it make sense to remove the ambiguity?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hybrid signature won't work. In that case, the hasId
would be false; the hook would check general permissions for the REST API and return early.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for confirming, but that's even worse. In that case, we need to make sure this behavior is well-documented, and we should probably throw an error if someone calls it with a resource object and a separate ID argument. Because, you know, TS won't be enough sometimes - some projects and developers just don't use it (or abuse it) in their dev environments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Throwing an error in a hook might be a bit risky. Depending on where the hook is called in the tree, the closest error boundary might be the one in the app root, preventing the whole app from loading.
I think logging a warning
is probably the safest solution here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logging a warning
makes sense 👍 I definitely didn't mean throwing a literal error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the warning using the @wordpress/warning
package, which seems to be the new preferred method for such cases.
cc @gziolo
Screenshot
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m still processing the usage of warning
util as I started to have second thoughts of how we can catch these warnings on production. Do we have to? How folks debug the cases where plugins conflict between each other? My reasoning was that if the app continues to work then we don’t need to add noise to the Browser Console when the app runs on production. I’m happy to discuss it further but for block registration it was a clear path forward as it’s mostly targeting the block author when developing the block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, the warning is useful to the developer who's trying to use the API, since it's clear that they're using it in an unexpected way (almost like a front-end counterpart of _doing_it_wrong()
. To me, it definitely doesn't make sense to surface that in production, it should only be seen in development mode.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This warning also has the same target audience - developers. The warnings that aren't actionable by the end user shouldn't be displayed in production environments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we reached a consensus here. I appreciate the parallel to _doint_it_wrong()
. That speaks well to me and maybe we should alias it this way one day to offer more clarity.
const permissions = useResourcePermissions( { | ||
kind: 'postType', | ||
name: postType, | ||
} ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice one 👍 I appreciate the resulting cleanup!
dispatch.receiveUserPermission( 'read/postType/wp_navigation', allowed ); | ||
dispatch.startResolution( 'canUser', [ | ||
'read', | ||
{ kind: 'postType', name: 'wp_navigation', id: undefined }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we have id: undefined
? Shouldn't it just be omitted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While selectorArgsToStateKey
removes trailing undefined
arguments, it doesn't do it inside the arguments themselves.
Omitting id
results canUser
not marked as resolved.
packages/block-library/src/navigation/test/use-navigation-menu.js
Outdated
Show resolved
Hide resolved
5c25763
to
9404acb
Compare
140d3de
to
6b1ac09
Compare
@tyxla, do you think this is in an OK shape to be merged? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tested this in the post editor, site editor, and widgets editor and everything worked well! ✅
The code also looks good 👍 I've only left a few minor suggestions that can be addressed before shipping.
Thanks @Mamaduka 🚀
const create = canUser( | ||
'create', | ||
isEntity | ||
? { kind: resource.kind, name: resource.name } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a good reason to not trust passing the resource
object here directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The resource
object can have an id
and the hook used to check create
actions without IDs. So, one reason is backward compatibility.
Usually, create permissions are checked using the list endpoint OPTIONS v2/pages
.
6b1ac09
to
56f1d2b
Compare
Thanks for the review and feedback, @tyxla! |
What?
This is a follow-up to #63322.
PR updates the
useResourcePermissions
hook to support new entity-aware syntax. I also replaced usage in the codebase.Why?
The new syntax should be preferred over direct REST API resource queries.
Testing Instructions
Testing Instructions for Keyboard
Same.
Screenshots or screencast