Files
wt b833266664
Continuous integration / Check (push) Successful in 35s
Continuous integration / Rustfmt (push) Successful in 31s
Continuous integration / build (push) Successful in 30s
Continuous integration / build-release (push) Successful in 51s
more refactor in frontend javascript
2025-11-28 02:28:52 +07:00

127 lines
3.5 KiB
JavaScript

let ws = null;
let currentUser = "";
let currentToken = "";
let currentRecipient = "";
let currentUserAvatar = "";
let currentUserPhone = "";
let chats = [];
let searchResults = [];
let messageHistory = {};
let currentChatTab = "existing";
let messageToDelete = null;
let isMobile = window.innerWidth <= 768;
let reconnectTimeout = null;
let reconnectAttempts = 0;
const MAX_RECONNECT_ATTEMPTS = 5;
const RECONNECT_DELAY = 3000;
function changePhoneNumber() {
const newPhone = prompt("Enter your new phone number:", currentUserPhone);
if (newPhone && newPhone.trim() !== "") {
// In a real app, you would send this to the server
currentUserPhone = newPhone.trim();
localStorage.setItem("chatUserPhone", currentUserPhone);
updateProfileModal();
showAlert("Phone number updated successfully!", "success");
}
}
function showTab(tabName) {
document
.querySelectorAll(".auth-tab")
.forEach((tab) => tab.classList.remove("active"));
document
.querySelectorAll(".auth-form")
.forEach((form) => form.classList.remove("active"));
if (tabName === "login") {
document.querySelector(".auth-tab:nth-child(1)").classList.add("active");
document.getElementById("loginForm").classList.add("active");
} else {
document.querySelector(".auth-tab:nth-child(2)").classList.add("active");
document.getElementById("registerForm").classList.add("active");
}
hideAlert();
}
function showAlert(message, type) {
const alert = document.getElementById("alert");
alert.textContent = message;
alert.className = `alert alert-${type}`;
alert.style.display = "block";
// Auto hide after 5 seconds
setTimeout(hideAlert, 5000);
}
function hideAlert() {
document.getElementById("alert").style.display = "none";
}
async function validateToken(token, username) {
try {
const response = await fetch("/api/validate-token", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ token }),
});
const data = await response.json();
if (data.success) {
currentUser = data.username;
currentToken = token;
currentUserAvatar = data.avatar_url;
localStorage.setItem("chatToken", token);
localStorage.setItem("chatUsername", data.username);
document.getElementById("authContainer").style.display = "none";
document.getElementById("chatContainer").style.display = "flex";
updateUserProfile();
loadChats();
connectWebSocket();
} else {
localStorage.removeItem("chatToken");
localStorage.removeItem("chatUsername");
showAlert("Session expired. Please login again.", "error");
}
} catch (error) {
console.error("Token validation failed:", error);
localStorage.removeItem("chatToken");
localStorage.removeItem("chatUsername");
showAlert("Session validation failed. Please login again.", "error");
}
}
function updateUserProfile() {
document.getElementById("currentUsername").textContent = currentUser;
updateAllAvatars();
}
function generateId() {
return Date.now().toString(36) + Math.random().toString(36).substr(2);
}
function formatTime(timestamp) {
if (!timestamp) return "";
const date = new Date(timestamp);
const now = new Date();
const diff = now - date;
if (diff < 24 * 60 * 60 * 1000) {
return date.toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
});
} else {
return date.toLocaleDateString([], {
month: "short",
day: "numeric",
});
}
}