Spaces:
Paused
Paused
Update public/admin.html
Browse files- public/admin.html +28 -266
public/admin.html
CHANGED
|
@@ -201,199 +201,6 @@
|
|
| 201 |
|
| 202 |
<script>
|
| 203 |
let currentUserId = null;
|
| 204 |
-
let eventSource = null;
|
| 205 |
-
let isTyping = false;
|
| 206 |
-
let typingTimeout = null;
|
| 207 |
-
|
| 208 |
-
// Initialize EventSource connection for admin
|
| 209 |
-
function initializeAdminEventSource() {
|
| 210 |
-
const protocol = window.location.protocol === 'https:' ? 'https:' : 'http:';
|
| 211 |
-
const host = window.location.host;
|
| 212 |
-
|
| 213 |
-
eventSource = new EventSource(`${protocol}//${host}/events/admin`);
|
| 214 |
-
|
| 215 |
-
eventSource.onopen = () => {
|
| 216 |
-
console.log('Admin connected to event stream');
|
| 217 |
-
showNotification('Connected to chat server', 'success');
|
| 218 |
-
};
|
| 219 |
-
|
| 220 |
-
eventSource.onerror = (error) => {
|
| 221 |
-
console.error('EventSource error:', error);
|
| 222 |
-
showNotification('Chat connection error', 'error');
|
| 223 |
-
};
|
| 224 |
-
|
| 225 |
-
eventSource.onmessage = (event) => {
|
| 226 |
-
try {
|
| 227 |
-
const data = JSON.parse(event.data);
|
| 228 |
-
handleEvent(data);
|
| 229 |
-
} catch (error) {
|
| 230 |
-
console.error('Error parsing event data:', error);
|
| 231 |
-
}
|
| 232 |
-
};
|
| 233 |
-
}
|
| 234 |
-
|
| 235 |
-
// Handle different event types
|
| 236 |
-
function handleEvent(data) {
|
| 237 |
-
switch (data.event) {
|
| 238 |
-
case 'connected':
|
| 239 |
-
console.log('Connected to event stream:', data.data);
|
| 240 |
-
break;
|
| 241 |
-
|
| 242 |
-
case 'new-message':
|
| 243 |
-
console.log('New message received:', data.data);
|
| 244 |
-
// Add message to current chat if it's the active user
|
| 245 |
-
if (data.data.message.user_id === currentUserId) {
|
| 246 |
-
addMessageToAdminChat(data.data.message);
|
| 247 |
-
}
|
| 248 |
-
break;
|
| 249 |
-
|
| 250 |
-
case 'user-typing':
|
| 251 |
-
if (data.data.userId === currentUserId) {
|
| 252 |
-
showTypingIndicator(data.data.userEmail);
|
| 253 |
-
}
|
| 254 |
-
break;
|
| 255 |
-
|
| 256 |
-
case 'user-stopped-typing':
|
| 257 |
-
if (data.data.userId === currentUserId) {
|
| 258 |
-
hideTypingIndicator();
|
| 259 |
-
}
|
| 260 |
-
break;
|
| 261 |
-
|
| 262 |
-
case 'message-sent':
|
| 263 |
-
console.log('Message sent successfully:', data.data);
|
| 264 |
-
break;
|
| 265 |
-
|
| 266 |
-
case 'message-error':
|
| 267 |
-
console.error('Message error:', data.data);
|
| 268 |
-
showNotification('Failed to send message: ' + data.data.error, 'error');
|
| 269 |
-
break;
|
| 270 |
-
|
| 271 |
-
case 'server-log':
|
| 272 |
-
// Handle server logs if needed
|
| 273 |
-
break;
|
| 274 |
-
|
| 275 |
-
case 'system-status':
|
| 276 |
-
// Handle system status updates if needed
|
| 277 |
-
break;
|
| 278 |
-
|
| 279 |
-
default:
|
| 280 |
-
console.log('Unknown event:', data.event, data.data);
|
| 281 |
-
}
|
| 282 |
-
}
|
| 283 |
-
|
| 284 |
-
// Add message to admin chat without reloading
|
| 285 |
-
function addMessageToAdminChat(message) {
|
| 286 |
-
const container = document.getElementById('chatMessages');
|
| 287 |
-
if (!container) return;
|
| 288 |
-
|
| 289 |
-
// Remove typing indicator if present
|
| 290 |
-
hideTypingIndicator();
|
| 291 |
-
|
| 292 |
-
const div = document.createElement('div');
|
| 293 |
-
div.className = `flex ${message.sender === 'admin' ? 'justify-end' : 'justify-start'} mb-3`;
|
| 294 |
-
div.innerHTML = `
|
| 295 |
-
<div class="max-w-xs px-4 py-2 rounded-2xl ${message.sender === 'admin' ? 'bg-blue-600' : 'bg-gray-700'}">
|
| 296 |
-
<p class="text-sm text-white">${escapeHtml(message.content)}</p>
|
| 297 |
-
<p class="text-xs opacity-70 mt-1 text-white">${new Date(message.created_at).toLocaleString()}</p>
|
| 298 |
-
</div>
|
| 299 |
-
`;
|
| 300 |
-
container.appendChild(div);
|
| 301 |
-
container.scrollTop = container.scrollHeight;
|
| 302 |
-
|
| 303 |
-
// Add message to chat history
|
| 304 |
-
addMessageToHistory(message);
|
| 305 |
-
}
|
| 306 |
-
|
| 307 |
-
// Show typing indicator
|
| 308 |
-
function showTypingIndicator(userEmail) {
|
| 309 |
-
const container = document.getElementById('chatMessages');
|
| 310 |
-
if (!container) return;
|
| 311 |
-
|
| 312 |
-
// Remove existing typing indicator
|
| 313 |
-
hideTypingIndicator();
|
| 314 |
-
|
| 315 |
-
const typingDiv = document.createElement('div');
|
| 316 |
-
typingDiv.id = 'typing-indicator';
|
| 317 |
-
typingDiv.className = 'flex justify-start mb-3';
|
| 318 |
-
typingDiv.innerHTML = `
|
| 319 |
-
<div class="max-w-xs px-4 py-2 rounded-2xl bg-gray-700">
|
| 320 |
-
<p class="text-sm text-white opacity-70">${userEmail} is typing...</p>
|
| 321 |
-
</div>
|
| 322 |
-
`;
|
| 323 |
-
container.appendChild(typingDiv);
|
| 324 |
-
container.scrollTop = container.scrollHeight;
|
| 325 |
-
}
|
| 326 |
-
|
| 327 |
-
// Hide typing indicator
|
| 328 |
-
function hideTypingIndicator() {
|
| 329 |
-
const typingIndicator = document.getElementById('typing-indicator');
|
| 330 |
-
if (typingIndicator) {
|
| 331 |
-
typingIndicator.remove();
|
| 332 |
-
}
|
| 333 |
-
}
|
| 334 |
-
|
| 335 |
-
// Add message to chat history for persistence
|
| 336 |
-
function addMessageToHistory(message) {
|
| 337 |
-
// Store in session storage for persistence during session
|
| 338 |
-
let chatHistory = JSON.parse(sessionStorage.getItem(`chat_${currentUserId}`) || '[]');
|
| 339 |
-
chatHistory.push(message);
|
| 340 |
-
|
| 341 |
-
// Keep only last 100 messages
|
| 342 |
-
if (chatHistory.length > 100) {
|
| 343 |
-
chatHistory = chatHistory.slice(-100);
|
| 344 |
-
}
|
| 345 |
-
|
| 346 |
-
sessionStorage.setItem(`chat_${currentUserId}`, JSON.stringify(chatHistory));
|
| 347 |
-
}
|
| 348 |
-
|
| 349 |
-
// Escape HTML to prevent XSS
|
| 350 |
-
function escapeHtml(text) {
|
| 351 |
-
const div = document.createElement('div');
|
| 352 |
-
div.textContent = text;
|
| 353 |
-
return div.innerHTML;
|
| 354 |
-
}
|
| 355 |
-
|
| 356 |
-
// Handle typing events
|
| 357 |
-
async function handleTyping() {
|
| 358 |
-
if (!currentUserId) return;
|
| 359 |
-
|
| 360 |
-
if (!isTyping) {
|
| 361 |
-
isTyping = true;
|
| 362 |
-
try {
|
| 363 |
-
await fetch('/api/admin/typing', {
|
| 364 |
-
method: 'POST',
|
| 365 |
-
headers: { 'Content-Type': 'application/json' },
|
| 366 |
-
body: JSON.stringify({ userId: currentUserId })
|
| 367 |
-
});
|
| 368 |
-
} catch (error) {
|
| 369 |
-
console.error('Error sending typing indicator:', error);
|
| 370 |
-
}
|
| 371 |
-
}
|
| 372 |
-
|
| 373 |
-
// Clear existing timeout
|
| 374 |
-
if (typingTimeout) {
|
| 375 |
-
clearTimeout(typingTimeout);
|
| 376 |
-
}
|
| 377 |
-
|
| 378 |
-
// Set timeout to stop typing indicator
|
| 379 |
-
typingTimeout = setTimeout(async () => {
|
| 380 |
-
isTyping = false;
|
| 381 |
-
try {
|
| 382 |
-
await fetch('/api/admin/stop-typing', {
|
| 383 |
-
method: 'POST',
|
| 384 |
-
headers: { 'Content-Type': 'application/json' },
|
| 385 |
-
body: JSON.stringify({ userId: currentUserId })
|
| 386 |
-
});
|
| 387 |
-
} catch (error) {
|
| 388 |
-
console.error('Error sending stop typing indicator:', error);
|
| 389 |
-
}
|
| 390 |
-
}, 1000);
|
| 391 |
-
}
|
| 392 |
-
|
| 393 |
-
// Initialize EventSource when page loads
|
| 394 |
-
document.addEventListener('DOMContentLoaded', function() {
|
| 395 |
-
initializeAdminEventSource();
|
| 396 |
-
});
|
| 397 |
|
| 398 |
// Auth Functions
|
| 399 |
async function sendOTP() {
|
|
@@ -577,44 +384,9 @@
|
|
| 577 |
document.getElementById('chatEmail').textContent = email;
|
| 578 |
document.getElementById('chatModal').classList.remove('hidden');
|
| 579 |
loadMessages();
|
| 580 |
-
|
| 581 |
-
// Join admin chat room
|
| 582 |
-
if (eventSource) {
|
| 583 |
-
console.log('Joined chat room for user:', userId);
|
| 584 |
-
} else {
|
| 585 |
-
console.warn('EventSource not connected, cannot join chat room');
|
| 586 |
-
}
|
| 587 |
-
|
| 588 |
-
// Add typing event listener to message input
|
| 589 |
-
const messageInput = document.getElementById('messageInput');
|
| 590 |
-
if (messageInput) {
|
| 591 |
-
messageInput.addEventListener('input', handleTyping);
|
| 592 |
-
messageInput.addEventListener('keydown', (e) => {
|
| 593 |
-
if (e.key === 'Enter' && !e.shiftKey) {
|
| 594 |
-
e.preventDefault();
|
| 595 |
-
sendMessage();
|
| 596 |
-
}
|
| 597 |
-
});
|
| 598 |
-
}
|
| 599 |
}
|
| 600 |
|
| 601 |
function closeChat() {
|
| 602 |
-
// Remove typing event listeners
|
| 603 |
-
const messageInput = document.getElementById('messageInput');
|
| 604 |
-
if (messageInput) {
|
| 605 |
-
messageInput.removeEventListener('input', handleTyping);
|
| 606 |
-
}
|
| 607 |
-
|
| 608 |
-
// Clear typing state
|
| 609 |
-
isTyping = false;
|
| 610 |
-
if (typingTimeout) {
|
| 611 |
-
clearTimeout(typingTimeout);
|
| 612 |
-
typingTimeout = null;
|
| 613 |
-
}
|
| 614 |
-
|
| 615 |
-
// Hide typing indicator
|
| 616 |
-
hideTypingIndicator();
|
| 617 |
-
|
| 618 |
document.getElementById('chatModal').classList.add('hidden');
|
| 619 |
currentUserId = null;
|
| 620 |
}
|
|
@@ -623,71 +395,61 @@
|
|
| 623 |
if (!currentUserId) return;
|
| 624 |
|
| 625 |
try {
|
| 626 |
-
const response = await fetch(`/api/admin/messages/${currentUserId}
|
| 627 |
const data = await response.json();
|
| 628 |
if (data.success) {
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
<div class="flex ${m.sender === 'admin' ? 'justify-end' : 'justify-start'} mb-3">
|
| 632 |
<div class="max-w-xs px-4 py-2 rounded-2xl ${m.sender === 'admin' ? 'bg-blue-600' : 'bg-gray-700'}">
|
| 633 |
-
<p class="text-sm
|
| 634 |
-
<p class="text-xs opacity-70 mt-1
|
| 635 |
</div>
|
| 636 |
</div>
|
| 637 |
`).join('');
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
// Store messages in session storage
|
| 641 |
-
sessionStorage.setItem(`chat_${currentUserId}`, JSON.stringify(data.messages));
|
| 642 |
}
|
| 643 |
} catch (error) {
|
| 644 |
-
console.error('Error loading messages:', error);
|
| 645 |
document.getElementById('chatMessages').innerHTML = '<div class="text-center text-red-400">Error loading messages</div>';
|
| 646 |
}
|
| 647 |
}
|
| 648 |
|
| 649 |
-
async
|
| 650 |
-
|
| 651 |
-
const message = messageInput.value.trim();
|
| 652 |
if (!message || !currentUserId) return;
|
| 653 |
|
| 654 |
-
//
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
|
| 664 |
try {
|
| 665 |
-
// Send message via API to save to database
|
| 666 |
const response = await fetch('/api/admin/messages', {
|
| 667 |
method: 'POST',
|
| 668 |
headers: { 'Content-Type': 'application/json' },
|
| 669 |
body: JSON.stringify({ userId: currentUserId, content: message })
|
| 670 |
});
|
| 671 |
-
|
| 672 |
const data = await response.json();
|
| 673 |
if (data.success) {
|
| 674 |
-
|
| 675 |
-
|
| 676 |
} else {
|
| 677 |
-
|
| 678 |
}
|
| 679 |
} catch (error) {
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
|
|
|
|
|
|
| 685 |
}
|
| 686 |
-
}
|
| 687 |
-
|
| 688 |
-
document.getElementById('chatForm').addEventListener('submit', async (e) => {
|
| 689 |
-
e.preventDefault();
|
| 690 |
-
await sendMessage();
|
| 691 |
});
|
| 692 |
|
| 693 |
// Utility Functions
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
// Auth Functions
|
| 206 |
async function sendOTP() {
|
|
|
|
| 384 |
document.getElementById('chatEmail').textContent = email;
|
| 385 |
document.getElementById('chatModal').classList.remove('hidden');
|
| 386 |
loadMessages();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
}
|
| 388 |
|
| 389 |
function closeChat() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 390 |
document.getElementById('chatModal').classList.add('hidden');
|
| 391 |
currentUserId = null;
|
| 392 |
}
|
|
|
|
| 395 |
if (!currentUserId) return;
|
| 396 |
|
| 397 |
try {
|
| 398 |
+
const response = await fetch(`/api/admin/messages/${currentUserId}`);
|
| 399 |
const data = await response.json();
|
| 400 |
if (data.success) {
|
| 401 |
+
document.getElementById('chatMessages').innerHTML = data.messages.map(m => `
|
| 402 |
+
<div class="flex ${m.sender === 'admin' ? 'justify-end' : 'justify-start'}">
|
|
|
|
| 403 |
<div class="max-w-xs px-4 py-2 rounded-2xl ${m.sender === 'admin' ? 'bg-blue-600' : 'bg-gray-700'}">
|
| 404 |
+
<p class="text-sm">${m.content}</p>
|
| 405 |
+
<p class="text-xs opacity-70 mt-1">${new Date(m.created_at).toLocaleString()}</p>
|
| 406 |
</div>
|
| 407 |
</div>
|
| 408 |
`).join('');
|
| 409 |
+
document.getElementById('chatMessages').scrollTop = document.getElementById('chatMessages').scrollHeight;
|
|
|
|
|
|
|
|
|
|
| 410 |
}
|
| 411 |
} catch (error) {
|
|
|
|
| 412 |
document.getElementById('chatMessages').innerHTML = '<div class="text-center text-red-400">Error loading messages</div>';
|
| 413 |
}
|
| 414 |
}
|
| 415 |
|
| 416 |
+
document.getElementById('chatForm').addEventListener('submit', async (e) => {
|
| 417 |
+
e.preventDefault();
|
| 418 |
+
const message = document.getElementById('messageInput').value.trim();
|
| 419 |
if (!message || !currentUserId) return;
|
| 420 |
|
| 421 |
+
// Get the send button and input field
|
| 422 |
+
const sendButton = e.target.querySelector('button[type="submit"]');
|
| 423 |
+
const messageInput = document.getElementById('messageInput');
|
| 424 |
+
const originalButtonContent = sendButton.innerHTML;
|
| 425 |
+
|
| 426 |
+
// Show loading state
|
| 427 |
+
sendButton.innerHTML = '<i class="fas fa-spinner loading"></i>';
|
| 428 |
+
sendButton.disabled = true;
|
| 429 |
+
messageInput.disabled = true;
|
| 430 |
|
| 431 |
try {
|
|
|
|
| 432 |
const response = await fetch('/api/admin/messages', {
|
| 433 |
method: 'POST',
|
| 434 |
headers: { 'Content-Type': 'application/json' },
|
| 435 |
body: JSON.stringify({ userId: currentUserId, content: message })
|
| 436 |
});
|
|
|
|
| 437 |
const data = await response.json();
|
| 438 |
if (data.success) {
|
| 439 |
+
document.getElementById('messageInput').value = '';
|
| 440 |
+
loadMessages();
|
| 441 |
} else {
|
| 442 |
+
showNotification('Failed to send message', 'error');
|
| 443 |
}
|
| 444 |
} catch (error) {
|
| 445 |
+
showNotification('Failed to send message', 'error');
|
| 446 |
+
} finally {
|
| 447 |
+
// Restore button and input state
|
| 448 |
+
sendButton.innerHTML = originalButtonContent;
|
| 449 |
+
sendButton.disabled = false;
|
| 450 |
+
messageInput.disabled = false;
|
| 451 |
+
messageInput.focus();
|
| 452 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
});
|
| 454 |
|
| 455 |
// Utility Functions
|