P2THE28 commited on
Commit
9395891
·
verified ·
1 Parent(s): 410eb92

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +94 -114
script.js CHANGED
@@ -1,22 +1,17 @@
1
- /* ------------------------------------------------
2
- Firebase Setup
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
- function el(id) {
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", credentials: "include" });
43
  await auth.signOut();
44
  updateAuthUI();
45
  }
46
 
47
- /* ------------------------------------------------
48
- AUTH UI
49
- --------------------------------------------------*/
50
  async function updateAuthUI() {
51
- const loginBtn = el("openGoogle");
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", { credentials: "include" });
60
  const j = await res.json();
61
 
62
  if (j.authenticated) {
63
- // Hide login button
64
- loginBtn.classList.add("hidden");
65
 
66
- // Show user profile
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
- loginBtn.classList.remove("hidden");
 
 
 
78
  userSection.classList.add("hidden");
79
- el("openDeep").disabled = true;
80
 
81
- loginBtn.onclick = handleGoogleLogin;
 
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
- el("logoutBtn").addEventListener("click", handleLogout);
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
- openModal("deep");
 
 
 
 
 
 
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
- function closeModal() {
127
- el("inputModal").classList.add("hidden");
128
- el("name").value = "";
129
- el("pitch").value = "";
130
- el("description").value = "";
131
- el("industry").value = "";
132
- }
 
 
 
 
 
 
 
 
 
133
 
134
- el("openFast").addEventListener("click", () => openModal("fast"));
135
- el("closeModal").addEventListener("click", closeModal);
136
- el("cancelBtn").addEventListener("click", closeModal);
137
 
138
- /* ------------------------------------------------
139
- Run Analysis
140
- --------------------------------------------------*/
141
  el("analyzeBtn").addEventListener("click", async function () {
142
- const mode = this.dataset.mode || "fast";
 
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
- credentials: "include",
161
- body: JSON.stringify({ name, pitch, description, industry, mode }),
 
 
 
 
 
162
  });
163
 
164
  const j = await resp.json();
165
 
166
  if (!resp.ok) {
167
- el("formStatus").textContent = j.error || "Analysis failed";
168
  return;
169
  }
170
 
171
  const id = j.id;
172
 
173
  el("formStatus").innerHTML = `
174
- Analysis ready
175
- <a href="/api/doc/${id}" target="_blank">View JSON</a> •
176
- <a href="/api/pdf/${id}" target="_blank">Download PDF</a>
177
  `;
178
 
179
  fetchHistory();
180
  } catch (err) {
181
- el("formStatus").textContent = "Network error: " + err.message;
182
  }
183
  });
184
 
185
- /* ------------------------------------------------
186
- Load History
187
- --------------------------------------------------*/
188
  async function fetchHistory() {
 
 
189
  try {
190
- const r = await fetch("/api/history?limit=10");
191
- const j = await r.json();
192
 
193
- const items = j.items || [];
194
- const container = el("historyList");
195
 
196
- container.innerHTML = "";
197
 
198
- if (items.length === 0) {
199
- container.innerHTML = `<div class="muted">No recent analyses</div>`;
200
  return;
201
  }
202
 
203
- items.forEach((it) => {
204
- const d = document.createElement("div");
205
- d.className = "history-item";
206
-
207
- d.innerHTML = `
208
- <strong>${escapeHtml(it.name || it.pitch || "Untitled")}</strong>
209
- <span class="muted">(${it.industry || ""})</span>
210
- <div class="muted small">
211
- Score: ${it.score || "—"} •
212
- <a href="/api/doc/${it.id}" target="_blank">json</a> •
213
- <a href="/api/pdf/${it.id}" target="_blank">pdf</a>
214
  </div>
215
  `;
216
 
217
- container.appendChild(d);
218
  });
219
  } catch (e) {
220
- console.log("History load failed", e);
221
  }
222
  }
223
 
224
  fetchHistory();
225
-
226
- /* ------------------------------------------------
227
- Helpers
228
- --------------------------------------------------*/
229
- function escapeHtml(s) {
230
- return String(s || "").replace(/[&<>"']/g, (c) => {
231
- return {
232
- "&": "&amp;",
233
- "<": "&lt;",
234
- ">": "&gt;",
235
- '"': "&quot;",
236
- "'": "&#39;",
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();