The UI worked but read as an operator console: no public face, no way to describe or sell the thing, and no account surface beyond the node list. This adds the missing halves and reorganises what was already there around the incident, which is the unit of value the rest of the pipeline is built to produce. A shared design system replaces per-page styling: components/ui (Button, Card, Badge, EmptyState, Skeleton, PageHeader), a type scale and shadow set in the Tailwind config, and light-mode tokens in globals.css. The existing html:not(.dark) remap mechanism is extended rather than replaced -- a parallel theming system would have been two sources of truth for the same colours. Public marketing pages (/, /features, /pricing, /faq) load without a session. middleware.ts gained a PUBLIC_PATHS allowlist to permit that; it remains a UX redirect and is still NOT an authorisation boundary, which the comment there says explicitly. Real enforcement is unchanged and still lives server-side in c2-core's auth.py. Chrome switching is done by pathname in ChromeSwitcher instead of by route group, because a route group would have collided on / and forced most of app/ to move for no behavioural gain. Billing and API keys ship as typed stubs, not integrations. lib/billing.ts and lib/apiKeys.ts define the data model and the screens consume it, but every mutating call throws with a message naming the backend route that has to exist first, and the sample data is labelled as sample. Nothing here can charge anyone or mint a real credential -- picking a payment processor and holding its keys is a decision for a human, and a half-wired checkout is worse than an obviously absent one. The severity work from the c2-core change lands here too. severity is now a filter and sort dimension on the incident list rather than decoration, since a busy dispatch channel is only readable if you can collapse it to moderate and above. routine gets a muted treatment because it is the majority of traffic, legacy "unknown" still renders nothing, and TypeBadge handles the new "other" incident type. Severity rendering moved into lib/severity.tsx so the incident list, incident detail and call rows cannot drift apart. Deliberately not touched: calls, map, alerts, nodes, systems, tokens, trips and admin. They already share the palette and stay coherent, and rewriting them would have buried the parts that actually needed to change. No colour tokens were renamed, so nothing regressed there. Verified with tsc --noEmit (npm run typecheck), clean. No runtime verification was possible and none was done. No new environment variables.
128 lines
5.3 KiB
TypeScript
128 lines
5.3 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
import Link from "next/link";
|
|
import { useNodes } from "@/lib/useNodes";
|
|
import { c2api } from "@/lib/c2api";
|
|
import type { UserRecord } from "@/lib/types";
|
|
import { StatusBadge } from "@/components/StatusBadge";
|
|
import { Card } from "@/components/ui/Card";
|
|
import { ErrorBanner } from "@/components/ui/EmptyState";
|
|
import { SkeletonRow } from "@/components/ui/Skeleton";
|
|
|
|
const UNASSIGNED = "__unassigned__";
|
|
|
|
export default function NodeOwnershipSettingsPage() {
|
|
const { nodes, loading: nodesLoading } = useNodes();
|
|
const [users, setUsers] = useState<UserRecord[]>([]);
|
|
const [loadingUsers, setLoadingUsers] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [savingNodeId, setSavingNodeId] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
c2api.listUsers()
|
|
.then(setUsers)
|
|
.catch((e) => setError(e instanceof Error ? e.message : String(e)))
|
|
.finally(() => setLoadingUsers(false));
|
|
}, []);
|
|
|
|
// Ownership (owned_node_ids) is only meaningful for operators elsewhere in the
|
|
// app (see Admin → Users); admins already have full access regardless.
|
|
const assignable = useMemo(() => users.filter((u) => u.role === "operator"), [users]);
|
|
|
|
const ownerByNode = useMemo(() => {
|
|
const map = new Map<string, UserRecord>();
|
|
for (const u of users) {
|
|
if (u.role !== "operator") continue;
|
|
for (const nodeId of u.owned_node_ids) map.set(nodeId, u);
|
|
}
|
|
return map;
|
|
}, [users]);
|
|
|
|
const reassign = useCallback(async (nodeId: string, newUid: string) => {
|
|
setSavingNodeId(nodeId);
|
|
setError(null);
|
|
try {
|
|
const prevOwner = ownerByNode.get(nodeId);
|
|
// Remove from previous owner, if any and different from the new one.
|
|
if (prevOwner && prevOwner.uid !== newUid) {
|
|
const next = prevOwner.owned_node_ids.filter((id) => id !== nodeId);
|
|
await c2api.updateUser(prevOwner.uid, { owned_node_ids: next });
|
|
setUsers((all) => all.map((u) => (u.uid === prevOwner.uid ? { ...u, owned_node_ids: next } : u)));
|
|
}
|
|
// Add to new owner, if one was selected.
|
|
if (newUid !== UNASSIGNED) {
|
|
const newOwner = users.find((u) => u.uid === newUid);
|
|
if (newOwner && !newOwner.owned_node_ids.includes(nodeId)) {
|
|
const next = [...newOwner.owned_node_ids, nodeId];
|
|
await c2api.updateUser(newOwner.uid, { owned_node_ids: next });
|
|
setUsers((all) => all.map((u) => (u.uid === newOwner.uid ? { ...u, owned_node_ids: next } : u)));
|
|
}
|
|
}
|
|
} catch (e) {
|
|
setError(e instanceof Error ? e.message : String(e));
|
|
} finally {
|
|
setSavingNodeId(null);
|
|
}
|
|
}, [ownerByNode, users]);
|
|
|
|
const loading = nodesLoading || loadingUsers;
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
<p className="text-sm text-gray-500">
|
|
Assign each node to the operator responsible for it. Operators only see and manage the nodes assigned to them here.
|
|
</p>
|
|
|
|
{error && <ErrorBanner message={error} />}
|
|
|
|
<Card padding="none" className="overflow-hidden">
|
|
<table className="w-full text-sm">
|
|
<thead>
|
|
<tr className="text-xs text-gray-500 uppercase tracking-wider border-b border-gray-800 bg-gray-900">
|
|
<th className="px-4 py-3 text-left">Node</th>
|
|
<th className="px-4 py-3 text-left hidden sm:table-cell">Status</th>
|
|
<th className="px-4 py-3 text-left">Owner</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{loading ? (
|
|
Array.from({ length: 3 }).map((_, i) => <SkeletonRow key={i} cols={3} />)
|
|
) : nodes.length === 0 ? (
|
|
<tr><td colSpan={3} className="px-4 py-8 text-center text-gray-600 text-sm">No nodes registered yet.</td></tr>
|
|
) : (
|
|
nodes.map((n) => {
|
|
const owner = ownerByNode.get(n.node_id);
|
|
return (
|
|
<tr key={n.node_id} className="border-b border-gray-800 last:border-0">
|
|
<td className="px-4 py-3">
|
|
<Link href={`/nodes/${n.node_id}`} className="text-white hover:text-indigo-300 transition-colors">
|
|
{n.name}
|
|
</Link>
|
|
<p className="text-gray-600 text-xs font-mono">{n.node_id}</p>
|
|
</td>
|
|
<td className="px-4 py-3 hidden sm:table-cell"><StatusBadge status={n.status} /></td>
|
|
<td className="px-4 py-3">
|
|
<select
|
|
value={owner?.uid ?? UNASSIGNED}
|
|
disabled={savingNodeId === n.node_id}
|
|
onChange={(e) => reassign(n.node_id, e.target.value)}
|
|
className="bg-gray-800 border border-gray-700 rounded-lg px-2 py-1.5 text-xs text-white focus:outline-none focus:border-indigo-500 disabled:opacity-50 max-w-[14rem]"
|
|
>
|
|
<option value={UNASSIGNED}>Unassigned</option>
|
|
{assignable.map((u) => (
|
|
<option key={u.uid} value={u.uid}>{u.display_name || u.email}</option>
|
|
))}
|
|
</select>
|
|
</td>
|
|
</tr>
|
|
);
|
|
})
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
</Card>
|
|
</div>
|
|
);
|
|
}
|