From efe825465d1d91456abec7323fef3789c9572591 Mon Sep 17 00:00:00 2001 From: zramsay Date: Fri, 21 Mar 2025 20:41:30 -0400 Subject: [PATCH] ffs --- src/app/api/analyze/route.ts | 20 +++++++ src/components/WildlifeIdentifier.tsx | 72 ++++++++++++++++++++++++-- src/components/wallet/WalletButton.tsx | 14 +++++ src/services/blockchain/seiService.ts | 45 +++++++++++++++- 4 files changed, 144 insertions(+), 7 deletions(-) diff --git a/src/app/api/analyze/route.ts b/src/app/api/analyze/route.ts index 58450c8..dde9aeb 100644 --- a/src/app/api/analyze/route.ts +++ b/src/app/api/analyze/route.ts @@ -380,11 +380,31 @@ export async function POST(req: NextRequest): Promise { // AFTER points are awarded, try to award tokens if wallet is connected // This is separate so issues with token rewards don't affect points try { + // Check all form data keys and values for debugging + const formDataEntries = Array.from(formData.entries()); + console.log('All form data entries:', formDataEntries.map(entry => ({ + key: entry[0], + value: entry[1], + valueType: typeof entry[1] + }))); + // Check if wallet address was provided in the form data const walletAddressRaw = formData.get('walletAddress'); // Ensure we get a string value - formData can have different types const walletAddress = walletAddressRaw ? String(walletAddressRaw) : null; + // More verbose debugging + console.log('Wallet address detailed extraction:', { + walletAddressRaw, + typeOfRaw: typeof walletAddressRaw, + isNull: walletAddressRaw === null, + isUndefined: walletAddressRaw === undefined, + isEmptyString: walletAddressRaw === '', + walletAddress, + hasWalletChecked: formData.has('walletChecked'), + walletStatus: formData.get('walletStatus') + }); + // Log the wallet address for debugging console.log('Wallet address for token award:', { rawValue: walletAddressRaw, diff --git a/src/components/WildlifeIdentifier.tsx b/src/components/WildlifeIdentifier.tsx index 3755964..704de0d 100644 --- a/src/components/WildlifeIdentifier.tsx +++ b/src/components/WildlifeIdentifier.tsx @@ -82,11 +82,28 @@ const WildlifeIdentifier: React.FC = () => { } } + // Hard-code a wallet connection check - let's be very explicit + const isConnected = isWalletConnected(); + console.log('Explicitly checking wallet connection status:', isConnected); + if (userAddress) { console.log('Found wallet address to include in upload:', userAddress); - formData.append('walletAddress', userAddress); + // Explicitly add as string to avoid any type issues + formData.append('walletAddress', String(userAddress)); + + // Debug check - verify the value was properly added + const check = formData.get('walletAddress'); + console.log('Verification after append:', { + walletAddressInForm: check, + matches: check === userAddress, + formDataKeys: Array.from(formData.keys()) + }); } else { console.log('No wallet address found for upload'); + + // Force add a wallet check flag to help debug + formData.append('walletChecked', 'true'); + formData.append('walletStatus', isConnected ? 'connected_but_no_address' : 'not_connected'); } console.log('Uploading image for analysis...'); @@ -184,12 +201,57 @@ const WildlifeIdentifier: React.FC = () => { {isProcessing ? 'Processing...' : 'Identify Wildlife'} - {!isWalletConnected() && ( -
+ {/* Wallet connection status and actions */} +
+
- Connect your wallet to also earn WILD tokens + {isWalletConnected() + ? `Wallet connected: ${getWalletAddress()?.substring(0, 10)}...` + : "Connect your wallet to also earn WILD tokens"} +
- )} + + {/* Always show connection status and manage button */} +
+ +
+
{error && ( diff --git a/src/components/wallet/WalletButton.tsx b/src/components/wallet/WalletButton.tsx index e8ca30a..6bd0557 100644 --- a/src/components/wallet/WalletButton.tsx +++ b/src/components/wallet/WalletButton.tsx @@ -58,9 +58,23 @@ const WalletButton: React.FC = () => { setError(null) try { + console.log('Attempting to connect wallet...'); const result = await connectSeiWallet() + + // After connection, verify and debug the connection state + console.log('Wallet connection result:', { + addressResult: result, + isConnected: isWalletConnected(), + currentAddress: getWalletAddress(), + inLocalStorage: typeof window !== 'undefined' ? localStorage.getItem('wildlife_wallet_address') : null + }); + if (!result) { + console.error('No wallet address returned from connection attempt'); setError('No wallet extension found. Please install Keplr or Leap wallet.') + } else { + // Force a refresh to ensure all components are aware of the new wallet state + window.dispatchEvent(new Event('storage')); // This can trigger components to recheck localStorage } } catch (err) { console.error('Wallet connection error:', err) diff --git a/src/services/blockchain/seiService.ts b/src/services/blockchain/seiService.ts index 535a9bd..490681d 100644 --- a/src/services/blockchain/seiService.ts +++ b/src/services/blockchain/seiService.ts @@ -203,14 +203,55 @@ export const disconnectSeiWallet = () => { * since the address was previously saved */ export const isWalletConnected = (): boolean => { - return currentAddress !== null; + // Check in-memory state first + if (currentAddress !== null) { + return true; + } + + // Try localStorage as fallback (client-side only) + if (typeof window !== 'undefined') { + try { + const savedAddress = localStorage.getItem('wildlife_wallet_address'); + if (savedAddress) { + console.log('Found wallet address in localStorage during connection check:', savedAddress); + // Update the in-memory state with the localStorage value + currentAddress = savedAddress; + return true; + } + } catch (e) { + console.warn('Error checking localStorage for wallet address:', e); + } + } + + return false; }; /** * Get the connected wallet's address + * This will check both in-memory state and localStorage */ export const getWalletAddress = (): string | null => { - return currentAddress; + // Check in-memory state first + if (currentAddress !== null) { + return currentAddress; + } + + // Try localStorage as fallback (client-side only) + if (typeof window !== 'undefined') { + try { + const savedAddress = localStorage.getItem('wildlife_wallet_address'); + if (savedAddress) { + console.log('Found wallet address in localStorage during address retrieval:', savedAddress); + // Update the in-memory state with the localStorage value + currentAddress = savedAddress; + return savedAddress; + } + } catch (e) { + console.warn('Error retrieving wallet address from localStorage:', e); + } + } + + return null; }; // Award tokens function removed - now handled by the backend service