Skip to content

Commit 66a33f9

Browse files
committed
corrected error if wallet connection is skipped
1 parent e45dbc4 commit 66a33f9

File tree

5 files changed

+68
-18
lines changed

5 files changed

+68
-18
lines changed

apps/box/src/components/WalletNotification.tsx

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useWalletConnection } from '../hooks/useWalletConnection';
44
import { useWalletNetwork } from '../hooks/useWalletNetwork';
55
import { useContractIntegration } from '../hooks/useContractIntegration';
66
import { useSDK } from '@metamask/sdk-react';
7+
import { useUserProfileStore } from '../stores/useUserProfileStore';
78

89
export type WalletNotificationType = 'connect' | 'network' | 'hidden';
910

@@ -30,13 +31,17 @@ export const WalletNotification: React.FC<WalletNotificationProps> = ({
3031
} = useWalletNetwork();
3132
const { initializeContracts, isInitializing } = useContractIntegration();
3233
const { account } = useSDK();
34+
const manualSignatureWalletAddress = useUserProfileStore((state) => state.manualSignatureWalletAddress);
3335
const [isLoading, setIsLoading] = useState(false);
3436
const [showAfterDelay, setShowAfterDelay] = useState(false);
3537
const [postLoadingDelay, setPostLoadingDelay] = useState(false);
3638

39+
// Determine if we have any account (MetaMask or manual signature)
40+
const hasAnyAccount = connected && account || manualSignatureWalletAddress;
41+
3742
// Add delay before showing connect wallet notification to prevent flicker
3843
useEffect(() => {
39-
if (!connected || !account) {
44+
if (!hasAnyAccount) {
4045
// Start timer to show connect notification after delay
4146
const timer = setTimeout(() => {
4247
setShowAfterDelay(true);
@@ -47,7 +52,7 @@ export const WalletNotification: React.FC<WalletNotificationProps> = ({
4752
// Wallet is connected, show immediately
4853
setShowAfterDelay(true);
4954
}
50-
}, [connected, account]);
55+
}, [hasAnyAccount]);
5156

5257
// Don't show notification during contract initialization to prevent flicker
5358
const isContractInitializing = isInitializing || false;
@@ -73,14 +78,22 @@ export const WalletNotification: React.FC<WalletNotificationProps> = ({
7378

7479
// Determine what type of notification to show
7580
const getNotificationType = (): WalletNotificationType => {
76-
if (!connected || !account) {
77-
// Only show connect notification after delay
78-
return showAfterDelay ? 'connect' : 'hidden';
81+
// If MetaMask is connected, check network
82+
if (connected && account) {
83+
if (!isOnCorrectNetwork) {
84+
return 'network';
85+
}
86+
return 'hidden';
7987
}
80-
if (!isOnCorrectNetwork) {
81-
return 'network';
88+
89+
// If MetaMask is not connected but we have a stored wallet address, don't show connect notification
90+
// (user is using manual signature, so MetaMask connection is optional)
91+
if (manualSignatureWalletAddress) {
92+
return 'hidden';
8293
}
83-
return 'hidden';
94+
95+
// No account at all, show connect notification after delay
96+
return showAfterDelay ? 'connect' : 'hidden';
8497
};
8598

8699
const notificationType = getNotificationType();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { useSDK } from '@metamask/sdk-react';
2+
import { useUserProfileStore } from '../stores/useUserProfileStore';
3+
4+
/**
5+
* Hook that provides account with fallback logic:
6+
* 1. If MetaMask is connected, use the MetaMask account
7+
* 2. If MetaMask is not connected but user signed manually, use the stored wallet address
8+
* 3. Otherwise, return null
9+
*/
10+
export const useAccountWithFallback = () => {
11+
const { account: metamaskAccount, connected } = useSDK();
12+
const manualSignatureWalletAddress = useUserProfileStore(
13+
(state) => state.manualSignatureWalletAddress
14+
);
15+
16+
// Return MetaMask account if connected
17+
if (connected && metamaskAccount) {
18+
return metamaskAccount;
19+
}
20+
21+
// Fallback to manually signed wallet address if available
22+
if (manualSignatureWalletAddress) {
23+
return manualSignatureWalletAddress;
24+
}
25+
26+
// No account available
27+
return null;
28+
};

apps/box/src/hooks/useContractIntegration.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useState, useEffect, useCallback, useRef } from 'react';
22
import { useSDK } from '@metamask/sdk-react';
33
import { useToast } from '@functionland/component-library';
44
import { useSettingsStore } from '../stores/useSettingsStore';
5+
import { useUserProfileStore } from '../stores/useUserProfileStore';
56
import { useWalletNetwork } from './useWalletNetwork';
67
import { getContractService, ContractService, resetContractService } from '../contracts/contractService';
78
import { SupportedChain } from '../contracts/types';
@@ -57,8 +58,12 @@ export const useContractIntegration = (options?: { showConnectedNotification?: b
5758
const { isOnCorrectNetwork } = useWalletNetwork();
5859
const selectedChain = useSettingsStore((state) => state.selectedChain);
5960
const setSelectedChain = useSettingsStore((state) => state.setSelectedChain);
61+
const manualSignatureWalletAddress = useUserProfileStore((state) => state.manualSignatureWalletAddress);
6062
const initializedChainRef = useRef<SupportedChain | null>(null);
6163
const showConnectedNotification = options?.showConnectedNotification ?? false;
64+
65+
// Use MetaMask account if available, otherwise fallback to manually signed wallet address
66+
const effectiveAccount = account || manualSignatureWalletAddress;
6267

6368
const [state, setState] = useState<ContractIntegrationState>({
6469
isInitialized: false,
@@ -75,10 +80,10 @@ export const useContractIntegration = (options?: { showConnectedNotification?: b
7580
const lastInitAttemptRef = useRef<number>(0);
7681

7782
const initializeContracts = useCallback(async (chain: SupportedChain, options: { allowNetworkSwitch?: boolean } = {}) => {
78-
console.log('initializeContracts called for chain:', chain, 'provider:', !!provider, 'account:', account);
83+
console.log('initializeContracts called for chain:', chain, 'provider:', !!provider, 'account:', account, 'effectiveAccount:', effectiveAccount);
7984

80-
if (!provider || !account) {
81-
console.log('Missing provider or account, cannot initialize contracts');
85+
if (!provider || !effectiveAccount) {
86+
console.log('Missing provider or effective account, cannot initialize contracts');
8287
setState(prev => ({
8388
...prev,
8489
isInitialized: false,
@@ -339,15 +344,16 @@ export const useContractIntegration = (options?: { showConnectedNotification?: b
339344
useEffect(() => {
340345
console.log('Contract integration useEffect triggered:', {
341346
account: !!account,
347+
effectiveAccount: !!effectiveAccount,
342348
provider: !!provider,
343349
selectedChain,
344350
isInitialized: state.isInitialized,
345351
isInitializing: state.isInitializing
346352
});
347353

348-
// Clear state when wallet disconnects
349-
if (!account || !provider) {
350-
console.log('Clearing contract state - no account or provider');
354+
// Clear state when wallet disconnects (both MetaMask and manual signature)
355+
if (!effectiveAccount || !provider) {
356+
console.log('Clearing contract state - no effective account or provider');
351357
setState({
352358
isInitialized: false,
353359
isInitializing: false,
@@ -383,6 +389,7 @@ export const useContractIntegration = (options?: { showConnectedNotification?: b
383389
}
384390

385391
// Check if we should show the switch button (same logic as WalletNotification)
392+
// Only check if MetaMask is connected (not for manual signature fallback)
386393
const shouldShowSwitchButton = account && provider && selectedChain && !isOnCorrectNetwork;
387394

388395
// If switch button should be shown, DO NOT perform any contract operations
@@ -407,7 +414,7 @@ export const useContractIntegration = (options?: { showConnectedNotification?: b
407414
}
408415

409416
// Only attempt initialization when wallet is connected AND on correct network
410-
const needsInitialization = account && provider && selectedChain && isOnCorrectNetwork && !state.isInitializing && !state.isInitialized && initializedChainRef.current !== selectedChain;
417+
const needsInitialization = effectiveAccount && provider && selectedChain && isOnCorrectNetwork && !state.isInitializing && !state.isInitialized && initializedChainRef.current !== selectedChain;
411418

412419
if (needsInitialization) {
413420
console.log('Wallet is connected and on correct network - attempting automatic contract initialization for chain:', selectedChain);
@@ -454,7 +461,7 @@ export const useContractIntegration = (options?: { showConnectedNotification?: b
454461
});
455462
}, 1500); // 1500ms debounce to allow network state sync
456463
}
457-
}, [account, provider, selectedChain, isOnCorrectNetwork, state.isInitialized, state.isInitializing]);
464+
}, [account, effectiveAccount, provider, selectedChain, isOnCorrectNetwork, state.isInitialized, state.isInitializing]);
458465

459466
// Cleanup timeout on unmount
460467
useEffect(() => {

apps/box/src/hooks/useWalletNetwork.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ export const useWalletNetwork = () => {
5858
return isCorrect;
5959
} catch (error: any) {
6060
console.error('Network check failed:', error);
61+
const errorMessage = error?.message || (typeof error === 'string' ? error : 'Network check failed');
6162
setState(prev => ({
6263
...prev,
6364
isCheckingNetwork: false,
64-
networkError: error.message,
65+
networkError: errorMessage,
6566
lastNetworkCheck: Date.now(),
6667
}));
6768
return false;

apps/box/src/stores/useUserProfileStore.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,8 @@ getEarnings: async (account?: string) => {
474474
throw new Error('Internet is not connected.');
475475
}
476476
} catch (error) {
477-
console.error('Network check failed:', error.message);
477+
const errorMessage = error instanceof Error ? error.message : (typeof error === 'string' ? error : 'Network check failed');
478+
console.error('Network check failed:', errorMessage);
478479
set({ bloxConnectionStatus: 'NO INTERNET' });
479480
return false;
480481
}

0 commit comments

Comments
 (0)