1- import { useContext , useState } from 'react' ;
1+ import { useContext , useEffect , useMemo , useRef , useState } from 'react' ;
22
33import PageContext from '@ac/Providers/PageContextProvider/PageContext' ;
44import SecondaryPageLayout from '@ac/layouts/SecondaryPageLayout' ;
@@ -9,9 +9,45 @@ import PasswordVerification from '../PasswordVerification';
99import PhoneVerification from '../PhoneVerification' ;
1010import VerificationMethodButton from '../VerificationMethodButton' ;
1111
12+ const isVerificationMethod = ( value : VerificationMethod | false ) : value is VerificationMethod =>
13+ value !== false ;
14+
1215const 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