Update public/index.html
Browse files- public/index.html +34 -46
public/index.html
CHANGED
|
@@ -4,16 +4,15 @@
|
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
<title>Reiker Wa-Web</title>
|
| 7 |
-
<link rel="stylesheet" href="public/styles.css">
|
| 8 |
</head>
|
| 9 |
-
<body>
|
| 10 |
-
<header>
|
| 11 |
<h1>Reiker Wa-Web</h1>
|
| 12 |
</header>
|
| 13 |
-
<main>
|
| 14 |
<!-- Loading spinner -->
|
| 15 |
-
<div id="loading">
|
| 16 |
-
<div class="spinner"></div>
|
| 17 |
<p>Loading messages...</p>
|
| 18 |
</div>
|
| 19 |
<!-- Chats will be dynamically loaded here -->
|
|
@@ -25,13 +24,11 @@
|
|
| 25 |
const loadingSpinner = document.getElementById('loading');
|
| 26 |
const socket = io(); // Connect to WebSocket
|
| 27 |
|
| 28 |
-
// Function to show/hide loading spinner
|
| 29 |
function toggleLoading(show) {
|
| 30 |
loadingSpinner.style.display = show ? 'block' : 'none';
|
| 31 |
chatList.style.display = show ? 'none' : 'block';
|
| 32 |
}
|
| 33 |
|
| 34 |
-
// Function to truncate content to 3 words or fewer
|
| 35 |
function truncateContent(content) {
|
| 36 |
const words = content.split(' ');
|
| 37 |
if (words.length > 3) {
|
|
@@ -40,43 +37,51 @@
|
|
| 40 |
return content;
|
| 41 |
}
|
| 42 |
|
| 43 |
-
// Function to clean up private DM numbers (remove @s.whatsapp.net)
|
| 44 |
function formatChatName(chatName) {
|
| 45 |
return chatName.includes('@s.whatsapp.net')
|
| 46 |
? chatName.replace('@s.whatsapp.net', '')
|
| 47 |
: chatName;
|
| 48 |
}
|
| 49 |
|
| 50 |
-
// Group messages by chat name and keep only the latest message
|
| 51 |
function groupMessagesByChat(messages) {
|
| 52 |
const grouped = {};
|
| 53 |
messages.forEach(message => {
|
| 54 |
-
message.chatName = formatChatName(message.chatName);
|
| 55 |
-
grouped[message.chatName] = message;
|
| 56 |
});
|
| 57 |
return grouped;
|
| 58 |
}
|
| 59 |
|
| 60 |
-
// Function to render grouped chats
|
| 61 |
function renderGroupedChats(chats) {
|
| 62 |
-
chatList.innerHTML = '';
|
| 63 |
const groupedChats = groupMessagesByChat(chats);
|
| 64 |
|
| 65 |
Object.entries(groupedChats).forEach(([chatName, latestMessage]) => {
|
| 66 |
const chatDiv = document.createElement('div');
|
| 67 |
chatDiv.className = 'chat';
|
| 68 |
-
chatDiv.setAttribute('data-jid', latestMessage.jid);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
// Chat info
|
| 71 |
const chatInfo = document.createElement('div');
|
| 72 |
chatInfo.className = 'chat-info';
|
| 73 |
chatInfo.innerHTML = `
|
| 74 |
-
<h2>${chatName}</h2>
|
| 75 |
-
<p>${truncateContent(latestMessage.content)}</p>
|
| 76 |
`;
|
| 77 |
chatDiv.appendChild(chatInfo);
|
| 78 |
|
| 79 |
-
// Make chat clickable
|
| 80 |
chatDiv.addEventListener('click', () => {
|
| 81 |
window.location.href = `/chat/${latestMessage.jid}`;
|
| 82 |
});
|
|
@@ -85,55 +90,38 @@
|
|
| 85 |
});
|
| 86 |
}
|
| 87 |
|
| 88 |
-
// Function to merge/update chats and render them
|
| 89 |
-
function updateChatList(chats, newMessage) {
|
| 90 |
-
const groupedChats = groupMessagesByChat(chats);
|
| 91 |
-
|
| 92 |
-
// Update or add the new message
|
| 93 |
-
const chatName = formatChatName(newMessage.chatName);
|
| 94 |
-
groupedChats[chatName] = newMessage;
|
| 95 |
-
|
| 96 |
-
// Sort by timestamp (latest first)
|
| 97 |
-
const sortedChats = Object.values(groupedChats).sort(
|
| 98 |
-
(a, b) => b.timestamp - a.timestamp
|
| 99 |
-
);
|
| 100 |
-
|
| 101 |
-
renderGroupedChats(sortedChats); // Render sorted chats
|
| 102 |
-
}
|
| 103 |
-
|
| 104 |
-
// Function to fetch and render chat list
|
| 105 |
async function loadChatList() {
|
| 106 |
-
toggleLoading(true);
|
| 107 |
try {
|
| 108 |
const res = await fetch('/messages.json');
|
| 109 |
-
if (!res.ok) {
|
| 110 |
-
throw new Error(`HTTP error! status: ${res.status}`);
|
| 111 |
-
}
|
| 112 |
const chats = await res.json();
|
| 113 |
-
renderGroupedChats(chats);
|
| 114 |
} catch (err) {
|
| 115 |
console.error('Failed to load chat list:', err);
|
| 116 |
chatList.innerHTML = '<p>Failed to load chats. Try again later.</p>';
|
| 117 |
} finally {
|
| 118 |
-
toggleLoading(false);
|
| 119 |
}
|
| 120 |
}
|
| 121 |
|
| 122 |
-
// Initial fetch of chat list
|
| 123 |
loadChatList();
|
| 124 |
|
| 125 |
-
// Listen for new messages via WebSocket
|
| 126 |
socket.on('new_message', (newMessage) => {
|
| 127 |
-
// Fetch the current chat list and update/merge the new message
|
| 128 |
const currentChats = Array.from(chatList.children).map(chat => ({
|
| 129 |
jid: chat.getAttribute('data-jid'),
|
| 130 |
chatName: chat.querySelector('h2').innerText,
|
| 131 |
content: chat.querySelector('p').innerText,
|
| 132 |
}));
|
| 133 |
|
| 134 |
-
// Merge the new message and render
|
| 135 |
updateChatList(currentChats, newMessage);
|
| 136 |
});
|
| 137 |
</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
</body>
|
| 139 |
</html>
|
|
|
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
<title>Reiker Wa-Web</title>
|
|
|
|
| 7 |
</head>
|
| 8 |
+
<body style="margin: 0; font-family: Arial, sans-serif; background-color: #121212; color: #e0e0e0;">
|
| 9 |
+
<header style="background-color: #2a2a2a; padding: 10px 20px; text-align: center; border-bottom: 1px solid #333; color: #fff;">
|
| 10 |
<h1>Reiker Wa-Web</h1>
|
| 11 |
</header>
|
| 12 |
+
<main style="padding: 10px;">
|
| 13 |
<!-- Loading spinner -->
|
| 14 |
+
<div id="loading" style="display: none; text-align: center; margin-top: 50px;">
|
| 15 |
+
<div class="spinner" style="border: 4px solid #444; border-top: 4px solid #1a73e8; border-radius: 50%; width: 30px; height: 30px; animation: spin 1s linear infinite;"></div>
|
| 16 |
<p>Loading messages...</p>
|
| 17 |
</div>
|
| 18 |
<!-- Chats will be dynamically loaded here -->
|
|
|
|
| 24 |
const loadingSpinner = document.getElementById('loading');
|
| 25 |
const socket = io(); // Connect to WebSocket
|
| 26 |
|
|
|
|
| 27 |
function toggleLoading(show) {
|
| 28 |
loadingSpinner.style.display = show ? 'block' : 'none';
|
| 29 |
chatList.style.display = show ? 'none' : 'block';
|
| 30 |
}
|
| 31 |
|
|
|
|
| 32 |
function truncateContent(content) {
|
| 33 |
const words = content.split(' ');
|
| 34 |
if (words.length > 3) {
|
|
|
|
| 37 |
return content;
|
| 38 |
}
|
| 39 |
|
|
|
|
| 40 |
function formatChatName(chatName) {
|
| 41 |
return chatName.includes('@s.whatsapp.net')
|
| 42 |
? chatName.replace('@s.whatsapp.net', '')
|
| 43 |
: chatName;
|
| 44 |
}
|
| 45 |
|
|
|
|
| 46 |
function groupMessagesByChat(messages) {
|
| 47 |
const grouped = {};
|
| 48 |
messages.forEach(message => {
|
| 49 |
+
message.chatName = formatChatName(message.chatName);
|
| 50 |
+
grouped[message.chatName] = message;
|
| 51 |
});
|
| 52 |
return grouped;
|
| 53 |
}
|
| 54 |
|
|
|
|
| 55 |
function renderGroupedChats(chats) {
|
| 56 |
+
chatList.innerHTML = '';
|
| 57 |
const groupedChats = groupMessagesByChat(chats);
|
| 58 |
|
| 59 |
Object.entries(groupedChats).forEach(([chatName, latestMessage]) => {
|
| 60 |
const chatDiv = document.createElement('div');
|
| 61 |
chatDiv.className = 'chat';
|
| 62 |
+
chatDiv.setAttribute('data-jid', latestMessage.jid);
|
| 63 |
+
chatDiv.style.cssText = `
|
| 64 |
+
display: flex;
|
| 65 |
+
justify-content: space-between;
|
| 66 |
+
align-items: center;
|
| 67 |
+
background-color: #1e1e1e;
|
| 68 |
+
padding: 15px;
|
| 69 |
+
border-radius: 8px;
|
| 70 |
+
margin-bottom: 10px;
|
| 71 |
+
cursor: pointer;
|
| 72 |
+
transition: background-color 0.2s ease;
|
| 73 |
+
`;
|
| 74 |
+
chatDiv.addEventListener('mouseover', () => chatDiv.style.backgroundColor = '#2c2c2c');
|
| 75 |
+
chatDiv.addEventListener('mouseout', () => chatDiv.style.backgroundColor = '#1e1e1e');
|
| 76 |
|
|
|
|
| 77 |
const chatInfo = document.createElement('div');
|
| 78 |
chatInfo.className = 'chat-info';
|
| 79 |
chatInfo.innerHTML = `
|
| 80 |
+
<h2 style="margin: 0; font-size: 1rem; font-weight: bold; color: #fff;">${chatName}</h2>
|
| 81 |
+
<p style="margin: 5px 0 0; font-size: 0.9rem; color: #aaa;">${truncateContent(latestMessage.content)}</p>
|
| 82 |
`;
|
| 83 |
chatDiv.appendChild(chatInfo);
|
| 84 |
|
|
|
|
| 85 |
chatDiv.addEventListener('click', () => {
|
| 86 |
window.location.href = `/chat/${latestMessage.jid}`;
|
| 87 |
});
|
|
|
|
| 90 |
});
|
| 91 |
}
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
async function loadChatList() {
|
| 94 |
+
toggleLoading(true);
|
| 95 |
try {
|
| 96 |
const res = await fetch('/messages.json');
|
| 97 |
+
if (!res.ok) throw new Error(`HTTP error! status: ${res.status}`);
|
|
|
|
|
|
|
| 98 |
const chats = await res.json();
|
| 99 |
+
renderGroupedChats(chats);
|
| 100 |
} catch (err) {
|
| 101 |
console.error('Failed to load chat list:', err);
|
| 102 |
chatList.innerHTML = '<p>Failed to load chats. Try again later.</p>';
|
| 103 |
} finally {
|
| 104 |
+
toggleLoading(false);
|
| 105 |
}
|
| 106 |
}
|
| 107 |
|
|
|
|
| 108 |
loadChatList();
|
| 109 |
|
|
|
|
| 110 |
socket.on('new_message', (newMessage) => {
|
|
|
|
| 111 |
const currentChats = Array.from(chatList.children).map(chat => ({
|
| 112 |
jid: chat.getAttribute('data-jid'),
|
| 113 |
chatName: chat.querySelector('h2').innerText,
|
| 114 |
content: chat.querySelector('p').innerText,
|
| 115 |
}));
|
| 116 |
|
|
|
|
| 117 |
updateChatList(currentChats, newMessage);
|
| 118 |
});
|
| 119 |
</script>
|
| 120 |
+
<style>
|
| 121 |
+
@keyframes spin {
|
| 122 |
+
0% { transform: rotate(0deg); }
|
| 123 |
+
100% { transform: rotate(360deg); }
|
| 124 |
+
}
|
| 125 |
+
</style>
|
| 126 |
</body>
|
| 127 |
</html>
|