Skip to content

Commit

Permalink
feat(react): return isReconnecting from useWallet (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanofa authored Jan 16, 2025
1 parent a0a6a03 commit 4e138ba
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 7 additions & 1 deletion docs/guides/react-quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ Now, in any component, you can use the `useWallet` hook to access the wallet man
import { useWallet } from '@txnlab/use-wallet-react'

function WalletMenu() {
const { wallets, activeWallet, activeAccount } = useWallet()
const { wallets, activeWallet, activeAccount, isReconnecting } = useWallet()

if (isReconnecting) {
return <div>Reconnecting to wallet...</div>
}

return (
<div>
Expand All @@ -76,6 +80,8 @@ function WalletMenu() {
}
```
The `isReconnecting` state is a boolean that helps manage wallet reconnection flows. It starts as `true` during both SSR and initial client-side mounting. During the first mount, the `WalletProvider` automatically attempts to restore any previously connected wallet sessions. Once this reconnection process completes - whether successful or not - `isReconnecting` switches to `false`.
## Signing Transactions
To sign and send transactions, you can use the library's `algodClient` instance and the `transactionSigner` provided by the active wallet.
Expand Down
9 changes: 7 additions & 2 deletions packages/use-wallet-react/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from '@txnlab/use-wallet'

interface IWalletContext {
manager: WalletManager
isReconnecting: boolean
algodClient: algosdk.Algodv2
setAlgodClient: React.Dispatch<React.SetStateAction<algosdk.Algodv2>>
}
Expand All @@ -33,7 +34,7 @@ export const useWallet = () => {
throw new Error('useWallet must be used within the WalletProvider')
}

const { manager, algodClient, setAlgodClient } = context
const { manager, isReconnecting, algodClient, setAlgodClient } = context

const activeNetwork = useStore(manager.store, (state) => state.activeNetwork)

Expand Down Expand Up @@ -108,6 +109,7 @@ export const useWallet = () => {

return {
wallets,
isReconnecting,
algodClient,
activeNetwork,
activeWallet,
Expand All @@ -129,6 +131,7 @@ interface WalletProviderProps {

export const WalletProvider = ({ manager, children }: WalletProviderProps): JSX.Element => {
const [algodClient, setAlgodClient] = React.useState(manager.algodClient)
const [isReconnecting, setIsReconnecting] = React.useState(true)

React.useEffect(() => {
manager.algodClient = algodClient
Expand All @@ -142,6 +145,8 @@ export const WalletProvider = ({ manager, children }: WalletProviderProps): JSX.
await manager.resumeSessions()
} catch (error) {
console.error('Error resuming sessions:', error)
} finally {
setIsReconnecting(false)
}
}

Expand All @@ -152,7 +157,7 @@ export const WalletProvider = ({ manager, children }: WalletProviderProps): JSX.
}, [manager])

return (
<WalletContext.Provider value={{ manager, algodClient, setAlgodClient }}>
<WalletContext.Provider value={{ manager, isReconnecting, algodClient, setAlgodClient }}>
{children}
</WalletContext.Provider>
)
Expand Down

0 comments on commit 4e138ba

Please sign in to comment.