@@ -27,6 +27,7 @@ import {
2727 SingleValueActionComponent ,
2828} from './api' ;
2929import { checkSecurity , isInterstitial , type SecurityLevel } from './utils' ;
30+ import { EMPTY_OBJECT } from './utils/constants.ts' ;
3031import {
3132 isPostRequestError ,
3233 isSignMessageError ,
@@ -272,7 +273,7 @@ export const BlinkContainer = ({
272273 adapter,
273274 websiteUrl,
274275 websiteText,
275- callbacks,
276+ callbacks = EMPTY_OBJECT ,
276277 securityLevel = DEFAULT_SECURITY_LEVEL ,
277278 Layout,
278279 selector,
@@ -339,14 +340,14 @@ export const BlinkContainer = ({
339340 } , [ initialAction , websiteUrl ] ) ;
340341
341342 useEffect ( ( ) => {
342- callbacks ? .onActionMount ?.(
343- action ,
344- websiteUrl ?? action . url ,
343+ callbacks . onActionMount ?.(
344+ initialAction ,
345+ websiteUrl ?? initialAction . url ,
345346 actionState . action ,
346347 ) ;
347- // we run this effect ONLY if the action changes
348+ // we run this effect ONLY once
348349 // eslint-disable-next-line react-hooks/exhaustive-deps
349- } , [ action . id ] ) ;
350+ } , [ ] ) ;
350351
351352 useEffect ( ( ) => {
352353 const liveDataConfig = action . liveData_experimental ;
@@ -454,6 +455,7 @@ export const BlinkContainer = ({
454455 ) {
455456 setActionState ( newActionState ) ;
456457 dispatch ( { type : ExecutionType . BLOCK } ) ;
458+ callbacks . onActionCancel ?.( action , component , 'security-state-changed' ) ;
457459 return ;
458460 }
459461
@@ -470,6 +472,7 @@ export const BlinkContainer = ({
470472 const account = await adapter . connect ( context ) ;
471473 if ( ! account ) {
472474 dispatch ( { type : ExecutionType . RESET } ) ;
475+ callbacks ?. onActionCancel ?.( action , component , 'wallet-not-connected' ) ;
473476 return ;
474477 }
475478
@@ -484,6 +487,7 @@ export const BlinkContainer = ({
484487 ? response . error
485488 : 'Transaction data missing' ,
486489 } ) ;
490+ callbacks . onActionError ?.( action , component , 'post-request-error' ) ;
487491 return ;
488492 }
489493
@@ -493,6 +497,7 @@ export const BlinkContainer = ({
493497 type : ExecutionType . FINISH ,
494498 successMessage : response . message ,
495499 } ) ;
500+ callbacks . onActionComplete ?.( action , component , signature ) ;
496501 return ;
497502 }
498503
@@ -501,6 +506,11 @@ export const BlinkContainer = ({
501506 type : ExecutionType . SOFT_RESET ,
502507 errorMessage : 'Missing signature for message' ,
503508 } ) ;
509+ callbacks . onActionError ?.(
510+ action ,
511+ component ,
512+ 'message-signature-missing' ,
513+ ) ;
504514 return ;
505515 }
506516
@@ -525,11 +535,19 @@ export const BlinkContainer = ({
525535 type : ExecutionType . FINISH ,
526536 successMessage : response . message ,
527537 } ) ;
538+ callbacks . onActionComplete ?.( action , component , signature ) ;
528539 return ;
529540 }
530541
531542 setAction ( nextAction ) ;
532543 dispatch ( { type : ExecutionType . RESET } ) ;
544+ callbacks . onActionChain ?.(
545+ action ,
546+ nextAction ,
547+ component ,
548+ response . type ,
549+ signature ,
550+ ) ;
533551 } ;
534552
535553 if ( response . type === 'transaction' || ! response . type ) {
@@ -540,10 +558,33 @@ export const BlinkContainer = ({
540558
541559 if ( ! signResult || isSignTransactionError ( signResult ) ) {
542560 dispatch ( { type : ExecutionType . RESET } ) ;
561+ callbacks . onActionCancel ?.(
562+ action ,
563+ component ,
564+ 'transaction-sign-cancel' ,
565+ ) ;
543566 return ;
544567 }
545568
546- await adapter . confirmTransaction ( signResult . signature , context ) ;
569+ const confirmationResult = await adapter
570+ . confirmTransaction ( signResult . signature , context )
571+ . then ( ( ) => ( { success : true as const } ) )
572+ . catch ( ( e ) => ( { success : false as const , message : e . message } ) ) ;
573+
574+ if ( ! confirmationResult . success ) {
575+ dispatch ( {
576+ type : ExecutionType . SOFT_RESET ,
577+ errorMessage :
578+ confirmationResult . message ?? 'Unknown error, please try again' ,
579+ } ) ;
580+ callbacks . onActionError ?.(
581+ action ,
582+ component ,
583+ 'transaction-confirmation-failed' ,
584+ signResult . signature ,
585+ ) ;
586+ return ;
587+ }
547588
548589 await chain ( signResult . signature ) ;
549590 return ;
@@ -554,6 +595,7 @@ export const BlinkContainer = ({
554595
555596 if ( ! signResult || isSignMessageError ( signResult ) ) {
556597 dispatch ( { type : ExecutionType . RESET } ) ;
598+ callbacks . onActionCancel ?.( action , component , 'message-sign-cancel' ) ;
557599 return ;
558600 }
559601
@@ -585,6 +627,7 @@ export const BlinkContainer = ({
585627 type : ExecutionType . SOFT_RESET ,
586628 errorMessage : ( e as Error ) . message ?? 'Unknown error, please try again' ,
587629 } ) ;
630+ callbacks . onActionError ?.( action , component , 'unknown-error' ) ;
588631 }
589632 } ;
590633
0 commit comments