Skip to content

Commit 7fbbefd

Browse files
committed
chore: Move helpers; More-generic typing
1 parent 18de277 commit 7fbbefd

File tree

3 files changed

+27
-35
lines changed

3 files changed

+27
-35
lines changed

packages/ui/src/components/SignIn/useSecondFactorSelection.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import type { SignInFactor } from '@clerk/shared/types';
22
import React from 'react';
33

4-
import { determineStartingSignInSecondFactor, secondFactorKey } from './utils';
4+
import { determineStartingSignInSecondFactor } from './utils';
55

6-
export function useSecondFactorSelection(availableFactors: SignInFactor[] | null) {
6+
const secondFactorKey = (factor: SignInFactor | null | undefined) => {
7+
if (!factor) {
8+
return '';
9+
}
10+
let key = factor.strategy;
11+
if ('phoneNumberId' in factor) {
12+
key += factor.phoneNumberId;
13+
}
14+
return key;
15+
};
16+
17+
export function useSecondFactorSelection<T extends SignInFactor = SignInFactor>(availableFactors: T[] | null) {
718
const lastPreparedFactorKeyRef = React.useRef('');
8-
const [currentFactor, setCurrentFactor] = React.useState<SignInFactor | null>(() =>
9-
determineStartingSignInSecondFactor(availableFactors),
19+
const [currentFactor, setCurrentFactor] = React.useState<T | null>(
20+
() => determineStartingSignInSecondFactor(availableFactors) as T | null,
1021
);
1122

1223
const [showAllStrategies, setShowAllStrategies] = React.useState<boolean>(!currentFactor);
@@ -16,7 +27,7 @@ export function useSecondFactorSelection(availableFactors: SignInFactor[] | null
1627
lastPreparedFactorKeyRef.current = secondFactorKey(currentFactor);
1728
};
1829

19-
const selectFactor = (factor: SignInFactor) => {
30+
const selectFactor = (factor: T) => {
2031
setCurrentFactor(factor);
2132
setShowAllStrategies(false);
2233
};

packages/ui/src/components/SignIn/utils.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,6 @@ import type { FormControlState } from '@/ui/utils/useFormControl';
1414
import { PREFERRED_SIGN_IN_STRATEGIES } from '../../common/constants';
1515
import { otpPrefFactorComparator, passwordPrefFactorComparator } from '../../utils/factorSorting';
1616

17-
// Specific to SignInSecondFactor, SignInClientTrust, & UserVerificationFactorTwo
18-
export const secondFactorKey = (factor: SignInFactor | null | undefined) => {
19-
if (!factor) {
20-
return '';
21-
}
22-
let key = factor.strategy;
23-
if ('phoneNumberId' in factor) {
24-
key += factor.phoneNumberId;
25-
}
26-
return key;
27-
};
28-
2917
const factorForIdentifier = (i: string | null) => (f: SignInFactor) => {
3018
return 'safeIdentifier' in f && f.safeIdentifier === i;
3119
};

packages/ui/src/components/UserVerification/UserVerificationFactorTwo.tsx

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { withCardStateProvider } from '@/ui/elements/contexts';
55
import { LoadingCard } from '@/ui/elements/LoadingCard';
66

77
import { useRouter } from '../../router';
8-
import { determineStartingSignInSecondFactor, secondFactorKey } from '../SignIn/utils';
8+
import { useSecondFactorSelection } from '../SignIn/useSecondFactorSelection';
99
import { secondFactorsAreEqual } from './useReverificationAlternativeStrategies';
1010
import { UserVerificationFactorTwoTOTP } from './UserVerificationFactorTwoTOTP';
1111
import { useUserVerificationSession, withUserVerificationSessionGuard } from './useUserVerificationSession';
@@ -33,27 +33,20 @@ export function UserVerificationFactorTwoComponent(): JSX.Element {
3333
);
3434
}, [sessionVerification.supportedSecondFactors]);
3535

36-
const lastPreparedFactorKeyRef = React.useRef('');
37-
const [currentFactor, setCurrentFactor] = React.useState<SessionVerificationSecondFactor | null>(
38-
() => determineStartingSignInSecondFactor(availableFactors) as SessionVerificationSecondFactor,
39-
);
40-
const [showAllStrategies, setShowAllStrategies] = React.useState<boolean>(!currentFactor);
41-
const toggleAllStrategies = () => setShowAllStrategies(s => !s);
36+
const {
37+
currentFactor,
38+
factorAlreadyPrepared,
39+
handleFactorPrepare,
40+
selectFactor,
41+
showAllStrategies,
42+
toggleAllStrategies,
43+
} = useSecondFactorSelection<SessionVerificationSecondFactor>(availableFactors);
4244

4345
const secondFactorsExcludingCurrent = useMemo(
4446
() => availableFactors?.filter(factor => !secondFactorsAreEqual(factor, currentFactor)),
4547
[availableFactors, currentFactor],
4648
);
4749

48-
const handleFactorPrepare = () => {
49-
lastPreparedFactorKeyRef.current = secondFactorKey(currentFactor);
50-
};
51-
52-
const selectFactor = (factor: SessionVerificationSecondFactor) => {
53-
setCurrentFactor(factor);
54-
toggleAllStrategies();
55-
};
56-
5750
const hasAlternativeStrategies = useMemo(
5851
() => (secondFactorsExcludingCurrent && secondFactorsExcludingCurrent.length > 0) || false,
5952
[secondFactorsExcludingCurrent],
@@ -84,7 +77,7 @@ export function UserVerificationFactorTwoComponent(): JSX.Element {
8477
case 'phone_code':
8578
return (
8679
<UVFactorTwoPhoneCodeCard
87-
factorAlreadyPrepared={lastPreparedFactorKeyRef.current === secondFactorKey(currentFactor)}
80+
factorAlreadyPrepared={factorAlreadyPrepared}
8881
onFactorPrepare={handleFactorPrepare}
8982
factor={currentFactor}
9083
onShowAlternativeMethodsClicked={toggleAllStrategies}
@@ -94,7 +87,7 @@ export function UserVerificationFactorTwoComponent(): JSX.Element {
9487
case 'totp':
9588
return (
9689
<UserVerificationFactorTwoTOTP
97-
factorAlreadyPrepared={lastPreparedFactorKeyRef.current === secondFactorKey(currentFactor)}
90+
factorAlreadyPrepared={factorAlreadyPrepared}
9891
onFactorPrepare={handleFactorPrepare}
9992
factor={currentFactor}
10093
onShowAlternativeMethodsClicked={toggleAllStrategies}

0 commit comments

Comments
 (0)