Spaces:
Sleeping
Sleeping
Update frontend/app.js
Browse files- frontend/app.js +39 -2
frontend/app.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
// Same-origin API calls
|
| 2 |
const API_BASE = "";
|
| 3 |
|
| 4 |
-
//
|
| 5 |
function getSessionId() {
|
| 6 |
let sid = localStorage.getItem("session_id");
|
| 7 |
if (!sid) {
|
|
@@ -11,6 +11,13 @@ function getSessionId() {
|
|
| 11 |
return sid;
|
| 12 |
}
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
function addMessage(text, who = "bot") {
|
| 15 |
const win = document.getElementById("chat-window");
|
| 16 |
const div = document.createElement("div");
|
|
@@ -26,12 +33,19 @@ function disableInput(reason = "Input disabled.") {
|
|
| 26 |
document.getElementById("status").textContent = reason;
|
| 27 |
}
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
function clearChat() {
|
| 30 |
const win = document.getElementById("chat-window");
|
| 31 |
win.innerHTML = "";
|
| 32 |
document.getElementById("status").textContent = "";
|
| 33 |
}
|
| 34 |
|
|
|
|
| 35 |
async function moderateText(text) {
|
| 36 |
const sid = getSessionId();
|
| 37 |
const res = await fetch(`${API_BASE}/moderate`, {
|
|
@@ -60,12 +74,15 @@ async function sendChat(text) {
|
|
| 60 |
return res.json(); // {reply}
|
| 61 |
}
|
| 62 |
|
|
|
|
| 63 |
function submitName() {
|
| 64 |
const nameInput = document.getElementById("name-input");
|
| 65 |
const name = (nameInput.value || "").trim();
|
| 66 |
if (!name) return;
|
| 67 |
document.getElementById("name-section").classList.add("hidden");
|
| 68 |
document.getElementById("chat-section").classList.remove("hidden");
|
|
|
|
|
|
|
| 69 |
addMessage(`Welcome, ${name}!`, "bot");
|
| 70 |
addMessage(name, "user"); // immediately show name in chat
|
| 71 |
}
|
|
@@ -93,7 +110,27 @@ async function submitMessage() {
|
|
| 93 |
}
|
| 94 |
}
|
| 95 |
|
| 96 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
document.getElementById("name-submit").addEventListener("click", submitName);
|
| 98 |
document.getElementById("name-input").addEventListener("keydown", (e) => {
|
| 99 |
if (e.key === "Enter") submitName();
|
|
|
|
| 1 |
// Same-origin API calls
|
| 2 |
const API_BASE = "";
|
| 3 |
|
| 4 |
+
// Session utilities
|
| 5 |
function getSessionId() {
|
| 6 |
let sid = localStorage.getItem("session_id");
|
| 7 |
if (!sid) {
|
|
|
|
| 11 |
return sid;
|
| 12 |
}
|
| 13 |
|
| 14 |
+
function setNewSessionId() {
|
| 15 |
+
const sid = crypto.randomUUID();
|
| 16 |
+
localStorage.setItem("session_id", sid);
|
| 17 |
+
return sid;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
// UI helpers
|
| 21 |
function addMessage(text, who = "bot") {
|
| 22 |
const win = document.getElementById("chat-window");
|
| 23 |
const div = document.createElement("div");
|
|
|
|
| 33 |
document.getElementById("status").textContent = reason;
|
| 34 |
}
|
| 35 |
|
| 36 |
+
function enableInput() {
|
| 37 |
+
document.getElementById("chat-input").disabled = false;
|
| 38 |
+
document.getElementById("send-btn").disabled = false;
|
| 39 |
+
document.getElementById("status").textContent = "";
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
function clearChat() {
|
| 43 |
const win = document.getElementById("chat-window");
|
| 44 |
win.innerHTML = "";
|
| 45 |
document.getElementById("status").textContent = "";
|
| 46 |
}
|
| 47 |
|
| 48 |
+
// Backend calls
|
| 49 |
async function moderateText(text) {
|
| 50 |
const sid = getSessionId();
|
| 51 |
const res = await fetch(`${API_BASE}/moderate`, {
|
|
|
|
| 74 |
return res.json(); // {reply}
|
| 75 |
}
|
| 76 |
|
| 77 |
+
// Flow handlers
|
| 78 |
function submitName() {
|
| 79 |
const nameInput = document.getElementById("name-input");
|
| 80 |
const name = (nameInput.value || "").trim();
|
| 81 |
if (!name) return;
|
| 82 |
document.getElementById("name-section").classList.add("hidden");
|
| 83 |
document.getElementById("chat-section").classList.remove("hidden");
|
| 84 |
+
clearChat();
|
| 85 |
+
enableInput();
|
| 86 |
addMessage(`Welcome, ${name}!`, "bot");
|
| 87 |
addMessage(name, "user"); // immediately show name in chat
|
| 88 |
}
|
|
|
|
| 110 |
}
|
| 111 |
}
|
| 112 |
|
| 113 |
+
// Top-bar actions
|
| 114 |
+
function goHome() {
|
| 115 |
+
// Reload root to show name section by default
|
| 116 |
+
window.location.href = "/";
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
function newUser() {
|
| 120 |
+
// Reset session, show name input again, clear chat, re-enable input
|
| 121 |
+
setNewSessionId();
|
| 122 |
+
clearChat();
|
| 123 |
+
enableInput();
|
| 124 |
+
document.getElementById("chat-section").classList.add("hidden");
|
| 125 |
+
document.getElementById("name-section").classList.remove("hidden");
|
| 126 |
+
document.getElementById("name-input").value = "";
|
| 127 |
+
document.getElementById("chat-input").value = "";
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
// Wire up buttons and Enter keys
|
| 131 |
+
document.getElementById("home-btn").addEventListener("click", goHome);
|
| 132 |
+
document.getElementById("new-user-btn").addEventListener("click", newUser);
|
| 133 |
+
|
| 134 |
document.getElementById("name-submit").addEventListener("click", submitName);
|
| 135 |
document.getElementById("name-input").addEventListener("keydown", (e) => {
|
| 136 |
if (e.key === "Enter") submitName();
|