@@ -6,9 +6,10 @@ import {
66 fetchAddressDetails ,
77 initTransport ,
88 fetchAddressBalance ,
9+ isLedgerTransportInitialized ,
910} from '../../lib/ledger' ;
1011import { useState , useEffect } from 'react' ;
11- import { Stack , Tabs , Breadcrumbs , Anchor , Button , Center } from '@mantine/core' ;
12+ import { Stack , Tabs , Breadcrumbs , Anchor , Button , Center , Modal , Text } from '@mantine/core' ;
1213import Header from '../../components/header' ;
1314import AddressesTab from './addresses-tab' ;
1415import OverviewTab from './overview-tab' ;
@@ -279,6 +280,7 @@ export default function Dashboard() {
279280 const [ enableGenerate , setEnableGenerate ] = useState ( false ) ;
280281 const [ mempoolEntryToReplace , setMempoolEntryToReplace ] = useState < IMempoolEntry | null > ( null ) ;
281282 const [ pendingTxId , setPendingTxId ] = useState < string | null > ( null ) ;
283+ const [ showConnectModal , setShowConnectModal ] = useState ( false ) ;
282284
283285 const { ref : containerRef , width : containerWidth , height : containerHeight } = useElementSize ( ) ;
284286
@@ -376,27 +378,32 @@ export default function Dashboard() {
376378
377379 let unloaded = false ;
378380
379- initTransport ( deviceType )
380- . then ( ( ) => {
381- if ( ! unloaded ) {
382- setTransportInitialized ( true ) ;
383-
384- return getXPubFromLedger ( ) . then ( ( xpub ) =>
385- setBIP32Base ( new KaspaBIP32 ( xpub . compressedPublicKey , xpub . chainCode ) ) ,
386- ) ;
387- }
381+ if ( ! isLedgerTransportInitialized ( ) ) {
382+ setShowConnectModal ( true ) ;
383+ } else {
384+ initTransport ( deviceType )
385+ . then ( ( ) => {
386+ if ( ! unloaded ) {
387+ setTransportInitialized ( true ) ;
388+
389+ return getXPubFromLedger ( ) . then ( ( xpub ) =>
390+ setBIP32Base ( new KaspaBIP32 ( xpub . compressedPublicKey , xpub . chainCode ) ) ,
391+ ) ;
392+ }
388393
389- return null ;
390- } )
391- . catch ( ( e ) => {
392- notifications . show ( {
393- title : 'Error' ,
394- color : 'red' ,
395- message : 'Please make sure your device is unlocked and the Kaspa app is open' ,
396- autoClose : false ,
394+ return null ;
395+ } )
396+ . catch ( ( e ) => {
397+ notifications . show ( {
398+ title : 'Error' ,
399+ color : 'red' ,
400+ message :
401+ 'Please make sure your device is unlocked and the Kaspa app is open' ,
402+ autoClose : false ,
403+ } ) ;
404+ console . error ( e ) ;
397405 } ) ;
398- console . error ( e ) ;
399- } ) ;
406+ }
400407
401408 return ( ) => {
402409 unloaded = true ;
@@ -455,6 +462,51 @@ export default function Dashboard() {
455462 < Breadcrumbs > { breadcrumbs } </ Breadcrumbs >
456463 </ Header >
457464
465+ < Modal
466+ centered
467+ opened = { showConnectModal }
468+ withCloseButton = { false }
469+ onClose = { ( ) => setShowConnectModal ( false ) }
470+ title = { 'Connect Ledger Device via ' + deviceType }
471+ >
472+ < Stack >
473+ < Text > Please connect your Ledger device and open the Kaspa app.</ Text >
474+ < Button
475+ onClick = { ( ) => {
476+ setShowConnectModal ( false ) ;
477+ initTransport ( deviceType )
478+ . then ( ( ) => {
479+ setTransportInitialized ( true ) ;
480+ return getXPubFromLedger ( ) . then ( ( xpub ) =>
481+ setBIP32Base (
482+ new KaspaBIP32 (
483+ xpub . compressedPublicKey ,
484+ xpub . chainCode ,
485+ ) ,
486+ ) ,
487+ ) ;
488+ } )
489+ . catch ( ( e ) => {
490+ if ( e . name === 'TransportOpenUserCancelled' ) {
491+ setShowConnectModal ( true ) ;
492+ } else {
493+ notifications . show ( {
494+ title : 'Error' ,
495+ color : 'red' ,
496+ message :
497+ 'Please make sure your device is unlocked and the Kaspa app is open' ,
498+ autoClose : false ,
499+ } ) ;
500+ }
501+ console . error ( e ) ;
502+ } ) ;
503+ } }
504+ >
505+ Connect Device
506+ </ Button >
507+ </ Stack >
508+ </ Modal >
509+
458510 < Center >
459511 < Tabs
460512 value = { activeTab }
0 commit comments