diff --git a/packages/block-library/src/gallery/v1/edit.js b/packages/block-library/src/gallery/v1/edit.js index 7f3852903aa23..2ac1df11c7279 100644 --- a/packages/block-library/src/gallery/v1/edit.js +++ b/packages/block-library/src/gallery/v1/edit.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { filter, find, get, isEmpty, map, reduce } from 'lodash'; +import { filter, find, get, isEmpty, map } from 'lodash'; /** * WordPress dependencies @@ -98,34 +98,29 @@ function GalleryEdit( props ) { const resizedImages = useMemo( () => { if ( isSelected ) { - return reduce( - attributes.ids, + return ( attributes.ids ?? [] ).reduce( ( currentResizedImages, id ) => { if ( ! id ) { return currentResizedImages; } const image = getMedia( id ); - const sizes = reduce( - imageSizes, - ( currentSizes, size ) => { - const defaultUrl = get( image, [ - 'sizes', - size.slug, - 'url', - ] ); - const mediaDetailsUrl = get( image, [ - 'media_details', - 'sizes', - size.slug, - 'source_url', - ] ); - return { - ...currentSizes, - [ size.slug ]: defaultUrl || mediaDetailsUrl, - }; - }, - {} - ); + const sizes = imageSizes.reduce( ( currentSizes, size ) => { + const defaultUrl = get( image, [ + 'sizes', + size.slug, + 'url', + ] ); + const mediaDetailsUrl = get( image, [ + 'media_details', + 'sizes', + size.slug, + 'source_url', + ] ); + return { + ...currentSizes, + [ size.slug ]: defaultUrl || mediaDetailsUrl, + }; + }, {} ); return { ...currentResizedImages, [ parseInt( id, 10 ) ]: sizes,