Skip to content

Commit 9f6d11d

Browse files
committed
race condition guard
1 parent 9f80789 commit 9f6d11d

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/components/WalletSwitcher.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
import { CustomError } from '@/contexts/ErrorContext';
1616
import { useWallet } from '@/contexts/WalletContext';
1717
import { useErrors } from '@/hooks/useErrors';
18-
import { loginAndUpdateState, clearState } from '@/state';
18+
import { clearState, loginAndUpdateState } from '@/state';
1919
import { t } from '@lingui/core/macro';
2020
import { Trans } from '@lingui/react/macro';
2121
import { LogOut, WalletIcon } from 'lucide-react';
@@ -29,7 +29,12 @@ interface WalletSwitcherProps {
2929

3030
export function WalletSwitcher({ isCollapsed, logout }: WalletSwitcherProps) {
3131
const navigate = useNavigate();
32-
const { wallet: currentWallet, setWallet, setIsSwitching } = useWallet();
32+
const {
33+
wallet: currentWallet,
34+
setWallet,
35+
setIsSwitching,
36+
isSwitching,
37+
} = useWallet();
3338
const { addError } = useErrors();
3439
const [wallets, setWallets] = useState<
3540
{ name: string; fingerprint: number; emoji: string | null }[]
@@ -60,6 +65,10 @@ export function WalletSwitcher({ isCollapsed, logout }: WalletSwitcherProps) {
6065
}, [addError]);
6166

6267
const handleSwitchWallet = async (fingerprint: number) => {
68+
if (isSwitching) {
69+
return;
70+
}
71+
6372
try {
6473
// Start switching: clear wallet, state, and set switching state
6574
setIsSwitching(true);
@@ -124,7 +133,9 @@ export function WalletSwitcher({ isCollapsed, logout }: WalletSwitcherProps) {
124133
<DropdownMenuItem
125134
key={wallet.fingerprint}
126135
onClick={() => handleSwitchWallet(wallet.fingerprint)}
127-
disabled={currentWallet?.fingerprint === wallet.fingerprint}
136+
disabled={
137+
isSwitching || currentWallet?.fingerprint === wallet.fingerprint
138+
}
128139
className='grid grid-cols-[auto_1fr_auto] items-center gap-3'
129140
>
130141
<div className='w-6 flex items-center justify-center'>
@@ -151,6 +162,7 @@ export function WalletSwitcher({ isCollapsed, logout }: WalletSwitcherProps) {
151162
<DropdownMenuSeparator />
152163
<DropdownMenuItem
153164
onClick={logout}
165+
disabled={isSwitching}
154166
className='text-destructive focus:text-destructive'
155167
>
156168
<LogOut className={className} aria-hidden='true' />

0 commit comments

Comments
 (0)