Files
linux_format_docs_check/app/static/app.js

136 lines
4.2 KiB
JavaScript
Raw Normal View History

2026-05-18 15:50:43 +08:00
const form = document.querySelector("#upload-form");
2026-05-26 10:32:47 +08:00
const skillUploadForm = document.querySelector("#skill-upload-form");
const skillCollectionSelect = document.querySelector("#skill-collection");
const skillUploadStatus = document.querySelector("#skill-upload-status");
2026-05-18 15:50:43 +08:00
const result = document.querySelector("#result");
const summary = document.querySelector("#summary");
const skills = document.querySelector("#skills");
const reviewDocxLink = document.querySelector("#download-review-docx");
2026-05-18 15:50:43 +08:00
const mdLink = document.querySelector("#download-md");
const progressBar = document.querySelector("#analysis-progress");
const statusText = document.querySelector("#analysis-status");
const button = form.querySelector("button");
async function pollTask(statusUrl) {
while (true) {
const response = await fetch(statusUrl);
const payload = await response.json();
progressBar.style.width = `${payload.progress || 0}%`;
statusText.textContent = payload.message || "分析中";
if (payload.status === "completed") {
return payload;
}
if (payload.status === "error") {
throw new Error(payload.error || "分析失败");
}
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
2026-05-26 10:32:47 +08:00
function renderSkillCollections(collections, selectedSlug) {
const currentValue = selectedSlug || skillCollectionSelect.value;
skillCollectionSelect.innerHTML = "";
collections.forEach((collection) => {
const option = document.createElement("option");
option.value = collection.slug;
option.textContent = `${collection.label}${collection.skill_count}`;
if (collection.slug === currentValue) {
option.selected = true;
}
skillCollectionSelect.appendChild(option);
});
}
async function refreshSkillCollections(selectedSlug) {
const response = await fetch("/skill-collections");
const payload = await response.json();
if (!response.ok) {
throw new Error(payload.detail || "技能合集刷新失败");
}
renderSkillCollections(payload.collections, selectedSlug);
}
skillUploadForm.addEventListener("submit", async (event) => {
event.preventDefault();
const uploadButton = skillUploadForm.querySelector("button");
uploadButton.disabled = true;
uploadButton.textContent = "上传中...";
skillUploadStatus.textContent = "正在上传并解压技能合集...";
try {
const data = new FormData(skillUploadForm);
const response = await fetch("/skill-collections/upload", {
method: "POST",
body: data,
});
const payload = await response.json();
if (!response.ok) {
throw new Error(payload.detail || "上传失败");
}
renderSkillCollections(payload.collections, payload.collection.slug);
skillUploadForm.reset();
skillUploadStatus.textContent = payload.message;
} catch (error) {
skillUploadStatus.textContent = error.message;
} finally {
uploadButton.disabled = false;
uploadButton.textContent = "上传合集";
}
});
refreshSkillCollections().catch((error) => {
skillUploadStatus.textContent = error.message;
});
2026-05-18 15:50:43 +08:00
form.addEventListener("submit", async (event) => {
event.preventDefault();
button.disabled = true;
button.textContent = "分析中...";
result.hidden = true;
progressBar.style.width = "0%";
statusText.textContent = "任务提交中...";
const data = new FormData(form);
if (!data.has("use_model")) {
data.set("use_model", "false");
}
try {
const response = await fetch("/analyze", {
method: "POST",
body: data,
});
const payload = await response.json();
if (!response.ok) {
throw new Error(payload.detail || "分析失败");
}
result.hidden = false;
const task = await pollTask(payload.status_url);
summary.textContent = task.summary;
skills.innerHTML = "";
task.matched_skills.forEach((name) => {
const item = document.createElement("span");
item.textContent = name;
skills.appendChild(item);
});
reviewDocxLink.href = task.downloads.review_docx;
2026-05-18 15:50:43 +08:00
mdLink.href = task.downloads.markdown;
} catch (error) {
summary.textContent = error.message;
skills.innerHTML = "";
result.hidden = false;
statusText.textContent = "分析失败";
} finally {
button.disabled = false;
button.textContent = "开始分析";
}
});