Skip to content

Commit 574a4b4

Browse files
authored
feat(account-center): auto select single verification method (#8032)
1 parent 73070f1 commit 574a4b4

File tree

1 file changed

+38
-5
lines changed
  • packages/account-center/src/components/VerificationMethodList

1 file changed

+38
-5
lines changed

packages/account-center/src/components/VerificationMethodList/index.tsx

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useContext, useState } from 'react';
1+
import { useContext, useEffect, useMemo, useRef, useState } from 'react';
22

33
import PageContext from '@ac/Providers/PageContextProvider/PageContext';
44
import SecondaryPageLayout from '@ac/layouts/SecondaryPageLayout';
@@ -9,9 +9,45 @@ import PasswordVerification from '../PasswordVerification';
99
import PhoneVerification from '../PhoneVerification';
1010
import VerificationMethodButton from '../VerificationMethodButton';
1111

12+
const isVerificationMethod = (value: VerificationMethod | false): value is VerificationMethod =>
13+
value !== false;
14+
1215
const VerificationMethodList = () => {
1316
const { userInfo } = useContext(PageContext);
1417
const [verifyingMethod, setVerifyingMethod] = useState<VerificationMethod>();
18+
// Track if we already auto-selected the only available method to avoid re-trigger on Back.
19+
const hasAutoSelectedMethod = useRef(false);
20+
21+
const hasPasswordVerification = Boolean(userInfo?.hasPassword);
22+
const hasEmailVerification = Boolean(userInfo?.primaryEmail);
23+
const hasPhoneVerification = Boolean(userInfo?.primaryPhone);
24+
25+
const availableMethods = useMemo(
26+
() =>
27+
[
28+
hasPasswordVerification && VerificationMethod.Password,
29+
hasEmailVerification && VerificationMethod.EmailCode,
30+
hasPhoneVerification && VerificationMethod.PhoneCode,
31+
].filter((method) => isVerificationMethod(method)),
32+
[hasEmailVerification, hasPasswordVerification, hasPhoneVerification]
33+
);
34+
35+
useEffect(() => {
36+
// Auto-select the lone available method once on mount.
37+
if (!verifyingMethod && availableMethods.length === 1 && !hasAutoSelectedMethod.current) {
38+
setVerifyingMethod(availableMethods[0]);
39+
// eslint-disable-next-line @silverhand/fp/no-mutation
40+
hasAutoSelectedMethod.current = true;
41+
return;
42+
}
43+
44+
// Clear selection if it no longer exists (e.g., user info changed).
45+
if (verifyingMethod && !availableMethods.includes(verifyingMethod)) {
46+
setVerifyingMethod(undefined);
47+
// eslint-disable-next-line @silverhand/fp/no-mutation
48+
hasAutoSelectedMethod.current = false;
49+
}
50+
}, [availableMethods, verifyingMethod]);
1551

1652
if (verifyingMethod === VerificationMethod.Password) {
1753
return (
@@ -43,16 +79,13 @@ const VerificationMethodList = () => {
4379
);
4480
}
4581

46-
const hasEmailVerification = Boolean(userInfo?.primaryEmail);
47-
const hasPhoneVerification = Boolean(userInfo?.primaryPhone);
48-
4982
return (
5083
<SecondaryPageLayout
5184
title="account_center.verification.title"
5285
description="account_center.verification.description"
5386
>
5487
<div style={{ display: 'flex', flexDirection: 'column', gap: '10px' }}>
55-
{userInfo?.hasPassword && (
88+
{hasPasswordVerification && (
5689
<VerificationMethodButton
5790
method={VerificationMethod.Password}
5891
onClick={() => {

0 commit comments

Comments
 (0)