From 686db17207f2db1fbba249a8188acace65da88ed Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Wed, 28 Aug 2024 13:00:47 +0530 Subject: [PATCH] Social: Default to the current post ID for share status --- ...fault-to-the-current-post-id-for-share-status | 4 ++++ .../src/social-store/resolvers.js | 11 +++++------ .../src/social-store/selectors/share-status.ts | 16 ++++++++++++---- 3 files changed, 21 insertions(+), 10 deletions(-) create mode 100644 projects/js-packages/publicize-components/changelog/update-social-default-to-the-current-post-id-for-share-status diff --git a/projects/js-packages/publicize-components/changelog/update-social-default-to-the-current-post-id-for-share-status b/projects/js-packages/publicize-components/changelog/update-social-default-to-the-current-post-id-for-share-status new file mode 100644 index 0000000000000..6cc4929fea690 --- /dev/null +++ b/projects/js-packages/publicize-components/changelog/update-social-default-to-the-current-post-id-for-share-status @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Social: Default to the current post ID for share status selector diff --git a/projects/js-packages/publicize-components/src/social-store/resolvers.js b/projects/js-packages/publicize-components/src/social-store/resolvers.js index d251cf764d01e..a9e5787ea35a9 100644 --- a/projects/js-packages/publicize-components/src/social-store/resolvers.js +++ b/projects/js-packages/publicize-components/src/social-store/resolvers.js @@ -63,15 +63,14 @@ export function getConnections() { /** * Resolves the post share status. * - * @param {number} postId - The post ID. + * @param {number} _postId - The post ID. * * @return {Function} Resolver */ -export function getPostShareStatus( postId ) { - return async ( { dispatch } ) => { - if ( ! postId ) { - return; - } +export function getPostShareStatus( _postId ) { + return async ( { dispatch, registry } ) => { + // Default to the current post ID if none is provided. + const postId = _postId || registry.select( editorStore ).getCurrentPostId(); try { dispatch( fetchPostShareStatus( postId ) ); diff --git a/projects/js-packages/publicize-components/src/social-store/selectors/share-status.ts b/projects/js-packages/publicize-components/src/social-store/selectors/share-status.ts index 657e42ddee929..814352c30352e 100644 --- a/projects/js-packages/publicize-components/src/social-store/selectors/share-status.ts +++ b/projects/js-packages/publicize-components/src/social-store/selectors/share-status.ts @@ -1,3 +1,5 @@ +import { createRegistrySelector } from '@wordpress/data'; +import { store as editorStore } from '@wordpress/editor'; import { PostShareStatus, SocialStoreState } from '../types'; /** @@ -6,8 +8,14 @@ import { PostShareStatus, SocialStoreState } from '../types'; * @param {SocialStoreState} state - State object. * @param {number} postId - The post ID. * - * @return {SocialStoreState[number]} - The post share status. + * @return {PostShareStatus} - The post share status. */ -export function getPostShareStatus( state: SocialStoreState, postId: number ): PostShareStatus { - return state.shareStatus?.[ postId ] ?? { shares: [] }; -} +export const getPostShareStatus = createRegistrySelector( + select => + ( state: SocialStoreState, postId?: number ): PostShareStatus => { + // Default to the current post ID if none is provided. + const id = postId || select( editorStore ).getCurrentPostId(); + + return state.shareStatus?.[ id ] ?? { shares: [] }; + } +);