Refactor captures, mappings, and curators modules to use Preact components
This commit is contained in:
1 parent
7d43fc7b7e
commit
4a329fb43d
5 files changed
+545
-459
No files matched your search
@@ -1,54 +1,68 @@
|
||||
import { ago, el, pill } from "../base/dom.jsx";
|
||||
import { h, Fragment } from "preact";
|
||||
import { ago } from "../base/dom.jsx";
|
||||
|
||||
export function Pill({ text, kind = "idle" }) {
|
||||
return <span className={`pill pill-${kind}`}>{text}</span>;
|
||||
}
|
||||
|
||||
/**
|
||||
* One captured event's summary row, plus its expandable detail row when
|
||||
* expanded is true. Split out from captures.js so it can be unit tested
|
||||
* without the page's fetch/SSE wiring, mirroring tasks/task-row.js.
|
||||
*/
|
||||
export function captureRow(c, { expanded, onToggle }) {
|
||||
const row = el(
|
||||
"tr.capture-row",
|
||||
{
|
||||
tabindex: "0",
|
||||
"aria-expanded": String(expanded),
|
||||
onclick: onToggle,
|
||||
onkeydown: (e) => {
|
||||
if (e.key !== "Enter" && e.key !== " ") return;
|
||||
e.preventDefault();
|
||||
onToggle(e);
|
||||
},
|
||||
},
|
||||
el("td.mono", c.source || "(unknown)"),
|
||||
el("td", { title: c.received_at || "" }, ago(c.received_at)),
|
||||
// Every captured event is unbound today: no playbook binding data model
|
||||
// exists yet (t2db.4). Once a binding can match an event, this pill
|
||||
// distinguishes bound from unbound instead of always reading "Unbound"
|
||||
// -- see docs/prds/event-capture-storage.md's "what this does not decide".
|
||||
el("td", pill("Unbound", "idle")),
|
||||
el("td.mono", c.content_type || "—"),
|
||||
el("td", el("button.btn.btn-small", { type: "button" }, expanded ? "Hide" : "View")),
|
||||
);
|
||||
return expanded ? [row, captureDetailRow(c)] : [row];
|
||||
}
|
||||
export function CaptureRow({ capture: c, expanded, onToggle }) {
|
||||
const handleKeyDown = (e) => {
|
||||
if (e.key !== "Enter" && e.key !== " ") return;
|
||||
e.preventDefault();
|
||||
onToggle(e);
|
||||
};
|
||||
|
||||
export function captureDetailRow(c) {
|
||||
return el(
|
||||
"tr.capture-detail-row",
|
||||
el(
|
||||
"td",
|
||||
{ colspan: 5 },
|
||||
el(
|
||||
"div.capture-detail",
|
||||
el("div.capture-detail-section", el("div.capture-detail-title", "Payload"), payloadView(c.body)),
|
||||
el("div.capture-detail-section", el("div.capture-detail-title", "Headers"), payloadView(c.headers)),
|
||||
),
|
||||
),
|
||||
return (
|
||||
<Fragment>
|
||||
<tr
|
||||
className="capture-row"
|
||||
tabIndex="0"
|
||||
aria-expanded={String(expanded)}
|
||||
onClick={onToggle}
|
||||
onKeyDown={handleKeyDown}
|
||||
>
|
||||
<td className="mono">{c.source || "(unknown)"}</td>
|
||||
<td title={c.received_at || ""}>{ago(c.received_at)}</td>
|
||||
<td><Pill text="Unbound" kind="idle" /></td>
|
||||
<td className="mono">{c.content_type || "—"}</td>
|
||||
<td>
|
||||
<button className="btn btn-small" type="button">
|
||||
{expanded ? "Hide" : "View"}
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
{expanded && <CaptureDetailRow capture={c} />}
|
||||
</Fragment>
|
||||
);
|
||||
}
|
||||
|
||||
function payloadView(raw) {
|
||||
if (!raw) return el("div.capture-empty", "(empty)");
|
||||
return el("pre.capture-payload", prettyPrint(raw));
|
||||
export function CaptureDetailRow({ capture: c }) {
|
||||
return (
|
||||
<tr className="capture-detail-row">
|
||||
<td colSpan="5">
|
||||
<div className="capture-detail">
|
||||
<div className="capture-detail-section">
|
||||
<div className="capture-detail-title">Payload</div>
|
||||
<PayloadView raw={c.body} />
|
||||
</div>
|
||||
<div className="capture-detail-section">
|
||||
<div className="capture-detail-title">Headers</div>
|
||||
<PayloadView raw={c.headers} />
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
}
|
||||
|
||||
function PayloadView({ raw }) {
|
||||
if (!raw) return <div className="capture-empty">(empty)</div>;
|
||||
return <pre className="capture-payload">{prettyPrint(raw)}</pre>;
|
||||
}
|
||||
|
||||
// prettyPrint indents valid JSON for readability; a non-JSON payload (the
|
||||
|
||||
@@ -1,7 +1,17 @@
|
||||
import { h, Fragment } from "preact";
|
||||
import { useState, useEffect } from "preact/hooks";
|
||||
import "./captures.css";
|
||||
import { api, subscribeEvents } from "../base/api.jsx";
|
||||
import { el, empty, mount, pill } from "../base/dom.jsx";
|
||||
import { captureRow } from "./capture-row.jsx";
|
||||
import { CaptureRow, Pill } from "./capture-row.jsx";
|
||||
|
||||
export function Empty({ title, detail }) {
|
||||
return (
|
||||
<div className="empty">
|
||||
<div className="empty-title">{title}</div>
|
||||
{detail && <div>{detail}</div>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Event inspector (t2db.2): the operator-facing half of capture. Recent
|
||||
@@ -17,100 +27,95 @@ import { captureRow } from "./capture-row.jsx";
|
||||
* as an invalidation signal, triggering a refetch of the authoritative list
|
||||
* rather than being merged into local state directly.
|
||||
*/
|
||||
export function capturesPage() {
|
||||
const root = el("div");
|
||||
const list = el("div.card");
|
||||
const streamState = pill("connecting", "idle");
|
||||
export function capturesPage(query) {
|
||||
return <CapturesApp query={query} />;
|
||||
}
|
||||
|
||||
let captures = [];
|
||||
let expandedId = null;
|
||||
let loadError = null;
|
||||
let enabled = true;
|
||||
function CapturesApp({ query }) {
|
||||
const [captures, setCaptures] = useState([]);
|
||||
const [expandedId, setExpandedId] = useState(null);
|
||||
const [loadError, setLoadError] = useState(null);
|
||||
const [enabled, setEnabled] = useState(true);
|
||||
const [streamState, setStreamState] = useState({ text: "connecting", kind: "idle" });
|
||||
|
||||
render();
|
||||
load();
|
||||
|
||||
const unsubscribe = subscribeEvents(
|
||||
(event) => {
|
||||
if (event.kind !== "capture") return;
|
||||
load();
|
||||
},
|
||||
(state) => {
|
||||
streamState.className = `pill pill-${state === "live" ? "ok" : "warn"}`;
|
||||
streamState.textContent = state;
|
||||
},
|
||||
);
|
||||
root.addEventListener("archie:teardown", unsubscribe);
|
||||
|
||||
async function load() {
|
||||
const load = async () => {
|
||||
try {
|
||||
const res = await api.captures(100);
|
||||
enabled = res.enabled !== false;
|
||||
captures = res.captures || [];
|
||||
loadError = null;
|
||||
setEnabled(res.enabled !== false);
|
||||
setCaptures(res.captures || []);
|
||||
setLoadError(null);
|
||||
} catch (err) {
|
||||
loadError = String(err.message || err);
|
||||
setLoadError(String(err.message || err));
|
||||
}
|
||||
renderRows();
|
||||
}
|
||||
};
|
||||
|
||||
function render() {
|
||||
mount(
|
||||
root,
|
||||
el(
|
||||
"div.page-head",
|
||||
el(
|
||||
"div",
|
||||
el("h1.page-title", "Event inspector"),
|
||||
el("p.page-sub", "Real inbound payloads archie has captured, most recent first."),
|
||||
),
|
||||
el("div.page-actions", streamState, el("button.btn", { onclick: load }, "Refresh")),
|
||||
),
|
||||
list,
|
||||
useEffect(() => {
|
||||
load();
|
||||
const unsubscribe = subscribeEvents(
|
||||
(event) => {
|
||||
if (event.kind !== "capture") return;
|
||||
load();
|
||||
},
|
||||
(state) => {
|
||||
setStreamState({
|
||||
text: state,
|
||||
kind: state === "live" ? "ok" : "warn",
|
||||
});
|
||||
}
|
||||
);
|
||||
renderRows();
|
||||
}
|
||||
return () => unsubscribe();
|
||||
}, []);
|
||||
|
||||
function renderRows() {
|
||||
const renderRows = () => {
|
||||
if (loadError) {
|
||||
mount(list, empty("Cannot reach archied", loadError));
|
||||
return;
|
||||
return <Empty title="Cannot reach archied" detail={loadError} />;
|
||||
}
|
||||
if (!enabled) {
|
||||
mount(list, empty("Capture is not configured", "This deployment has no capture storage wired up."));
|
||||
return;
|
||||
return <Empty title="Capture is not configured" detail="This deployment has no capture storage wired up." />;
|
||||
}
|
||||
if (!captures.length) {
|
||||
mount(
|
||||
list,
|
||||
empty("No captures yet", "Point a webhook at /webhooks/capture/<source> and it will show up here."),
|
||||
);
|
||||
return;
|
||||
return <Empty title="No captures yet" detail="Point a webhook at /webhooks/capture/<source> and it will show up here." />;
|
||||
}
|
||||
mount(
|
||||
list,
|
||||
el(
|
||||
"div.table-scroll",
|
||||
el(
|
||||
"table.table",
|
||||
el(
|
||||
"thead",
|
||||
el(
|
||||
"tr",
|
||||
...["Source", "Received", "Binding", "Content type", ""].map((h) => el("th", h)),
|
||||
),
|
||||
),
|
||||
el("tbody", ...captures.flatMap((c) => captureRow(c, {
|
||||
expanded: expandedId === c.id,
|
||||
onToggle: () => {
|
||||
expandedId = expandedId === c.id ? null : c.id;
|
||||
renderRows();
|
||||
},
|
||||
}))),
|
||||
),
|
||||
),
|
||||
return (
|
||||
<div className="table-scroll">
|
||||
<table className="table">
|
||||
<thead>
|
||||
<tr>
|
||||
{["Source", "Received", "Binding", "Content type", ""].map((h, i) => (
|
||||
<th key={i}>{h}</th>
|
||||
))}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{captures.map((c) => (
|
||||
<CaptureRow
|
||||
key={c.id}
|
||||
capture={c}
|
||||
expanded={expandedId === c.id}
|
||||
onToggle={() => setExpandedId(expandedId === c.id ? null : c.id)}
|
||||
/>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
return root;
|
||||
return (
|
||||
<div>
|
||||
<div className="page-head">
|
||||
<div>
|
||||
<h1 className="page-title">Event inspector</h1>
|
||||
<p className="page-sub">Real inbound payloads archie has captured, most recent first.</p>
|
||||
</div>
|
||||
<div className="page-actions">
|
||||
<Pill text={streamState.text} kind={streamState.kind} />
|
||||
<button className="btn" onClick={load}>Refresh</button>
|
||||
</div>
|
||||
</div>
|
||||
<div className="card">
|
||||
{renderRows()}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
+102
-79
@@ -1,6 +1,21 @@
|
||||
import { h, Fragment } from "preact";
|
||||
import { useState, useEffect } from "preact/hooks";
|
||||
import "./curators.css";
|
||||
import { api } from "../base/api.jsx";
|
||||
import { el, empty, mount, pill, ago } from "../base/dom.jsx";
|
||||
import { ago } from "../base/dom.jsx";
|
||||
|
||||
function Pill({ text, kind = "idle" }) {
|
||||
return <span className={`pill pill-${kind}`}>{text}</span>;
|
||||
}
|
||||
|
||||
function Empty({ title, detail }) {
|
||||
return (
|
||||
<div className="empty">
|
||||
<div className="empty-title">{title}</div>
|
||||
{detail && <div>{detail}</div>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Curator observability (archie-core-1786637489932-6). Backed by GET
|
||||
@@ -9,96 +24,104 @@ import { el, empty, mount, pill, ago } from "../base/dom.jsx";
|
||||
* activity (last run, action count, and the actions themselves with the
|
||||
* reason each one happened) -- see internal/webui/api_curators.go.
|
||||
*/
|
||||
export function curatorsPage() {
|
||||
const root = el("div");
|
||||
const cardGrid = el("div.grid.grid-2");
|
||||
export function curatorsPage(query) {
|
||||
return <CuratorsApp query={query} />;
|
||||
}
|
||||
|
||||
render();
|
||||
load();
|
||||
function CuratorsApp({ query }) {
|
||||
const [curators, setCurators] = useState([]);
|
||||
const [loadError, setLoadError] = useState(null);
|
||||
|
||||
function render() {
|
||||
mount(
|
||||
root,
|
||||
el(
|
||||
"div.page-head",
|
||||
el(
|
||||
"div",
|
||||
el("h1.page-title", "Curators"),
|
||||
el("p.page-sub", "Background agents that maintain memory and skills: what ran, and why."),
|
||||
),
|
||||
el("div.page-actions", el("button.btn", { onclick: load }, "Refresh")),
|
||||
),
|
||||
cardGrid,
|
||||
);
|
||||
}
|
||||
|
||||
async function load() {
|
||||
const load = async () => {
|
||||
try {
|
||||
const res = await api.curators();
|
||||
renderCards(res?.curators || []);
|
||||
setCurators(res?.curators || []);
|
||||
setLoadError(null);
|
||||
} catch (err) {
|
||||
mount(cardGrid, empty("Cannot reach archied", String(err.message || err)));
|
||||
setLoadError(String(err.message || err));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function renderCards(curators) {
|
||||
useEffect(() => {
|
||||
load();
|
||||
}, []);
|
||||
|
||||
const renderCards = () => {
|
||||
if (loadError) {
|
||||
return <Empty title="Cannot reach archied" detail={loadError} />;
|
||||
}
|
||||
if (!curators.length) {
|
||||
mount(
|
||||
cardGrid,
|
||||
empty(
|
||||
"No curators registered",
|
||||
"Curators are background agent loops that maintain memory and skills. None are registered on this daemon.",
|
||||
),
|
||||
return (
|
||||
<Empty
|
||||
title="No curators registered"
|
||||
detail="Curators are background agent loops that maintain memory and skills. None are registered on this daemon."
|
||||
/>
|
||||
);
|
||||
return;
|
||||
}
|
||||
mount(cardGrid, ...curators.map(curatorCard));
|
||||
}
|
||||
return curators.map(c => <CuratorCard key={c.name} curator={c} />);
|
||||
};
|
||||
|
||||
function healthKind(status) {
|
||||
switch (status) {
|
||||
case "healthy":
|
||||
return "ok";
|
||||
case "degraded":
|
||||
return "warn";
|
||||
case "unhealthy":
|
||||
return "danger";
|
||||
default:
|
||||
return "idle";
|
||||
}
|
||||
return (
|
||||
<div>
|
||||
<div className="page-head">
|
||||
<div>
|
||||
<h1 className="page-title">Curators</h1>
|
||||
<p className="page-sub">Background agents that maintain memory and skills: what ran, and why.</p>
|
||||
</div>
|
||||
<div className="page-actions">
|
||||
<button className="btn" onClick={load}>Refresh</button>
|
||||
</div>
|
||||
</div>
|
||||
<div className="grid grid-2">
|
||||
{renderCards()}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function healthKind(status) {
|
||||
switch (status) {
|
||||
case "healthy":
|
||||
return "ok";
|
||||
case "degraded":
|
||||
return "warn";
|
||||
case "unhealthy":
|
||||
return "danger";
|
||||
default:
|
||||
return "idle";
|
||||
}
|
||||
}
|
||||
|
||||
function curatorCard(c) {
|
||||
const actions = c.recent_actions || [];
|
||||
return el(
|
||||
"div.card.curator-card",
|
||||
el(
|
||||
"div.card-head",
|
||||
el("h3.card-title", c.name),
|
||||
pill(c.health?.status || "unknown", healthKind(c.health?.status)),
|
||||
),
|
||||
c.health?.message && el("p.curator-health-message", c.health.message),
|
||||
el(
|
||||
"p.curator-meta",
|
||||
c.last_run_at
|
||||
function CuratorCard({ curator: c }) {
|
||||
const actions = c.recent_actions || [];
|
||||
return (
|
||||
<div className="card curator-card">
|
||||
<div className="card-head">
|
||||
<h3 className="card-title">{c.name}</h3>
|
||||
<Pill text={c.health?.status || "unknown"} kind={healthKind(c.health?.status)} />
|
||||
</div>
|
||||
{c.health?.message && <p className="curator-health-message">{c.health.message}</p>}
|
||||
<p className="curator-meta">
|
||||
{c.last_run_at
|
||||
? `Last ran ${ago(c.last_run_at)} · ${c.last_run_actions} action${c.last_run_actions === 1 ? "" : "s"}`
|
||||
: "Has not run yet",
|
||||
),
|
||||
actions.length
|
||||
? el(
|
||||
"ul.curator-actions",
|
||||
...actions.map((a) =>
|
||||
el(
|
||||
"li.curator-action",
|
||||
el("div.curator-action-head", el("span.curator-action-type", a.type || "action"), el("span.curator-action-time", ago(a.at))),
|
||||
a.detail && el("p.curator-action-detail", a.detail),
|
||||
a.reason && el("p.curator-action-reason", `Why: ${a.reason}`),
|
||||
),
|
||||
),
|
||||
)
|
||||
: el("p.curator-meta", "No recorded activity yet."),
|
||||
);
|
||||
}
|
||||
|
||||
return root;
|
||||
: "Has not run yet"}
|
||||
</p>
|
||||
{actions.length ? (
|
||||
<ul className="curator-actions">
|
||||
{actions.map((a, i) => (
|
||||
<li className="curator-action" key={i}>
|
||||
<div className="curator-action-head">
|
||||
<span className="curator-action-type">{a.type || "action"}</span>
|
||||
<span className="curator-action-time">{ago(a.at)}</span>
|
||||
</div>
|
||||
{a.detail && <p className="curator-action-detail">{a.detail}</p>}
|
||||
{a.reason && <p className="curator-action-reason">Why: {a.reason}</p>}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
) : (
|
||||
<p className="curator-meta">No recorded activity yet.</p>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,4 +1,8 @@
|
||||
import { el, pill } from "../base/dom.jsx";
|
||||
import { h, Fragment } from "preact";
|
||||
|
||||
export function Pill({ text, kind = "idle" }) {
|
||||
return <span className={`pill pill-${kind}`}>{text}</span>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pure rendering + path/type logic for the mapping editor, split out from
|
||||
@@ -40,30 +44,40 @@ export function pathAppendIndex(path, index) {
|
||||
* without the operator ever typing a JSON path by hand -- "click through
|
||||
* its payload, bind JSON paths" per docs/prds/payload-field-mapping.md.
|
||||
*/
|
||||
export function payloadTree(value, { path = "", name = "", onPick } = {}) {
|
||||
export function PayloadTree({ value, path = "", name = "", onPick }) {
|
||||
if (value !== null && typeof value === "object") {
|
||||
const entries = Array.isArray(value)
|
||||
? value.map((v, i) => [String(i), v, pathAppendIndex(path, i)])
|
||||
: Object.entries(value).map(([k, v]) => [k, v, pathAppendKey(path, k)]);
|
||||
return el(
|
||||
"ul.mapping-tree-list",
|
||||
...entries.map(([key, child, childPath]) =>
|
||||
el(
|
||||
"li.mapping-tree-item",
|
||||
el(
|
||||
"span.mapping-tree-key",
|
||||
{ onclick: (e) => { e.stopPropagation(); onPick?.({ path: childPath, type: fieldTypeFromValue(child), name: key }); } },
|
||||
key,
|
||||
),
|
||||
payloadTree(child, { path: childPath, name: key, onPick }),
|
||||
),
|
||||
),
|
||||
return (
|
||||
<ul className="mapping-tree-list">
|
||||
{entries.map(([key, child, childPath]) => (
|
||||
<li className="mapping-tree-item" key={childPath}>
|
||||
<span
|
||||
className="mapping-tree-key"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
if (onPick) onPick({ path: childPath, type: fieldTypeFromValue(child), name: key });
|
||||
}}
|
||||
>
|
||||
{key}
|
||||
</span>
|
||||
<PayloadTree value={child} path={childPath} name={key} onPick={onPick} />
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
);
|
||||
}
|
||||
return el(
|
||||
"span.mapping-tree-value",
|
||||
{ onclick: (e) => { e.stopPropagation(); onPick?.({ path, type: fieldTypeFromValue(value), name }); } },
|
||||
JSON.stringify(value),
|
||||
return (
|
||||
<span
|
||||
className="mapping-tree-value"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
if (onPick) onPick({ path, type: fieldTypeFromValue(value), name });
|
||||
}}
|
||||
>
|
||||
{JSON.stringify(value)}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -73,69 +87,83 @@ export function payloadTree(value, { path = "", name = "", onPick } = {}) {
|
||||
* rather than a false pass, since an unresolved field is not the same
|
||||
* claim as a resolved one.
|
||||
*/
|
||||
export function fieldsTable(fields, { preview, onRemove, onNameChange, onTypeChange, onRequiredChange } = {}) {
|
||||
export function FieldsTable({ fields, preview, onRemove, onNameChange, onTypeChange, onRequiredChange }) {
|
||||
if (!fields.length) {
|
||||
return el("div.mapping-empty-fields", "Click a value in the payload to bind a field.");
|
||||
return <div className="mapping-empty-fields">Click a value in the payload to bind a field.</div>;
|
||||
}
|
||||
const failureByField = new Map((preview?.failures || []).map((f) => [f.field_name, f]));
|
||||
return el(
|
||||
"table.table.mapping-fields-table",
|
||||
el(
|
||||
"thead",
|
||||
el("tr", ...["Field name", "Path", "Type", "Required", "Preview", ""].map((h) => el("th", h))),
|
||||
),
|
||||
el(
|
||||
"tbody",
|
||||
...fields.map((f, i) =>
|
||||
el(
|
||||
"tr",
|
||||
el("td", el("input.mapping-input", {
|
||||
value: f.name,
|
||||
oninput: (e) => onNameChange?.(i, e.target.value),
|
||||
})),
|
||||
el("td.mono", f.path),
|
||||
el(
|
||||
"td",
|
||||
el(
|
||||
"select.mapping-select",
|
||||
{ onchange: (e) => onTypeChange?.(i, e.target.value) },
|
||||
...FIELD_TYPES.map((t) => el("option", { value: t, selected: t === f.type }, t)),
|
||||
),
|
||||
),
|
||||
el("td", el("input", {
|
||||
type: "checkbox",
|
||||
checked: f.required,
|
||||
onchange: (e) => onRequiredChange?.(i, e.target.checked),
|
||||
})),
|
||||
el("td", previewStatus(f, preview, failureByField)),
|
||||
el("td", el("button.btn.btn-small", { type: "button", onclick: () => onRemove?.(i) }, "Remove")),
|
||||
),
|
||||
),
|
||||
),
|
||||
return (
|
||||
<table className="table mapping-fields-table">
|
||||
<thead>
|
||||
<tr>
|
||||
{["Field name", "Path", "Type", "Required", "Preview", ""].map((h, i) => (
|
||||
<th key={i}>{h}</th>
|
||||
))}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{fields.map((f, i) => (
|
||||
<tr key={i}>
|
||||
<td>
|
||||
<input
|
||||
className="mapping-input"
|
||||
value={f.name}
|
||||
onInput={(e) => onNameChange?.(i, e.target.value)}
|
||||
/>
|
||||
</td>
|
||||
<td className="mono">{f.path}</td>
|
||||
<td>
|
||||
<select
|
||||
className="mapping-select"
|
||||
value={f.type}
|
||||
onChange={(e) => onTypeChange?.(i, e.target.value)}
|
||||
>
|
||||
{FIELD_TYPES.map((t) => (
|
||||
<option key={t} value={t}>{t}</option>
|
||||
))}
|
||||
</select>
|
||||
</td>
|
||||
<td>
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={f.required}
|
||||
onChange={(e) => onRequiredChange?.(i, e.target.checked)}
|
||||
/>
|
||||
</td>
|
||||
<td>
|
||||
<PreviewStatus field={f} preview={preview} failureByField={failureByField} />
|
||||
</td>
|
||||
<td>
|
||||
<button className="btn btn-small" type="button" onClick={() => onRemove?.(i)}>Remove</button>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
);
|
||||
}
|
||||
|
||||
function previewStatus(field, preview, failureByField) {
|
||||
if (!preview) return pill("not previewed", "idle");
|
||||
function PreviewStatus({ field, preview, failureByField }) {
|
||||
if (!preview) return <Pill text="not previewed" kind="idle" />;
|
||||
const failure = failureByField.get(field.name);
|
||||
if (failure) return pill(failure.reason, "danger");
|
||||
if (failure) return <Pill text={failure.reason} kind="danger" />;
|
||||
if (Object.prototype.hasOwnProperty.call(preview.values || {}, field.name)) {
|
||||
return pill(JSON.stringify(preview.values[field.name]), "ok");
|
||||
return <Pill text={JSON.stringify(preview.values[field.name])} kind="ok" />;
|
||||
}
|
||||
return pill("skipped (optional)", "idle");
|
||||
return <Pill text="skipped (optional)" kind="idle" />;
|
||||
}
|
||||
|
||||
/** One saved mapping's summary row for the mapping list. */
|
||||
export function mappingRow(m, { onEdit, onDelete }) {
|
||||
return el(
|
||||
"tr",
|
||||
el("td", m.name),
|
||||
el("td.mono", m.source_hint || "—"),
|
||||
el("td", String(m.fields?.length ?? 0)),
|
||||
el(
|
||||
"td",
|
||||
el("button.btn.btn-small", { type: "button", onclick: () => onEdit?.(m) }, "Edit"),
|
||||
el("button.btn.btn-small", { type: "button", onclick: () => onDelete?.(m) }, "Delete"),
|
||||
),
|
||||
export function MappingRow({ mapping: m, onEdit, onDelete }) {
|
||||
return (
|
||||
<tr>
|
||||
<td>{m.name}</td>
|
||||
<td className="mono">{m.source_hint || "—"}</td>
|
||||
<td>{String(m.fields?.length ?? 0)}</td>
|
||||
<td>
|
||||
<button className="btn btn-small" type="button" onClick={() => onEdit?.(m)}>Edit</button>
|
||||
<button className="btn btn-small" type="button" onClick={() => onDelete?.(m)}>Delete</button>
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
}
|
||||
+203
-187
@@ -1,7 +1,17 @@
|
||||
import { h, Fragment } from "preact";
|
||||
import { useState, useEffect } from "preact/hooks";
|
||||
import "./mappings.css";
|
||||
import { api } from "../base/api.jsx";
|
||||
import { el, empty, mount } from "../base/dom.jsx";
|
||||
import { fieldsTable, mappingRow, payloadTree } from "./mapping-editor.jsx";
|
||||
import { FieldsTable, MappingRow, PayloadTree } from "./mapping-editor.jsx";
|
||||
|
||||
export function Empty({ title, detail }) {
|
||||
return (
|
||||
<div className="empty">
|
||||
<div className="empty-title">{title}</div>
|
||||
{detail && <div>{detail}</div>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Payload field mapping (t2db.3): bind named fields to JSON paths inside a
|
||||
@@ -10,36 +20,37 @@ import { fieldsTable, mappingRow, payloadTree } from "./mapping-editor.jsx";
|
||||
* a mapping + matcher to a workflow) is a separate, not-yet-built page --
|
||||
* this one only produces the mapping entity and its preview.
|
||||
*/
|
||||
export function mappingsPage() {
|
||||
const root = el("div");
|
||||
export function mappingsPage(query) {
|
||||
return <MappingsApp query={query} />;
|
||||
}
|
||||
|
||||
let mappings = [];
|
||||
let captures = [];
|
||||
let loadError = null;
|
||||
let editor = null;
|
||||
function MappingsApp({ query }) {
|
||||
const [mappings, setMappings] = useState([]);
|
||||
const [captures, setCaptures] = useState([]);
|
||||
const [loadError, setLoadError] = useState(null);
|
||||
const [editor, setEditor] = useState(null);
|
||||
|
||||
render();
|
||||
load();
|
||||
|
||||
async function load() {
|
||||
const load = async () => {
|
||||
try {
|
||||
const [mRes, cRes] = await Promise.all([api.mappings(), api.captures(100)]);
|
||||
mappings = mRes.mappings || [];
|
||||
captures = cRes.captures || [];
|
||||
loadError = null;
|
||||
setMappings(mRes.mappings || []);
|
||||
setCaptures(cRes.captures || []);
|
||||
setLoadError(null);
|
||||
} catch (err) {
|
||||
loadError = String(err.message || err);
|
||||
setLoadError(String(err.message || err));
|
||||
}
|
||||
render();
|
||||
}
|
||||
};
|
||||
|
||||
function startCreate() {
|
||||
editor = { id: null, name: "", sourceHint: "", fields: [], captureId: null, payload: null, payloadError: null, preview: null, actionError: null };
|
||||
render();
|
||||
}
|
||||
useEffect(() => {
|
||||
load();
|
||||
}, []);
|
||||
|
||||
function startEdit(m) {
|
||||
editor = {
|
||||
const startCreate = () => {
|
||||
setEditor({ id: null, name: "", sourceHint: "", fields: [], captureId: null, payload: null, payloadError: null, preview: null, actionError: null });
|
||||
};
|
||||
|
||||
const startEdit = (m) => {
|
||||
setEditor({
|
||||
id: m.id,
|
||||
name: m.name,
|
||||
sourceHint: m.source_hint || "",
|
||||
@@ -49,86 +60,84 @@ export function mappingsPage() {
|
||||
payloadError: null,
|
||||
preview: null,
|
||||
actionError: null,
|
||||
};
|
||||
render();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
async function remove(m) {
|
||||
const remove = async (m) => {
|
||||
if (!window.confirm(`Delete mapping "${m.name}"?`)) return;
|
||||
try {
|
||||
await api.mappingDelete(m.id);
|
||||
await load();
|
||||
} catch (err) {
|
||||
loadError = String(err.message || err);
|
||||
render();
|
||||
setLoadError(String(err.message || err));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function selectCapture(captureId) {
|
||||
const selectCapture = (captureId) => {
|
||||
const c = captures.find((c) => String(c.id) === String(captureId));
|
||||
editor.captureId = c ? c.id : null;
|
||||
editor.preview = null;
|
||||
if (!c) {
|
||||
editor.payload = null;
|
||||
editor.payloadError = null;
|
||||
} else {
|
||||
try {
|
||||
editor.payload = JSON.parse(c.body);
|
||||
editor.payloadError = null;
|
||||
} catch {
|
||||
editor.payload = null;
|
||||
editor.payloadError = "This capture's body is not valid JSON, so its fields cannot be mapped by path.";
|
||||
setEditor(prev => {
|
||||
const next = { ...prev, captureId: c ? c.id : null, preview: null };
|
||||
if (!c) {
|
||||
next.payload = null;
|
||||
next.payloadError = null;
|
||||
} else {
|
||||
try {
|
||||
next.payload = JSON.parse(c.body);
|
||||
next.payloadError = null;
|
||||
} catch {
|
||||
next.payload = null;
|
||||
next.payloadError = "This capture's body is not valid JSON, so its fields cannot be mapped by path.";
|
||||
}
|
||||
}
|
||||
}
|
||||
render();
|
||||
}
|
||||
return next;
|
||||
});
|
||||
};
|
||||
|
||||
function addField({ path, type, name }) {
|
||||
const existing = new Set(editor.fields.map((f) => f.name));
|
||||
let candidate = name || "field";
|
||||
let n = 1;
|
||||
while (existing.has(candidate)) candidate = `${name || "field"}_${++n}`;
|
||||
editor.fields.push({ name: candidate, path, type, required: true });
|
||||
editor.preview = null;
|
||||
render();
|
||||
}
|
||||
const addField = ({ path, type, name }) => {
|
||||
setEditor(prev => {
|
||||
const existing = new Set(prev.fields.map((f) => f.name));
|
||||
let candidate = name || "field";
|
||||
let n = 1;
|
||||
while (existing.has(candidate)) candidate = `${name || "field"}_${++n}`;
|
||||
const newFields = [...prev.fields, { name: candidate, path, type, required: true }];
|
||||
return { ...prev, fields: newFields, preview: null };
|
||||
});
|
||||
};
|
||||
|
||||
function removeField(i) {
|
||||
editor.fields.splice(i, 1);
|
||||
editor.preview = null;
|
||||
render();
|
||||
}
|
||||
const removeField = (i) => {
|
||||
setEditor(prev => {
|
||||
const newFields = prev.fields.slice();
|
||||
newFields.splice(i, 1);
|
||||
return { ...prev, fields: newFields, preview: null };
|
||||
});
|
||||
};
|
||||
|
||||
// Every field mutation goes through here so a stale preview can never be
|
||||
// shown against fields it wasn't actually resolved against -- see
|
||||
// docs/prds/payload-field-mapping.md's "never show a mapping the operator
|
||||
// has not seen resolve against real data."
|
||||
function mutateField(i, patch) {
|
||||
Object.assign(editor.fields[i], patch);
|
||||
editor.preview = null;
|
||||
render();
|
||||
}
|
||||
const mutateField = (i, patch) => {
|
||||
setEditor(prev => {
|
||||
const newFields = prev.fields.slice();
|
||||
newFields[i] = { ...newFields[i], ...patch };
|
||||
return { ...prev, fields: newFields, preview: null };
|
||||
});
|
||||
};
|
||||
|
||||
const renameField = (i, name) => mutateField(i, { name });
|
||||
const retypeField = (i, type) => mutateField(i, { type });
|
||||
const requireField = (i, required) => mutateField(i, { required });
|
||||
|
||||
async function preview() {
|
||||
const preview = async () => {
|
||||
if (!editor.captureId) {
|
||||
editor.actionError = "Pick a captured event to preview against.";
|
||||
render();
|
||||
setEditor(prev => ({ ...prev, actionError: "Pick a captured event to preview against." }));
|
||||
return;
|
||||
}
|
||||
try {
|
||||
editor.preview = await api.mappingPreview(editor.captureId, editor.fields);
|
||||
editor.actionError = null;
|
||||
const previewRes = await api.mappingPreview(editor.captureId, editor.fields);
|
||||
setEditor(prev => ({ ...prev, preview: previewRes, actionError: null }));
|
||||
} catch (err) {
|
||||
editor.actionError = String(err.message || err);
|
||||
setEditor(prev => ({ ...prev, actionError: String(err.message || err) }));
|
||||
}
|
||||
render();
|
||||
}
|
||||
};
|
||||
|
||||
async function save() {
|
||||
const save = async () => {
|
||||
const body = { name: editor.name, source_hint: editor.sourceHint, fields: editor.fields };
|
||||
try {
|
||||
if (editor.id) {
|
||||
@@ -136,121 +145,128 @@ export function mappingsPage() {
|
||||
} else {
|
||||
await api.mappingCreate(body);
|
||||
}
|
||||
editor = null;
|
||||
setEditor(null);
|
||||
await load();
|
||||
} catch (err) {
|
||||
editor.actionError = String(err.message || err);
|
||||
render();
|
||||
setEditor(prev => ({ ...prev, actionError: String(err.message || err) }));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function cancel() {
|
||||
editor = null;
|
||||
render();
|
||||
}
|
||||
const cancel = () => {
|
||||
setEditor(null);
|
||||
};
|
||||
|
||||
function render() {
|
||||
mount(root, editor ? renderEditor() : renderList());
|
||||
}
|
||||
|
||||
function renderList() {
|
||||
return el(
|
||||
"div",
|
||||
el(
|
||||
"div.page-head",
|
||||
el(
|
||||
"div",
|
||||
el("h1.page-title", "Field mappings"),
|
||||
el("p.page-sub", "Bind named fields to JSON paths from a real captured event, ready for a playbook binding."),
|
||||
),
|
||||
el("div.page-actions", el("button.btn.btn-primary", { type: "button", onclick: startCreate }, "New mapping")),
|
||||
),
|
||||
loadError
|
||||
? empty("Cannot reach archied", loadError)
|
||||
: mappings.length
|
||||
? el(
|
||||
"div.card",
|
||||
el(
|
||||
"div.table-scroll",
|
||||
el(
|
||||
"table.table",
|
||||
el("thead", el("tr", ...["Name", "Source hint", "Fields", ""].map((h) => el("th", h)))),
|
||||
el("tbody", ...mappings.map((m) => mappingRow(m, { onEdit: startEdit, onDelete: remove }))),
|
||||
),
|
||||
),
|
||||
)
|
||||
: empty("No mappings yet", "Create one from a captured event to reuse fields in a future playbook binding."),
|
||||
const renderList = () => {
|
||||
return (
|
||||
<div>
|
||||
<div className="page-head">
|
||||
<div>
|
||||
<h1 className="page-title">Field mappings</h1>
|
||||
<p className="page-sub">Bind named fields to JSON paths from a real captured event, ready for a playbook binding.</p>
|
||||
</div>
|
||||
<div className="page-actions">
|
||||
<button className="btn btn-primary" type="button" onClick={startCreate}>New mapping</button>
|
||||
</div>
|
||||
</div>
|
||||
{loadError ? (
|
||||
<Empty title="Cannot reach archied" detail={loadError} />
|
||||
) : mappings.length ? (
|
||||
<div className="card">
|
||||
<div className="table-scroll">
|
||||
<table className="table">
|
||||
<thead>
|
||||
<tr>
|
||||
{["Name", "Source hint", "Fields", ""].map((h, i) => (
|
||||
<th key={i}>{h}</th>
|
||||
))}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{mappings.map((m) => (
|
||||
<MappingRow key={m.id} mapping={m} onEdit={() => startEdit(m)} onDelete={() => remove(m)} />
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<Empty title="No mappings yet" detail="Create one from a captured event to reuse fields in a future playbook binding." />
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
function renderEditor() {
|
||||
return el(
|
||||
"div",
|
||||
el(
|
||||
"div.page-head",
|
||||
el("div", el("h1.page-title", editor.id ? "Edit mapping" : "New mapping")),
|
||||
el(
|
||||
"div.page-actions",
|
||||
el("button.btn", { type: "button", onclick: cancel }, "Cancel"),
|
||||
el("button.btn.btn-primary", { type: "button", onclick: save }, "Save"),
|
||||
),
|
||||
),
|
||||
editor.actionError ? el("div.mapping-error", editor.actionError) : null,
|
||||
el(
|
||||
"div.card.mapping-form",
|
||||
el(
|
||||
"label.mapping-form-row",
|
||||
"Name",
|
||||
el("input.mapping-input", {
|
||||
value: editor.name,
|
||||
oninput: (e) => { editor.name = e.target.value; },
|
||||
}),
|
||||
),
|
||||
el(
|
||||
"label.mapping-form-row",
|
||||
"Source hint (organisational only)",
|
||||
el("input.mapping-input", {
|
||||
value: editor.sourceHint,
|
||||
oninput: (e) => { editor.sourceHint = e.target.value; },
|
||||
}),
|
||||
),
|
||||
el(
|
||||
"label.mapping-form-row",
|
||||
"Preview against captured event",
|
||||
el(
|
||||
"select.mapping-select",
|
||||
{ onchange: (e) => selectCapture(e.target.value) },
|
||||
el("option", { value: "" }, "— pick a captured event —"),
|
||||
...captures.map((c) =>
|
||||
el("option", { value: c.id, selected: String(editor.captureId) === String(c.id) },
|
||||
`${c.source || "(unknown)"} — ${c.received_at || ""}`),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
el(
|
||||
"div.card.mapping-fields",
|
||||
el("div.mapping-section-title", "Bound fields"),
|
||||
fieldsTable(editor.fields, {
|
||||
preview: editor.preview,
|
||||
onRemove: removeField,
|
||||
onNameChange: renameField,
|
||||
onTypeChange: retypeField,
|
||||
onRequiredChange: requireField,
|
||||
}),
|
||||
el("button.btn", { type: "button", onclick: preview }, "Preview"),
|
||||
),
|
||||
el(
|
||||
"div.card.mapping-payload",
|
||||
el("div.mapping-section-title", "Payload — click a value to bind it"),
|
||||
editor.payloadError
|
||||
? el("div.mapping-error", editor.payloadError)
|
||||
: editor.payload !== null
|
||||
? payloadTree(editor.payload, { onPick: addField })
|
||||
: el("div.mapping-empty-fields", "Pick a captured event above to see its payload."),
|
||||
),
|
||||
const renderEditor = () => {
|
||||
return (
|
||||
<div>
|
||||
<div className="page-head">
|
||||
<div>
|
||||
<h1 className="page-title">{editor.id ? "Edit mapping" : "New mapping"}</h1>
|
||||
</div>
|
||||
<div className="page-actions">
|
||||
<button className="btn" type="button" onClick={cancel}>Cancel</button>
|
||||
<button className="btn btn-primary" type="button" onClick={save}>Save</button>
|
||||
</div>
|
||||
</div>
|
||||
{editor.actionError && <div className="mapping-error">{editor.actionError}</div>}
|
||||
<div className="card mapping-form">
|
||||
<label className="mapping-form-row">
|
||||
Name
|
||||
<input
|
||||
className="mapping-input"
|
||||
value={editor.name}
|
||||
onInput={(e) => setEditor(prev => ({ ...prev, name: e.target.value }))}
|
||||
/>
|
||||
</label>
|
||||
<label className="mapping-form-row">
|
||||
Source hint (organisational only)
|
||||
<input
|
||||
className="mapping-input"
|
||||
value={editor.sourceHint}
|
||||
onInput={(e) => setEditor(prev => ({ ...prev, sourceHint: e.target.value }))}
|
||||
/>
|
||||
</label>
|
||||
<label className="mapping-form-row">
|
||||
Preview against captured event
|
||||
<select
|
||||
className="mapping-select"
|
||||
value={editor.captureId || ""}
|
||||
onChange={(e) => selectCapture(e.target.value)}
|
||||
>
|
||||
<option value="">— pick a captured event —</option>
|
||||
{captures.map((c) => (
|
||||
<option key={c.id} value={c.id}>
|
||||
{c.source || "(unknown)"} — {c.received_at || ""}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
<div className="card mapping-fields">
|
||||
<div className="mapping-section-title">Bound fields</div>
|
||||
<FieldsTable
|
||||
fields={editor.fields}
|
||||
preview={editor.preview}
|
||||
onRemove={removeField}
|
||||
onNameChange={renameField}
|
||||
onTypeChange={retypeField}
|
||||
onRequiredChange={requireField}
|
||||
/>
|
||||
<button className="btn" type="button" onClick={preview}>Preview</button>
|
||||
</div>
|
||||
<div className="card mapping-payload">
|
||||
<div className="mapping-section-title">Payload — click a value to bind it</div>
|
||||
{editor.payloadError ? (
|
||||
<div className="mapping-error">{editor.payloadError}</div>
|
||||
) : editor.payload !== null ? (
|
||||
<PayloadTree value={editor.payload} onPick={addField} />
|
||||
) : (
|
||||
<div className="mapping-empty-fields">Pick a captured event above to see its payload.</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
return root;
|
||||
return editor ? renderEditor() : renderList();
|
||||
}
|
||||
Reference in new issue
Block a user