219 lines
8.3 KiB
JavaScript
219 lines
8.3 KiB
JavaScript
/* Brain of Reese — Sources page (knowledge base index view).
|
||
*
|
||
* Wires the real `GET /api/docs` endpoint (import phase): stat cards +
|
||
* full-width document table, or the designed empty state when nothing is
|
||
* indexed yet. Cells are built with DOM APIs (textContent) — never
|
||
* innerHTML with document-derived data (XSS-safe by construction).
|
||
*
|
||
* Phase 19: the page joins the shared header (assets/header.js) — the
|
||
* whoami gate below runs on the module's cached promise (one request per
|
||
* page, shared with the header toggling).
|
||
*
|
||
* Phase 34 task 02: the header's functional controls are module-owned
|
||
* (assets/header.js): the Sync sources state machine (#sync-btn's
|
||
* §7.4 lifecycle — the page only renders #sync-result +
|
||
* #sync-error-banner off the module's "bor:sync-status" event) and the
|
||
* New chat binding (on a non-chat page "new chat" means going to the
|
||
* chat, fresh — the module clears the phase-14 conversation key and
|
||
* navigates to "/").
|
||
*
|
||
* Phase 26: the table's path links open the document in the
|
||
* almost-fullscreen modal overlay (assets/document-modal.js) on the
|
||
* SAME page — no new tab, no navigation. The link keeps its
|
||
* /document.html href as the no-JS / context-menu escape hatch;
|
||
* left-clicks are intercepted (preventDefault) and routed to
|
||
* openDocumentModal. The module is loaded through the relative import
|
||
* below — the header.js single-evaluation design (no direct <script>
|
||
* tag; esbuild inlines it into the page bundle).
|
||
*/
|
||
|
||
import { fetchIsAdmin, fmtSyncResult, initSharedHeader } from "./header.js";
|
||
import { openDocumentModal } from "./document-modal.js"; // phase 26: row links open the same-page modal
|
||
|
||
const tbody = document.querySelector("#docs-tbody");
|
||
const emptyEl = document.querySelector("#sources-empty");
|
||
const tableWrap = document.querySelector(".table-wrap");
|
||
const statCards = document.querySelector("#stat-cards");
|
||
const gateEl = document.querySelector("#sources-gate");
|
||
const statDocs = document.querySelector("#stat-docs");
|
||
const statChunks = document.querySelector("#stat-chunks");
|
||
const statLast = document.querySelector("#stat-last");
|
||
|
||
/* Phase 16: whoami BEFORE the docs fetch. Anonymous visitors get the
|
||
* sign-in gate (stat cards + table hidden) and NO /api/docs call — the
|
||
* catalog is admin-only. The document viewer itself stays public (the
|
||
* soft rule), so the gate copy points at what keeps working.
|
||
* Phase 19: the whoami request is the shared header module's cached
|
||
* promise — the same single request initSharedHeader() awaited. */
|
||
function isAdmin() {
|
||
return fetchIsAdmin();
|
||
}
|
||
|
||
function fmtDate(iso) {
|
||
try {
|
||
return new Date(iso).toLocaleString();
|
||
} catch {
|
||
return iso;
|
||
}
|
||
}
|
||
|
||
/* Viewer link (phase 10) — same encoded URL the chat chips use; both
|
||
* query values are percent-encoded (paths contain slashes, sometimes
|
||
* spaces). Phase 26: this is the href the .doc-link CARRIES (no-JS /
|
||
* context-menu escape hatch) — the left-click opens the same-page modal
|
||
* instead. */
|
||
export function documentUrl(source, path) {
|
||
return "/document.html?source=" + encodeURIComponent(source) + "&path=" + encodeURIComponent(path);
|
||
}
|
||
|
||
async function loadDocs() {
|
||
let r;
|
||
try {
|
||
r = await fetch("/api/docs");
|
||
} catch {
|
||
showEmpty();
|
||
return;
|
||
}
|
||
if (!r.ok) {
|
||
showEmpty();
|
||
return;
|
||
}
|
||
const { documents } = await r.json();
|
||
if (!documents.length) {
|
||
showEmpty();
|
||
return;
|
||
}
|
||
|
||
tbody.replaceChildren();
|
||
let totalChunks = 0;
|
||
let last = "";
|
||
for (const d of documents) {
|
||
totalChunks += d.chunks;
|
||
if (d.indexed_at > last) last = d.indexed_at;
|
||
tbody.appendChild(makeRow(d));
|
||
}
|
||
statDocs.textContent = String(documents.length);
|
||
statChunks.textContent = String(totalChunks);
|
||
statLast.textContent = last ? fmtDate(last) : "–";
|
||
emptyEl.hidden = true;
|
||
tableWrap.hidden = false;
|
||
}
|
||
|
||
function makeRow(d) {
|
||
const tr = document.createElement("tr");
|
||
|
||
const sourceTd = document.createElement("td");
|
||
sourceTd.textContent = d.source; // document-derived text — never innerHTML
|
||
tr.appendChild(sourceTd);
|
||
|
||
// Path cell: a link to the document (phase 10), full path as the
|
||
// accessible/hover name (the column is ellipsized). Phase 26: the
|
||
// left-click opens the same-page modal — no new tab (document-modal.js);
|
||
// the href stays as the no-JS / context-menu escape hatch.
|
||
const pathTd = document.createElement("td");
|
||
pathTd.title = d.path; // full path on hover (column is ellipsized)
|
||
const link = document.createElement("a");
|
||
link.className = "doc-link";
|
||
link.href = documentUrl(d.source, d.path);
|
||
link.addEventListener("click", (e) => {
|
||
e.preventDefault(); // no new tab (phase 26) — the modal takes over
|
||
e.stopPropagation();
|
||
openDocumentModal(d.source, d.path, link);
|
||
});
|
||
link.title = d.path; // full path as the link's hover/accessible name
|
||
link.textContent = d.path;
|
||
pathTd.appendChild(link);
|
||
tr.appendChild(pathTd);
|
||
|
||
for (const value of [d.title, String(d.chunks), fmtDate(d.indexed_at)]) {
|
||
const td = document.createElement("td");
|
||
td.textContent = value;
|
||
tr.appendChild(td);
|
||
}
|
||
return tr;
|
||
}
|
||
|
||
function showEmpty() {
|
||
statDocs.textContent = "0";
|
||
statChunks.textContent = "0";
|
||
statLast.textContent = "–";
|
||
emptyEl.hidden = false;
|
||
if (tableWrap) tableWrap.hidden = true;
|
||
}
|
||
|
||
/* ---------- Phase 32 (state machine module-owned from phase 34
|
||
* task 02): the #sync-result line + #sync-error-banner ----------
|
||
*
|
||
* The #sync-btn state machine itself (click → POST /api/sync, the 2 s
|
||
* GET /api/sync/status poll, the running / success / failed button
|
||
* states, the admin-only load re-attach) lives in the shared header
|
||
* module (assets/header.js) — so the SAME button markup on ANY page
|
||
* behaves identically. This page keeps only the page-specific
|
||
* rendering: the aria-live last-result line and the role="alert" error
|
||
* banner, driven by the module's "bor:sync-status" event (detail = the
|
||
* GET /api/sync/status object):
|
||
*
|
||
* running → clear the result line, hide the banner (a new run starts
|
||
* clean — the module emits the frame immediately on
|
||
* click/boot, no 2 s poll lag);
|
||
* success → the last-result counts in #sync-result (fmtSyncResult —
|
||
* "added" always shown, zero terms omitted) + the catalog
|
||
* re-fetches live (the KB just changed — never a stale
|
||
* table) + the banner hidden;
|
||
* failed → #sync-error-banner with the error text, result cleared;
|
||
* idle → hide the banner, clear the result (a run vanishing with
|
||
* a server restart, or the post-403 cleanup).
|
||
*/
|
||
const syncResult = document.querySelector("#sync-result");
|
||
const syncErrorBanner = document.querySelector("#sync-error-banner");
|
||
const syncErrorText = document.querySelector("#sync-error-text");
|
||
|
||
function showSyncError(detail) {
|
||
if (syncErrorText) syncErrorText.textContent = detail || "The sync failed.";
|
||
if (syncErrorBanner) syncErrorBanner.hidden = false;
|
||
}
|
||
|
||
function hideSyncError() {
|
||
if (syncErrorText) syncErrorText.textContent = "";
|
||
if (syncErrorBanner) syncErrorBanner.hidden = true;
|
||
}
|
||
|
||
window.addEventListener("bor:sync-status", (e) => {
|
||
const status = e.detail || {};
|
||
if (status.state === "running") {
|
||
if (syncResult) syncResult.textContent = "";
|
||
hideSyncError();
|
||
} else if (status.state === "success") {
|
||
if (syncResult) syncResult.textContent = fmtSyncResult(status.detail);
|
||
hideSyncError();
|
||
// The KB just changed — refresh the catalog live so the table,
|
||
// stats, and empty state never sit stale under the "Synced" label
|
||
// (the sync is the page's own action; a reload should not be
|
||
// needed to see it).
|
||
loadDocs();
|
||
} else if (status.state === "failed") {
|
||
if (syncResult) syncResult.textContent = "";
|
||
showSyncError(status.error);
|
||
} else {
|
||
// idle
|
||
if (syncResult) syncResult.textContent = "";
|
||
hideSyncError();
|
||
}
|
||
});
|
||
|
||
(async () => {
|
||
await initSharedHeader(); // phase 19: Sign in/out + Sources link in the shared bar
|
||
if (!(await isAdmin())) {
|
||
// Anonymous: gate in, catalog out, and no /api/docs request at all.
|
||
if (statCards) statCards.hidden = true;
|
||
if (tableWrap) tableWrap.hidden = true;
|
||
if (emptyEl) emptyEl.hidden = true;
|
||
if (gateEl) gateEl.hidden = false;
|
||
return;
|
||
}
|
||
if (gateEl) gateEl.hidden = true;
|
||
loadDocs();
|
||
// Phase 34 task 02: the sync re-attach is module-owned (header.js
|
||
// boots it on the same cached whoami) — nothing to start here.
|
||
})();
|