Skip to content

Commit 1afb67c

Browse files
committed
chore: Added useTransition to wallet button
1 parent cf83cf8 commit 1afb67c

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

src/components/ConnectAccount.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import React from 'react';
22
import { stellarNetwork } from '../contracts/util';
33
import FundAccountButton from './FundAccountButton';
4+
import { WalletButton } from './WalletButton';
45

56
const ConnectAccount: React.FC = () => {
67
return (
78
<div style={{ display: 'flex', flexDirection: 'row', alignItems: 'center', gap: '10px', verticalAlign: 'middle' }}>
8-
<div>Wallet Button here</div>
9+
<WalletButton />
910
{stellarNetwork !== "mainnet" && <FundAccountButton />}
1011
</div>
1112
);

src/components/WalletButton.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import { useState } from 'react';
77

88
export const WalletButton = () => {
99
const [showDisconnectModal, setShowDisconnectModal] = useState(false);
10-
const { address } = useWallet()
10+
const { address, isPending, hasSyncedOnce } = useWallet();
11+
12+
const buttonLabel = !hasSyncedOnce || isPending ? "Pending..." : "Connect"
1113

1214
if (!address) {
1315
return (
14-
<Button variant="primary" size="lg" onClick={() => void connectWallet()}>
15-
Connect
16+
<Button variant="primary" size="md" onClick={() => void connectWallet()}>
17+
{buttonLabel}
1618
</Button>
1719
)
1820
}

src/providers/WalletProvider.tsx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
import { createContext, useEffect, useMemo, useState } from "react";
1+
import { createContext, useEffect, useMemo, useRef, useState } from "react";
22
import { wallet } from "../util/wallet";
33
import storage from "../util/storage";
4+
import { useTransition } from 'react';
45

56
export interface WalletContextType {
67
address?: string;
78
network?: string;
89
networkPassphrase?: string;
10+
isPending?: boolean;
11+
hasSyncedOnce?: boolean
912
};
1013

1114
export const WalletContext = // eslint-disable-line react-refresh/only-export-components
1215
createContext<WalletContextType>({});
1316

1417
export const WalletProvider = ({ children }: { children: React.ReactNode }) => {
15-
const [address, setAddress] = useState<string | undefined>(
16-
() => storage.getItem("walletAddress") ?? undefined
17-
);
18+
const [address, setAddress] = useState<string>();
1819
const [network, setNetwork] = useState<string>();
1920
const [networkPassphrase, setNetworkPassphrase] = useState<string>();
21+
const [isPending, startTransition] = useTransition();
22+
const [hasSyncedOnce, setHasSyncedOnce] = useState(false);
23+
const isFirstRun = useRef(true);
2024

2125
const nullify = () => {
2226
setNetwork(undefined)
@@ -25,6 +29,7 @@ export const WalletProvider = ({ children }: { children: React.ReactNode }) => {
2529
}
2630

2731
const updateCurrentWalletState = async () => {
32+
2833
// There is no way, with StellarWalletsKit, to check if the wallet is
2934
// installed/connected/authorized. We need to manage that on our side by
3035
// checking our storage item.
@@ -69,6 +74,15 @@ export const WalletProvider = ({ children }: { children: React.ReactNode }) => {
6974
await new Promise((res) => {
7075
timer = setTimeout(res, 1000)
7176
});
77+
if (isFirstRun.current) {
78+
startTransition(async () => {
79+
await updateCurrentWalletState()
80+
setHasSyncedOnce(true);
81+
});
82+
isFirstRun.current = false;
83+
} else {
84+
void updateCurrentWalletState();
85+
}
7286
await updateCurrentWalletState()
7387
}
7488
})()
@@ -84,7 +98,9 @@ export const WalletProvider = ({ children }: { children: React.ReactNode }) => {
8498
address,
8599
network,
86100
networkPassphrase,
87-
}), [address, network, networkPassphrase]);
101+
isPending,
102+
hasSyncedOnce
103+
}), [address, network, networkPassphrase, isPending, hasSyncedOnce]);
88104

89105
return (
90106
<WalletContext value={contextValue}>

src/util/storage.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
*/
1414
type Schema = {
1515
walletId: string;
16-
walletAddress: string;
1716
};
1817

1918
/**

src/util/wallet.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@ export const connectWallet = async () => {
3939

4040
// Now open selected wallet's login flow by calling `getAddress` --
4141
// Yes, it's strange that a getter has a side effect of opening a modal
42-
void kit.getAddress().then(({ address }) => {
42+
void kit.getAddress().then(() => {
4343
// Once `getAddress` returns successfully, we know they actually
4444
// connected the selected wallet, and we set our localStorage
4545
storage.setItem("walletId", selectedId);
46-
storage.setItem("walletAddress", address);
4746
})
4847
},
4948
});
@@ -52,7 +51,6 @@ export const connectWallet = async () => {
5251
export const disconnectWallet = async () => {
5352
await kit.disconnect();
5453
storage.removeItem("walletId");
55-
storage.removeItem("walletAddress");
5654
}
5755

5856
export const wallet = kit;

0 commit comments

Comments
 (0)