diff --git a/README.md b/README.md index 68d058a..a422508 100644 --- a/README.md +++ b/README.md @@ -172,7 +172,12 @@ All modules from the original plan are built: groups their pages (Infrastructure, Apps, Backups, Network, Monitoring, Automation), 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 - 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, 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 diff --git a/server/test/integrationRows.test.ts b/server/test/integrationRows.test.ts new file mode 100644 index 0000000..2050f11 --- /dev/null +++ b/server/test/integrationRows.test.ts @@ -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 => ({ + 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"); +}); diff --git a/web/src/pages/Integrations.tsx b/web/src/pages/Integrations.tsx index da80560..1a694ef 100644 --- a/web/src/pages/Integrations.tsx +++ b/web/src/pages/Integrations.tsx @@ -16,33 +16,40 @@ import RunbookEntry from "../components/RunbookEntry"; import IntegrationInspector from "../components/IntegrationInspector"; import ReadNowButton from "../components/ReadNowButton"; import { groupOfType, INSPECTABLE, TYPE_GROUPS, TYPE_LABELS, TYPE_PAGES } from "../utils/integrationTypes"; +import { hostOf, integrationRow, integrationStatus, type IntegrationRow } from "../utils/integrationRows"; +import { useSortable } from "../hooks/useSortable"; +import SortableTh from "../components/SortableTh"; -/** "pve.example.lan" out of "https://pve.example.lan:8006/", or the text itself when it isn't an address. */ -function hostOf(baseUrl: string): string { - if (!baseUrl) return ""; +function HealthBadge({ i }: { i: IntegrationSummary }) { + const status = integrationStatus(i); + if (status.key === "disabled") return {status.label}; + if (status.key === "failing") return {status.label}; + if (status.key === "working") return {status.label}; + return ( + + {status.label} + + ); +} + +const LAYOUT_KEY = "integrations.layout"; + +/** Cards or table: the address says so when it does, else what this browser last chose, else cards. */ +function layoutOf(param: string | null): "cards" | "table" { + if (param === "table" || param === "cards") return param; try { - return new URL(baseUrl).host; + return localStorage.getItem(LAYOUT_KEY) === "table" ? "table" : "cards"; } catch { - return baseUrl; + return "cards"; } } -function HealthBadge({ i }: { i: IntegrationSummary }) { - const health = i.health; - if (!i.enabled || health?.status === "disabled") return Disabled; - if (health?.status === "failing") { - return ( - - Failing — last {health.consecutiveFailures} call{health.consecutiveFailures === 1 ? "" : "s"} failed - - ); +function rememberLayout(layout: "cards" | "table") { + try { + localStorage.setItem(LAYOUT_KEY, layout); + } catch { + /* a private window: the choice then lives in the address alone */ } - if (health?.status === "ok") return Working; - return ( - - Not checked yet - - ); } function typeBadgeStyle(colors: Record, type: IntegrationType): CSSProperties { @@ -151,8 +158,23 @@ function IntegrationsList({ user, tabs }: { user: CurrentUser; tabs: ReactNode } const matchesStatus = (i: IntegrationSummary) => statusFilter === "" || (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 rows = sortedIntegrations ? sortedIntegrations.map(integrationRow) : null; + const { sorted: sortedRows, sortKey, sortDir, requestSort } = useSortable(rows, "name", "asc", { url: true }); + // The cards in groups, as the sidebar groups their pages (a kind nobody has placed goes under "Other"). const groupedIntegrations = (() => { const groups = new Map(); @@ -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 && ( + + Open + + )} + {isOperator && i.enabled && } + {isAdmin && ( + <> + + {i.enabled && INSPECTABLE.includes(i.type) && ( + + )} + + + + + )} + + ); + } + + /** 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 ( + + Test {result.ok ? `passed${result.latencyMs !== undefined ? ` (${result.latencyMs}ms)` : ""}` : "failed"} + + ); + } + return ( <>
@@ -238,6 +307,16 @@ function IntegrationsList({ user, tabs }: { user: CurrentUser; tabs: ReactNode } } + actions={ +
+ + +
+ } shown={sortedIntegrations?.length} total={integrations?.length} onExport={exportCsv} @@ -245,8 +324,51 @@ function IntegrationsList({ user, tabs }: { user: CurrentUser; tabs: ReactNode } />
+ {layout === "table" && sortedRows && sortedRows.length > 0 && ( +
+
+ + + + label="Name" sortKeyName="name" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> + label="Kind" sortKeyName="kind" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> + label="Group" sortKeyName="group" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> + label="Address" sortKeyName="host" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> + label="Status" sortKeyName="statusRank" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> + + + + + + {sortedRows.map(({ integration: i }) => ( + + + + + + + + + + ))} + +
Last 30 daysActions
{i.name} + + {TYPE_LABELS[i.type]} + + {groupOfType(i.type).label}{hostOf(i.baseUrl) || "no web address"} + + {testBadge(i)} + {testResults[i.id] && !testResults[i.id].ok && testingId !== i.id &&
{testResults[i.id].error}
} +
{i.enabled ? : —} +
{actionsOf(i)}
+
+
+
+ )} +
- {groupedIntegrations.map((group) => ( + {layout === "cards" && groupedIntegrations.map((group) => (

@@ -255,7 +377,6 @@ function IntegrationsList({ user, tabs }: { user: CurrentUser; tabs: ReactNode }

{group.items.map((i) => { const result = testResults[i.id]; - const page = TYPE_PAGES[i.type]; return (
@@ -271,11 +392,7 @@ function IntegrationsList({ user, tabs }: { user: CurrentUser; tabs: ReactNode }
- {result && testingId !== i.id && ( - - Test {result.ok ? `passed${result.latencyMs !== undefined ? ` (${result.latencyMs}ms)` : ""}` : "failed"} - - )} + {testBadge(i)}
{i.enabled && (
@@ -286,35 +403,7 @@ function IntegrationsList({ user, tabs }: { user: CurrentUser; tabs: ReactNode }
{result && !result.ok && testingId !== i.id &&
{result.error}
} -
- {i.enabled && ( - - Open - - )} - {isOperator && i.enabled && } - {isAdmin && ( - <> - - {i.enabled && INSPECTABLE.includes(i.type) && ( - - )} - - - - - )} -
+
{actionsOf(i)}
diff --git a/web/src/utils/integrationRows.ts b/web/src/utils/integrationRows.ts new file mode 100644 index 0000000..227300d --- /dev/null +++ b/web/src/utils/integrationRows.ts @@ -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): 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 }; +}