Files
Homelab-manager/web/src/pages/Users.tsx
T
bobbanandClaude Sonnet 5.5 1432b148e9 Fold four pages into the pages they belong to; add a Backups menu group
Dependencies become a view of the Map (graph with the editor under it), Sessions the
'Signed in now' tab of Users, the Audit Log the 'Raw audit log' tab of the Change log, and the
Diagnostic Log the 'Call log' tab of Integrations (admins). Backup coverage and Proxmox Backup
move to a new Backups group. Old addresses redirect; a shared ViewTabs component does the tabs.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-19 22:21:21 +02:00

168 lines
6.5 KiB
TypeScript

import { useEffect, useState, type ReactNode } from "react";
import { Link, useSearchParams } from "react-router-dom";
import ViewTabs from "../components/ViewTabs";
import SessionsView from "../views/SessionsView";
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";
import { ErrorNotice } from "../components/Notice";
import { SearchBox } from "../components/TableToolbar";
import { useTableSearch } from "../hooks/useTableSearch";
import { EmptyRow, LoadingRow } from "../components/EmptyState";
import Avatar from "../components/Avatar";
const ROLES: UserRole[] = ["viewer", "operator", "admin"];
/** People and Signed in now: who has an account and what they may do, and who is signed in at this moment. Switched by `?view=sessions`. */
const userTabs = (view: "people" | "sessions") => (
<ViewTabs
label="Users view"
tabs={[
{ to: "/users", label: "People", active: view === "people" },
{ to: "/users?view=sessions", label: "Signed in now", active: view === "sessions" },
]}
/>
);
export default function Users() {
const [params] = useSearchParams();
if (params.get("view") === "sessions") {
return (
<>
<div className="d-flex align-items-center mb-2">
<h2 className="page-title mb-0">Users</h2>
</div>
{userTabs("sessions")}
<SessionsView />
</>
);
}
return <People tabs={userTabs("people")} />;
}
function People({ tabs }: { tabs: ReactNode }) {
const [users, setUsers] = useState<UserRecord[] | null>(null);
const [error, setError] = useState<string | null>(null);
const [savingId, setSavingId] = useState<number | null>(null);
// Roles follow sign-in groups (Settings → Sign-in): they are shown, not set here.
const [followGroups, setFollowGroups] = useState(false);
function load() {
api.users
.list()
.then((res) => {
setUsers(res.users);
setFollowGroups(res.rolesFollowGroups);
})
.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 tableSearch = useTableSearch(users);
const { sorted, sortKey, sortDir, requestSort } = useSortable(tableSearch.filtered, undefined, "asc", { url: true });
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>
{tabs}
<ErrorNotice error={error} />
{followGroups && (
<div className="alert alert-info">
Roles are set from sign-in groups, so they can't be changed here: change a person's group in the identity provider, and their role follows at their next sign-in.{" "}
<Link to="/settings/sign-in">Sign-in settings</Link>
</div>
)}
<div className="card">
<div className="card-header">
<SearchBox value={tableSearch.query} onChange={tableSearch.setQuery} />
</div>
<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} />
<th>Groups at last sign-in</th>
<SortableTh<UserRecord>
label="Last login"
sortKeyName="lastLoginAt"
activeKey={sortKey}
direction={sortDir}
onSort={requestSort}
/>
</tr>
</thead>
<tbody>
{sorted?.map((u) => (
<tr key={u.id}>
<td>
<span className="d-inline-flex align-items-center gap-2">
<Avatar name={u.name ?? u.email ?? u.oidcSub} src={u.avatarUrl} />
{u.name ?? "—"}
</span>
</td>
<td>{u.email ?? "—"}</td>
<td style={{ maxWidth: 160 }}>
<select
className="form-select form-select-sm"
value={u.role}
disabled={savingId === u.id || followGroups}
title={followGroups ? "Set from sign-in groups" : undefined}
onChange={(e) => changeRole(u.id, e.target.value as UserRole)}
>
{ROLES.map((r) => (
<option key={r} value={r}>
{r}
</option>
))}
</select>
</td>
<td className="text-secondary small">
{u.groups === null ? "—" : u.groups.length === 0 ? "none" : u.groups.length <= 3 ? u.groups.join(", ") : `${u.groups.slice(0, 3).join(", ")} and ${u.groups.length - 3} more`}
</td>
<td>{u.lastLoginAt ? formatDateTime(new Date(u.lastLoginAt)) : "never"}</td>
</tr>
))}
{users === null && <LoadingRow colSpan={5} />}
{sorted?.length === 0 && (
<EmptyRow colSpan={5} title="No users yet" hint="People appear here the first time they sign in through Authentik; you decide their role." filtered={tableSearch.active ? () => tableSearch.setQuery("") : undefined} />
)}
</tbody>
</table>
</div>
</div>
</>
);
}