Sorting/CSV export were already app-wide; tables with a real chance of growing into dozens or hundreds of rows (a busy tailnet, a big DNS zone, a homelab's full IP inventory, a Gitea org with many repos, ...) had no pagination at all, making them a long unbroken scroll. New usePagination hook (client-side slicing over an already-sorted/ filtered array, 20 rows per page) and a matching Pagination component (Prev/Next + "Page X of Y (N total)", hidden entirely when everything fits on one page). The current page is clamped to the valid range on every render rather than reset via an effect, so switching to a smaller data set (a different selected integration, a filter that narrows the result) can never strand the view on a now-nonexistent page -- no per-page "reset on change" wiring needed anywhere. Applied to Audit Log, DNS zones and records, IP Addresses, Secrets, Servers (manage table), Docker containers, Proxmox guests, Semaphore templates, Gitea repos, and Tailscale devices. CSV export keeps exporting the full sorted/filtered array regardless of which page is currently shown -- pagination only affects what's rendered on screen. Left the already-small tables (Synology volumes/disks, Users, Integrations, per-node Proxmox storage) unpaginated, and left the Diagnostic Log's existing server-driven pagination as-is rather than bolting a second, different pagination scheme onto it. Verified the clamping logic directly: a normal page, the trailing partial page, a requested page beyond the end (clamps to the last valid page instead of rendering empty), and an empty result set (clamps to page 0 with a page count of 1 instead of a negative range). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
310 lines
11 KiB
TypeScript
310 lines
11 KiB
TypeScript
import { useEffect, useMemo, useState } from "react";
|
|
import { api, type CurrentUser, type SecretInput, type SecretRecord, type SecretStatus } from "../api/client";
|
|
import { downloadCsv } from "../utils/csv";
|
|
import { useSortable } from "../hooks/useSortable";
|
|
import SortableTh from "../components/SortableTh";
|
|
import { usePagination } from "../hooks/usePagination";
|
|
import Pagination from "../components/Pagination";
|
|
|
|
const TYPE_LABELS: Record<SecretRecord["type"], string> = {
|
|
api_token: "API Token",
|
|
ssl_certificate: "SSL Certificate",
|
|
password: "Password",
|
|
generic: "Generic",
|
|
};
|
|
|
|
const STATUS_BADGE: Record<SecretStatus, string> = {
|
|
ok: "bg-green-lt text-green",
|
|
expiring: "bg-yellow-lt text-yellow",
|
|
expired: "bg-red-lt text-red",
|
|
};
|
|
|
|
const emptyForm: SecretInput = {
|
|
name: "",
|
|
type: "generic",
|
|
description: "",
|
|
expiryDate: "",
|
|
warnDays: 30,
|
|
notes: "",
|
|
};
|
|
|
|
export default function Secrets({ user }: { user: CurrentUser }) {
|
|
const canEdit = user.role === "admin" || user.role === "operator";
|
|
const [secrets, setSecrets] = useState<SecretRecord[] | null>(null);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [search, setSearch] = useState("");
|
|
const [statusFilter, setStatusFilter] = useState<"all" | SecretStatus>("all");
|
|
const [editingId, setEditingId] = useState<number | null>(null);
|
|
const [form, setForm] = useState<SecretInput>(emptyForm);
|
|
const [saving, setSaving] = useState(false);
|
|
|
|
function load() {
|
|
api.secrets
|
|
.list()
|
|
.then((res) => setSecrets(res.secrets))
|
|
.catch((err) => setError(String(err)));
|
|
}
|
|
|
|
useEffect(load, []);
|
|
|
|
const filtered = useMemo(() => {
|
|
if (!secrets) return [];
|
|
return secrets.filter((s) => {
|
|
if (statusFilter !== "all" && s.status !== statusFilter) return false;
|
|
const q = search.trim().toLowerCase();
|
|
if (!q) return true;
|
|
return s.name.toLowerCase().includes(q) || (s.description ?? "").toLowerCase().includes(q);
|
|
});
|
|
}, [secrets, search, statusFilter]);
|
|
|
|
function startEdit(s: SecretRecord) {
|
|
setEditingId(s.id);
|
|
setForm({
|
|
name: s.name,
|
|
type: s.type,
|
|
description: s.description ?? "",
|
|
expiryDate: s.expiryDate,
|
|
warnDays: s.warnDays,
|
|
notes: s.notes ?? "",
|
|
});
|
|
}
|
|
|
|
function cancelEdit() {
|
|
setEditingId(null);
|
|
setForm(emptyForm);
|
|
}
|
|
|
|
async function submit(e: React.FormEvent) {
|
|
e.preventDefault();
|
|
setError(null);
|
|
setSaving(true);
|
|
try {
|
|
if (editingId) {
|
|
await api.secrets.update(editingId, form);
|
|
} else {
|
|
await api.secrets.create(form);
|
|
}
|
|
cancelEdit();
|
|
load();
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : String(err));
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
}
|
|
|
|
async function remove(s: SecretRecord) {
|
|
if (!confirm(`Delete secret "${s.name}"?`)) return;
|
|
setError(null);
|
|
try {
|
|
await api.secrets.remove(s.id);
|
|
load();
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : String(err));
|
|
}
|
|
}
|
|
|
|
const { sorted, sortKey, sortDir, requestSort } = useSortable(filtered);
|
|
const { pageItems, page, setPage, pageCount, totalCount } = usePagination(sorted);
|
|
|
|
function exportCsv() {
|
|
downloadCsv(
|
|
"secrets.csv",
|
|
["Name", "Type", "Description", "Expiry Date", "Warn Days", "Status", "Days Left", "Notes"],
|
|
(sorted ?? []).map((s) => [
|
|
s.name,
|
|
TYPE_LABELS[s.type],
|
|
s.description ?? "",
|
|
s.expiryDate,
|
|
s.warnDays,
|
|
s.status,
|
|
s.daysLeft,
|
|
s.notes ?? "",
|
|
]),
|
|
);
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<h2 className="page-title mb-3">Secrets</h2>
|
|
{error && <div className="alert alert-danger">{error}</div>}
|
|
|
|
{canEdit && (
|
|
<div className="card mb-3">
|
|
<div className="card-header">
|
|
<h3 className="card-title">{editingId ? "Edit secret" : "Add a secret"}</h3>
|
|
</div>
|
|
<form onSubmit={submit}>
|
|
<div className="card-body row g-3">
|
|
<div className="col-md-4">
|
|
<label className="form-label">Name</label>
|
|
<input
|
|
className="form-control"
|
|
required
|
|
value={form.name}
|
|
onChange={(e) => setForm({ ...form, name: e.target.value })}
|
|
/>
|
|
</div>
|
|
<div className="col-md-3">
|
|
<label className="form-label">Type</label>
|
|
<select
|
|
className="form-select"
|
|
value={form.type}
|
|
onChange={(e) => setForm({ ...form, type: e.target.value as SecretInput["type"] })}
|
|
>
|
|
{Object.entries(TYPE_LABELS).map(([value, label]) => (
|
|
<option key={value} value={value}>
|
|
{label}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</div>
|
|
<div className="col-md-2">
|
|
<label className="form-label">Expiry date</label>
|
|
<input
|
|
type="date"
|
|
className="form-control"
|
|
required
|
|
value={form.expiryDate}
|
|
onChange={(e) => setForm({ ...form, expiryDate: e.target.value })}
|
|
/>
|
|
</div>
|
|
<div className="col-md-3">
|
|
<label className="form-label">Warn (days before)</label>
|
|
<input
|
|
type="number"
|
|
min={0}
|
|
className="form-control"
|
|
value={form.warnDays}
|
|
onChange={(e) => setForm({ ...form, warnDays: Number(e.target.value) })}
|
|
/>
|
|
</div>
|
|
<div className="col-md-6">
|
|
<label className="form-label">Description</label>
|
|
<input
|
|
className="form-control"
|
|
value={form.description}
|
|
onChange={(e) => setForm({ ...form, description: e.target.value })}
|
|
/>
|
|
</div>
|
|
<div className="col-md-6">
|
|
<label className="form-label">Notes</label>
|
|
<input
|
|
className="form-control"
|
|
value={form.notes}
|
|
onChange={(e) => setForm({ ...form, notes: e.target.value })}
|
|
/>
|
|
</div>
|
|
</div>
|
|
<div className="card-footer d-flex gap-2">
|
|
<button type="submit" className="btn btn-primary" disabled={saving}>
|
|
{editingId ? "Save changes" : "Add secret"}
|
|
</button>
|
|
{editingId && (
|
|
<button type="button" className="btn" onClick={cancelEdit}>
|
|
Cancel
|
|
</button>
|
|
)}
|
|
</div>
|
|
</form>
|
|
</div>
|
|
)}
|
|
|
|
<div className="card">
|
|
<div className="card-header">
|
|
<div className="d-flex flex-wrap gap-2 align-items-center w-100">
|
|
<input
|
|
className="form-control"
|
|
style={{ maxWidth: 260 }}
|
|
placeholder="Search name or description…"
|
|
value={search}
|
|
onChange={(e) => setSearch(e.target.value)}
|
|
/>
|
|
<select
|
|
className="form-select"
|
|
style={{ maxWidth: 160 }}
|
|
value={statusFilter}
|
|
onChange={(e) => setStatusFilter(e.target.value as typeof statusFilter)}
|
|
>
|
|
<option value="all">All statuses</option>
|
|
<option value="ok">OK</option>
|
|
<option value="expiring">Expiring</option>
|
|
<option value="expired">Expired</option>
|
|
</select>
|
|
<button className="btn btn-outline-secondary ms-auto" onClick={exportCsv}>
|
|
Export CSV
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div className="table-responsive">
|
|
<table className="table table-vcenter card-table">
|
|
<thead>
|
|
<tr>
|
|
<SortableTh<SecretRecord> label="Name" sortKeyName="name" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
|
<SortableTh<SecretRecord> label="Type" sortKeyName="type" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
|
<SortableTh<SecretRecord>
|
|
label="Expiry"
|
|
sortKeyName="expiryDate"
|
|
activeKey={sortKey}
|
|
direction={sortDir}
|
|
onSort={requestSort}
|
|
/>
|
|
<SortableTh<SecretRecord>
|
|
label="Days left"
|
|
sortKeyName="daysLeft"
|
|
activeKey={sortKey}
|
|
direction={sortDir}
|
|
onSort={requestSort}
|
|
/>
|
|
<SortableTh<SecretRecord> label="Status" sortKeyName="status" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
|
<SortableTh<SecretRecord>
|
|
label="Description"
|
|
sortKeyName="description"
|
|
activeKey={sortKey}
|
|
direction={sortDir}
|
|
onSort={requestSort}
|
|
/>
|
|
{canEdit && <th className="w-1">Actions</th>}
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{(pageItems ?? []).map((s) => (
|
|
<tr key={s.id}>
|
|
<td>{s.name}</td>
|
|
<td>{TYPE_LABELS[s.type]}</td>
|
|
<td>{s.expiryDate}</td>
|
|
<td>{s.daysLeft < 0 ? `${Math.abs(s.daysLeft)}d ago` : `${s.daysLeft}d`}</td>
|
|
<td>
|
|
<span className={`badge ${STATUS_BADGE[s.status]}`}>{s.status}</span>
|
|
</td>
|
|
<td className="text-secondary">{s.description ?? "—"}</td>
|
|
{canEdit && (
|
|
<td>
|
|
<div className="btn-list flex-nowrap">
|
|
<button className="btn btn-sm" onClick={() => startEdit(s)}>
|
|
Edit
|
|
</button>
|
|
<button className="btn btn-sm btn-outline-danger" onClick={() => remove(s)}>
|
|
Delete
|
|
</button>
|
|
</div>
|
|
</td>
|
|
)}
|
|
</tr>
|
|
))}
|
|
{(sorted ?? []).length === 0 && (
|
|
<tr>
|
|
<td colSpan={canEdit ? 7 : 6} className="text-secondary text-center">
|
|
No secrets tracked yet.
|
|
</td>
|
|
</tr>
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<Pagination page={page} pageCount={pageCount} totalCount={totalCount} onPageChange={setPage} />
|
|
</div>
|
|
</>
|
|
);
|
|
}
|