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
Expand Up @@ -316,7 +316,7 @@ export const setGooglePhotosPickeCachedSessionId = ( sessionId: string | null )
sessionId,
604800, // 7 days
'/',
`.${ window.location.hostname }`
`.${ window.location.hostname.split( '.' ).slice( -2 ).join( '.' ) }`
);
};

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
Expand Up @@ -5,6 +5,7 @@ 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,35 +16,73 @@ function GooglePhotos( props ) {
createPickerSession,
fetchPickerSession,
getPickerStatus,
setAuthenticated,
} = props;

const [ cachedSessionId ] = useState( getGooglePhotosPickerCachedSessionId() );
const [ isCachedSessionChecked, setIsCachedSessionChecked ] = useState( false );
const [ pickerFeatureEnabled, setPickerFeatureEnabled ] = useState( null );
const [ isCachedSessionChecked, setIsCachedSessionChecked ] = useState( false );
const [ isAuthUpgradeRequired, setIsAuthUpgradeRequired ] = useState( false );

const isLoadingState = pickerFeatureEnabled === null;
const isPickerSessionAccurate = pickerSession !== null && ! ( 'code' in pickerSession );
const isSessionExpired =
pickerSession?.expireTime && moment( pickerSession.expireTime ).isBefore( new Date() );

// Check if the picker feature is enabled and the connection status
useEffect( () => {
getPickerStatus().then( feature => {
feature && setPickerFeatureEnabled( feature.enabled );
getPickerStatus().then( picker => {
setPickerFeatureEnabled( picker.enabled );

switch ( picker.connection_status ) {
case 'ok':
setAuthenticated( true );
setIsAuthUpgradeRequired( false );
break;

case 'invalid':
setAuthenticated( true );
setIsAuthUpgradeRequired( true );
break;

case 'not_connected':
setAuthenticated( false );
setIsAuthUpgradeRequired( false );
break;
}
} );
}, [ isAuthenticated, getPickerStatus, setAuthenticated ] );

cachedSessionId === null && setIsCachedSessionChecked( true );
cachedSessionId &&
fetchPickerSession( cachedSessionId ).then( () => setIsCachedSessionChecked( true ) );
}, [ getPickerStatus, fetchPickerSession, cachedSessionId ] );
// Check if the user has a cached session
useEffect( () => {
if ( pickerFeatureEnabled && isAuthenticated && ! isAuthUpgradeRequired ) {
Promise.resolve( cachedSessionId )
.then( id => ( id ? fetchPickerSession( id ) : id ) )
.finally( () => setIsCachedSessionChecked( true ) );
}
}, [
isAuthenticated,
pickerFeatureEnabled,
isAuthUpgradeRequired,
cachedSessionId,
fetchPickerSession,
] );

// Create a new picker session if the cached session is not accurate
// or if the session has expired
useEffect( () => {
if (
pickerFeatureEnabled &&
isCachedSessionChecked &&
isAuthenticated &&
! isAuthUpgradeRequired &&
( ! isPickerSessionAccurate || isSessionExpired )
) {
createPickerSession();
}
}, [
pickerFeatureEnabled,
isAuthUpgradeRequired,
isCachedSessionChecked,
isPickerSessionAccurate,
isAuthenticated,
Expand All @@ -52,14 +91,18 @@ function GooglePhotos( props ) {
pickerSession,
] );

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

if ( ! isAuthenticated ) {
return <GooglePhotosAuth { ...props } />;
}

if ( isAuthUpgradeRequired ) {
return <GooglePhotosAuthUpgrade { ...props } />;
}

if ( pickerFeatureEnabled && ! pickerSession?.mediaItemsSet ) {
return <GooglePhotosPickerButton { ...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