Skip to content

Commit

Permalink
don't update the value that comes from the useSelect
Browse files Browse the repository at this point in the history
  • Loading branch information
scruffian committed Dec 8, 2023
1 parent 2d3ba7f commit 85161be
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions packages/block-library/src/post-featured-image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
store as blockEditorStore,
__experimentalUseBorderProps as useBorderProps,
} from '@wordpress/block-editor';
import { useMemo } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { upload } from '@wordpress/icons';
import { store as noticesStore } from '@wordpress/notices';
Expand Down Expand Up @@ -67,7 +68,8 @@ export default function PostFeaturedImageEdit( {
linkTarget,
useFirstImageFromPost,
} = attributes;
let [ featuredImage, setFeaturedImage ] = useEntityProp(

const [ storedFeaturedImage, setFeaturedImage ] = useEntityProp(
'postType',
postTypeSlug,
'featured_media',
Expand All @@ -83,22 +85,32 @@ export default function PostFeaturedImageEdit( {
postId
);

if ( ! featuredImage && useFirstImageFromPost && postContent ) {
const featuredImage = useMemo( () => {
if ( storedFeaturedImage ) {
return storedFeaturedImage;
}

if ( ! useFirstImageFromPost ) {
return;
}

const firstImageCloser = /<!--\s+\/wp:(?:core\/)?image\s+-->/.exec(
postContent
);
const content = firstImageCloser
? postContent.slice(
0,
firstImageCloser.index + firstImageCloser[ 0 ].length
)
: '';

if ( ! firstImageCloser ) {
return;
}

const content = postContent.slice(
0,
firstImageCloser.index + firstImageCloser[ 0 ].length
);

const blocks = parse( content );
const imageBlock = blocks.find( ( { name } ) => name === 'core/image' );
if ( imageBlock?.attributes?.id ) {
featuredImage = imageBlock.attributes.id;
}
}
return imageBlock?.attributes?.id;
}, [ storedFeaturedImage, useFirstImageFromPost, postContent ] );

const { media, postType, postPermalink } = useSelect(
( select ) => {
Expand Down

0 comments on commit 85161be

Please sign in to comment.