Mostly finished loading. User avatar stuff is left to do 😭

This commit is contained in:
MrMasrozYTLIVE 2024-06-09 09:44:52 +03:00
parent d33d31debf
commit 1271c48571

30
app.js
View File

@ -24,7 +24,7 @@ let servePort;
// Object to store all the information we want to save // Object to store all the information we want to save
let config = { let config = {
userName: 'Anonymous', userName: '',
userAvatar: '', userAvatar: '',
rooms: [] rooms: []
}; };
@ -95,8 +95,13 @@ async function initialize() {
}); });
} }
readConfigFromFile();
config.rooms.forEach(room => {
addRoomToList(room);
});
const registerDiv = document.querySelector('#register'); const registerDiv = document.querySelector('#register');
if (registerDiv) { if (registerDiv && !config.userName) {
registerDiv.classList.remove('hidden'); registerDiv.classList.remove('hidden');
} }
@ -185,7 +190,7 @@ async function continueRegistration(regUsername) {
return; return;
} }
config.username = regUsername; config.userName = regUsername;
setupDiv.classList.remove('hidden'); setupDiv.classList.remove('hidden');
document.querySelector('#register').classList.add('hidden'); document.querySelector('#register').classList.add('hidden');
loadingDiv.classList.add('hidden'); loadingDiv.classList.add('hidden');
@ -193,7 +198,7 @@ async function continueRegistration(regUsername) {
const randomTopic = crypto.randomBytes(32); const randomTopic = crypto.randomBytes(32);
document.querySelector('#chat-room-topic').innerText = truncateHash(b4a.toString(randomTopic, 'hex')); document.querySelector('#chat-room-topic').innerText = truncateHash(b4a.toString(randomTopic, 'hex'));
writeJsonToFile("./config.json", config); writeConfigToFile("./config.json");
} }
async function createChatRoom() { async function createChatRoom() {
@ -240,7 +245,7 @@ function addRoomToList(topic) {
roomList.appendChild(roomItem); roomList.appendChild(roomItem);
config.rooms.push(topic); config.rooms.push(topic);
writeJsonToFile("./config.json", config); writeConfigToFile("./config.json");
} }
function switchRoom(topic) { function switchRoom(topic) {
@ -262,7 +267,7 @@ function leaveRoom() {
document.querySelector('#setup').classList.remove('hidden'); document.querySelector('#setup').classList.remove('hidden');
config.rooms = config.rooms.filter(e => e !== currentRoom.topic); config.rooms = config.rooms.filter(e => e !== currentRoom.topic);
writeJsonToFile("./config.json", config); writeConfigToFile("./config.json");
} }
function sendMessage(e) { function sendMessage(e) {
@ -397,7 +402,7 @@ async function updateIcon(username, avatarBuffer) {
userIcon.src = avatarUrl; userIcon.src = avatarUrl;
config.userAvatar = avatarUrl; config.userAvatar = avatarUrl;
writeJsonToFile("./config.json", config); writeConfigToFile("./config.json");
} }
} }
@ -413,11 +418,18 @@ function toggleSetupView() {
setupDiv.classList.toggle('hidden'); setupDiv.classList.toggle('hidden');
} }
function writeJsonToFile(filePath, data) { function writeConfigToFile(filePath) {
fs.writeFile(filePath, JSON.stringify(data), (err) => { fs.writeFile(filePath, JSON.stringify(config), (err) => {
if (err) return console.error(err); if (err) return console.error(err);
console.log("File has been created"); console.log("File has been created");
}); });
} }
function readConfigFromFile(filePath) {
fs.readFile(filePath, 'utf8', (err, data) => {
if(err) return console.error("Error while reading config.json! ", err);
config = JSON.parse(data);
});
}
initialize(); initialize();