diff --git a/app.js b/app.js index fab61fb..1546d7c 100644 --- a/app.js +++ b/app.js @@ -18,9 +18,6 @@ let userAvatar = ''; let registeredUsers = JSON.parse(localStorage.getItem('registeredUsers')) || {}; let peerCount = 0; -// Load user information from localStorage -let currentUser = JSON.parse(localStorage.getItem('currentUser')) || { username: 'Anonymous', avatar: '' }; - async function initialize() { swarm = new Hyperswarm(); @@ -53,15 +50,18 @@ async function initialize() { messageForm.addEventListener('submit', sendMessage); } - // Set current user if available in localStorage - if (currentUser.username) { - userName = currentUser.username; - userAvatar = currentUser.avatar || ''; + const savedUser = localStorage.getItem('currentUser'); + if (savedUser) { + const user = JSON.parse(savedUser); + userName = user.username; + userAvatar = user.avatar || ''; const setupDiv = document.querySelector('#setup'); - const registerDiv = document.querySelector('#register'); - if (userName !== 'Anonymous' && setupDiv) { + if (setupDiv) { setupDiv.classList.remove('hidden'); - } else if (registerDiv) { + } + } else { + const registerDiv = document.querySelector('#register'); + if (registerDiv) { registerDiv.classList.remove('hidden'); } } @@ -253,7 +253,15 @@ function onMessageAdded(from, message, avatar) { $header.textContent = from; const $text = document.createElement('div'); - $text.textContent = message; + $text.classList.add('message-text'); + + // Split message by line breaks and create a new paragraph for each line + const lines = message.split('\n'); + lines.forEach(line => { + const $line = document.createElement('p'); + $line.textContent = line; + $text.appendChild($line); + }); $content.appendChild($header); $content.appendChild($text); @@ -272,9 +280,3 @@ async function updateIcon(username, avatarBuffer) { } initialize(); - -// Save the current user's information before closing the application -window.addEventListener('beforeunload', () => { - localStorage.setItem('currentUser', JSON.stringify({ username: userName, avatar: userAvatar })); -}); - diff --git a/index.html b/index.html index 784ed99..6f25aee 100644 --- a/index.html +++ b/index.html @@ -55,11 +55,24 @@