Update script.js
Browse files
script.js
CHANGED
|
@@ -9,7 +9,10 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
| 9 |
const chatContainer = document.getElementById("chat-container");
|
| 10 |
const typing = document.getElementById("typing");
|
| 11 |
const loginForm = document.getElementById("login-form");
|
|
|
|
| 12 |
const chatSection = document.getElementById("chat-section");
|
|
|
|
|
|
|
| 13 |
const modal = document.getElementById("settings-modal");
|
| 14 |
|
| 15 |
// ------------------
|
|
@@ -52,19 +55,24 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
| 52 |
// Check Session
|
| 53 |
// ------------------
|
| 54 |
async function checkSession() {
|
| 55 |
-
if (!token) return;
|
| 56 |
try {
|
| 57 |
const res = await fetch("/api/session", { headers: authHeaders() });
|
| 58 |
const data = await res.json();
|
| 59 |
if (data.user) {
|
| 60 |
loginForm.style.display = "none";
|
|
|
|
| 61 |
chatSection.style.display = "block";
|
|
|
|
|
|
|
| 62 |
} else {
|
| 63 |
localStorage.removeItem("jwtToken");
|
| 64 |
token = null;
|
|
|
|
| 65 |
}
|
| 66 |
} catch (err) {
|
| 67 |
console.error("Session check failed:", err);
|
|
|
|
| 68 |
}
|
| 69 |
}
|
| 70 |
|
|
@@ -91,6 +99,8 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
| 91 |
alert("✅ " + data.message);
|
| 92 |
loginForm.style.display = "none";
|
| 93 |
chatSection.style.display = "block";
|
|
|
|
|
|
|
| 94 |
}
|
| 95 |
} catch (err) {
|
| 96 |
alert("❌ Login failed: Could not reach server");
|
|
@@ -178,33 +188,47 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
| 178 |
};
|
| 179 |
|
| 180 |
// ------------------
|
| 181 |
-
// Load AI Apps / Bots
|
| 182 |
// ------------------
|
| 183 |
async function loadAIApps() {
|
|
|
|
| 184 |
try {
|
| 185 |
-
const res = await fetch("/api/
|
| 186 |
const data = await res.json();
|
| 187 |
-
const appsContainer = document.getElementById("ai-apps-container");
|
| 188 |
appsContainer.innerHTML = "";
|
| 189 |
-
|
| 190 |
-
|
|
|
|
| 191 |
const div = document.createElement("div");
|
| 192 |
-
div.className = "ai-app";
|
| 193 |
-
div.
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
appsContainer.appendChild(div);
|
| 199 |
});
|
|
|
|
|
|
|
| 200 |
}
|
| 201 |
} catch (err) {
|
| 202 |
console.error("Failed to load AI apps:", err);
|
|
|
|
| 203 |
}
|
| 204 |
}
|
| 205 |
|
| 206 |
// ------------------
|
| 207 |
-
//
|
| 208 |
// ------------------
|
| 209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
});
|
|
|
|
| 9 |
const chatContainer = document.getElementById("chat-container");
|
| 10 |
const typing = document.getElementById("typing");
|
| 11 |
const loginForm = document.getElementById("login-form");
|
| 12 |
+
const signupForm = document.getElementById("signup-form");
|
| 13 |
const chatSection = document.getElementById("chat-section");
|
| 14 |
+
const aiAppsSection = document.getElementById("ai-apps-section");
|
| 15 |
+
const appsContainer = document.getElementById("ai-apps-container");
|
| 16 |
const modal = document.getElementById("settings-modal");
|
| 17 |
|
| 18 |
// ------------------
|
|
|
|
| 55 |
// Check Session
|
| 56 |
// ------------------
|
| 57 |
async function checkSession() {
|
| 58 |
+
if (!token) return false;
|
| 59 |
try {
|
| 60 |
const res = await fetch("/api/session", { headers: authHeaders() });
|
| 61 |
const data = await res.json();
|
| 62 |
if (data.user) {
|
| 63 |
loginForm.style.display = "none";
|
| 64 |
+
signupForm.style.display = "none";
|
| 65 |
chatSection.style.display = "block";
|
| 66 |
+
aiAppsSection.style.display = "block";
|
| 67 |
+
return true;
|
| 68 |
} else {
|
| 69 |
localStorage.removeItem("jwtToken");
|
| 70 |
token = null;
|
| 71 |
+
return false;
|
| 72 |
}
|
| 73 |
} catch (err) {
|
| 74 |
console.error("Session check failed:", err);
|
| 75 |
+
return false;
|
| 76 |
}
|
| 77 |
}
|
| 78 |
|
|
|
|
| 99 |
alert("✅ " + data.message);
|
| 100 |
loginForm.style.display = "none";
|
| 101 |
chatSection.style.display = "block";
|
| 102 |
+
aiAppsSection.style.display = "block";
|
| 103 |
+
loadAIApps();
|
| 104 |
}
|
| 105 |
} catch (err) {
|
| 106 |
alert("❌ Login failed: Could not reach server");
|
|
|
|
| 188 |
};
|
| 189 |
|
| 190 |
// ------------------
|
| 191 |
+
// Load AI Apps / Bots
|
| 192 |
// ------------------
|
| 193 |
async function loadAIApps() {
|
| 194 |
+
if (!token) return;
|
| 195 |
try {
|
| 196 |
+
const res = await fetch("/api/ai_apps", { headers: authHeaders() });
|
| 197 |
const data = await res.json();
|
|
|
|
| 198 |
appsContainer.innerHTML = "";
|
| 199 |
+
|
| 200 |
+
if (data.apps && data.apps.length > 0) {
|
| 201 |
+
data.apps.forEach(app => {
|
| 202 |
const div = document.createElement("div");
|
| 203 |
+
div.className = "ai-app-card";
|
| 204 |
+
div.innerHTML = `
|
| 205 |
+
<h3>${app.name}</h3>
|
| 206 |
+
<p>Language: ${app.language}</p>
|
| 207 |
+
<button onclick="openAIApp('${app.id}')">Open App</button>
|
| 208 |
+
`;
|
| 209 |
appsContainer.appendChild(div);
|
| 210 |
});
|
| 211 |
+
} else {
|
| 212 |
+
appsContainer.textContent = "No AI apps yet.";
|
| 213 |
}
|
| 214 |
} catch (err) {
|
| 215 |
console.error("Failed to load AI apps:", err);
|
| 216 |
+
appsContainer.textContent = "Error loading AI apps.";
|
| 217 |
}
|
| 218 |
}
|
| 219 |
|
| 220 |
// ------------------
|
| 221 |
+
// Open AI App
|
| 222 |
// ------------------
|
| 223 |
+
window.openAIApp = function(appId) {
|
| 224 |
+
alert("Opening AI App ID: " + appId);
|
| 225 |
+
// Future: Load its code or open a chat for experimentation
|
| 226 |
+
};
|
| 227 |
+
|
| 228 |
+
// ------------------
|
| 229 |
+
// Run session check on load
|
| 230 |
+
// ------------------
|
| 231 |
+
checkSession().then(valid => {
|
| 232 |
+
if (valid) loadAIApps();
|
| 233 |
+
});
|
| 234 |
});
|