Spaces:
Paused
Paused
Update index.html
Browse files- index.html +51 -0
index.html
CHANGED
|
@@ -600,6 +600,57 @@
|
|
| 600 |
userInput.addEventListener('keypress', (e) => { if (e.key === 'Enter') sendMessage(); });
|
| 601 |
renderQuestionBubbles();
|
| 602 |
updateWidget(null);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 603 |
</script>
|
| 604 |
</body>
|
| 605 |
</html>
|
|
|
|
| 600 |
userInput.addEventListener('keypress', (e) => { if (e.key === 'Enter') sendMessage(); });
|
| 601 |
renderQuestionBubbles();
|
| 602 |
updateWidget(null);
|
| 603 |
+
// ---------- Load chat history from Firebase ----------
|
| 604 |
+
let historyLoaded = false;
|
| 605 |
+
|
| 606 |
+
async function loadChatHistory() {
|
| 607 |
+
try {
|
| 608 |
+
const res = await fetch(`/api/history?session_id=${sessionId}`);
|
| 609 |
+
const data = await res.json();
|
| 610 |
+
if (data.messages && data.messages.length) {
|
| 611 |
+
const chatMessagesDiv = document.getElementById('chatMessages');
|
| 612 |
+
// Keep the welcome message (first bot message)
|
| 613 |
+
const children = Array.from(chatMessagesDiv.children);
|
| 614 |
+
let welcomeIndex = -1;
|
| 615 |
+
for (let i = 0; i < children.length; i++) {
|
| 616 |
+
const child = children[i];
|
| 617 |
+
if (child.classList && child.classList.contains('message') &&
|
| 618 |
+
child.classList.contains('bot-msg') &&
|
| 619 |
+
child.innerText.includes("Assalam-o-Alaikum")) {
|
| 620 |
+
welcomeIndex = i;
|
| 621 |
+
break;
|
| 622 |
+
}
|
| 623 |
+
}
|
| 624 |
+
// Remove all messages except the welcome message
|
| 625 |
+
for (let i = children.length - 1; i >= 0; i--) {
|
| 626 |
+
if (i !== welcomeIndex) {
|
| 627 |
+
children[i].remove();
|
| 628 |
+
}
|
| 629 |
+
}
|
| 630 |
+
// Add history messages after welcome message
|
| 631 |
+
for (let msg of data.messages) {
|
| 632 |
+
if (msg.role === 'user') {
|
| 633 |
+
addMessage(msg.content, 'user');
|
| 634 |
+
} else if (msg.role === 'assistant') {
|
| 635 |
+
addMessage(msg.content, 'bot');
|
| 636 |
+
}
|
| 637 |
+
}
|
| 638 |
+
}
|
| 639 |
+
} catch (err) {
|
| 640 |
+
console.error("Failed to load history", err);
|
| 641 |
+
}
|
| 642 |
+
}
|
| 643 |
+
|
| 644 |
+
// Modify the existing chatIcon.onclick to also load history
|
| 645 |
+
const originalOnClick = chatIcon.onclick;
|
| 646 |
+
chatIcon.onclick = async () => {
|
| 647 |
+
chatModal.style.display = 'flex';
|
| 648 |
+
if (questionsContainer.children.length === 0) renderQuestionBubbles();
|
| 649 |
+
if (!historyLoaded) {
|
| 650 |
+
await loadChatHistory();
|
| 651 |
+
historyLoaded = true;
|
| 652 |
+
}
|
| 653 |
+
};
|
| 654 |
</script>
|
| 655 |
</body>
|
| 656 |
</html>
|