Spaces:
Sleeping
Sleeping
Update script.js
Browse files
script.js
CHANGED
|
@@ -1,22 +1,17 @@
|
|
| 1 |
-
/*
|
| 2 |
-
|
| 3 |
-
|
|
|
|
| 4 |
const firebaseConfig = {
|
| 5 |
apiKey: "YOUR_API_KEY",
|
| 6 |
authDomain: "YOUR_PROJECT.firebaseapp.com",
|
| 7 |
projectId: "YOUR_PROJECT",
|
| 8 |
};
|
| 9 |
-
|
| 10 |
firebase.initializeApp(firebaseConfig);
|
| 11 |
-
const auth = firebase.auth();
|
| 12 |
|
| 13 |
-
|
| 14 |
-
return document.getElementById(id);
|
| 15 |
-
}
|
| 16 |
|
| 17 |
-
/*
|
| 18 |
-
AUTH – Google Login
|
| 19 |
-
--------------------------------------------------*/
|
| 20 |
async function handleGoogleLogin() {
|
| 21 |
const provider = new firebase.auth.GoogleAuthProvider();
|
| 22 |
|
|
@@ -24,11 +19,9 @@ async function handleGoogleLogin() {
|
|
| 24 |
const result = await auth.signInWithPopup(provider);
|
| 25 |
const idToken = await result.user.getIdToken();
|
| 26 |
|
| 27 |
-
// Send token to backend
|
| 28 |
await fetch("/api/login", {
|
| 29 |
method: "POST",
|
| 30 |
headers: { "Content-Type": "application/json" },
|
| 31 |
-
credentials: "include",
|
| 32 |
body: JSON.stringify({ token: idToken }),
|
| 33 |
});
|
| 34 |
|
|
@@ -38,17 +31,16 @@ async function handleGoogleLogin() {
|
|
| 38 |
}
|
| 39 |
}
|
| 40 |
|
|
|
|
| 41 |
async function handleLogout() {
|
| 42 |
-
await fetch("/api/logout", { method: "POST"
|
| 43 |
await auth.signOut();
|
| 44 |
updateAuthUI();
|
| 45 |
}
|
| 46 |
|
| 47 |
-
/*
|
| 48 |
-
AUTH UI
|
| 49 |
-
--------------------------------------------------*/
|
| 50 |
async function updateAuthUI() {
|
| 51 |
-
const
|
| 52 |
const userSection = el("userSection");
|
| 53 |
const userPhoto = el("userPhoto");
|
| 54 |
const userDropdown = el("userDropdown");
|
|
@@ -56,184 +48,172 @@ async function updateAuthUI() {
|
|
| 56 |
const userEmail = el("userEmail");
|
| 57 |
|
| 58 |
try {
|
| 59 |
-
const res = await fetch("/api/me"
|
| 60 |
const j = await res.json();
|
| 61 |
|
| 62 |
if (j.authenticated) {
|
| 63 |
-
//
|
| 64 |
-
|
| 65 |
|
| 66 |
-
//
|
| 67 |
userSection.classList.remove("hidden");
|
| 68 |
-
userPhoto.src = j.picture || "https://i.imgur.com/5cF5pUo.png";
|
| 69 |
-
|
| 70 |
userName.textContent = j.name || "User";
|
| 71 |
userEmail.textContent = j.email || "";
|
|
|
|
| 72 |
|
| 73 |
-
// Enable deep analysis
|
| 74 |
el("openDeep").disabled = false;
|
| 75 |
-
|
| 76 |
} else {
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
| 78 |
userSection.classList.add("hidden");
|
| 79 |
-
el("openDeep").disabled = true;
|
| 80 |
|
| 81 |
-
|
|
|
|
| 82 |
}
|
| 83 |
} catch (e) {
|
| 84 |
-
console.warn("Auth check failed", e);
|
| 85 |
}
|
| 86 |
}
|
| 87 |
-
|
| 88 |
updateAuthUI();
|
| 89 |
|
| 90 |
-
/*
|
| 91 |
-
Dropdown Toggle
|
| 92 |
-
--------------------------------------------------*/
|
| 93 |
el("userSection").addEventListener("click", () => {
|
| 94 |
el("userDropdown").classList.toggle("hidden");
|
| 95 |
});
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
Auth Guard for Deep Analysis
|
| 101 |
-
--------------------------------------------------*/
|
| 102 |
-
el("openDeep").addEventListener("click", async () => {
|
| 103 |
-
const me = await fetch("/api/me", { credentials: "include" }).then((r) =>
|
| 104 |
-
r.json()
|
| 105 |
-
);
|
| 106 |
-
|
| 107 |
-
if (!me.authenticated) {
|
| 108 |
-
alert("Please login to use Deep Analysis.");
|
| 109 |
-
return;
|
| 110 |
}
|
|
|
|
| 111 |
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
});
|
| 114 |
|
| 115 |
-
/*
|
| 116 |
-
Open / Close Modal
|
| 117 |
-
--------------------------------------------------*/
|
| 118 |
function openModal(mode) {
|
| 119 |
el("inputModal").classList.remove("hidden");
|
| 120 |
el("modalTitle").textContent =
|
| 121 |
mode === "deep" ? "Start Deep Analysis" : "Start Fast Analysis";
|
| 122 |
-
el("formStatus").textContent = "";
|
| 123 |
el("analyzeBtn").dataset.mode = mode;
|
|
|
|
| 124 |
}
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
-
|
| 135 |
-
el("closeModal").
|
| 136 |
-
el("cancelBtn").
|
| 137 |
|
| 138 |
-
/*
|
| 139 |
-
Run Analysis
|
| 140 |
-
--------------------------------------------------*/
|
| 141 |
el("analyzeBtn").addEventListener("click", async function () {
|
| 142 |
-
const mode = this.dataset.mode
|
|
|
|
| 143 |
const name = el("name").value.trim();
|
| 144 |
const pitch = el("pitch").value.trim();
|
| 145 |
const description = el("description").value.trim();
|
| 146 |
const industry = el("industry").value.trim();
|
| 147 |
|
| 148 |
if (!name && !pitch && !description) {
|
| 149 |
-
el("formStatus").textContent =
|
| 150 |
-
"Please fill at least Name or Pitch or Description.";
|
| 151 |
return;
|
| 152 |
}
|
| 153 |
|
| 154 |
-
el("formStatus").textContent = "Running analysis...";
|
| 155 |
|
| 156 |
try {
|
| 157 |
const resp = await fetch("/api/analyze", {
|
| 158 |
method: "POST",
|
| 159 |
headers: { "Content-Type": "application/json" },
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
});
|
| 163 |
|
| 164 |
const j = await resp.json();
|
| 165 |
|
| 166 |
if (!resp.ok) {
|
| 167 |
-
el("formStatus").textContent = j.error || "
|
| 168 |
return;
|
| 169 |
}
|
| 170 |
|
| 171 |
const id = j.id;
|
| 172 |
|
| 173 |
el("formStatus").innerHTML = `
|
| 174 |
-
Analysis
|
| 175 |
-
<a href="/api/doc/${id}" target="_blank">View JSON</a> •
|
| 176 |
-
<a href="/api/pdf/${id}" target="_blank">Download
|
| 177 |
`;
|
| 178 |
|
| 179 |
fetchHistory();
|
| 180 |
} catch (err) {
|
| 181 |
-
el("formStatus").textContent = "Network error
|
| 182 |
}
|
| 183 |
});
|
| 184 |
|
| 185 |
-
/*
|
| 186 |
-
Load History
|
| 187 |
-
--------------------------------------------------*/
|
| 188 |
async function fetchHistory() {
|
|
|
|
|
|
|
| 189 |
try {
|
| 190 |
-
|
| 191 |
-
const j = await r.json();
|
| 192 |
|
| 193 |
-
const
|
| 194 |
-
const
|
| 195 |
|
| 196 |
-
|
| 197 |
|
| 198 |
-
if (items.length === 0) {
|
| 199 |
-
|
| 200 |
return;
|
| 201 |
}
|
| 202 |
|
| 203 |
-
items.forEach((it) => {
|
| 204 |
-
const
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
<strong>${
|
| 209 |
-
<span class="muted">(${it.industry || "
|
| 210 |
-
<div class="
|
| 211 |
-
|
| 212 |
-
<a href="/api/doc/${it.id}" target="_blank">json</a> •
|
| 213 |
-
<a href="/api/pdf/${it.id}" target="_blank">
|
| 214 |
</div>
|
| 215 |
`;
|
| 216 |
|
| 217 |
-
|
| 218 |
});
|
| 219 |
} catch (e) {
|
| 220 |
-
console.
|
| 221 |
}
|
| 222 |
}
|
| 223 |
|
| 224 |
fetchHistory();
|
| 225 |
-
|
| 226 |
-
/* ------------------------------------------------
|
| 227 |
-
Helpers
|
| 228 |
-
--------------------------------------------------*/
|
| 229 |
-
function escapeHtml(s) {
|
| 230 |
-
return String(s || "").replace(/[&<>"']/g, (c) => {
|
| 231 |
-
return {
|
| 232 |
-
"&": "&",
|
| 233 |
-
"<": "<",
|
| 234 |
-
">": ">",
|
| 235 |
-
'"': """,
|
| 236 |
-
"'": "'",
|
| 237 |
-
}[c];
|
| 238 |
-
});
|
| 239 |
-
}
|
|
|
|
| 1 |
+
/* ======================= SHORTCUT ======================= */
|
| 2 |
+
const el = (id) => document.getElementById(id);
|
| 3 |
+
|
| 4 |
+
/* ======================= FIREBASE CONFIG ======================= */
|
| 5 |
const firebaseConfig = {
|
| 6 |
apiKey: "YOUR_API_KEY",
|
| 7 |
authDomain: "YOUR_PROJECT.firebaseapp.com",
|
| 8 |
projectId: "YOUR_PROJECT",
|
| 9 |
};
|
|
|
|
| 10 |
firebase.initializeApp(firebaseConfig);
|
|
|
|
| 11 |
|
| 12 |
+
const auth = firebase.auth();
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
/* ======================= LOGIN FUNCTION ======================= */
|
|
|
|
|
|
|
| 15 |
async function handleGoogleLogin() {
|
| 16 |
const provider = new firebase.auth.GoogleAuthProvider();
|
| 17 |
|
|
|
|
| 19 |
const result = await auth.signInWithPopup(provider);
|
| 20 |
const idToken = await result.user.getIdToken();
|
| 21 |
|
|
|
|
| 22 |
await fetch("/api/login", {
|
| 23 |
method: "POST",
|
| 24 |
headers: { "Content-Type": "application/json" },
|
|
|
|
| 25 |
body: JSON.stringify({ token: idToken }),
|
| 26 |
});
|
| 27 |
|
|
|
|
| 31 |
}
|
| 32 |
}
|
| 33 |
|
| 34 |
+
/* ======================= LOGOUT FUNCTION ======================= */
|
| 35 |
async function handleLogout() {
|
| 36 |
+
await fetch("/api/logout", { method: "POST" });
|
| 37 |
await auth.signOut();
|
| 38 |
updateAuthUI();
|
| 39 |
}
|
| 40 |
|
| 41 |
+
/* ======================= UPDATE LOGIN UI ======================= */
|
|
|
|
|
|
|
| 42 |
async function updateAuthUI() {
|
| 43 |
+
const openGoogle = el("openGoogle");
|
| 44 |
const userSection = el("userSection");
|
| 45 |
const userPhoto = el("userPhoto");
|
| 46 |
const userDropdown = el("userDropdown");
|
|
|
|
| 48 |
const userEmail = el("userEmail");
|
| 49 |
|
| 50 |
try {
|
| 51 |
+
const res = await fetch("/api/me");
|
| 52 |
const j = await res.json();
|
| 53 |
|
| 54 |
if (j.authenticated) {
|
| 55 |
+
// (A) HIDE LOGIN BUTTON
|
| 56 |
+
openGoogle.classList.add("hidden");
|
| 57 |
|
| 58 |
+
// (B) SHOW PROFILE SECTION
|
| 59 |
userSection.classList.remove("hidden");
|
|
|
|
|
|
|
| 60 |
userName.textContent = j.name || "User";
|
| 61 |
userEmail.textContent = j.email || "";
|
| 62 |
+
userPhoto.src = j.picture || "https://i.imgur.com/4ZQZ4ZQ.png";
|
| 63 |
|
|
|
|
| 64 |
el("openDeep").disabled = false;
|
|
|
|
| 65 |
} else {
|
| 66 |
+
// (A) SHOW LOGIN
|
| 67 |
+
openGoogle.classList.remove("hidden");
|
| 68 |
+
|
| 69 |
+
// (B) HIDE PROFILE
|
| 70 |
userSection.classList.add("hidden");
|
|
|
|
| 71 |
|
| 72 |
+
openGoogle.onclick = handleGoogleLogin;
|
| 73 |
+
el("openDeep").disabled = true;
|
| 74 |
}
|
| 75 |
} catch (e) {
|
| 76 |
+
console.warn("Auth check failed:", e);
|
| 77 |
}
|
| 78 |
}
|
|
|
|
| 79 |
updateAuthUI();
|
| 80 |
|
| 81 |
+
/* ======================= DROPDOWN TOGGLE ======================= */
|
|
|
|
|
|
|
| 82 |
el("userSection").addEventListener("click", () => {
|
| 83 |
el("userDropdown").classList.toggle("hidden");
|
| 84 |
});
|
| 85 |
|
| 86 |
+
document.addEventListener("click", (e) => {
|
| 87 |
+
if (!el("userSection").contains(e.target)) {
|
| 88 |
+
el("userDropdown").classList.add("hidden");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
}
|
| 90 |
+
});
|
| 91 |
|
| 92 |
+
/* ======================= SMOOTH SCROLL TO CARDS ======================= */
|
| 93 |
+
document.querySelectorAll("#openFast, #openDeep").forEach((btn) => {
|
| 94 |
+
btn.addEventListener("click", () => {
|
| 95 |
+
document
|
| 96 |
+
.getElementById("analysisSection")
|
| 97 |
+
.scrollIntoView({ behavior: "smooth" });
|
| 98 |
+
});
|
| 99 |
});
|
| 100 |
|
| 101 |
+
/* ======================= OPEN MODAL ======================= */
|
|
|
|
|
|
|
| 102 |
function openModal(mode) {
|
| 103 |
el("inputModal").classList.remove("hidden");
|
| 104 |
el("modalTitle").textContent =
|
| 105 |
mode === "deep" ? "Start Deep Analysis" : "Start Fast Analysis";
|
|
|
|
| 106 |
el("analyzeBtn").dataset.mode = mode;
|
| 107 |
+
el("formStatus").textContent = "";
|
| 108 |
}
|
| 109 |
|
| 110 |
+
/* Fast Analysis Card Button */
|
| 111 |
+
document.querySelectorAll(".start").forEach((b) => {
|
| 112 |
+
b.addEventListener("click", () => {
|
| 113 |
+
openModal(b.dataset.mode);
|
| 114 |
+
});
|
| 115 |
+
});
|
| 116 |
+
|
| 117 |
+
/* Deep Button Auth Guard */
|
| 118 |
+
el("openDeep").addEventListener("click", async () => {
|
| 119 |
+
const me = await fetch("/api/me").then((r) => r.json());
|
| 120 |
+
|
| 121 |
+
if (!me.authenticated) {
|
| 122 |
+
alert("Please login first.");
|
| 123 |
+
return;
|
| 124 |
+
}
|
| 125 |
+
});
|
| 126 |
|
| 127 |
+
/* Close Modal */
|
| 128 |
+
el("closeModal").onclick = () => el("inputModal").classList.add("hidden");
|
| 129 |
+
el("cancelBtn").onclick = () => el("inputModal").classList.add("hidden");
|
| 130 |
|
| 131 |
+
/* ======================= ANALYZE ======================= */
|
|
|
|
|
|
|
| 132 |
el("analyzeBtn").addEventListener("click", async function () {
|
| 133 |
+
const mode = this.dataset.mode;
|
| 134 |
+
|
| 135 |
const name = el("name").value.trim();
|
| 136 |
const pitch = el("pitch").value.trim();
|
| 137 |
const description = el("description").value.trim();
|
| 138 |
const industry = el("industry").value.trim();
|
| 139 |
|
| 140 |
if (!name && !pitch && !description) {
|
| 141 |
+
el("formStatus").textContent = "Please fill at least one field.";
|
|
|
|
| 142 |
return;
|
| 143 |
}
|
| 144 |
|
| 145 |
+
el("formStatus").textContent = "Running AI analysis...";
|
| 146 |
|
| 147 |
try {
|
| 148 |
const resp = await fetch("/api/analyze", {
|
| 149 |
method: "POST",
|
| 150 |
headers: { "Content-Type": "application/json" },
|
| 151 |
+
body: JSON.stringify({
|
| 152 |
+
name,
|
| 153 |
+
pitch,
|
| 154 |
+
description,
|
| 155 |
+
industry,
|
| 156 |
+
mode,
|
| 157 |
+
}),
|
| 158 |
});
|
| 159 |
|
| 160 |
const j = await resp.json();
|
| 161 |
|
| 162 |
if (!resp.ok) {
|
| 163 |
+
el("formStatus").textContent = j.error || "Error!";
|
| 164 |
return;
|
| 165 |
}
|
| 166 |
|
| 167 |
const id = j.id;
|
| 168 |
|
| 169 |
el("formStatus").innerHTML = `
|
| 170 |
+
Analysis Ready 🎉 <br>
|
| 171 |
+
<a href="/api/doc/${id}" target="_blank">View JSON</a> •
|
| 172 |
+
<a href="/api/pdf/${id}" target="_blank">Download TXT</a>
|
| 173 |
`;
|
| 174 |
|
| 175 |
fetchHistory();
|
| 176 |
} catch (err) {
|
| 177 |
+
el("formStatus").textContent = "Network error.";
|
| 178 |
}
|
| 179 |
});
|
| 180 |
|
| 181 |
+
/* ======================= HISTORY LOADER ======================= */
|
|
|
|
|
|
|
| 182 |
async function fetchHistory() {
|
| 183 |
+
const historyList = el("historyList");
|
| 184 |
+
|
| 185 |
try {
|
| 186 |
+
historyList.innerHTML = "Loading...";
|
|
|
|
| 187 |
|
| 188 |
+
const r = await fetch("/api/history");
|
| 189 |
+
const j = await r.json();
|
| 190 |
|
| 191 |
+
historyList.innerHTML = "";
|
| 192 |
|
| 193 |
+
if (!j.items || j.items.length === 0) {
|
| 194 |
+
historyList.innerHTML = '<div class="muted">No recent analyses</div>';
|
| 195 |
return;
|
| 196 |
}
|
| 197 |
|
| 198 |
+
j.items.forEach((it) => {
|
| 199 |
+
const div = document.createElement("div");
|
| 200 |
+
div.className = "history-item";
|
| 201 |
+
|
| 202 |
+
div.innerHTML = `
|
| 203 |
+
<strong>${it.name || it.pitch || "Unnamed"}</strong>
|
| 204 |
+
<span class="muted">(${it.industry || "General"})</span>
|
| 205 |
+
<div class="small muted">
|
| 206 |
+
Mode: ${it.mode} •
|
| 207 |
+
<a href="/api/doc/${it.id}" target="_blank">json</a> •
|
| 208 |
+
<a href="/api/pdf/${it.id}" target="_blank">txt</a>
|
| 209 |
</div>
|
| 210 |
`;
|
| 211 |
|
| 212 |
+
historyList.appendChild(div);
|
| 213 |
});
|
| 214 |
} catch (e) {
|
| 215 |
+
console.error(e);
|
| 216 |
}
|
| 217 |
}
|
| 218 |
|
| 219 |
fetchHistory();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|