Skip to content

Commit

Permalink
fix: disconnection rerendiring issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Agilulfo1820 committed Jan 24, 2025
1 parent 728be67 commit 1de6668
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions packages/vechain-kit/src/hooks/api/wallet/useWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ export const useWallet = (): UseWalletReturnType => {
isLoadingLoginOAuth ||
!ready;

// Add a single connection state that considers all factors
const [isConnected, setIsConnected] = useState(false);

// Connection type
const connectionSource: ConnectionSource = isConnectedWithCrossApp
? {
Expand All @@ -122,10 +124,19 @@ export const useWallet = (): UseWalletReturnType => {
isConnectedWithCrossApp;

setIsConnected(isNowConnected);

// Force re-render of dependent components
if (!isNowConnected) {
// Clear any cached wallet data
clearConnectionCache();
// Dispatch event to trigger re-renders
window.dispatchEvent(new Event('wallet_disconnected'));
}
}, [
isConnectedWithDappKit,
isConnectedWithSocialLogin,
isConnectedWithCrossApp,
clearConnectionCache,
connectionSource,
]);

Expand Down Expand Up @@ -179,16 +190,20 @@ export const useWallet = (): UseWalletReturnType => {
// Modify the disconnect function to ensure state updates
const disconnect = useCallback(async () => {
try {
// First set connection state to false
setIsConnected(false);

// Then perform disconnection logic
if (isConnectedWithDappKit) {
dappKitDisconnect();
await dappKitDisconnect();
} else if (isConnectedWithSocialLogin) {
logout();
await logout();
} else if (isConnectedWithCrossApp) {
disconnectCrossApp();
await disconnectCrossApp();
}

clearConnectionCache();
window.dispatchEvent(new Event('wallet_disconnected'));
setIsConnected(false);
} catch (error) {
console.error('Error during disconnect:', error);
}
Expand Down

0 comments on commit 1de6668

Please sign in to comment.