Skip to content

Fix wallet reconnection state loss (Closes #136)#156

Open
giftexceed wants to merge 2 commits into
Core-Foundry:mainfrom
giftexceed:fix/wallet-reconnection-state-136
Open

Fix wallet reconnection state loss (Closes #136)#156
giftexceed wants to merge 2 commits into
Core-Foundry:mainfrom
giftexceed:fix/wallet-reconnection-state-136

Conversation

@giftexceed

Copy link
Copy Markdown

Summary

Fixes intermittent loss of application/session state after a wallet reconnect. Investigation traced it to three issues in the dashboard's reconnection flow (dashboard/src/services/wallet.ts + walletStore.ts):

  1. Transient failure wiped the saved session. restoreWalletSession() cleared the persisted wallet id on any fetchAddress() failure. A temporary RPC blip, a wallet extension still loading, or a locked account permanently deleted the user's saved wallet, so on the next load they appeared disconnected. The persisted session is now treated as the source of truth — a failed refresh surfaces a recoverable error but keeps the session.
  2. Transient null-address updates clobbered a live session. The kit emits intermediate STATE_UPDATED events with no address mid-reconnect; honouring them flipped the UI back to "Connect Wallet". Only the dedicated DISCONNECT event now tears a session down.
  3. StrictMode double-invoke race. The restore effect ran twice with no guard, letting two concurrent restores race into clearing each other. Concurrent calls now share a single in-flight promise.

Additional improvements

  • Persist the resolved address so the connected UI is restored optimistically on reload (state persists immediately, before the async refresh).
  • New isReconnecting state with a "Reconnecting…" button affordance.
  • Clear stale errors when starting a connect; disconnectWallet always clears local state without throwing.

Tests

Added integration tests (kit mocked via Jest moduleNameMapper):

  • src/services/wallet.test.tsx — successful reconnect, transient-failure keeps session, transient-null guard, disconnect teardown, concurrent-restore coalescing, no-op when no wallet saved.
  • src/components/WalletConnectButton.test.tsx — restored-session UI + connected UI surviving a reconnection blip.

import.meta.env was isolated into src/config/stellarNetwork.ts (mapped to a test stub) so the existing Jest runner — which executes modules as CommonJS — keeps working without enabling native ESM (which breaks the jest-axe suites).

Verification

  • jest: 31 passed (was 22; +9 new) ✅
  • tsc --noEmit: clean ✅
  • eslint --max-warnings=0: clean ✅
  • vite build: succeeds ✅

Acceptance criteria

  • State persists after reconnection
  • Wallet reconnects reliably
  • Tests pass

Closes #136

Users occasionally lost their session after reconnecting their wallet.
Three root causes in the reconnection flow:

- restoreWalletSession() erased the persisted wallet id on ANY refresh
  failure, so a transient RPC blip / extension-not-ready / locked-account
  error permanently dropped the saved session. It now keeps the session
  and surfaces a recoverable error instead.
- The STATE_UPDATED handler honoured transient null-address updates the
  kit emits mid-reconnect, flipping the UI back to "disconnected". Only an
  explicit DISCONNECT event now tears a live session down.
- StrictMode double-invoked the restore effect with no guard, letting two
  concurrent restores race. Concurrent calls now share one in-flight
  promise.

Also: persist the resolved address so the connected UI is restored
optimistically on reload; add an isReconnecting state with a
"Reconnecting…" affordance; clear errors when starting a connect; and
make disconnect always clear local state without throwing.

Adds integration tests (wallet service + WalletConnectButton) covering
successful reconnect, transient-failure persistence, transient-null
guarding, disconnect teardown, and concurrent-restore coalescing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Fix Wallet Reconnection State

1 participant