-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathuse-get-media.native.js
45 lines (41 loc) · 1.34 KB
/
use-get-media.native.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
const EMPTY_IMAGE_MEDIA = [];
/**
* Retrieves the extended media info for each gallery image from the store. This is used to
* determine which image size options are available for the current gallery.
*
* @param {Array} innerBlockImages An array of the innerBlock images currently in the gallery.
*
* @return {Array} An array of media info options for each gallery image.
*/
export default function useGetMedia( innerBlockImages = [] ) {
return useSelect(
( select ) => {
const imagesUploading = innerBlockImages.some(
( { attributes } ) => attributes?.url?.indexOf( 'file:' ) === 0
);
const imageIds = innerBlockImages
.filter( ( { attributes } ) => {
const { id, url } = attributes;
return id !== undefined && url?.indexOf( 'file:' ) !== 0;
} )
.map( ( imageBlock ) => imageBlock.attributes.id );
if ( imageIds.length === 0 || imagesUploading ) {
return EMPTY_IMAGE_MEDIA;
}
return (
select( coreStore ).getMediaItems( {
include: imageIds.join( ',' ),
per_page:
imageIds.length /* 'hard' limit necessary as unbounded queries aren't supported on native */,
orderby: 'include',
} ) ?? EMPTY_IMAGE_MEDIA
);
},
[ innerBlockImages ]
);
}