Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External media: Show connection upgrade screen in transition from Photos to Picker #40416

Merged
merged 12 commits into from
Dec 5, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Cover case with connection transition from Google Photos to Google Photos Picker
Original file line number Diff line number Diff line change
Expand Up @@ -568,13 +568,13 @@ $grid-size: 8px;
}

.jetpack-external-media-auth {
max-width: 340px;
max-width: 400px;
margin: 0 auto;
padding-bottom: 80px;
text-align: center;

p {
margin: 2em 0;
margin: 0 0 2em 0;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { __ } from '@wordpress/i18n';
import { GooglePhotosLogo } from '../../../icons';
import GooglePhotosDisconnect from './google-photos-disconnect';

export default function GooglePhotosAuthUpgrade( props ) {
const { setAuthenticated } = props;

return (
<div className="jetpack-external-media-auth">
<GooglePhotosLogo />

<p>
{ __(
"We've updated our Google Photos service. You will need to disconnect and reconnect to continue accessing your photos.",
'jetpack'
) }
</p>

<GooglePhotosDisconnect setAuthenticated={ setAuthenticated } />
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function GooglePhotosPickerButton( props ) {

useEffect( () => {
const interval = setInterval( () => {
pickerSession.id && fetchPickerSession( pickerSession.id );
pickerSession?.id && fetchPickerSession( pickerSession.id );
}, 3000 );
return () => clearInterval( interval );
}, [ fetchPickerSession, pickerSession?.id ] );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import moment from 'moment';
import { useEffect, useState } from 'react';
import { useEffect, useState, useCallback } from 'react';
import MediaLoadingPlaceholder from '../../media-browser/placeholder';
import { getGooglePhotosPickerCachedSessionId } from '../../media-service';
import { MediaSource } from '../../media-service/types';
import withMedia from '../with-media';
import GooglePhotosAuth from './google-photos-auth';
import GooglePhotosAuthUpgrade from './google-photos-auth-upgrade';
import GooglePhotosMedia from './google-photos-media';
import GooglePhotosPickerButton from './google-photos-picker-button';

Expand All @@ -15,23 +16,50 @@ function GooglePhotos( props ) {
createPickerSession,
fetchPickerSession,
getPickerStatus,
setAuthenticated,
} = props;
const [ cachedSessionId ] = useState( getGooglePhotosPickerCachedSessionId() );
const [ isCachedSessionChecked, setIsCachedSessionChecked ] = useState( false );
const [ pickerFeatureEnabled, setPickerFeatureEnabled ] = useState( null );
const [ authUpgradeRequired, setAuthUpgradeRequired ] = useState( false );
const isPickerSessionAccurate = pickerSession !== null && ! ( 'code' in pickerSession );
const isSessionExpired =
pickerSession?.expireTime && moment( pickerSession.expireTime ).isBefore( new Date() );

const catchAuthErrors = useCallback(
error => {
if ( error.code === 'authorization_required' ) {
setAuthenticated( false );
}

// If the picker session endpoint returns a 404
// the user needs to upgrade their auth
if ( error.code === 'rest_not_found' ) {
setAuthUpgradeRequired( true );
}
},
[ setAuthenticated, setAuthUpgradeRequired ]
);

useEffect( () => {
! isAuthenticated && setAuthUpgradeRequired( false );
}, [ isAuthenticated ] );

useEffect( () => {
getPickerStatus().then( feature => {
feature && setPickerFeatureEnabled( feature.enabled );
} );

cachedSessionId === null && setIsCachedSessionChecked( true );
ouikhuan marked this conversation as resolved.
Show resolved Hide resolved
cachedSessionId &&
fetchPickerSession( cachedSessionId ).then( () => setIsCachedSessionChecked( true ) );
}, [ getPickerStatus, fetchPickerSession, cachedSessionId ] );
fetchPickerSession( cachedSessionId )
ouikhuan marked this conversation as resolved.
Show resolved Hide resolved
.then( () => {
setIsCachedSessionChecked( true );
} )
.catch( error => {
setIsCachedSessionChecked( true );
catchAuthErrors( error );
} );
}, [ getPickerStatus, fetchPickerSession, cachedSessionId, catchAuthErrors ] );

useEffect( () => {
if (
Expand All @@ -40,7 +68,7 @@ function GooglePhotos( props ) {
isAuthenticated &&
( ! isPickerSessionAccurate || isSessionExpired )
) {
createPickerSession();
createPickerSession().catch( catchAuthErrors );
}
}, [
pickerFeatureEnabled,
Expand All @@ -50,12 +78,17 @@ function GooglePhotos( props ) {
isSessionExpired,
createPickerSession,
pickerSession,
catchAuthErrors,
] );

if ( pickerFeatureEnabled === null || ! isCachedSessionChecked ) {
return <MediaLoadingPlaceholder />;
}

if ( authUpgradeRequired ) {
return <GooglePhotosAuthUpgrade { ...props } />;
}
bogiii marked this conversation as resolved.
Show resolved Hide resolved

if ( ! isAuthenticated ) {
return <GooglePhotosAuth { ...props } />;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,24 @@ export default function withMedia( mediaSource = MediaSource.Unknown ) {
.then( session => {
setGooglePhotosPickerSession( session );
return session;
} )
.catch( this.handleApiError );
} );
};

fetchPickerSession = sessionId => {
return apiFetch( {
path: `/wpcom/v2/external-media/session/google_photos/${ sessionId }`,
method: 'GET',
} ).then( setGooglePhotosPickerSession );
} )
.then( response => {
if ( 'code' in response ) {
throw response;
}
return response;
} )
.then( session => {
setGooglePhotosPickerSession( session );
return session;
} );
};

deletePickerSession = ( sessionId, updateState = true ) => {
Expand Down
Loading