1- import { useContext , useState } from "react" ;
1+ import { useContext , useEffect , useState } from "react" ;
22import { Button , Modal , Text } from "@stellar/design-system" ;
33import { ISupportedWallet } from "@creit.tech/stellar-wallets-kit" ;
44import { useStore } from "@/store/useStore" ;
@@ -7,6 +7,7 @@ import { useAccountInfo } from "@/query/useAccountInfo";
77
88import { shortenStellarAddress } from "@/helpers/shortenStellarAddress" ;
99import { getNetworkHeaders } from "@/helpers/getNetworkHeaders" ;
10+ import { localStorageSavedWallet } from "@/helpers/localStorageSavedWallet" ;
1011
1112import { ConnectedModal } from "@/components/WalletKit/ConnectedModal" ;
1213import { WalletKitContext } from "@/components/WalletKit/WalletKitContextProvider" ;
@@ -15,56 +16,109 @@ import { trackEvent, TrackingEvent } from "@/metrics/tracking";
1516
1617export const ConnectWallet = ( ) => {
1718 const { network, walletKit, updateWalletKit } = useStore ( ) ;
19+ const [ connected , setConnected ] = useState < boolean > ( false ) ;
1820 const [ isModalVisible , setShowModal ] = useState ( false ) ;
1921 const [ errorMessageOnConnect , setErrorMessageOnConnect ] = useState ( "" ) ;
2022 const walletKitInstance = useContext ( WalletKitContext ) ;
23+ const savedWallet = localStorageSavedWallet . get ( ) ;
2124
2225 const { data : accountInfo , refetch : fetchAccountInfo } = useAccountInfo ( {
2326 publicKey : walletKit ?. publicKey || "" ,
2427 horizonUrl : network ?. horizonUrl || "" ,
2528 headers : network ? getNetworkHeaders ( network , "horizon" ) : { } ,
2629 } ) ;
2730
28- const resetWalletKit = ( ) => {
31+ const disconnect = ( ) => {
2932 updateWalletKit ( {
3033 publicKey : undefined ,
3134 walletType : undefined ,
3235 } ) ;
3336
3437 setShowModal ( false ) ;
38+ setConnected ( false ) ;
39+ localStorageSavedWallet . remove ( ) ;
3540 } ;
3641
42+ useEffect ( ( ) => {
43+ let t : NodeJS . Timeout ;
44+
45+ if (
46+ ! connected &&
47+ ! ! savedWallet ?. id &&
48+ ! [ undefined , "false" , "wallet_connect" ] . includes ( savedWallet ?. id ) &&
49+ savedWallet . network . id === network . id
50+ ) {
51+ t = setTimeout ( ( ) => {
52+ walletKitInstance . walletKit ?. setWallet ( savedWallet . id ) ;
53+ handleSetWalletAddress ( ) ;
54+ clearTimeout ( t ) ;
55+ } , 750 ) ;
56+ }
57+
58+ return ( ) => {
59+ clearTimeout ( t ) ;
60+ } ;
61+ } , [ savedWallet ?. id , connected , walletKitInstance ] ) ;
62+
63+ async function handleSetWalletAddress ( ) : Promise < boolean > {
64+ try {
65+ const addressResult = await walletKitInstance . walletKit ?. getAddress ( ) ;
66+
67+ if ( ! addressResult ?. address ) {
68+ return false ;
69+ }
70+ const publicKey = addressResult . address ;
71+
72+ if ( ! publicKey ) {
73+ return false ;
74+ }
75+
76+ updateWalletKit ( {
77+ publicKey,
78+ walletType : savedWallet ?. id ,
79+ } ) ;
80+ setConnected ( true ) ;
81+
82+ return true ;
83+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
84+ } catch ( e ) {
85+ return false ;
86+ }
87+ }
88+
3789 const connectWallet = async ( ) => {
3890 try {
3991 await walletKitInstance . walletKit ?. openModal ( {
4092 onWalletSelected : async ( option : ISupportedWallet ) => {
41- try {
42- walletKitInstance . walletKit ?. setWallet ( option . id ) ;
43- const addressResult =
44- await walletKitInstance . walletKit ?. getAddress ( ) ;
45-
46- if ( addressResult ?. address ) {
47- updateWalletKit ( {
48- publicKey : addressResult . address ,
49- walletType : option . id ,
50- } ) ;
51-
52- trackEvent ( TrackingEvent . WALLET_KIT_SELECTED , {
53- walletType : option . id ,
54- } ) ;
55- }
56- } catch ( e : unknown ) {
57- // Ledger sends a message with the error code, so we need to check for that
58- const errorMessage =
59- ( e as { message ?: string } ) ?. message || "Unknown error occurred" ;
93+ walletKitInstance . walletKit ?. setWallet ( option . id ) ;
94+ const isWalletConnected = await handleSetWalletAddress ( ) ;
95+
96+ if ( ! isWalletConnected ) {
97+ const errorMessage = "Unable to load wallet information" ;
6098 setErrorMessageOnConnect ( errorMessage ) ;
61- resetWalletKit ( ) ;
99+ disconnect ( ) ;
100+ return ;
62101 }
102+
103+ localStorageSavedWallet . set ( {
104+ id : option . id ,
105+ network : {
106+ id : network . id ,
107+ label : network . label ,
108+ } ,
109+ } ) ;
110+
111+ trackEvent ( TrackingEvent . WALLET_KIT_SELECTED , {
112+ walletType : option . id ,
113+ } ) ;
63114 } ,
64115 } ) ;
65116 // eslint-disable-next-line @typescript-eslint/no-unused-vars
66117 } catch ( e ) {
67- resetWalletKit ( ) ;
118+ const errorMessage =
119+ ( e as { message ?: string } ) ?. message || "Unknown error occurred" ;
120+ setErrorMessageOnConnect ( errorMessage ) ;
121+ disconnect ( ) ;
68122 }
69123 } ;
70124
@@ -82,7 +136,7 @@ export const ConnectWallet = () => {
82136 isVisible = { isModalVisible }
83137 showModal = { setShowModal }
84138 publicKey = { walletKit ?. publicKey || "" }
85- onDisconnect = { resetWalletKit }
139+ onDisconnect = { disconnect }
86140 balance = {
87141 xlmBalance ?. balance
88142 ? `${ xlmBalance . balance } XLM`
0 commit comments