diff --git a/chatApp/account.html b/chatApp/account.html index 41cb25e..e8f0e3f 100644 --- a/chatApp/account.html +++ b/chatApp/account.html @@ -135,6 +135,12 @@ .message { margin-bottom: 15px; display: flex; + animation: slideIn 0.3s ease; + } + + @keyframes slideIn { + from { opacity: 0; transform: translateY(10px); } + to { opacity: 1; transform: translateY(0); } } .message.sent { @@ -247,7 +253,7 @@ .empty-state i { font-size: 60px; margin-bottom: 15px; - background: linear-gradient(135d deg, #667eea, #764ba2); + background: linear-gradient(135deg, #667eea, #764ba2); -webkit-background-clip: text; -webkit-text-fill-color: transparent; } @@ -292,6 +298,8 @@ let web3, contract; let currentChat = null; let allContacts = []; + let messageStore = new Map(); // Store messages for each chat + let blockchainCheckTimer; const CONTRACT_ADDRESS = '0xd9145CCE52D386f254917e481eB44e9943F39138'; const CONTRACT_ABI = [ @@ -319,16 +327,16 @@ } ]; - // Initialize app - window.addEventListener('load', function() { + // Initialize app - NO REFRESH + window.addEventListener('load', async function() { if (!myAccount) { alert('Please login first!'); window.location.href = 'index.html'; return; } - console.log('🚀 Starting BlockChat for:', myAccount); - initializeApp(); + console.log('🚀 Starting BlockChat (NO REFRESH MODE)'); + await initializeApp(); }); async function initializeApp() { @@ -348,8 +356,11 @@ // Load contacts await loadContacts(); - // Start monitoring for new messages - startMessageMonitoring(); + // Load all existing messages ONCE + await loadAllExistingMessages(); + + // Start checking for NEW messages only (every 6 seconds) + startNewMessageChecker(); } catch (error) { console.error('❌ Initialization failed:', error); @@ -383,9 +394,68 @@ } } + // Load all existing messages ONCE when app starts + async function loadAllExistingMessages() { + try { + console.log('📨 Loading all existing messages...'); + + const events = await contract.getPastEvents('message', { + fromBlock: 0, + toBlock: 'latest' + }); + + console.log(`Found ${events.length} existing messages on blockchain`); + + // Store messages by chat address + events.forEach(event => { + const from = event.returnValues.from.toLowerCase(); + const to = event.returnValues.to.toLowerCase(); + const message = event.returnValues.message; + const timestamp = event.returnValues.timestamp; + const myAddr = myAccount.toLowerCase(); + + // Determine which chat this message belongs to + let chatWith = null; + let isMyMessage = false; + + if (from === myAddr) { + chatWith = to; + isMyMessage = true; + } else if (to === myAddr) { + chatWith = from; + isMyMessage = false; + } + + if (chatWith) { + if (!messageStore.has(chatWith)) { + messageStore.set(chatWith, []); + } + + messageStore.get(chatWith).push({ + message: message, + timestamp: timestamp, + isMyMessage: isMyMessage, + blockNumber: event.blockNumber, + id: `${from}-${to}-${event.blockNumber}-${event.transactionHash}` + }); + } + }); + + // Sort all message arrays by block number + messageStore.forEach(messages => { + messages.sort((a, b) => a.blockNumber - b.blockNumber); + }); + + console.log('💾 Stored messages for', messageStore.size, 'conversations'); + + } catch (error) { + console.error('❌ Error loading existing messages:', error); + } + } + function openChat(contactAddress, index) { - currentChat = contactAddress; - console.log('💬 Opening chat with:', contactAddress); + currentChat = contactAddress.toLowerCase(); + console.log('💬 Opening chat with:', currentChat); // Update contact selection document.querySelectorAll('.contact-item').forEach(item => item.classList.remove('active')); @@ -394,8 +464,8 @@ // Show chat interface showChatInterface(contactAddress, index + 1); - // Load messages for this chat - loadMessagesForChat(contactAddress); + // Display messages for this chat (from memory - NO BLOCKCHAIN CALL) + displayStoredMessages(currentChat); } function showChatInterface(contactAddress, contactNumber) { @@ -410,11 +480,6 @@