This commit is contained in:
zramsay 2025-03-21 20:41:30 -04:00
parent 70ee3ca6a6
commit efe825465d
4 changed files with 144 additions and 7 deletions

View File

@ -380,11 +380,31 @@ export async function POST(req: NextRequest): Promise<NextResponse> {
// 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,

View File

@ -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'}
</button>
{!isWalletConnected() && (
<div className="bg-amber-900/20 border border-amber-500/20 text-amber-400 px-4 py-3 rounded-xl text-center flex items-center justify-center gap-2">
{/* Wallet connection status and actions */}
<div className="bg-amber-900/20 border border-amber-500/20 text-amber-400 px-4 py-3 rounded-xl text-center">
<div className="flex items-center justify-center gap-2 mb-2">
<AlertCircle className="w-5 h-5" />
<span>Connect your wallet to also earn WILD tokens</span>
<span>{isWalletConnected()
? `Wallet connected: ${getWalletAddress()?.substring(0, 10)}...`
: "Connect your wallet to also earn WILD tokens"}
</span>
</div>
)}
{/* Always show connection status and manage button */}
<div className="flex justify-center mt-2">
<button
onClick={async () => {
if (isWalletConnected()) {
// If connected, disconnect wallet
const { disconnectSeiWallet } = await import('../services/blockchain/seiService');
disconnectSeiWallet();
// Force state update
if (typeof window !== 'undefined') {
localStorage.removeItem('wildlife_wallet_address');
}
// Force component re-render
window.location.reload();
} else {
// If not connected, connect wallet
const { connectSeiWallet } = await import('../services/blockchain/seiService');
const address = await connectSeiWallet();
if (address) {
console.log('Connected wallet with address:', address);
// Force state update and reload to ensure all components reflect the new state
window.location.reload();
} else {
console.error('Failed to connect wallet');
}
}
}}
className={`px-4 py-2 rounded ${
isWalletConnected()
? 'bg-red-700 hover:bg-red-800 text-white'
: 'bg-amber-700 hover:bg-amber-800 text-white'
}`}
>
{isWalletConnected() ? 'Disconnect Wallet' : 'Connect Wallet'}
</button>
</div>
</div>
</div>
{error && (

View File

@ -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)

View File

@ -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