← Source tree

web_demo/app.js

"use strict";
const byId = id => document.getElementById(id);
const session = new ReviewDemo.Session(globalThis.SYNTHETIC_REPORT);
let displayedDigest = null;
let uiEpoch = 0;
function message(text, state) { byId("status").textContent = text; byId("state").textContent = state; }
function lockExport() { displayedDigest = null; byId("confirm").disabled = true; byId("export").disabled = true; byId("digest").value = ""; }
function draw(report) {
  byId("rows").replaceChildren();
  for (const row of report.rows) {
    const tr = document.createElement("tr");
    const hints = [...row.flags];
    if (row.ml.status === "not_scored") hints.push("Not scored: " + row.ml.reason);
    const values = [row.row_number, row.original.record_id, row.original.currency, row.original.amount,
      hints.length ? hints.join(" · ") : "No flag — not proof of correctness"];
    values.forEach((value, i) => { const td = document.createElement("td"); td.textContent = String(value); if (i === 4) td.className = row.needs_review ? "flag" : "ok"; tr.append(td); });
    byId("rows").append(tr);
  }
}
byId("review").addEventListener("click", async () => {
  const epoch = ++uiEpoch;
  lockExport(); byId("review").disabled = true;
  message("Preparing the complete synthetic report…", "Review in progress");
  try {
    const viewed = await session.review();
    if (epoch !== uiEpoch) return;
    displayedDigest = viewed.digest;
    byId("digest").value = viewed.digest;
    byId("report").textContent = JSON.stringify(viewed.document, null, 2);
    byId("details").open = true; draw(viewed.document.review);
    byId("confirm").disabled = false;
    message("Inspect the full report below. Confirmation only unlocks this internal demo export; it does not approve any client work.", "Awaiting demo confirmation");
  } catch (error) { if (epoch === uiEpoch) message(error.message, "Review unavailable"); }
  finally { if (epoch === uiEpoch) byId("review").disabled = false; }
});
byId("confirm").addEventListener("click", () => {
  try { session.confirm(displayedDigest); byId("confirm").disabled = true; byId("export").disabled = false;
    message("Demo snapshot confirmed. Download includes every original row and all review hints. No data has been sent.", "Demo snapshot confirmed");
  } catch (error) { lockExport(); message(error.message, "Confirmation refused"); }
});
byId("export").addEventListener("click", () => {
  try {
    const text = session.export();
    const url = URL.createObjectURL(new Blob([text], { type: "application/json;charset=utf-8" }));
    const link = document.createElement("a"); link.href = url; link.download = "reviewqueue_synthetic_review.json";
    document.body.append(link); link.click(); link.remove(); setTimeout(() => URL.revokeObjectURL(url), 30000);
    message("A browser download was requested. Check your downloads; this page cannot verify that a file was saved. Nothing was submitted.", "Demo download requested");
  } catch (error) { message(error.message, "Export refused"); }
});
byId("reset").addEventListener("click", () => {
  ++uiEpoch; session.reset(); lockExport(); byId("review").disabled = false;
  byId("rows").replaceChildren(); byId("report").textContent = "Review the report to load this snapshot."; byId("details").open = false;
  message("Session reset. Review the full report again before a new demo confirmation.", "Ready to inspect");
});

function runLiveModel() {
  try {
    const result = ReviewInference.score(globalThis.REVIEWQUEUE_MODEL, byId("amount").value, byId("quantity").value);
    byId("prediction").textContent = result.needs_review ? "Review hint: this synthetic record is unusual for the demo reference." : "No promoted review hint. This does not establish that a record is correct.";
    byId("prediction").className = result.needs_review ? "prediction flagged" : "prediction normal";
    byId("prediction-details").hidden = false;
    byId("prediction-details").textContent = JSON.stringify(result, null, 2);
  } catch (error) {
    byId("prediction").textContent = error.message;
    byId("prediction").className = "prediction flagged";
    byId("prediction-details").hidden = true;
  }
}
byId("score").addEventListener("click", runLiveModel);
byId("normal").addEventListener("click", () => { byId("amount").value = "119.00"; byId("quantity").value = "1"; runLiveModel(); });
byId("extreme").addEventListener("click", () => { byId("amount").value = "99000.00"; byId("quantity").value = "20"; runLiveModel(); });
for (const id of ["amount", "quantity"]) byId(id).addEventListener("input", () => {
  byId("prediction").textContent = "Inputs changed. Run the model again to update the result.";
  byId("prediction").className = ""; byId("prediction-details").hidden = true;
});