This commit is contained in:
2026-07-15 18:40:04 -05:00
parent 5c69c40453
commit 84b7ee0a47
2 changed files with 98 additions and 86 deletions

View File

@@ -2,6 +2,7 @@ let state;
let groups = [];
let selectedIndex = -1;
let pdfViewer = null;
let currentPdfKey = null;
let loadRequestId = 0;
const people = document.querySelector("#people");
@@ -112,69 +113,23 @@ function renderList() {
updateStatus(shown.length);
}
async function loadPdf(index) {
async function openDocument(index, { force = false } = {}) {
const doc = state.documents[index];
if (!doc) return;
// Deduplicate: don't reload the same document
if (index === selectedIndex && pdfViewer) {
const key = `${doc._letter}/${doc.file_name}`;
// Deduplicate: ignore click on already-loaded document unless forced
if (!force && key === currentPdfKey) {
return;
}
if (pdfViewer) {
pdfViewer.destroy();
}
pdfViewer = new PdfViewer(pdfContainer);
viewer.classList.remove("loaded");
pdfMessage.hidden = false;
pdfMessage.textContent = "Loading PDF\u2026";
const reqId = "pdf-" + (++loadRequestId);
console.log(
`[BizMatch QC] PDF ${reqId} selection: ${doc._letter}/${doc.file_name}`,
);
console.log(`[BizMatch QC] PDF ${reqId} prepare requested`);
const startTime = performance.now();
try {
const response = await fetch("/api/pdf/prepare", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ index, requestId: reqId }),
});
if (!response.ok) {
const body = await response.json().catch(() => ({}));
throw new Error(body.error || `Prepare failed: HTTP ${response.status}`);
}
const data = await response.json();
const prepareMs = Math.round(performance.now() - startTime);
console.log(
`[BizMatch QC] PDF ${reqId} prepare: ${prepareMs}ms, disk cache ${data.cacheStatus}`,
);
if (index !== selectedIndex) {
// A newer selection happened while preparing, don't show stale result
return;
}
viewer.classList.add("loaded");
pdfMessage.hidden = true;
console.log(`[BizMatch QC] PDF ${reqId} viewer load started`);
await pdfViewer.load(data.url, data.size);
console.log(`[BizMatch QC] PDF ${reqId} rendering complete`);
} catch (error) {
if (index !== selectedIndex) return;
const message = error instanceof Error ? error.message : String(error);
pdfMessage.textContent = `Cannot open PDF: ${message}`;
pdfMessage.hidden = false;
}
}
function select(index) {
// Mark as loading this document
currentPdfKey = key;
selectedIndex = index;
const doc = state.documents[index];
if (!doc) return;
renderList();
// Update detail panel
let rowIdx = 0;
fields.innerHTML = `
<h2>${esc(doc.name_from_filename)}</h2>
@@ -202,8 +157,64 @@ function select(index) {
}).join("")
}
`;
void loadPdf(index);
renderList();
// Load PDF
if (pdfViewer) {
pdfViewer.destroy();
}
pdfViewer = new PdfViewer(pdfContainer);
viewer.classList.remove("loaded");
pdfMessage.hidden = false;
pdfMessage.textContent = "Loading PDF\u2026";
const reqId = "pdf-" + (++loadRequestId);
console.log(
`[BizMatch QC] Document selected: ${key}`,
);
console.log(`[BizMatch QC] PDF prepare starting: ${reqId}`);
const startTime = performance.now();
try {
const response = await fetch("/api/pdf/prepare", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ index, requestId: reqId }),
});
if (!response.ok) {
const body = await response.json().catch(() => ({}));
throw new Error(body.error || `Prepare failed: HTTP ${response.status}`);
}
const data = await response.json();
const prepareMs = Math.round(performance.now() - startTime);
console.log(
`[BizMatch QC] PDF prepare: ${prepareMs}ms, disk cache ${data.cacheStatus}`,
);
console.log(`[BizMatch QC] PDF viewer URL received: ${reqId}`);
if (key !== currentPdfKey) {
// A newer document was selected while preparing
return;
}
viewer.classList.add("loaded");
pdfMessage.hidden = true;
await pdfViewer.load(data.url, data.size);
console.log(`[BizMatch QC] PDF ${reqId} rendering complete`);
} catch (error) {
if (key !== currentPdfKey) return;
const message = error instanceof Error ? error.message : String(error);
pdfMessage.textContent = `Cannot open PDF: ${message}`;
pdfMessage.hidden = false;
}
}
function select(index) {
openDocument(index);
}
// Exposed for future retry from viewer; use openDocument(_, {force:true})
function _selectForced(index) {
openDocument(index, { force: true });
}
people.addEventListener("click", (event) => {