Spaces:
Sleeping
Sleeping
File size: 18,998 Bytes
3e802a5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
document.addEventListener("DOMContentLoaded", () => {
// --- Constants ---
const GITHUB_TOKEN_KEY = "github_access_token";
// --- DOM Element Cache ---
const docForm = document.getElementById("doc-form");
const submitBtn = document.getElementById("submit-btn");
const authSection = document.getElementById("auth-section");
const mainContent = document.getElementById("main-content");
const githubLoginBtn = document.getElementById("github-login-btn");
const selectZipBtn = document.getElementById("select-zip-btn");
const selectGithubBtn = document.getElementById("select-github-btn");
const zipInputs = document.getElementById("zip-inputs");
const githubInputs = document.getElementById("github-inputs");
const zipFileInput = document.getElementById("zip-file");
const repoSelect = document.getElementById("repo-select");
const baseBranchSelect = document.getElementById("base-branch-select");
const newBranchInput = document.getElementById("new-branch-input");
const branchNameError = document.getElementById("branch-name-error");
const fileTreeContainer = document.getElementById("file-tree-container");
const fileTree = document.getElementById("file-tree");
const liveProgressView = document.getElementById("live-progress-view");
const resultSection = document.getElementById("result-section");
const resultLink = document.getElementById("result-link");
const logOutput = document.getElementById("log-output");
// --- Helper Functions ---
const showView = (viewId) => {
[authSection, mainContent, liveProgressView, resultSection].forEach((el) =>
el.classList.add("hidden")
);
document.getElementById(viewId).classList.remove("hidden");
};
const sanitizeForId = (str) =>
`subtask-${str.replace(/[^a-zA-Z0-9-]/g, "-")}`;
const resetProgressView = () => {
document
.querySelectorAll(".phase-item")
.forEach((item) => (item.dataset.status = "pending"));
document
.querySelectorAll(".subtask-list")
.forEach((list) => (list.innerHTML = ""));
logOutput.textContent = "";
};
const createTreeHtml = (nodes, pathPrefix = "") => {
let html = "<ul>";
nodes.forEach((node) => {
const fullPath = pathPrefix ? `${pathPrefix}/${node.name}` : node.name;
const isDir = !!node.children;
html += `<li><input type="checkbox" name="exclude_paths" value="${fullPath}" id="cb-${fullPath}"> <label for="cb-${fullPath}"><strong>${
node.name
}${isDir ? "/" : ""}</strong></label>`;
if (isDir) html += createTreeHtml(node.children, fullPath);
html += "</li>";
});
html += "</ul>";
return html;
};
// --- Core Logic (unchanged app behavior) ---
const checkBranchName = async () => {
const repoFullName = repoSelect.value;
const branchName = newBranchInput.value.trim();
const token = localStorage.getItem(GITHUB_TOKEN_KEY);
branchNameError.textContent = "";
branchNameError.style.display = "none";
if (
!repoFullName ||
!branchName ||
!token ||
!selectGithubBtn.classList.contains("active")
) {
submitBtn.disabled = false;
return;
}
submitBtn.disabled = true;
submitBtn
.querySelector(".btn-inner span:last-child")
?.replaceWith(document.createTextNode("Checking branch..."));
try {
const response = await fetch(
`/api/github/branch-exists?repo_full_name=${encodeURIComponent(
repoFullName
)}&branch_name=${encodeURIComponent(branchName)}`,
{
headers: { Authorization: `Bearer ${token}` },
}
);
if (!response.ok) {
const errData = await response.json();
throw new Error(errData.detail || `Server error: ${response.status}`);
}
const data = await response.json();
if (data.exists) {
branchNameError.textContent = `Branch '${branchName}' already exists. Please choose another name.`;
branchNameError.style.display = "block";
submitBtn.disabled = true;
} else {
submitBtn.disabled = false;
}
} catch (error) {
console.error("Error checking branch name:", error);
branchNameError.textContent = `Could not verify branch name. ${error.message}`;
branchNameError.style.display = "block";
submitBtn.disabled = false; // Allow submission, server will catch it if it's a real issue.
} finally {
// Restore button text
const inner = submitBtn.querySelector(".btn-inner");
if (inner) {
inner.innerHTML =
'<span class="loader" aria-hidden="true"></span><span>Generate Documentation</span>';
}
}
};
const handleAuth = () => {
const urlParams = new URLSearchParams(window.location.search);
const token = urlParams.get("token");
const error = urlParams.get("error");
if (error) alert(`Authentication failed: ${error}`);
if (token && token !== "None")
localStorage.setItem(GITHUB_TOKEN_KEY, token);
window.history.replaceState({}, document.title, "/");
if (localStorage.getItem(GITHUB_TOKEN_KEY)) {
// --- Logged In User Flow ---
showView("main-content");
fetchGithubRepos();
switchMode("github");
} else {
// --- Logged Out User Flow (FIXED) ---
// 1. Always show the main form content, not the separate auth screen.
// This makes the form, including ZIP upload, always accessible.
showView("main-content");
// 2. The auth section with the login button should be visible.
// We manually un-hide it because showView is designed to show only one view.
authSection.classList.remove("hidden");
// 3. Default to ZIP mode and disable the GitHub tab.
switchMode("zip");
selectGithubBtn.disabled = true;
}
};
const fetchGithubRepos = async () => {
const token = localStorage.getItem(GITHUB_TOKEN_KEY);
if (!token) return;
try {
const response = await fetch("/api/github/repos", {
headers: { Authorization: `Bearer ${token}` },
});
if (response.status === 401) {
localStorage.removeItem(GITHUB_TOKEN_KEY);
alert("GitHub session expired. Please log in again.");
handleAuth();
return;
}
if (!response.ok) throw new Error("Failed to fetch repos");
const repos = await response.json();
repoSelect.innerHTML =
'<option value="">-- Select a repository --</option>';
repos.forEach((repo) => {
const option = document.createElement("option");
option.value = repo.full_name;
option.textContent = repo.full_name;
option.dataset.defaultBranch = repo.default_branch;
repoSelect.appendChild(option);
});
} catch (error) {
console.error(error);
}
};
const fetchRepoBranches = async (repoFullName, defaultBranch) => {
const token = localStorage.getItem(GITHUB_TOKEN_KEY);
if (!token || !repoFullName) return;
baseBranchSelect.innerHTML = "<option>Loading branches...</option>";
baseBranchSelect.disabled = true;
try {
const response = await fetch(
`/api/github/branches?repo_full_name=${repoFullName}`,
{
headers: { Authorization: `Bearer ${token}` },
}
);
if (!response.ok) throw new Error("Failed to fetch branches");
const branches = await response.json();
baseBranchSelect.innerHTML = "";
branches.forEach((branchName) => {
const option = document.createElement("option");
option.value = branchName;
option.textContent = branchName;
if (branchName === defaultBranch) option.selected = true;
baseBranchSelect.appendChild(option);
});
} catch (error) {
console.error(error);
baseBranchSelect.innerHTML = `<option>Error loading branches</option>`;
} finally {
baseBranchSelect.disabled = false;
}
};
const fetchAndBuildTree = async (repoFullName, branch) => {
fileTreeContainer.classList.remove("hidden");
fileTree.innerHTML = "<em>Loading repository file tree...</em>";
const token = localStorage.getItem(GITHUB_TOKEN_KEY);
if (!token || !repoFullName || !branch) return;
try {
const response = await fetch(
`/api/github/tree?repo_full_name=${repoFullName}&branch=${branch}`,
{
headers: { Authorization: `Bearer ${token}` },
}
);
if (!response.ok)
throw new Error(
`Failed to fetch file tree (status: ${response.status})`
);
const treeData = await response.json();
fileTree.innerHTML = createTreeHtml(treeData);
} catch (error) {
console.error(error);
fileTree.innerHTML = `<em style="color: #ef4444;">${error.message}</em>`;
}
};
// --- In script.js ---
const switchMode = (mode) => {
fileTreeContainer.classList.add("hidden");
if (mode === "github") {
selectGithubBtn.classList.add("active");
selectZipBtn.classList.remove("active");
githubInputs.classList.remove("hidden");
zipInputs.classList.add("hidden");
// GitHub Mode: ZIP is not required, GitHub fields are.
zipFileInput.required = false;
repoSelect.required = true;
baseBranchSelect.required = true;
newBranchInput.required = true;
if (repoSelect.value)
fetchAndBuildTree(repoSelect.value, baseBranchSelect.value);
} else { // This is ZIP mode
selectZipBtn.classList.add("active");
selectGithubBtn.classList.remove("active");
zipInputs.classList.remove("hidden");
githubInputs.classList.add("hidden");
// ZIP Mode: ZIP is required, GitHub fields are not.
zipFileInput.required = true;
repoSelect.required = false;
baseBranchSelect.required = false; // <-- FIX: Added this line
newBranchInput.required = false; // <-- FIX: Added this line
}
};
const handleFormSubmit = (e) => {
e.preventDefault();
resetProgressView();
showView("live-progress-view");
submitBtn.disabled = true;
const formData = new FormData(docForm);
const endpoint = selectGithubBtn.classList.contains("active")
? "/process-github"
: "/process-zip";
const headers = new Headers();
if (endpoint === "/process-github") {
headers.append(
"Authorization",
`Bearer ${localStorage.getItem(GITHUB_TOKEN_KEY)}`
);
}
const options = { method: "POST", body: formData, headers: headers };
fetch(endpoint, options)
.then((response) => {
if (!response.ok) {
return response.json().then((errData) => {
throw new Error(
`Server error: ${response.status} - ${
errData.detail || "Unknown error"
}`
);
});
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
function push() {
reader
.read()
.then(({ done, value }) => {
if (done) {
if (buffer) {
try {
const json = JSON.parse(buffer);
handleStreamEvent(json.type, json.payload);
} catch (e) {
console.error(
"Error parsing final buffer chunk:",
buffer,
e
);
}
}
return;
}
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n");
buffer = lines.pop();
for (const line of lines) {
if (line.trim() === "") continue;
try {
const json = JSON.parse(line);
handleStreamEvent(json.type, json.payload);
} catch (e) {
console.error("Failed to parse JSON line:", line, e);
}
}
push();
})
.catch((err) => {
console.error("Stream reading error:", err);
handleStreamEvent("error", `Stream error: ${err.message}`);
});
}
push();
})
.catch((err) => handleStreamEvent("error", `${err.message}`));
};
const handleStreamEvent = (type, payload) => {
switch (type) {
case "phase": {
const phaseEl = document.getElementById(`phase-${payload.id}`);
if (phaseEl) phaseEl.dataset.status = payload.status;
break;
}
case "subtask": {
const subtaskId = sanitizeForId(payload.id);
let subtaskEl = document.getElementById(subtaskId);
const listEl = document.getElementById(payload.listId);
if (!subtaskEl && listEl) {
subtaskEl = document.createElement("li");
subtaskEl.id = subtaskId;
subtaskEl.textContent = payload.name;
listEl.appendChild(subtaskEl);
}
if (subtaskEl) subtaskEl.dataset.status = payload.status;
break;
}
case "log": {
logOutput.textContent += payload.message.replace(/\\n/g, "\n") + "\n";
logOutput.scrollTop = logOutput.scrollHeight;
break;
}
case "error": {
document
.querySelectorAll('.phase-item[data-status="in-progress"]')
.forEach((el) => (el.dataset.status = "error"));
logOutput.textContent += `\n\n--- ERROR ---\n${payload}\n`;
submitBtn.disabled = false;
break;
}
case "done": {
showView("result-section");
resultLink.innerHTML = `<p>${payload.message}</p>`;
if (payload.type === "zip") {
resultLink.innerHTML += `<a href="/download/${payload.download_path}" class="button-link" download>Download ZIP</a>`;
} else if (payload.url) {
const linkText = payload.url.includes("/pull/new/")
? "Create Pull Request"
: "View Repository";
resultLink.innerHTML += `<a href="${payload.url}" target="_blank" rel="noopener noreferrer" class="button-link">${linkText}</a>`;
}
submitBtn.disabled = false;
break;
}
}
};
// --- Events ---
githubLoginBtn.addEventListener(
"click",
() => (window.location.href = "/login/github")
);
selectZipBtn.addEventListener("click", () => switchMode("zip"));
selectGithubBtn.addEventListener("click", () => switchMode("github"));
docForm.addEventListener("submit", handleFormSubmit);
repoSelect.addEventListener("change", async (e) => {
const selectedOption = e.target.options[e.target.selectedIndex];
const repoFullName = selectedOption.value;
const defaultBranch = selectedOption.dataset.defaultBranch || "";
if (repoFullName) {
await fetchRepoBranches(repoFullName, defaultBranch);
fetchAndBuildTree(repoFullName, baseBranchSelect.value);
checkBranchName();
} else {
baseBranchSelect.innerHTML = "";
fileTreeContainer.classList.add("hidden");
}
});
baseBranchSelect.addEventListener("change", () => {
fetchAndBuildTree(repoSelect.value, baseBranchSelect.value);
});
newBranchInput.addEventListener("blur", checkBranchName);
// Initialize
handleAuth();
// --- UI Enhancements: glow cursor on buttons ---
document.querySelectorAll(".btn-glow").forEach((btn) => {
btn.addEventListener("pointermove", (e) => {
const rect = btn.getBoundingClientRect();
const x = ((e.clientX - rect.left) / rect.width) * 100;
const y = ((e.clientY - rect.top) / rect.height) * 100;
btn.style.setProperty("--x", x + "%");
btn.style.setProperty("--y", y + "%");
});
});
// --- Ripple effect for buttons and links ---
function addRipple(e) {
const el = e.currentTarget;
const rect = el.getBoundingClientRect();
const circle = document.createElement("span");
circle.className = "ripple";
circle.style.left = `${e.clientX - rect.left}px`;
circle.style.top = `${e.clientY - rect.top}px`;
el.appendChild(circle);
setTimeout(() => circle.remove(), 600);
}
document.querySelectorAll("button.btn, .button-link").forEach((el) => {
el.addEventListener("click", addRipple);
});
// --- Subtle parallax tilt on hover for panels and phase items ---
const tiltEls = document.querySelectorAll(
".panel, .phase-item, .terminal, .glass-card"
);
tiltEls.forEach((el) => {
let enter = false;
el.addEventListener("pointerenter", () => {
enter = true;
});
el.addEventListener("pointerleave", () => {
enter = false;
el.style.transform = "";
});
el.addEventListener("pointermove", (e) => {
if (!enter) return;
const rect = el.getBoundingClientRect();
const cx = rect.left + rect.width / 2;
const cy = rect.top + rect.height / 2;
const dx = (e.clientX - cx) / rect.width;
const dy = (e.clientY - cy) / rect.height;
const max = 6;
el.style.transform = `rotateX(${(-dy * max).toFixed(2)}deg) rotateY(${(
dx * max
).toFixed(2)}deg) translateZ(0)`;
});
});
// --- Optional: lightweight animated background canvas (respects reduced motion) ---
const prefersReduced = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
const canvas = document.getElementById("bg-canvas");
if (canvas && !prefersReduced) {
const ctx = canvas.getContext("2d", { alpha: true });
let w, h, dots;
function resize() {
w = canvas.width = window.innerWidth;
h = canvas.height = window.innerHeight;
dots = Array.from(
{ length: Math.min(90, Math.floor((w * h) / 60000)) },
() => ({
x: Math.random() * w,
y: Math.random() * h,
vx: (Math.random() - 0.5) * 0.4,
vy: (Math.random() - 0.5) * 0.4,
})
);
}
function step() {
ctx.clearRect(0, 0, w, h);
ctx.fillStyle = "rgba(34, 211, 238, 0.6)";
const threshold = 120;
for (let i = 0; i < dots.length; i++) {
const a = dots[i];
a.x += a.vx;
a.y += a.vy;
if (a.x < 0 || a.x > w) a.vx *= -1;
if (a.y < 0 || a.y > h) a.vy *= -1;
ctx.beginPath();
ctx.arc(a.x, a.y, 1.2, 0, Math.PI * 2);
ctx.fill();
for (let j = i + 1; j < dots.length; j++) {
const b = dots[j];
const dx = a.x - b.x,
dy = a.y - b.y;
const dist = Math.hypot(dx, dy);
if (dist < threshold) {
const alpha = (1 - dist / threshold) * 0.2;
ctx.strokeStyle = `rgba(96,165,250,${alpha})`;
ctx.beginPath();
ctx.moveTo(a.x, a.y);
ctx.lineTo(b.x, b.y);
ctx.stroke();
}
}
}
requestAnimationFrame(step);
}
window.addEventListener("resize", resize);
resize();
step();
}
});
|