Skip to content

Commit

Permalink
disconnect wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
PhiMarHal committed Dec 2, 2024
1 parent 6ade2e0 commit 3952423
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 22 deletions.
59 changes: 43 additions & 16 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -839,45 +839,72 @@ async function updateWalletDisplay() {
}

try {
// Check if user has registered a name
const user = await contract.users(userAddress);
const displayText = user.name ? user.name : `${userAddress.slice(0, 6)}...${userAddress.slice(-4)}`;

walletInfo.innerHTML = `
<span class="wallet-address"
title="Click to disconnect, right-click to copy address"
style="cursor: pointer;"
title="${userAddress}"
style="cursor: pointer;"
>${displayText}</span>
`;

// Get the span element
const addressSpan = walletInfo.querySelector('.wallet-address');

// Left click to disconnect
addressSpan.addEventListener('click', async () => {
userAddress = null;
signer = null;
contract = new ethers.Contract(CONFIG.CONTRACT_ADDRESS, CONFIG.CONTRACT_ABI, provider);
await updateWalletDisplay();
showStatus('Wallet disconnected', 'success');
let longPressTimer;
let isLongPress = false;

// Desktop events
addressSpan.addEventListener('click', async (e) => {
if (!isLongPress) {
userAddress = null;
signer = null;
contract = new ethers.Contract(CONFIG.CONTRACT_ADDRESS, CONFIG.CONTRACT_ABI, provider);
await updateWalletDisplay();
showStatus('Wallet disconnected', 'success');
}
});

// Right click to copy address
addressSpan.addEventListener('contextmenu', async (e) => {
e.preventDefault(); // Prevent the context menu from appearing
e.preventDefault();
await copyAddress();
});

// Mobile events
addressSpan.addEventListener('touchstart', (e) => {
isLongPress = false;
longPressTimer = setTimeout(() => {
isLongPress = true;
copyAddress();
}, 500); // 500ms for long press
});

addressSpan.addEventListener('touchend', (e) => {
clearTimeout(longPressTimer);
if (!isLongPress) {
// Handle as normal click (disconnect)
addressSpan.click();
}
});

addressSpan.addEventListener('touchmove', (e) => {
clearTimeout(longPressTimer);
isLongPress = false;
});

async function copyAddress() {
try {
await navigator.clipboard.writeText(userAddress);
showStatus('Address copied to clipboard!', 'success');
} catch (error) {
showStatus('Failed to copy address', 'error');
}
});
}

} catch (error) {
console.error('Error fetching user info:', error);
walletInfo.innerHTML = `
<span class="wallet-address"
title="Click to disconnect, right-click to copy address"
title="Click to disconnect, hold to copy address"
style="cursor: pointer;"
>${userAddress.slice(0, 6)}...${userAddress.slice(-4)}</span>
`;
Expand Down
20 changes: 14 additions & 6 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -225,34 +225,42 @@ body {
box-sizing: border-box;
}

/* Add to styles.css */
#wallet-info {
flex: 1;
height: 38px; /* Consistent height for both states */
display: flex;
align-items: center;
}

#connect-wallet {
padding: 16px 32px;
padding: 12px 24px; /* Reduced padding to match address display */
background: var(--button-primary);
color: white;
font-size: 24px;
font-family: monospace;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background 0.2s ease;
}

#connect-wallet:hover {
background: var(--button-hover);
}

.wallet-address {
color: var(--text-primary);
font-size: 24px;
font-family: monospace;
padding: 16px 32px;
padding: 8px 16px;
background: var(--background-dark);
border-radius: 4px;
user-select: none; /* Prevent text selection on mobile */
}


#connect-wallet:hover {
background: var(--button-hover);
}


/* Main words container */
#words-display {
margin: 0 auto; /* Changed from 20px auto */
Expand Down

0 comments on commit 3952423

Please sign in to comment.