alleyinteractive / alley-scripts

A collection of scripts and utilities for Alley projects.
https://alley-scripts.alley.dev/
6 stars 1 forks source link

PostSelector v2 #45

Closed kevinfodness closed 1 year ago

kevinfodness commented 1 year ago

Problem

We need to rethink the post selector. It was built at a time when REST API support for looking up a post by ID without knowing its type was not possible. Now that we can do that, we should prioritize saving just the post ID via the post selector and enabling a preview of the content based on just an ID as well. It should be significantly simpler to integrate a PostSelector than it is today.

Proposed Solution

Let's reconfigure the PostSelector component in the following ways:

Example Usage

<PostPicker
  allowedTypes={['post', 'page']}
  onReset={() => setAttributes({ myCoolPost: 0 })}
  onSelect={({ id: next }) => setAttributes({ myCoolPost: next })}
  render={(post) => (
    <div class="my-cool-post-container">
      <SafeHTML html={post.title.rendered} tag="h2" />
      <SafeHTML html={post.content.rendered} tag="div" />
    </div>
  ))}
  value={myCoolPost}
/>

The value of post in the render callback will be obtained by getting the post by ID using the search REST API endpoint and using the include query parameter, like: /wp-json/wp/v2/search?include=1234. Once we have the response from the search endpoint for the selected post, we will know the type and subtype, and can make a subsequent query for the full post object. For performance reasons, we should cache the association of IDs to subtypes so we don't have to look it up more than necessary, and we can use useSelect to fetch entities once we know their type by doing select('core').getEntityRecord('postType', postType, postId).

kevinfodness commented 1 year ago

Another possible modification would be to restrict the post picker to picking only one post at a time. MediaUpload allows for multiselect, but I think in most cases the PostPicker is used for selecting one post at a time, and having to extract the ID from an array of post objects in the onSelect callback isn't intuitive, especially when selecting just one post. From an editorial experience, it's likely also preferable to have one component responsible for each post selection to make it easier to pick a new post or reset the selection.

mogmarsh commented 1 year ago

Another possible feature would be the ability to override the url of the endpoint used. If, for some reason, you didn't want to post to /wp-json/wp/v2/search?include= but you could provide another endpoint that behaved in the same way (accepted the same parameters, returned the same data format), then you could pass a custom url. The default would be to use the search url if nothing is provided. Some real world examples would be:

jakewrfoster commented 1 year ago

As someone who has struggled with the inherent complexities of the post selector, I love this enhancement request. Big plus 1 to restricting the PostPicker to 1 selection at a time. Not only would that reduce technical complexity, I agree that it also makes more editorial sense.

mogmarsh commented 1 year ago

In addition to searching, we've had a request for showing the most recent posts. Maybe this can all be done in a modal similar to the media modal - show most recent by default with a search field as well.

kevinfodness commented 1 year ago

Summarizing the discussion above, we should have v2 only select one post. I'll update the issue description accordingly.

kevinfodness commented 1 year ago

Adding another request here so it doesn't get lost - please include the post type alongside the post title in the results in some way.

mboynes commented 1 year ago

Closing this since #133 was merged