Add a Cards / Table switch to the Integrations page, sortable and remembered

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-11-06 23:36:53 +01:00
1 parent 87e07b2209
commit fa7cc282bb
4 files changed
+275 -58

No files matched your search

+6 -1
View File
@@ -172,7 +172,12 @@ All modules from the original plan are built:
groups their pages (Infrastructure, Apps, Backups, Network, Monitoring, Automation), groups their pages (Infrastructure, Apps, Backups, Network, Monitoring, Automation),
with an admin-only "Add integration" button — its kind picker is the same groups, with an admin-only "Add integration" button — its kind picker is the same groups,
with a search box — and edit/enable/disable/delete actions per row. The dedicated with a search box — and edit/enable/disable/delete actions per row. The dedicated
pages are where you actually use each one. pages are where you actually use each one. A **Cards / Table** switch beside the search box
shows the same integrations as a table instead — name, kind, group, address, status, the
last 30 days and the same buttons — sortable on any column (Status puts the failing ones
first); the choice is in the address (`?layout=table`, with `?sort=`), so it can be linked or
saved as a view, and this browser remembers it for the next visit. Search and the status
filter work in both.
- Every table in the app is click-to-sort on any column (numbers, booleans, - Every table in the app is click-to-sort on any column (numbers, booleans,
and dates/text sort correctly regardless of how the column formats them) and dates/text sort correctly regardless of how the column formats them)
and has an "Export CSV" button next to it that exports whatever's and has an "Export CSV" button next to it that exports whatever's
+73
View File
@@ -0,0 +1,73 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { hostOf, integrationRow, integrationStatus } from "../../web/src/utils/integrationRows.js";
import { sortItems } from "../../web/src/hooks/useSortable.js";
import type { IntegrationSummary } from "../../web/src/api/client.js";
const make = (over: Partial<IntegrationSummary> = {}): IntegrationSummary => ({
id: 1,
type: "proxmox",
name: "pve",
baseUrl: "https://pve.example.lan:8006/",
enabled: true,
createdAt: "2026-01-01T00:00:00Z",
...over,
});
test("a disabled integration reads as disabled whatever its health says", () => {
assert.equal(integrationStatus(make({ enabled: false, health: { status: "failing", consecutiveFailures: 4 } })).key, "disabled");
assert.equal(integrationStatus(make({ health: { status: "disabled", consecutiveFailures: 0 } })).key, "disabled");
});
test("failing says how many calls failed, in the singular too", () => {
assert.equal(integrationStatus(make({ health: { status: "failing", consecutiveFailures: 1 } })).label, "Failing — last 1 call failed");
assert.equal(integrationStatus(make({ health: { status: "failing", consecutiveFailures: 3 } })).label, "Failing — last 3 calls failed");
});
test("working, and not checked yet when nothing has called it", () => {
assert.equal(integrationStatus(make({ health: { status: "ok", consecutiveFailures: 0 } })).key, "working");
assert.equal(integrationStatus(make()).key, "unchecked");
assert.equal(integrationStatus(make({ health: { status: "unknown", consecutiveFailures: 0 } })).key, "unchecked");
});
test("the status ranks put what needs attention first", () => {
const rank = (i: IntegrationSummary) => integrationStatus(i).rank;
const failing = rank(make({ health: { status: "failing", consecutiveFailures: 2 } }));
const unchecked = rank(make());
const working = rank(make({ health: { status: "ok", consecutiveFailures: 0 } }));
const disabled = rank(make({ enabled: false }));
assert.ok(failing < unchecked && unchecked < working && working < disabled);
});
test("hostOf gives the host and port, and the text itself when it is not an address", () => {
assert.equal(hostOf("https://pve.example.lan:8006/"), "pve.example.lan:8006");
assert.equal(hostOf("http://10.0.0.5"), "10.0.0.5");
assert.equal(hostOf("not a url"), "not a url");
assert.equal(hostOf(""), "");
});
test("a row carries the raw values the table sorts on", () => {
const row = integrationRow(make({ type: "pbs", name: "backup", baseUrl: "" }));
assert.equal(row.kind, "Proxmox Backup Server");
assert.equal(row.group, "Backups");
assert.equal(row.host, "");
assert.equal(row.name, "backup");
});
test("the table sorts by status with the failing ones first, and by name within the same status", () => {
const rows = [
integrationRow(make({ id: 1, name: "b-ok", health: { status: "ok", consecutiveFailures: 0 } })),
integrationRow(make({ id: 2, name: "a-ok", health: { status: "ok", consecutiveFailures: 0 } })),
integrationRow(make({ id: 3, name: "c-down", health: { status: "failing", consecutiveFailures: 2 } })),
integrationRow(make({ id: 4, name: "d-off", enabled: false })),
].sort((a, b) => a.name.localeCompare(b.name)); // arrives in name order, as the page gives it
const sorted = sortItems(rows, "statusRank", "asc").map((r) => r.name);
// sortItems is stable, so the name order survives inside each status
assert.deepEqual(sorted, ["c-down", "a-ok", "b-ok", "d-off"]);
assert.deepEqual(sortItems(rows, "kind", "asc").map((r) => r.id).length, 4);
});
test("a kind nobody has placed in a group is listed under Other", () => {
const row = integrationRow(make({ type: "nonexistent" as never }));
assert.equal(row.group, "Other");
});
+146 -57
View File
@@ -16,33 +16,40 @@ import RunbookEntry from "../components/RunbookEntry";
import IntegrationInspector from "../components/IntegrationInspector"; import IntegrationInspector from "../components/IntegrationInspector";
import ReadNowButton from "../components/ReadNowButton"; import ReadNowButton from "../components/ReadNowButton";
import { groupOfType, INSPECTABLE, TYPE_GROUPS, TYPE_LABELS, TYPE_PAGES } from "../utils/integrationTypes"; import { groupOfType, INSPECTABLE, TYPE_GROUPS, TYPE_LABELS, TYPE_PAGES } from "../utils/integrationTypes";
import { hostOf, integrationRow, integrationStatus, type IntegrationRow } from "../utils/integrationRows";
/** "pve.example.lan" out of "https://pve.example.lan:8006/", or the text itself when it isn't an address. */ import { useSortable } from "../hooks/useSortable";
function hostOf(baseUrl: string): string { import SortableTh from "../components/SortableTh";
if (!baseUrl) return "";
try {
return new URL(baseUrl).host;
} catch {
return baseUrl;
}
}
function HealthBadge({ i }: { i: IntegrationSummary }) { function HealthBadge({ i }: { i: IntegrationSummary }) {
const health = i.health; const status = integrationStatus(i);
if (!i.enabled || health?.status === "disabled") return <span className="badge bg-secondary-lt text-secondary">Disabled</span>; if (status.key === "disabled") return <span className="badge bg-secondary-lt text-secondary">{status.label}</span>;
if (health?.status === "failing") { if (status.key === "failing") return <span className="badge bg-red-lt text-red">{status.label}</span>;
if (status.key === "working") return <span className="badge bg-green-lt text-green">{status.label}</span>;
return ( return (
<span className="badge bg-red-lt text-red"> <span className="badge bg-secondary-lt text-secondary" title="Nothing has called it since the app started. Use Test to check it now.">
Failing — last {health.consecutiveFailures} call{health.consecutiveFailures === 1 ? "" : "s"} failed {status.label}
</span> </span>
); );
} }
if (health?.status === "ok") return <span className="badge bg-green-lt text-green">Working</span>;
return ( const LAYOUT_KEY = "integrations.layout";
<span className="badge bg-secondary-lt text-secondary" title="Nothing has called it since the app started. Use Test to check it now.">
Not checked yet /** Cards or table: the address says so when it does, else what this browser last chose, else cards. */
</span> function layoutOf(param: string | null): "cards" | "table" {
); if (param === "table" || param === "cards") return param;
try {
return localStorage.getItem(LAYOUT_KEY) === "table" ? "table" : "cards";
} catch {
return "cards";
}
}
function rememberLayout(layout: "cards" | "table") {
try {
localStorage.setItem(LAYOUT_KEY, layout);
} catch {
/* a private window: the choice then lives in the address alone */
}
} }
function typeBadgeStyle(colors: Record<string, string>, type: IntegrationType): CSSProperties { function typeBadgeStyle(colors: Record<string, string>, type: IntegrationType): CSSProperties {
@@ -151,8 +158,23 @@ function IntegrationsList({ user, tabs }: { user: CurrentUser; tabs: ReactNode }
const matchesStatus = (i: IntegrationSummary) => const matchesStatus = (i: IntegrationSummary) =>
statusFilter === "" || statusFilter === "" ||
(statusFilter === "disabled" ? !i.enabled : statusFilter === "failing" ? i.enabled && i.health?.status === "failing" : i.enabled && i.health?.status === "ok"); (statusFilter === "disabled" ? !i.enabled : statusFilter === "failing" ? i.enabled && i.health?.status === "failing" : i.enabled && i.health?.status === "ok");
const layout = layoutOf(searchParams.get("layout"));
function chooseLayout(next: "cards" | "table") {
rememberLayout(next);
setSearchParams(
(prev) => {
const p = new URLSearchParams(prev);
p.set("layout", next);
return p;
},
{ replace: true },
);
}
const sortedIntegrations = tableSearch.filtered ? [...tableSearch.filtered].filter(matchesStatus).sort((a, b) => a.name.localeCompare(b.name)) : null; const sortedIntegrations = tableSearch.filtered ? [...tableSearch.filtered].filter(matchesStatus).sort((a, b) => a.name.localeCompare(b.name)) : null;
const rows = sortedIntegrations ? sortedIntegrations.map(integrationRow) : null;
const { sorted: sortedRows, sortKey, sortDir, requestSort } = useSortable<IntegrationRow>(rows, "name", "asc", { url: true });
// The cards in groups, as the sidebar groups their pages (a kind nobody has placed goes under "Other"). // The cards in groups, as the sidebar groups their pages (a kind nobody has placed goes under "Other").
const groupedIntegrations = (() => { const groupedIntegrations = (() => {
const groups = new Map<string, { id: string; label: string; items: IntegrationSummary[] }>(); const groups = new Map<string, { id: string; label: string; items: IntegrationSummary[] }>();
@@ -170,6 +192,53 @@ function IntegrationsList({ user, tabs }: { user: CurrentUser; tabs: ReactNode }
); );
} }
/** The buttons of a card or a table row: the same, so the two views can do the same things. */
function actionsOf(i: IntegrationSummary) {
const page = TYPE_PAGES[i.type];
return (
<>
{i.enabled && (
<Link to={page} className="btn btn-sm btn-primary">
Open
</Link>
)}
{isOperator && i.enabled && <ReadNowButton integrationId={i.id} name={i.name} onDone={loadIntegrations} />}
{isAdmin && (
<>
<button className="btn btn-sm" onClick={() => testIntegration(i)} disabled={testingId === i.id}>
{testingId === i.id ? "Testing…" : "Test"}
</button>
{i.enabled && INSPECTABLE.includes(i.type) && (
<button className="btn btn-sm" onClick={() => setInspecting(i)} title="Read it once now and see what each request got back — to check that what is shown is what is there">
Inspect
</button>
)}
<button className="btn btn-sm" onClick={() => setEditingIntegration(i)}>
Edit
</button>
<button className="btn btn-sm" onClick={() => toggleEnabled(i)}>
{i.enabled ? "Disable" : "Enable"}
</button>
<button className="btn btn-sm btn-outline-danger" onClick={() => removeIntegration(i)}>
Delete
</button>
</>
)}
</>
);
}
/** What a test of it said, beside its status. */
function testBadge(i: IntegrationSummary) {
const result = testResults[i.id];
if (!result || testingId === i.id) return null;
return (
<span className={`badge ms-2 ${result.ok ? "bg-green-lt text-green" : "bg-red-lt text-red"}`}>
Test {result.ok ? `passed${result.latencyMs !== undefined ? ` (${result.latencyMs}ms)` : ""}` : "failed"}
</span>
);
}
return ( return (
<> <>
<div className="d-flex align-items-center mb-3"> <div className="d-flex align-items-center mb-3">
@@ -238,6 +307,16 @@ function IntegrationsList({ user, tabs }: { user: CurrentUser; tabs: ReactNode }
<option value="disabled">Disabled</option> <option value="disabled">Disabled</option>
</select> </select>
} }
actions={
<div className="btn-group btn-group-sm" role="group" aria-label="How to show the integrations">
<button type="button" className={`btn ${layout === "cards" ? "active" : ""}`} aria-pressed={layout === "cards"} onClick={() => chooseLayout("cards")}>
Cards
</button>
<button type="button" className={`btn ${layout === "table" ? "active" : ""}`} aria-pressed={layout === "table"} onClick={() => chooseLayout("table")}>
Table
</button>
</div>
}
shown={sortedIntegrations?.length} shown={sortedIntegrations?.length}
total={integrations?.length} total={integrations?.length}
onExport={exportCsv} onExport={exportCsv}
@@ -245,8 +324,51 @@ function IntegrationsList({ user, tabs }: { user: CurrentUser; tabs: ReactNode }
/> />
</div> </div>
{layout === "table" && sortedRows && sortedRows.length > 0 && (
<div className="card mb-3">
<div className="table-responsive">
<table className="table table-vcenter card-table">
<thead>
<tr>
<SortableTh<IntegrationRow> label="Name" sortKeyName="name" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<SortableTh<IntegrationRow> label="Kind" sortKeyName="kind" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<SortableTh<IntegrationRow> label="Group" sortKeyName="group" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<SortableTh<IntegrationRow> label="Address" sortKeyName="host" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<SortableTh<IntegrationRow> label="Status" sortKeyName="statusRank" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<th scope="col">Last 30 days</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
{sortedRows.map(({ integration: i }) => (
<tr key={i.id}>
<td className="fw-bold text-nowrap">{i.name}</td>
<td>
<span className="badge" style={typeBadgeStyle(integrationColors, i.type)}>
{TYPE_LABELS[i.type]}
</span>
</td>
<td className="text-secondary">{groupOfType(i.type).label}</td>
<td className="text-secondary">{hostOf(i.baseUrl) || "no web address"}</td>
<td>
<HealthBadge i={i} />
{testBadge(i)}
{testResults[i.id] && !testResults[i.id].ok && testingId !== i.id && <div className="text-red small mt-1">{testResults[i.id].error}</div>}
</td>
<td>{i.enabled ? <UptimeRibbon kind="integrations" id={i.id} name={i.name} /> : <span className="text-secondary">—</span>}</td>
<td>
<div className="btn-list">{actionsOf(i)}</div>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
)}
<div className="row row-cards"> <div className="row row-cards">
{groupedIntegrations.map((group) => ( {layout === "cards" && groupedIntegrations.map((group) => (
<Fragment key={group.id}> <Fragment key={group.id}>
<div className="col-12"> <div className="col-12">
<h4 className="mb-0 mt-2"> <h4 className="mb-0 mt-2">
@@ -255,7 +377,6 @@ function IntegrationsList({ user, tabs }: { user: CurrentUser; tabs: ReactNode }
</div> </div>
{group.items.map((i) => { {group.items.map((i) => {
const result = testResults[i.id]; const result = testResults[i.id];
const page = TYPE_PAGES[i.type];
return ( return (
<div className="col-md-6 col-xl-4" key={i.id}> <div className="col-md-6 col-xl-4" key={i.id}>
<div className="card h-100"> <div className="card h-100">
@@ -271,11 +392,7 @@ function IntegrationsList({ user, tabs }: { user: CurrentUser; tabs: ReactNode }
</div> </div>
<div className="mb-3"> <div className="mb-3">
<HealthBadge i={i} /> <HealthBadge i={i} />
{result && testingId !== i.id && ( {testBadge(i)}
<span className={`badge ms-2 ${result.ok ? "bg-green-lt text-green" : "bg-red-lt text-red"}`}>
Test {result.ok ? `passed${result.latencyMs !== undefined ? ` (${result.latencyMs}ms)` : ""}` : "failed"}
</span>
)}
</div> </div>
{i.enabled && ( {i.enabled && (
<div className="mb-3"> <div className="mb-3">
@@ -286,35 +403,7 @@ function IntegrationsList({ user, tabs }: { user: CurrentUser; tabs: ReactNode }
<RunbookEntry targetType="integration" targetId={i.id} targetName={i.name} canEdit={isOperator} offerAdd /> <RunbookEntry targetType="integration" targetId={i.id} targetName={i.name} canEdit={isOperator} offerAdd />
</div> </div>
{result && !result.ok && testingId !== i.id && <div className="text-red small mb-3">{result.error}</div>} {result && !result.ok && testingId !== i.id && <div className="text-red small mb-3">{result.error}</div>}
<div className="btn-list mt-auto"> <div className="btn-list mt-auto">{actionsOf(i)}</div>
{i.enabled && (
<Link to={page} className="btn btn-sm btn-primary">
Open
</Link>
)}
{isOperator && i.enabled && <ReadNowButton integrationId={i.id} name={i.name} onDone={loadIntegrations} />}
{isAdmin && (
<>
<button className="btn btn-sm" onClick={() => testIntegration(i)} disabled={testingId === i.id}>
{testingId === i.id ? "Testing…" : "Test"}
</button>
{i.enabled && INSPECTABLE.includes(i.type) && (
<button className="btn btn-sm" onClick={() => setInspecting(i)} title="Read it once now and see what each request got back — to check that what is shown is what is there">
Inspect
</button>
)}
<button className="btn btn-sm" onClick={() => setEditingIntegration(i)}>
Edit
</button>
<button className="btn btn-sm" onClick={() => toggleEnabled(i)}>
{i.enabled ? "Disable" : "Enable"}
</button>
<button className="btn btn-sm btn-outline-danger" onClick={() => removeIntegration(i)}>
Delete
</button>
</>
)}
</div>
</div> </div>
</div> </div>
</div> </div>
+50
View File
@@ -0,0 +1,50 @@
import type { IntegrationSummary } from "../api/client";
import { groupOfType, TYPE_LABELS } from "./integrationTypes";
export type IntegrationStatusKey = "disabled" | "failing" | "working" | "unchecked";
export interface IntegrationStatus {
key: IntegrationStatusKey;
/** Worst first, so the default order of a table puts what needs attention at the top. */
rank: number;
/** In words, as the badge reads. */
label: string;
}
/** Pure: how an integration is — the one place that decides it, so the cards and the table cannot disagree. */
export function integrationStatus(i: Pick<IntegrationSummary, "enabled" | "health">): IntegrationStatus {
const health = i.health;
if (!i.enabled || health?.status === "disabled") return { key: "disabled", rank: 3, label: "Disabled" };
if (health?.status === "failing") {
const n = health.consecutiveFailures;
return { key: "failing", rank: 0, label: `Failing — last ${n} call${n === 1 ? "" : "s"} failed` };
}
if (health?.status === "ok") return { key: "working", rank: 2, label: "Working" };
return { key: "unchecked", rank: 1, label: "Not checked yet" };
}
/** "pve.example.lan" out of "https://pve.example.lan:8006/", or the text itself when it isn't an address. */
export function hostOf(baseUrl: string): string {
if (!baseUrl) return "";
try {
return new URL(baseUrl).host;
} catch {
return baseUrl;
}
}
/** What the table sorts on: the raw values, not however a cell happens to render them. */
export interface IntegrationRow {
integration: IntegrationSummary;
id: number;
name: string;
kind: string;
group: string;
host: string;
statusRank: number;
createdAt: string;
}
export function integrationRow(i: IntegrationSummary): IntegrationRow {
return { integration: i, id: i.id, name: i.name, kind: TYPE_LABELS[i.type], group: groupOfType(i.type).label, host: hostOf(i.baseUrl), statusRank: integrationStatus(i).rank, createdAt: i.createdAt };
}