chore: improve wallet reconnection

This commit is contained in:
Danish Arora 2025-09-18 10:06:56 +05:30
parent a07fa3f7ba
commit 5ea4d7343f
No known key found for this signature in database
GPG Key ID: 1C6EF37CDAE1426E

View File

@ -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<User | null> => {
try {