11import { useReverification , useUser } from '@clerk/shared/react' ;
22import type { PhoneNumberResource , UserResource } from '@clerk/shared/types' ;
3- import React , { useRef } from 'react' ;
3+ import React , { useMemo , useRef } from 'react' ;
44
55import { useWizard , Wizard } from '@/common' ;
66import { MfaBackupCodeList } from '@/components/UserProfile/MfaBackupCodeList' ;
@@ -27,6 +27,10 @@ type MFAVerifyPhoneForSessionTasksProps = {
2727 onReset : ( ) => void ;
2828} ;
2929
30+ export const getAvailablePhonesFromUser = ( user : UserResource | undefined | null ) => {
31+ return user ?. phoneNumbers . filter ( p => ! p . reservedForSecondFactor ) || [ ] ;
32+ } ;
33+
3034const MFAVerifyPhoneForSessionTasks = ( props : MFAVerifyPhoneForSessionTasksProps ) => {
3135 const { onSuccess, resourceRef, onReset } = props ;
3236 const card = useCardState ( ) ;
@@ -257,7 +261,7 @@ const SmsCodeScreen = (props: SmsCodeScreenProps) => {
257261 return null ;
258262 }
259263
260- const availablePhones = user . phoneNumbers . filter ( p => ! p . reservedForSecondFactor ) ;
264+ const availablePhones = getAvailablePhonesFromUser ( user ) ;
261265
262266 return (
263267 < Flow . Part part = 'phoneCode' >
@@ -267,14 +271,15 @@ const SmsCodeScreen = (props: SmsCodeScreenProps) => {
267271 badgeText = { localizationKeys ( 'taskSetupMfa.badge' ) }
268272 sx = { t => ( {
269273 paddingTop : t . space . $8 ,
270- paddingLeft : t . space . $8 ,
271- paddingRight : t . space . $8 ,
274+ paddingInline : t . space . $8 ,
272275 } ) }
273276 >
274277 < Header . Title localizationKey = { localizationKeys ( 'taskSetupMfa.smsCode.title' ) } />
275278 < Header . Subtitle localizationKey = { localizationKeys ( 'taskSetupMfa.smsCode.subtitle' ) } />
276279 </ Header . Root >
277- < Card . Alert > { card . error } </ Card . Alert >
280+ < Flex sx = { t => ( { paddingInline : t . space . $8 } ) } >
281+ < Card . Alert > { card . error } </ Card . Alert >
282+ </ Flex >
278283 < Col >
279284 < Actions
280285 role = 'menu'
@@ -342,13 +347,28 @@ type SmsCodeFlowProps = {
342347 goToStartStep : ( ) => void ;
343348} ;
344349
350+ // This is the order of the steps in the wizard
351+ const STEPS = {
352+ ADD_PHONE : 0 ,
353+ VERIFY_PHONE : 1 ,
354+ SELECT_PHONE : 2 ,
355+ SUCCESS : 3 ,
356+ } as const ;
357+
345358export const SmsCodeFlow = ( props : SmsCodeFlowProps ) => {
346359 const { onSuccess, goToStartStep } = props ;
347360 const { user } = useUser ( ) ;
348361
349362 const ref = useRef < PhoneNumberResource > ( ) ;
350- const availablePhones = user ?. phoneNumbers . filter ( p => ! p . reservedForSecondFactor ) || [ ] ;
351- const wizard = useWizard ( { defaultStep : availablePhones . length > 0 ? 2 : 0 } ) ;
363+
364+ const availablePhones = useMemo ( ( ) => {
365+ if ( ! user ) {
366+ return [ ] ;
367+ }
368+ return getAvailablePhonesFromUser ( user ) ;
369+ } , [ user ] ) ;
370+
371+ const wizard = useWizard ( { defaultStep : availablePhones . length > 0 ? STEPS . SELECT_PHONE : STEPS . ADD_PHONE } ) ;
352372
353373 return (
354374 < Card . Root >
@@ -360,24 +380,24 @@ export const SmsCodeFlow = (props: SmsCodeFlowProps) => {
360380 < AddPhoneForSessionTasks
361381 resourceRef = { ref }
362382 onSuccess = { wizard . nextStep }
363- onUseExistingNumberClick = { ( ) => wizard . goToStep ( 2 ) }
364- onReset = { ( ) => ( availablePhones . length > 0 ? wizard . goToStep ( 2 ) : goToStartStep ( ) ) }
383+ onUseExistingNumberClick = { ( ) => wizard . goToStep ( STEPS . SELECT_PHONE ) }
384+ onReset = { ( ) => ( availablePhones . length > 0 ? wizard . goToStep ( STEPS . SELECT_PHONE ) : goToStartStep ( ) ) }
365385 />
366386 { /* Step 1: Verify phone */ }
367387 < MFAVerifyPhoneForSessionTasks
368388 resourceRef = { ref }
369- onSuccess = { ( ) => wizard . goToStep ( 3 ) }
370- onReset = { ( ) => wizard . goToStep ( 2 ) }
389+ onSuccess = { ( ) => wizard . goToStep ( STEPS . SUCCESS ) }
390+ onReset = { ( ) => wizard . goToStep ( STEPS . VERIFY_PHONE ) }
371391 />
372392 { /* Step 2: Phone selection (default if available phones) */ }
373393 < SmsCodeScreen
374394 availablePhones = { availablePhones }
375395 onSuccess = { wizard . nextStep }
376396 onReset = { goToStartStep }
377- onAddPhoneClick = { ( ) => wizard . goToStep ( 0 ) }
397+ onAddPhoneClick = { ( ) => wizard . goToStep ( STEPS . ADD_PHONE ) }
378398 onUnverifiedPhoneClick = { ( phone : PhoneNumberResource ) => {
379399 ref . current = phone ;
380- wizard . goToStep ( 1 ) ;
400+ wizard . goToStep ( STEPS . SELECT_PHONE ) ;
381401 } }
382402 resourceRef = { ref }
383403 />
0 commit comments