session-desktop/ts/components/session/registration/SignInTab.tsx

208 lines
6.4 KiB
TypeScript
Raw Normal View History

2021-08-18 03:46:23 +02:00
import React, { useContext, useState } from 'react';
import { Flex } from '../../basic/Flex';
import { SpacerLG } from '../../basic/Text';
2021-04-22 10:03:58 +02:00
import { SessionButton, SessionButtonColor, SessionButtonType } from '../SessionButton';
import { SessionSpinner } from '../SessionSpinner';
2021-08-18 03:54:53 +02:00
import {
RegistrationContext,
RegistrationPhase,
signInWithLinking,
signInWithRecovery,
} from './RegistrationStages';
2021-02-26 05:29:30 +01:00
import { RegistrationUserDetails } from './RegistrationUserDetails';
import { GoBackMainMenuButton } from './SignUpTab';
2021-02-26 05:29:30 +01:00
import { TermsAndConditions } from './TermsAndConditions';
export enum SignInMode {
Default,
UsingRecoveryPhrase,
LinkDevice,
}
// tslint:disable: use-simple-attributes
// tslint:disable: react-unused-props-and-state
2021-02-26 05:29:30 +01:00
const LinkDeviceButton = (props: { onLinkDeviceButtonClicked: () => any }) => {
return (
<SessionButton
onClick={props.onLinkDeviceButtonClicked}
buttonType={SessionButtonType.BrandOutline}
buttonColor={SessionButtonColor.Green}
text={window.i18n('linkDevice')}
/>
);
};
2021-04-22 10:03:58 +02:00
const RestoreUsingRecoveryPhraseButton = (props: { onRecoveryButtonClicked: () => any }) => {
2021-02-26 05:29:30 +01:00
return (
<SessionButton
onClick={props.onRecoveryButtonClicked}
buttonType={SessionButtonType.BrandOutline}
buttonColor={SessionButtonColor.Green}
text={window.i18n('restoreUsingRecoveryPhrase')}
dataTestId="restore-using-recovery"
2021-02-26 05:29:30 +01:00
/>
);
};
const ContinueYourSessionButton = (props: {
handleContinueYourSessionClick: () => any;
2021-03-01 02:05:39 +01:00
disabled: boolean;
2021-02-26 05:29:30 +01:00
}) => {
return (
<SessionButton
onClick={props.handleContinueYourSessionClick}
buttonType={SessionButtonType.Brand}
buttonColor={SessionButtonColor.Green}
text={window.i18n('continueYourSession')}
2021-03-01 02:05:39 +01:00
disabled={props.disabled}
2021-02-26 05:29:30 +01:00
/>
);
};
2021-03-01 02:05:39 +01:00
const SignInContinueButton = (props: {
2021-02-26 05:29:30 +01:00
signInMode: SignInMode;
2021-03-01 02:05:39 +01:00
disabled: boolean;
2021-02-26 05:29:30 +01:00
handleContinueYourSessionClick: () => any;
}) => {
if (props.signInMode === SignInMode.Default) {
2021-11-08 01:03:08 +01:00
return null;
2021-02-26 05:29:30 +01:00
}
return (
<ContinueYourSessionButton
handleContinueYourSessionClick={props.handleContinueYourSessionClick}
2021-03-01 02:05:39 +01:00
disabled={props.disabled}
2021-02-26 05:29:30 +01:00
/>
);
};
2021-03-01 02:05:39 +01:00
const SignInButtons = (props: {
signInMode: SignInMode;
onRecoveryButtonClicked: () => any;
onLinkDeviceButtonClicked: () => any;
}) => {
if (props.signInMode !== SignInMode.Default) {
2021-11-08 01:03:08 +01:00
return null;
2021-03-01 02:05:39 +01:00
}
return (
<div>
2021-04-22 10:03:58 +02:00
<RestoreUsingRecoveryPhraseButton onRecoveryButtonClicked={props.onRecoveryButtonClicked} />
<SpacerLG />
2021-04-22 10:03:58 +02:00
<LinkDeviceButton onLinkDeviceButtonClicked={props.onLinkDeviceButtonClicked} />
2021-03-01 02:05:39 +01:00
</div>
);
};
2021-08-18 03:46:23 +02:00
export const SignInTab = () => {
const { setRegistrationPhase, signInMode, setSignInMode } = useContext(RegistrationContext);
2021-08-16 01:27:29 +02:00
2021-02-26 05:29:30 +01:00
const [recoveryPhrase, setRecoveryPhrase] = useState('');
2021-04-22 10:03:58 +02:00
const [recoveryPhraseError, setRecoveryPhraseError] = useState(undefined as string | undefined);
2021-02-26 05:29:30 +01:00
const [displayName, setDisplayName] = useState('');
const [displayNameError, setDisplayNameError] = useState<string | undefined>('');
const [loading, setIsLoading] = useState(false);
2021-02-26 05:29:30 +01:00
2021-03-01 02:05:39 +01:00
const isRecovery = signInMode === SignInMode.UsingRecoveryPhrase;
const isLinking = signInMode === SignInMode.LinkDevice;
2021-02-26 05:29:30 +01:00
const showTermsAndConditions = signInMode !== SignInMode.Default;
2021-03-01 02:05:39 +01:00
// show display name input only if we are trying to recover from seed.
// We don't need a display name when we link a device, as the display name
// from the configuration message will be used.
const showDisplayNameField = isRecovery;
// Display name is required only on isRecoveryMode
2021-04-22 10:03:58 +02:00
const displayNameOK = (isRecovery && !displayNameError && !!displayName) || isLinking;
2021-03-01 02:05:39 +01:00
// Seed is mandatory no matter which mode
const seedOK = recoveryPhrase && !recoveryPhraseError;
const activateContinueButton = seedOK && displayNameOK && !loading;
2021-03-01 02:05:39 +01:00
2021-03-02 06:49:41 +01:00
const continueYourSession = async () => {
if (isRecovery) {
await signInWithRecovery({
displayName,
userRecoveryPhrase: recoveryPhrase,
});
} else if (isLinking) {
setIsLoading(true);
await signInWithLinking({
userRecoveryPhrase: recoveryPhrase,
});
setIsLoading(false);
}
};
2021-02-26 05:29:30 +01:00
return (
<div className="session-registration__content">
{signInMode !== SignInMode.Default && (
<>
<GoBackMainMenuButton />
<RegistrationUserDetails
showDisplayNameField={showDisplayNameField}
showSeedField={true}
displayName={displayName}
handlePressEnter={continueYourSession}
onDisplayNameChanged={(name: string) => {
const sanitizedName = name.replace(window.displayNameRegex, '');
const trimName = sanitizedName.trim();
setDisplayName(sanitizedName);
setDisplayNameError(!trimName ? window.i18n('displayNameEmpty') : undefined);
}}
onSeedChanged={(seed: string) => {
setRecoveryPhrase(seed);
setRecoveryPhraseError(!seed ? window.i18n('recoveryPhraseEmpty') : undefined);
}}
recoveryPhrase={recoveryPhrase}
stealAutoFocus={true}
/>
</>
2021-02-26 05:29:30 +01:00
)}
<SignInButtons
signInMode={signInMode}
onRecoveryButtonClicked={() => {
2021-08-16 01:27:29 +02:00
setRegistrationPhase(RegistrationPhase.SignIn);
2021-02-26 05:29:30 +01:00
setSignInMode(SignInMode.UsingRecoveryPhrase);
setRecoveryPhrase('');
setDisplayName('');
setIsLoading(false);
2021-02-26 05:29:30 +01:00
}}
onLinkDeviceButtonClicked={() => {
2021-08-16 01:27:29 +02:00
setRegistrationPhase(RegistrationPhase.SignIn);
2021-02-26 05:29:30 +01:00
setSignInMode(SignInMode.LinkDevice);
setRecoveryPhrase('');
setDisplayName('');
setIsLoading(false);
2021-02-26 05:29:30 +01:00
}}
2021-03-01 02:05:39 +01:00
/>
<SignInContinueButton
signInMode={signInMode}
2021-03-02 06:49:41 +01:00
handleContinueYourSessionClick={continueYourSession}
2021-03-01 02:05:39 +01:00
disabled={!activateContinueButton}
2021-02-26 05:29:30 +01:00
/>
{loading && (
<Flex
container={true}
justifyContent="center"
alignItems="center"
style={{
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
pointerEvents: 'all',
backgroundColor: '#00000088',
}}
>
<SessionSpinner loading={true} />
</Flex>
)}
2021-02-26 05:29:30 +01:00
{showTermsAndConditions && <TermsAndConditions />}
</div>
);
};