Skip to content
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

useShortCodeTransform: Fetch media data in single request #40945

Merged
merged 2 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/block-library/src/gallery/use-get-media.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default function useGetMedia( innerBlockImages ) {
return select( coreStore ).getMediaItems( {
include: imageIds.join( ',' ),
per_page: -1,
orderby: 'include',
Copy link
Member Author

@Mamaduka Mamaduka May 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way, data cache keys are the same, so we re-use data from the store instead of re-fetching it.

} );
},
[ innerBlockImages ]
Expand Down
36 changes: 19 additions & 17 deletions packages/block-library/src/gallery/use-short-code-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,13 @@ export default function useShortCodeTransform( shortCodeTransforms ) {
if ( ! shortCodeTransforms || shortCodeTransforms.length === 0 ) {
return;
}
const getMedia = select( coreStore ).getMedia;
return shortCodeTransforms.map( ( image ) => {
const imageData = getMedia( image.id );
if ( imageData ) {
return {
id: imageData.id,
type: 'image',
url: imageData.source_url,
mime: imageData.mime_type,
alt: imageData.alt_text,
link: imageData.link,
caption: imageData?.caption?.raw,
};
}
return undefined;

const imageIds = shortCodeTransforms.map( ( image ) => image.id );

return select( coreStore ).getMediaItems( {
include: imageIds.join( ',' ),
per_page: -1,
orderby: 'include',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need this to preserve the original image order from the shortcode.

} );
},
[ shortCodeTransforms ]
Expand All @@ -47,7 +39,17 @@ export default function useShortCodeTransform( shortCodeTransforms ) {
return;
}

if ( every( newImageData, ( img ) => img && img.url ) ) {
return newImageData;
if ( every( newImageData, ( img ) => img && img.source_url ) ) {
return newImageData.map( ( imageData ) => {
return {
id: imageData.id,
type: 'image',
url: imageData.source_url,
mime: imageData.mime_type,
alt: imageData.alt_text,
link: imageData.link,
caption: imageData?.caption?.raw,
};
} );
Comment on lines +43 to +53
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this outside of the mapSelect function to avoid returning a new array reference and causing unnecessary re-renders.

}
}