File size: 7,911 Bytes
77de2c1 |
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 |
/* --- DOM refs --- */
const form = document.getElementById("review-form");
const urlInput = document.getElementById("url-input");
const submitBtn = document.getElementById("submit-btn");
const inputError = document.getElementById("input-error");
const metaEl = document.getElementById("meta");
const metaRepo = document.getElementById("meta-repo");
const metaPath = document.getElementById("meta-path");
const metaBranch = document.getElementById("meta-branch");
const tabsEl = document.getElementById("tabs");
const tabContent = document.getElementById("tab-content");
const streamError = document.getElementById("stream-error");
const tabButtons = document.querySelectorAll(".tab");
const GITHUB_BLOB_RE = /^https?:\/\/github\.com\/[^/]+\/[^/]+\/blob\/[^/]+\/.+$/;
/* --- Section parsing --- */
const SECTION_KEYS = ["summary", "quality", "performance", "security", "suggestions", "verdicts"];
// Maps heading text (lowercased) to our section key
const HEADING_MAP = {
summary: "summary",
"code quality": "quality",
performance: "performance",
security: "security",
suggestions: "suggestions",
verdicts: "verdicts",
};
function parseSections(markdown) {
const sections = {};
let currentKey = null;
for (const line of markdown.split("\n")) {
const m = line.match(/^## (.+)$/);
if (m) {
const title = m[1].trim().toLowerCase();
const matched = Object.entries(HEADING_MAP).find(([kw]) => title.includes(kw));
if (matched) {
currentKey = matched[1];
sections[currentKey] = "";
continue;
}
}
if (currentKey) {
sections[currentKey] = (sections[currentKey] || "") + line + "\n";
}
}
return sections;
}
// Detect which section the model is currently writing to (last matched heading)
function detectCurrentSection(markdown) {
let last = null;
for (const line of markdown.split("\n")) {
const m = line.match(/^## (.+)$/);
if (m) {
const title = m[1].trim().toLowerCase();
const matched = Object.entries(HEADING_MAP).find(([kw]) => title.includes(kw));
if (matched) last = matched[1];
}
}
return last;
}
/* --- Diff-aware marked renderer --- */
function escapeHtml(str) {
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
}
function isDiffContent(text) {
const lines = text.split("\n");
let diffLines = 0;
for (const l of lines) {
if (l.startsWith("+") || l.startsWith("-") || l.startsWith("@@")) diffLines++;
}
return diffLines >= 2;
}
function renderDiffBlock(text) {
const lines = text.split("\n").map((line) => {
const escaped = escapeHtml(line);
if (line.startsWith("+++") || line.startsWith("---")) {
return `<span class="diff-line diff-info">${escaped}</span>`;
}
if (line.startsWith("+")) {
return `<span class="diff-line diff-add">${escaped}</span>`;
}
if (line.startsWith("-")) {
return `<span class="diff-line diff-del">${escaped}</span>`;
}
if (line.startsWith("@@")) {
return `<span class="diff-line diff-info">${escaped}</span>`;
}
return `<span class="diff-line diff-neutral">${escaped}</span>`;
});
return `<pre class="diff-block"><code>${lines.join("")}</code></pre>`;
}
const renderer = {
code({ text, lang, language }) {
const codeLang = (lang || language || "").toLowerCase().trim();
// Render as diff if tagged as diff OR if content looks like a diff
if (codeLang === "diff" || (!codeLang && isDiffContent(text))) {
return renderDiffBlock(text);
}
const langClass = codeLang ? ` class="language-${escapeHtml(codeLang)}"` : "";
return `<pre><code${langClass}>${escapeHtml(text)}</code></pre>`;
},
};
marked.use({ renderer });
function renderMarkdown(md) {
return DOMPurify.sanitize(marked.parse(md));
}
/* --- Tab management --- */
let activeTab = "summary";
let manualSwitch = false;
let currentSections = {};
tabButtons.forEach((btn) => {
btn.addEventListener("click", () => {
manualSwitch = true;
setActiveTab(btn.dataset.section);
renderActiveTab();
});
});
function setActiveTab(section) {
activeTab = section;
tabButtons.forEach((btn) => {
btn.classList.toggle("active", btn.dataset.section === section);
});
}
function renderActiveTab() {
const md = currentSections[activeTab] || "";
if (md.trim()) {
tabContent.innerHTML = renderMarkdown(md);
tabContent.classList.remove("tab-content-empty");
} else {
tabContent.textContent = "Waiting for content\u2026";
tabContent.classList.add("tab-content-empty");
}
}
function updateTabs(sections, currentStreamSection) {
currentSections = sections;
// Mark tabs that have content + which one is streaming
tabButtons.forEach((btn) => {
const key = btn.dataset.section;
btn.classList.toggle("has-content", !!sections[key]?.trim());
btn.classList.toggle("streaming", key === currentStreamSection);
});
// Auto-switch to the section currently being written (unless user clicked a tab)
if (!manualSwitch && currentStreamSection) {
setActiveTab(currentStreamSection);
}
renderActiveTab();
}
/* --- Review flow --- */
let currentSource = null;
form.addEventListener("submit", (e) => {
e.preventDefault();
startReview();
});
/* --- Sample buttons --- */
document.querySelectorAll(".sample-btn").forEach((btn) => {
btn.addEventListener("click", () => {
urlInput.value = btn.dataset.url;
startReview();
});
});
function startReview() {
const url = urlInput.value.trim();
// Reset
inputError.hidden = true;
streamError.hidden = true;
metaEl.hidden = true;
tabsEl.hidden = true;
tabContent.textContent = "";
manualSwitch = false;
setActiveTab("summary");
tabButtons.forEach((btn) => {
btn.classList.remove("has-content", "streaming");
});
if (!url) { showInputError("Please enter a GitHub file URL."); return; }
if (!GITHUB_BLOB_RE.test(url)) {
showInputError("URL must be a GitHub blob URL (e.g. https://github.com/owner/repo/blob/main/file.js).");
return;
}
if (currentSource) { currentSource.close(); currentSource = null; }
setLoading(true);
let markdown = "";
const source = new EventSource(`/api/review?url=${encodeURIComponent(url)}`);
currentSource = source;
source.addEventListener("meta", (e) => {
const data = JSON.parse(e.data);
metaRepo.textContent = `${data.owner}/${data.repo}`;
metaPath.textContent = data.path;
metaBranch.textContent = data.branch;
metaEl.hidden = false;
tabsEl.hidden = false;
tabContent.classList.add("is-streaming");
});
source.addEventListener("content", (e) => {
const data = JSON.parse(e.data);
markdown += data.text;
const sections = parseSections(markdown);
const current = detectCurrentSection(markdown);
updateTabs(sections, current);
});
source.addEventListener("done", () => {
cleanup();
// Final render with no streaming section
const sections = parseSections(markdown);
updateTabs(sections, null);
});
source.addEventListener("error", (e) => {
if (e.data) {
const data = JSON.parse(e.data);
showStreamError(data.message);
} else {
showStreamError("Connection lost. Please try again.");
}
cleanup();
});
function cleanup() {
source.close();
currentSource = null;
setLoading(false);
tabContent.classList.remove("is-streaming");
tabButtons.forEach((btn) => btn.classList.remove("streaming"));
}
}
/* --- Helpers --- */
function setLoading(on) {
submitBtn.disabled = on;
submitBtn.textContent = on ? "Reviewing\u2026" : "Review Code";
document.body.classList.toggle("loading", on);
}
function showInputError(msg) {
inputError.textContent = msg;
inputError.hidden = false;
}
function showStreamError(msg) {
streamError.textContent = msg;
streamError.hidden = false;
}
|