"use strict"; const listSection = document.querySelector("#history-list"); const detailSection = document.querySelector("#history-detail"); const detailTitle = document.querySelector("#history-detail-title"); const detailBody = document.querySelector("#history-detail-body"); const requestedId = new URLSearchParams(window.location.search).get("id"); // The readable wording of every action that closes an alert (section 26.4). The // dashboard and the form carry the same map, and it is duplicated here on // purpose, so the history can show the action that was carried out // (section 27). const ACTION_LABELS = { rotate: "Rotate key", replace_hardware: "Replace hardware", remove_software: "Remove software", ignore: "Ignore alert", contact_updated: "Contact details updated", }; function display(value) { if (value === null || value === undefined || value === "") { return "—"; } return String(value); } function displayFlag(value) { if (value === 1) { return "Yes"; } if (value === 0) { return "No"; } return "—"; } function emptyNote(text) { const note = document.createElement("p"); note.className = "empty-state"; note.textContent = text; return note; } function definitionList(entries) { const list = document.createElement("dl"); list.className = "detail-list"; for (const [label, value] of entries) { const term = document.createElement("dt"); term.textContent = label; const detail = document.createElement("dd"); detail.textContent = display(value); list.append(term, detail); } return list; } function dataTable(headers, rows) { const table = document.createElement("table"); table.className = "detail-table"; const head = document.createElement("thead"); const headRow = document.createElement("tr"); for (const header of headers) { const cell = document.createElement("th"); cell.scope = "col"; cell.textContent = header; headRow.append(cell); } head.append(headRow); const body = document.createElement("tbody"); for (const row of rows) { const bodyRow = document.createElement("tr"); for (const value of row) { const cell = document.createElement("td"); cell.textContent = display(value); bodyRow.append(cell); } body.append(bodyRow); } table.append(head, body); return table; } function card(title, content) { const section = document.createElement("section"); section.className = "detail-card"; const heading = document.createElement("h3"); heading.textContent = title; section.append(heading, content); return section; } function keyLabel(record) { // The key name is read from the key row, so a saved check shows the name the // key has now, which the settings may change (section 25.2). const labels = (record.key_checks || []).map( (entry) => `Key ${entry.key_number}: ${entry.name}`, ); return labels.length ? labels.join(", ") : "—"; } function summaryEntries(record) { const entries = [ ["Check date", record.checked_at], ["Next check due", record.next_check_due], ["Key checked", keyLabel(record)], ["Wallet software", record.wallet_software], ["Software version", record.software_version], ["Software compatibility", record.software_compatibility_status], ["Software notes", record.software_comments], // The contact confirmation is a stored status, so it is shown as text // (section 23.1). ["Contact details confirmed", record.contact_info_verified], ["Comments", record.comments], ]; if (record.sparrow_version) { entries.push(["Sparrow version (legacy record)", record.sparrow_version]); } if (record.nunchuk_version) { entries.push(["Nunchuk version (legacy record)", record.nunchuk_version]); } return entries; } function keyEntries(keyCheck) { const entries = [["Availability", displayFlag(keyCheck.available)]]; if (keyCheck.type === "hardware") { entries.push(["Hardware works", displayFlag(keyCheck.hardware_works)]); entries.push(["Firmware version", keyCheck.firmware_version]); entries.push(["Seed backup verified", keyCheck.seed_backup_verified]); } // The user records the address path only; the verification result is derived // by the server, so it is shown as a derived value (sections 9 and 25.1). entries.push(["Address path", keyCheck.address_path]); entries.push(["Address path verified (derived)", keyCheck.address_path_verified]); entries.push(["Comments", keyCheck.comments]); return entries; } function securityTable(securityChecks) { if (!securityChecks.length) { return emptyNote("No security statuses were recorded for this check."); } return dataTable( ["Provider", "Status", "Comment"], securityChecks.map((entry) => [entry.provider, entry.status, entry.comments]), ); } function closedAlertsTable(closedAlerts) { // The closures that belong to a key are shown with the code, the source, the // action written out as a readable label, and the date (section 27). if (!closedAlerts.length) { return emptyNote("No follow-up was closed on this check."); } return dataTable( ["Alert", "Source", "Action", "Closed"], closedAlerts.map((entry) => [ entry.code, entry.source, ACTION_LABELS[entry.action] || entry.action, entry.acknowledged_at, ]), ); } function renderDetail(record) { detailTitle.textContent = `Check from ${record.checked_at}`; const fragment = document.createDocumentFragment(); fragment.append(card("Summary", definitionList(summaryEntries(record)))); for (const keyCheck of record.key_checks || []) { fragment.append(card( `Key ${keyCheck.key_number}: ${keyCheck.name}`, definitionList(keyEntries(keyCheck)), )); } fragment.append(card("Security status", securityTable(record.ai_security_checks || []))); fragment.append(card("Follow-up closed", closedAlertsTable(record.closed_alerts || []))); detailBody.replaceChildren(fragment); detailSection.hidden = false; } function keyNumbersText(check) { const numbers = check.key_numbers || []; if (!numbers.length) { return "Key not recorded"; } return numbers.map((number) => `Key ${number}`).join(", "); } function renderList(checks) { listSection.replaceChildren(); if (!checks.length) { const empty = document.createElement("p"); empty.className = "empty-state"; empty.textContent = "No health checks have been saved yet."; listSection.append(empty); return; } const list = document.createElement("ul"); list.className = "history-list"; for (const check of checks) { const item = document.createElement("li"); item.className = "history-item"; const text = document.createElement("div"); const heading = document.createElement("h3"); heading.textContent = check.checked_at; const meta = document.createElement("p"); meta.textContent = `${keyNumbersText(check)} · Next check due ${check.next_check_due} · Software ${check.software_compatibility_status}`; text.append(heading, meta); const link = document.createElement("a"); link.className = "secondary-button"; link.href = `/history?id=${check.id}`; link.textContent = "View"; item.append(text, link); list.append(item); } listSection.append(list); } async function loadList() { try { const response = await fetch("/api/health-checks", { headers: { Accept: "application/json" } }); if (!response.ok) { throw new Error("Saved checks are unavailable."); } renderList(await response.json()); } catch { listSection.replaceChildren(); const message = document.createElement("p"); message.className = "empty-state"; message.textContent = "Saved health checks could not be loaded. Please reload the page."; listSection.append(message); } } async function loadDetail() { if (!requestedId) { return; } try { const response = await fetch(`/api/health-checks/${encodeURIComponent(requestedId)}`, { headers: { Accept: "application/json" }, }); if (!response.ok) { throw new Error("Health check not found."); } renderDetail(await response.json()); } catch { detailTitle.textContent = "Health check not found"; detailBody.replaceChildren(); detailSection.hidden = false; } } loadList(); loadDetail();