File size: 4,287 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | 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: <br />,
link: (
<Button
variant="link"
id="loginAsAnotherUser"
className="continue-as-user__change-user-link"
onClick={ onChangeAccount }
/>
),
},
args: { userName },
comment: 'Link to continue login as different user',
} );
const gravatarLink = (
<div className="continue-as-user__gravatar-content">
<Gravatar
user={ currentUser }
className="continue-as-user__gravatar"
imgSize={ 400 }
size={ 110 }
/>
<div className="continue-as-user__username">{ userName }</div>
<div className="continue-as-user__email">{ currentUser.email }</div>
</div>
);
if ( isWoo ) {
return (
<div className="continue-as-user">
<div className="continue-as-user__user-info">
{ gravatarLink }
<div className="continue-as-user__not-you">
<Button
variant="tertiary"
id="loginAsAnotherUser"
className="continue-as-user__change-user-link"
onClick={ onChangeAccount }
>
{ translate( 'Sign in as a different user' ) }
</Button>
</div>
</div>
<Button
variant="primary"
className="continue-as-user__continue-button"
isBusy={ validatingPath }
href={ validatedPath || '/' }
__next40pxDefaultSize
>
{ translate( 'Continue' ) }
</Button>
</div>
);
}
if ( isBlazePro ) {
return (
<div className="continue-as-user">
<div className="continue-as-user__user-info">
{ gravatarLink }
<div className="continue-as-user__not-you">
<Button
variant="tertiary"
id="loginAsAnotherUser"
className="continue-as-user__change-user-link"
onClick={ onChangeAccount }
>
{ translate( 'Sign in as a different user' ) }
</Button>
</div>
</div>
<Button
variant="primary"
className="continue-as-user__continue-button"
isBusy={ validatingPath }
href={ validatedPath || '/' }
__next40pxDefaultSize
>
{ `${ translate( 'Continue as', {
context: 'Continue as an existing WordPress.com user',
} ) } ${ userName }` }
</Button>
<SocialToS />
</div>
);
}
return (
<div className="continue-as-user">
<div className="continue-as-user__user-info">
{ gravatarLink }
<Button
variant="primary"
isBusy={ validatingPath }
href={ validatedPath || '/' }
__next40pxDefaultSize
className="continue-as-user__continue-button"
>
{ translate( 'Continue' ) }
</Button>
</div>
<div className="continue-as-user__not-you">{ notYouText }</div>
</div>
);
}
|