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

[TS migration] Migrate 'SettingsSecurity2FA' page to TypeScript #35359

Merged
merged 24 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/hooks/useWindowDimensions/index.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default function (): WindowDimensions {
const isSmallScreenWidth = true;
const isMediumScreenWidth = false;
const isLargeScreenWidth = false;
const isExtraSmallScreenWidth = windowWidth <= variables.extraSmallMobileResponsiveWidthBreakpoint;

return {
windowWidth,
Expand All @@ -20,5 +21,6 @@ export default function (): WindowDimensions {
isSmallScreenWidth,
isMediumScreenWidth,
isLargeScreenWidth,
isExtraSmallScreenWidth,
};
}
2 changes: 2 additions & 0 deletions src/hooks/useWindowDimensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default function (): WindowDimensions {
const isSmallScreenWidth = windowWidth <= variables.mobileResponsiveWidthBreakpoint;
const isMediumScreenWidth = windowWidth > variables.mobileResponsiveWidthBreakpoint && windowWidth <= variables.tabletResponsiveWidthBreakpoint;
const isLargeScreenWidth = windowWidth > variables.tabletResponsiveWidthBreakpoint;
const isExtraSmallScreenWidth = windowWidth <= variables.extraSmallMobileResponsiveWidthBreakpoint;

return {
windowWidth,
Expand All @@ -22,5 +23,6 @@ export default function (): WindowDimensions {
isSmallScreenWidth,
isMediumScreenWidth,
isLargeScreenWidth,
isExtraSmallScreenWidth,
};
}
1 change: 1 addition & 0 deletions src/hooks/useWindowDimensions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type WindowDimensions = {
isSmallScreenWidth: boolean;
isMediumScreenWidth: boolean;
isLargeScreenWidth: boolean;
isExtraSmallScreenWidth: boolean;
};

export default WindowDimensions;
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@ import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import useThemeStyles from '@hooks/useThemeStyles';
import * as TwoFactorAuthActions from '@userActions/TwoFactorAuthActions';
import StepWrapperPropTypes from './StepWrapperPropTypes';
import type StepWrapperPropTypes from './StepWrapperPropTypes';
allgandalf marked this conversation as resolved.
Show resolved Hide resolved

function StepWrapper({
title = '',
stepCounter = null,
stepCounter,
onBackButtonPress = () => TwoFactorAuthActions.quitAndNavigateBack(),
children = null,
shouldEnableKeyboardAvoidingView = true,
onEntryTransitionEnd,
}) {
}: StepWrapperPropTypes) {
const styles = useThemeStyles();
const shouldShowStepCounter = Boolean(stepCounter);

const {animationDirection} = useAnimatedStepContext();
const { animationDirection } = useAnimatedStepContext();

return (
<ScreenWrapper
Expand All @@ -29,12 +27,11 @@ function StepWrapper({
>
<AnimatedStep
style={[styles.flex1]}
onAnimationEnd={onEntryTransitionEnd}
onAnimationEnd={onEntryTransitionEnd as () => void}
allgandalf marked this conversation as resolved.
Show resolved Hide resolved
direction={animationDirection}
>
<HeaderWithBackButton
title={title}
shouldShowStepCounter={shouldShowStepCounter}
stepCounter={stepCounter}
onBackButtonPress={onBackButtonPress}
/>
Expand All @@ -44,7 +41,6 @@ function StepWrapper({
);
}

StepWrapper.propTypes = StepWrapperPropTypes;
StepWrapper.displayName = 'StepWrapper';

export default StepWrapper;
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import PropTypes from 'prop-types';

export default {
type StepWrapperPropTypes = {
/** Title of the Header */
allgandalf marked this conversation as resolved.
Show resolved Hide resolved
title: PropTypes.string,
title?: string,

/** Data to display a step counter in the header */
stepCounter: PropTypes.shape({
stepCounter?: {
/** Current step */
allgandalf marked this conversation as resolved.
Show resolved Hide resolved
step: PropTypes.number,
step: number,
/** Total number of steps */
total: PropTypes.number,
total?: number,
/** Text to display next to the step counter */
text: PropTypes.string,
}),
text?: string,
},

/** Method to trigger when pressing back button of the header */
onBackButtonPress: PropTypes.func,
onBackButtonPress?: () => void,

/** Called when navigated Screen's transition is finished. It does not fire when user exits the page. */
onEntryTransitionEnd: PropTypes.func,
onEntryTransitionEnd?: () => void,

/** Children components */
children: PropTypes.node,
children?: React.ReactNode,

allgandalf marked this conversation as resolved.
Show resolved Hide resolved
/** Flag to indicate if the keyboard avoiding view should be enabled */
shouldEnableKeyboardAvoidingView: PropTypes.bool,
};
shouldEnableKeyboardAvoidingView?: boolean,

}

export default StepWrapperPropTypes;
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, {useEffect, useState} from 'react';
import {ActivityIndicator, ScrollView, View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import React, { useEffect, useState } from 'react';
import { ActivityIndicator, ScrollView, View } from 'react-native';
import { withOnyx } from 'react-native-onyx';
import type { Route } from '@src/ROUTES';
import type { TranslationPaths } from '@src/languages/types';
import Button from '@components/Button';
import FixedFooter from '@components/FixedFooter';
import FormHelpMessage from '@components/FormHelpMessage';
Expand All @@ -18,23 +19,31 @@ import Clipboard from '@libs/Clipboard';
import localFileDownload from '@libs/localFileDownload';
import StepWrapper from '@pages/settings/Security/TwoFactorAuth/StepWrapper/StepWrapper';
import useTwoFactorAuthContext from '@pages/settings/Security/TwoFactorAuth/TwoFactorAuthContext/useTwoFactorAuth';
import {defaultAccount, TwoFactorAuthPropTypes} from '@pages/settings/Security/TwoFactorAuth/TwoFactorAuthPropTypes';
import type { TwoFactorAuthStepOnyxProps } from '@pages/settings/Security/TwoFactorAuth/TwoFactorAuthPropTypes';
import * as Session from '@userActions/Session';
import * as TwoFactorAuthActions from '@userActions/TwoFactorAuthActions';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';

function CodesStep({account = defaultAccount, backTo}) {
type CodesStepProps = TwoFactorAuthStepOnyxProps & {
/** The route to go back to when the user cancels */
allgandalf marked this conversation as resolved.
Show resolved Hide resolved
backTo: Route | undefined;
}

function CodesStep({
account,
backTo,
}: CodesStepProps) {
const theme = useTheme();
const styles = useThemeStyles();
const {translate} = useLocalize();
const {isExtraSmallScreenWidth, isSmallScreenWidth} = useWindowDimensions();
const { translate } = useLocalize();
const { isExtraSmallScreenWidth, isSmallScreenWidth } = useWindowDimensions();
allgandalf marked this conversation as resolved.
Show resolved Hide resolved
const [error, setError] = useState('');

const {setStep} = useTwoFactorAuthContext();
const { setStep } = useTwoFactorAuthContext();

useEffect(() => {
if (account.requiresTwoFactorAuth || account.recoveryCodes) {
if (account?.requiresTwoFactorAuth ?? account?.recoveryCodes) {
return;
allgandalf marked this conversation as resolved.
Show resolved Hide resolved
}
Session.toggleTwoFactorAuth(true);
Expand Down Expand Up @@ -62,23 +71,24 @@ function CodesStep({account = defaultAccount, backTo}) {
<View style={styles.mv3}>
<Text>{translate('twoFactorAuth.codesLoseAccess')}</Text>
</View>
<View style={styles.twoFactorAuthCodesBox({isExtraSmallScreenWidth, isSmallScreenWidth})}>
{account.isLoading ? (
<View style={styles.twoFactorAuthCodesBox({ isExtraSmallScreenWidth, isSmallScreenWidth })}>
{account?.isLoading ? (
<View style={styles.twoFactorLoadingContainer}>
<ActivityIndicator color={theme.spinner} />
</View>
) : (
<>
<View style={styles.twoFactorAuthCodesContainer}>
{Boolean(account.recoveryCodes) &&
_.map(account.recoveryCodes.split(', '), (code) => (
{Boolean(account?.recoveryCodes) &&
(account?.recoveryCodes?.split(', ').map((code) => (
<Text
style={styles.twoFactorAuthCode}
key={code}
>
{code}
</Text>
))}
)))}

</View>
<View style={styles.twoFactorAuthCodesButtonsContainer}>
<PressableWithDelayToggle
Expand All @@ -87,43 +97,49 @@ function CodesStep({account = defaultAccount, backTo}) {
icon={Expensicons.Copy}
inline={false}
onPress={() => {
Clipboard.setString(account.recoveryCodes);
Clipboard.setString(account?.recoveryCodes ?? '');
setError('');
TwoFactorAuthActions.setCodesAreCopied();
}}
styles={[styles.button, styles.buttonMedium, styles.twoFactorAuthCodesButton]}
textStyles={[styles.buttonMediumText]}
accessible={false}
tooltipText=''
tooltipTextChecked=''
allgandalf marked this conversation as resolved.
Show resolved Hide resolved
/>
<PressableWithDelayToggle
text={translate('common.download')}
icon={Expensicons.Download}
onPress={() => {
localFileDownload('two-factor-auth-codes', account.recoveryCodes);
localFileDownload('two-factor-auth-codes', account?.recoveryCodes ?? '');
setError('');
TwoFactorAuthActions.setCodesAreCopied();
}}
inline={false}
styles={[styles.button, styles.buttonMedium, styles.twoFactorAuthCodesButton]}
textStyles={[styles.buttonMediumText]}
accessible={false}
tooltipText=''
tooltipTextChecked=''
/>
</View>
</>
)}
</View>
</Section>
<FixedFooter style={[styles.mtAuto, styles.pt5]}>
{!_.isEmpty(error) && (
{Object.keys(error).length !== 0 && (
<FormHelpMessage
allgandalf marked this conversation as resolved.
Show resolved Hide resolved
isError
message={translate(error)}
message={translate(error as TranslationPaths)}
style={[styles.mb3]}
allgandalf marked this conversation as resolved.
Show resolved Hide resolved
/>
)}
<Button
success
text={translate('common.next')}
onPress={() => {
if (!account.codesAreCopied) {
if (!account?.codesAreCopied) {
setError('twoFactorAuth.errorStepCodes');
return;
}
Expand All @@ -136,10 +152,8 @@ function CodesStep({account = defaultAccount, backTo}) {
);
}

CodesStep.propTypes = TwoFactorAuthPropTypes;
CodesStep.displayName = 'CodesStep';

// eslint-disable-next-line rulesdir/onyx-props-must-have-default
export default withOnyx({
account: {key: ONYXKEYS.ACCOUNT},
export default withOnyx<CodesStepProps, TwoFactorAuthStepOnyxProps>({
account: { key: ONYXKEYS.ACCOUNT },
allgandalf marked this conversation as resolved.
Show resolved Hide resolved
})(CodesStep);
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as TwoFactorAuthActions from '@userActions/TwoFactorAuthActions';

function DisabledStep() {
const styles = useThemeStyles();
const {translate} = useLocalize();
const { translate } = useLocalize();

return (
<StepWrapper title={translate('twoFactorAuth.disableTwoFactorAuth')}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {useState} from 'react';
import {ScrollView, View} from 'react-native';
import React, { useState } from 'react';
import { ScrollView, View } from 'react-native';
import ConfirmModal from '@components/ConfirmModal';
import * as Expensicons from '@components/Icon/Expensicons';
import * as Illustrations from '@components/Icon/Illustrations';
Expand All @@ -18,9 +18,9 @@ function EnabledStep() {
const styles = useThemeStyles();
const [isConfirmModalVisible, setIsConfirmModalVisible] = useState(false);

const {setStep} = useTwoFactorAuthContext();
const { setStep } = useTwoFactorAuthContext();

const {translate} = useLocalize();
const { translate } = useLocalize();

return (
<StepWrapper title={translate('twoFactorAuth.headerTitle')}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import PropTypes from 'prop-types';
import React from 'react';
import ConfirmationPage from '@components/ConfirmationPage';
import LottieAnimations from '@components/LottieAnimations';
Expand All @@ -7,21 +6,20 @@ import Navigation from '@navigation/Navigation';
import StepWrapper from '@pages/settings/Security/TwoFactorAuth/StepWrapper/StepWrapper';
import useTwoFactorAuthContext from '@pages/settings/Security/TwoFactorAuth/TwoFactorAuthContext/useTwoFactorAuth';
import * as TwoFactorAuthActions from '@userActions/TwoFactorAuthActions';
import type { Route } from '@src/ROUTES';
import CONST from '@src/CONST';

const propTypes = {
type SuccessStepProps = {
/** The route where user needs to be redirected after setting up 2FA */
allgandalf marked this conversation as resolved.
Show resolved Hide resolved
backTo: PropTypes.string,
backTo?: Route;
};

const defaultProps = {
backTo: '',
};

function SuccessStep({backTo}) {
const {setStep} = useTwoFactorAuthContext();
function SuccessStep({
backTo,
}: SuccessStepProps) {
const { setStep } = useTwoFactorAuthContext();

const {translate} = useLocalize();
const { translate } = useLocalize();

return (
<StepWrapper
Expand Down Expand Up @@ -49,7 +47,4 @@ function SuccessStep({backTo}) {
);
}

SuccessStep.propTypes = propTypes;
SuccessStep.defaultProps = defaultProps;

export default SuccessStep;
Loading
Loading