import { Button } from '@wordpress/components';
import { useTranslate } from 'i18n-calypso';
import { useEffect, useState } from 'react';
import Gravatar from 'calypso/components/gravatar';
import wpcom from 'calypso/lib/wp';
import SocialToS from '../authentication/social/social-tos';
import './continue-as-user.scss';
// Validate redirect URL using the REST endpoint.
// Return validated URL in case of success, `null` in case of failure.
function useValidatedURL( redirectUrl ) {
const [ url, setURL ] = useState( '' );
const [ isLoading, setIsLoading ] = useState( false );
useEffect( () => {
if ( redirectUrl ) {
setIsLoading( true );
wpcom.req
.get( '/me/validate-redirect', { redirect_url: redirectUrl } )
.then( ( res ) => {
setURL( res?.redirect_to );
setIsLoading( false );
} )
.catch( () => {
setURL( null );
setIsLoading( false );
} );
}
}, [ redirectUrl ] );
return { url, loading: isLoading && !! redirectUrl };
}
export default function ContinueAsUser( {
currentUser,
onChangeAccount,
redirectPath,
isWoo,
isBlazePro,
} ) {
const translate = useTranslate();
const { url: validatedPath, loading: validatingPath } = useValidatedURL( redirectPath );
const userName = currentUser.display_name || currentUser.username;
// Render ContinueAsUser straight away, even before validation.
// This helps avoid jarring layout shifts. It's not ideal that the link URL changes transparently
// like that, but it is better than the alternative, and in practice it should happen quicker than
// the user can notice.
const notYouText = translate( 'Not you?{{br/}}Log in with {{link}}another account{{/link}}', {
components: {
br:
,
link: (
),
},
args: { userName },
comment: 'Link to continue login as different user',
} );
const gravatarLink = (