Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/block-library/src/utils/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,20 @@ export function useCanEditEntity( kind, name, recordId ) {
*/
export function useUploadMediaFromBlobURL( args = {} ) {
const latestArgs = useRef( args );
const hasUploadStarted = useRef( false );
const { getSettings } = useSelect( blockEditorStore );

useLayoutEffect( () => {
latestArgs.current = args;
} );

useEffect( () => {
// Uploading is a special effect that can't be canceled via the cleanup method.
// The extra check avoids duplicate uploads in development mode (React.StrictMode).
if ( hasUploadStarted.current ) {
return;
}

if (
! latestArgs.current.url ||
! isBlobURL( latestArgs.current.url )
Expand All @@ -55,6 +62,8 @@ export function useUploadMediaFromBlobURL( args = {} ) {
const { url, allowedTypes, onChange, onError } = latestArgs.current;
const { mediaUpload } = getSettings();

hasUploadStarted.current = true;

mediaUpload( {
filesList: [ file ],
allowedTypes,
Expand All @@ -65,10 +74,12 @@ export function useUploadMediaFromBlobURL( args = {} ) {

revokeBlobURL( url );
onChange( media );
hasUploadStarted.current = false;
},
onError: ( message ) => {
revokeBlobURL( url );
onError( message );
hasUploadStarted.current = false;
},
} );
}, [ getSettings ] );
Expand Down