Skip to content

Commit

Permalink
[RNMobile] Cover case of GUID being empty after upload finish (#30130)
Browse files Browse the repository at this point in the history
* Fetch GUID manually in case it's empty upon upload finish

* Add changelog

* Bump version to 0.13.9-alpha
  • Loading branch information
fluiddot authored Apr 18, 2023
1 parent 6a61749 commit 53a8457
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

VideoPress block: Cover case of GUID being empty after upload finish.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import { MediaUploadProgress, VIDEO_ASPECT_RATIO } from '@wordpress/block-editor';
import { Icon } from '@wordpress/components';
import { usePreferredColorSchemeStyle } from '@wordpress/compose';
import { useCallback, useState, Platform } from '@wordpress/element';
import { store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { useCallback, useEffect, useState, Platform } from '@wordpress/element';
import {
requestImageFailedRetryDialog,
requestImageUploadCancelDialog,
Expand Down Expand Up @@ -43,6 +45,30 @@ const UploaderProgress = ( { file, onDone, onReset, isInteractionDisabled } ) =>
);

const [ isUploadFailed, setIsUploadFailed ] = useState( false );
const [ isFetchingGUID, setIsFetchingGUID ] = useState( false );
const [ mediaServerID, setMediaServerID ] = useState();

// In case the GUID is empty, we fetch it manually via `media` endpoint.
const { media } = useSelect(
select => ( {
media:
! isFetchingGUID || typeof mediaServerID === 'undefined'
? undefined
: select( coreStore ).getMedia( mediaServerID ),
} ),
[ isFetchingGUID, mediaServerID ]
);
useEffect( () => {
if ( isFetchingGUID && media ) {
const { jetpack_videopress_guid: videopressGUID } = media;
setIsFetchingGUID( false );
setMediaServerID( undefined );
onDone( {
id: mediaServerID,
guid: videopressGUID,
} );
}
}, [ isFetchingGUID, mediaServerID, media ] );

const onPress = useCallback( () => {
if ( isUploadFailed ) {
Expand All @@ -64,6 +90,16 @@ const UploaderProgress = ( { file, onDone, onReset, isInteractionDisabled } ) =>
android: metadata?.videopressGUID,
ios: metadata?.id,
} );

// In most cases the VideoPress GUID will be returned upon upload finish.
// However, since the video is asynchronously added to VideoPress, there's
// a chance that GUID could be empty. For this case, we'll fetch it manually.
if ( ! guid ) {
setMediaServerID( mediaServerId );
setIsFetchingGUID( true );
return;
}

onDone( { id: mediaServerId, guid } );
},
[ onDone ]
Expand Down

0 comments on commit 53a8457

Please sign in to comment.