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:
1 parent
87e07b2209
commit
fa7cc282bb
4 files changed
+274
-57
No files matched your search
+145
-56
@@ -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 <span className="badge bg-secondary-lt text-secondary">{status.label}</span>;
|
||||
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 (
|
||||
<span className="badge bg-secondary-lt text-secondary" title="Nothing has called it since the app started. Use Test to check it now.">
|
||||
{status.label}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
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 <span className="badge bg-secondary-lt text-secondary">Disabled</span>;
|
||||
if (health?.status === "failing") {
|
||||
return (
|
||||
<span className="badge bg-red-lt text-red">
|
||||
Failing — last {health.consecutiveFailures} call{health.consecutiveFailures === 1 ? "" : "s"} failed
|
||||
</span>
|
||||
);
|
||||
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 <span className="badge bg-green-lt text-green">Working</span>;
|
||||
return (
|
||||
<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
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
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) =>
|
||||
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<IntegrationRow>(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<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 (
|
||||
<>
|
||||
<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>
|
||||
</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}
|
||||
total={integrations?.length}
|
||||
onExport={exportCsv}
|
||||
@@ -245,8 +324,51 @@ function IntegrationsList({ user, tabs }: { user: CurrentUser; tabs: ReactNode }
|
||||
/>
|
||||
</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">
|
||||
{groupedIntegrations.map((group) => (
|
||||
{layout === "cards" && groupedIntegrations.map((group) => (
|
||||
<Fragment key={group.id}>
|
||||
<div className="col-12">
|
||||
<h4 className="mb-0 mt-2">
|
||||
@@ -255,7 +377,6 @@ function IntegrationsList({ user, tabs }: { user: CurrentUser; tabs: ReactNode }
|
||||
</div>
|
||||
{group.items.map((i) => {
|
||||
const result = testResults[i.id];
|
||||
const page = TYPE_PAGES[i.type];
|
||||
return (
|
||||
<div className="col-md-6 col-xl-4" key={i.id}>
|
||||
<div className="card h-100">
|
||||
@@ -271,11 +392,7 @@ function IntegrationsList({ user, tabs }: { user: CurrentUser; tabs: ReactNode }
|
||||
</div>
|
||||
<div className="mb-3">
|
||||
<HealthBadge i={i} />
|
||||
{result && testingId !== i.id && (
|
||||
<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>
|
||||
)}
|
||||
{testBadge(i)}
|
||||
</div>
|
||||
{i.enabled && (
|
||||
<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 />
|
||||
</div>
|
||||
{result && !result.ok && testingId !== i.id && <div className="text-red small mb-3">{result.error}</div>}
|
||||
<div className="btn-list mt-auto">
|
||||
{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 className="btn-list mt-auto">{actionsOf(i)}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -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 };
|
||||
}
|
||||
Reference in new issue
Block a user