Applies the useSortable/SortableTh infrastructure introduced in the Diagnostic Log commit to the rest of the app's tables: Audit Log, Users, Servers (manage table), server task tables (grouped by schedule type, on both the all-servers and per-server views), DNS providers/zones/records, Docker containers, Gitea repos, Integrations (manage table), IP Addresses, Proxmox guests, Secrets (already had CSV, gained sorting), Semaphore templates, Synology volumes/disks, and Tailscale devices. Every column header is now click-to-sort (again on the raw field, not its formatted display -- a byte count sorts numerically even though the cell shows "1.2 GB", a date sorts chronologically even though the cell shows "5d 15h 2m"), and every table got an "Export CSV" button next to its Refresh button, exporting whatever's currently sorted/filtered via the existing downloadCsv util. Grouped tables (ServerTaskTable renders one sub-table per schedule type) can't call the useSortable hook per group without breaking the Rules of Hooks, so extracted its comparison core as a standalone sortItems() function, driven by one shared sort-state pair at the component's top level and applied per group. Deliberately left two small (1-6 row) tables embedded inside stat cards unsorted -- Proxmox's per-node storage list and ServerDetail's agent-reported disk list -- since they're secondary detail inside an already-scannable card, not primary list content; happy to add if useful in practice. Verified the shared sort core (sortItems) directly: numeric-aware string compare (so "item2" sorts before "item10"), numbers, booleans, and that null/undefined always sort to the end regardless of direction. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
110 lines
3.8 KiB
TypeScript
110 lines
3.8 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
import { api, type UserRecord, type UserRole } from "../api/client";
|
|
import { formatDateTime } from "../utils/date";
|
|
import { useSortable } from "../hooks/useSortable";
|
|
import SortableTh from "../components/SortableTh";
|
|
import { downloadCsv } from "../utils/csv";
|
|
|
|
const ROLES: UserRole[] = ["viewer", "operator", "admin"];
|
|
|
|
export default function Users() {
|
|
const [users, setUsers] = useState<UserRecord[] | null>(null);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [savingId, setSavingId] = useState<number | null>(null);
|
|
|
|
function load() {
|
|
api.users
|
|
.list()
|
|
.then((res) => setUsers(res.users))
|
|
.catch((err) => setError(String(err)));
|
|
}
|
|
|
|
useEffect(load, []);
|
|
|
|
async function changeRole(id: number, role: UserRole) {
|
|
setError(null);
|
|
setSavingId(id);
|
|
try {
|
|
await api.users.updateRole(id, role);
|
|
load();
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : String(err));
|
|
} finally {
|
|
setSavingId(null);
|
|
}
|
|
}
|
|
|
|
const { sorted, sortKey, sortDir, requestSort } = useSortable(users);
|
|
|
|
function exportCsv() {
|
|
if (!sorted) return;
|
|
downloadCsv(
|
|
"users.csv",
|
|
["Name", "Email", "Role", "Last login"],
|
|
sorted.map((u) => [u.name ?? "", u.email ?? "", u.role, u.lastLoginAt ? formatDateTime(new Date(u.lastLoginAt)) : "never"]),
|
|
);
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<div className="d-flex align-items-center mb-3">
|
|
<h2 className="page-title mb-0">Users</h2>
|
|
<button className="btn btn-outline-secondary ms-auto" onClick={exportCsv} disabled={!sorted || sorted.length === 0}>
|
|
Export CSV
|
|
</button>
|
|
</div>
|
|
{error && <div className="alert alert-danger">{error}</div>}
|
|
<div className="card">
|
|
<div className="table-responsive">
|
|
<table className="table table-vcenter card-table">
|
|
<thead>
|
|
<tr>
|
|
<SortableTh<UserRecord> label="Name" sortKeyName="name" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
|
<SortableTh<UserRecord> label="Email" sortKeyName="email" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
|
<SortableTh<UserRecord> label="Role" sortKeyName="role" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
|
<SortableTh<UserRecord>
|
|
label="Last login"
|
|
sortKeyName="lastLoginAt"
|
|
activeKey={sortKey}
|
|
direction={sortDir}
|
|
onSort={requestSort}
|
|
/>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{sorted?.map((u) => (
|
|
<tr key={u.id}>
|
|
<td>{u.name ?? "—"}</td>
|
|
<td>{u.email ?? "—"}</td>
|
|
<td style={{ maxWidth: 160 }}>
|
|
<select
|
|
className="form-select form-select-sm"
|
|
value={u.role}
|
|
disabled={savingId === u.id}
|
|
onChange={(e) => changeRole(u.id, e.target.value as UserRole)}
|
|
>
|
|
{ROLES.map((r) => (
|
|
<option key={r} value={r}>
|
|
{r}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</td>
|
|
<td>{u.lastLoginAt ? formatDateTime(new Date(u.lastLoginAt)) : "never"}</td>
|
|
</tr>
|
|
))}
|
|
{sorted?.length === 0 && (
|
|
<tr>
|
|
<td colSpan={4} className="text-secondary text-center">
|
|
No users yet.
|
|
</td>
|
|
</tr>
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</>
|
|
);
|
|
}
|