From 5ea4d7343fccec77c6eb0d88ffc31fce4285acdb Mon Sep 17 00:00:00 2001 From: Danish Arora Date: Thu, 18 Sep 2025 10:06:56 +0530 Subject: [PATCH] chore: improve wallet reconnection --- src/contexts/AuthContext.tsx | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/contexts/AuthContext.tsx b/src/contexts/AuthContext.tsx index c79d0b6..bdeb7b8 100644 --- a/src/contexts/AuthContext.tsx +++ b/src/contexts/AuthContext.tsx @@ -16,6 +16,8 @@ import { localDatabase } from '@/lib/database/LocalDatabase'; import { useAppKitAccount, useDisconnect, modal } from '@reown/appkit/react'; import { MessageService } from '@/lib/services/MessageService'; import { UserIdentityService } from '@/lib/services/UserIdentityService'; +import { reconnect } from '@wagmi/core'; +import { config as wagmiConfig } from '@/lib/wallet/config'; interface AuthContextType { currentUser: User | null; @@ -81,6 +83,31 @@ export function AuthProvider({ children }: { children: React.ReactNode }) { } }, [bitcoinAccount, ethereumAccount]); + // Try to auto-reconnect EVM wallets on app mount + useEffect(() => { + void reconnect(wagmiConfig) + }, []); + + // Attempt reconnect when network comes back online + useEffect(() => { + const handleOnline = () => { + void reconnect(wagmiConfig) + }; + window.addEventListener('online', handleOnline); + return () => window.removeEventListener('online', handleOnline); + }, []); + + // Attempt reconnect when tab becomes visible again + useEffect(() => { + const handleVisibility = () => { + if (document.visibilityState === 'visible') { + reconnect(wagmiConfig).catch(() => {}); + } + }; + document.addEventListener('visibilitychange', handleVisibility); + return () => document.removeEventListener('visibilitychange', handleVisibility); + }, []); + // Helper functions for user persistence const loadStoredUser = async (): Promise => { try {