11import type { ConnectError } from '@corbado/web-core' ;
22import { ConnectErrorType } from '@corbado/web-core' ;
3+ import type { AppendCompletionType } from '@corbado/web-core/dist/models/connect/append' ;
34import log from 'loglevel' ;
45import React , { useCallback , useEffect , useRef , useState } from 'react' ;
56
@@ -132,11 +133,23 @@ const AppendInitScreen = () => {
132133 }
133134
134135 setAttestationOptions ( startAppendRes . val . attestationOptions ) ;
135- statefulLoader . current . finish ( ) ;
136-
137136 log . debug ( 'startAppendRes' , startAppendRes , flags ) ;
137+
138+ if ( startAppendRes . val . conditionalAppend ) {
139+ log . debug ( 'starting conditional create' ) ;
140+ const handledByConditionalCreate = await handleConditionalCreate ( startAppendRes . val . attestationOptions ) ;
141+ log . debug ( 'handledByConditionalCreate' , handledByConditionalCreate ) ;
142+
143+ if ( handledByConditionalCreate ) {
144+ statefulLoader . current . finish ( ) ;
145+ return ;
146+ }
147+ }
148+
149+ statefulLoader . current . finish ( ) ;
138150 if ( startAppendRes . val . autoAppend || flags . hasSupportForAutomaticAppend ( ) ) {
139- await handleSubmit ( startAppendRes . val . attestationOptions , false ) ;
151+ console . log ( 'starting auto-append' ) ;
152+ await handleSubmit ( startAppendRes . val . attestationOptions , 'auto' ) ;
140153 }
141154 } ;
142155
@@ -153,25 +166,25 @@ const AppendInitScreen = () => {
153166 } , [ ] ) ;
154167
155168 const handleSubmit = useCallback (
156- async ( attestationOptions : string , showErrorIfCancelled : boolean ) => {
169+ async ( attestationOptions : string , completionType : AppendCompletionType ) => {
157170 if ( appendLoading || skipping ) {
158171 return ;
159172 }
160173
161174 setAppendLoading ( true ) ;
162175 setErrorMessage ( undefined ) ;
163176
164- const res = await getConnectService ( ) . completeAppend ( attestationOptions ) ;
177+ const res = await getConnectService ( ) . completeAppend ( attestationOptions , completionType ) ;
165178 if ( res . err ) {
166179 if ( res . val . type === ConnectErrorType . ExcludeCredentialsMatch ) {
167180 return handleSituation ( AppendSituationCode . ClientExcludeCredentialsMatch , res . val ) ;
168181 }
169182
170183 if ( res . val . type === ConnectErrorType . Cancel ) {
171- if ( showErrorIfCancelled ) {
172- return handleSituation ( AppendSituationCode . ClientPasskeyOperationCancelled , res . val ) ;
173- } else {
184+ if ( completionType === 'auto' ) {
174185 return handleSituation ( AppendSituationCode . ClientPasskeyOperationCancelledSilent , res . val ) ;
186+ } else {
187+ return handleSituation ( AppendSituationCode . ClientPasskeyOperationCancelled , res . val ) ;
175188 }
176189 }
177190
@@ -184,7 +197,26 @@ const AppendInitScreen = () => {
184197 aaguidIcon : res . val . passkeyOperation . aaguidDetails ?. iconLight ,
185198 } ) ;
186199 } ,
187- [ config , getConnectService , appendLoading , skipping ] ,
200+ [ getConnectService , appendLoading , skipping ] ,
201+ ) ;
202+
203+ const handleConditionalCreate = useCallback (
204+ async ( attestationOptions : string ) => {
205+ const res = await getConnectService ( ) . completeAppend ( attestationOptions , 'conditional' ) ;
206+ if ( res . err ) {
207+ await handleSituation ( AppendSituationCode . ClientPasskeyOperationErrorSilent , res . val ) ;
208+
209+ return res . val . type === ConnectErrorType . RaceTimeout ;
210+ }
211+
212+ navigateToScreen ( AppendScreenType . Success , {
213+ aaguidName : res . val . passkeyOperation . aaguidDetails ?. name ,
214+ aaguidIcon : res . val . passkeyOperation . aaguidDetails ?. iconLight ,
215+ } ) ;
216+
217+ return true ;
218+ } ,
219+ [ getConnectService ] ,
188220 ) ;
189221
190222 const handleSituation = async ( situationCode : AppendSituationCode , error ?: ConnectError ) => {
@@ -222,6 +254,10 @@ const AppendInitScreen = () => {
222254 case AppendSituationCode . ExplicitSkipByUser :
223255 await handleSkip ( situationCode , true ) ;
224256 break ;
257+ case AppendSituationCode . ClientPasskeyOperationErrorSilent :
258+ void handleErrorSoft ( situationCode , false , false , error ) ;
259+ setAppendLoading ( false ) ;
260+ break ;
225261 }
226262 } ;
227263
@@ -250,7 +286,14 @@ const AppendInitScreen = () => {
250286 void onReadMoreClick ( ) ;
251287 setAppendInitState ( AppendInitState . ShowBenefits ) ;
252288 } }
253- handleSubmit = { ( ) => void handleSubmit ( attestationOptions , true ) }
289+ handleSubmit = { ( ) => {
290+ let completionType : AppendCompletionType = 'manual' ;
291+ if ( errorMessage ) {
292+ completionType = 'manual-retry' ;
293+ }
294+
295+ void handleSubmit ( attestationOptions , completionType ) ;
296+ } }
254297 handleSkip = { ( ) => onSkip ( ) }
255298 />
256299 ) ;
0 commit comments