Files
factorio-clusterio/packages/web_ui/src/components/RoleViewPage.tsx
T
bbassieandClaude Opus 5.5 78f1e8a993 Live update roles on the user and role view pages
The user page set the role selector once and never updated it. The role
page reset its whole form on every role update, wiping unsaved edits.

Both pages now follow the live data and keep local edits on top. The role
page stores only the edited fields and drops them once they match the role
or when the viewer loses core.role.update. The user page syncs the
selector while it is not dirty and drops deleted roles from a dirty
selection. A role deleted while open now says so instead of "not found".

Fixes #775, fixes #776

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 22:52:59 +00:00

399 lines
12 KiB
TypeScript

import React, { useEffect, useContext, useState, useMemo, useRef } from "react";
import { useNavigate, useParams } from "react-router-dom";
import { Button, Checkbox, Col, Grid, Input, Popconfirm, Row, Space, Spin, Tooltip } from "antd";
import { ExclamationCircleOutlined, StarOutlined, DeleteOutlined} from "@ant-design/icons";
import * as lib from "@clusterio/lib";
import { useRole } from "../model/roles";
import { useAccount } from "../model/account";
import ControlContext from "./ControlContext";
import PageHeader from "./PageHeader";
import PageLayout from "./PageLayout";
import PluginExtra from "./PluginExtra";
import ResourceNotes from "./ResourceNotes";
import { notifyErrorHandler } from "../util/notify";
/** Splits a permission name into parts, removing the last part */
function getGroupName(name: string) {
const parts = name.split(".");
return parts.length <= 2
? parts.slice(0, 2).join(".")
: parts.slice(0, parts.length - 1).join(".");
}
/** Converts a group name into Title Case with _ and . removed */
function formatGroupTitle(name: string) {
return name
.replace(/\./g, " / ")
.replace(/_/g, " ")
.split(" ")
.filter(Boolean)
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join(" ");
}
/** Check if a permission is dangerous, in the future we might have metadata for this */
function isDangerous(permission: lib.Permission) {
return ["delete", "kill", "stop", "revoke", "update", "core.admin"]
.some(word => permission.name.includes(word));
}
/** A single permission item inside a group; includes a checkbox, name, and description */
function PermissionItem({
permission, baseline, checked, disabled, onChange,
}: {
permission: lib.Permission;
checked: boolean;
baseline: boolean;
disabled: boolean;
onChange: (value: boolean) => void;
}) {
const modified = checked !== baseline;
return (
<div style={{
marginBottom: 8,
padding: modified ? "4px 6px" : undefined,
background: modified ? "#2a1912" : undefined,
borderRadius: 4,
}}>
<div style={{ display: "flex", alignItems: "center" }}>
<Checkbox checked={checked} disabled={disabled} onChange={e => onChange(e.target.checked)}>
<Space size={6}>
<span>{permission.title}</span>
{permission.grantByDefault && (
<Tooltip title="Granted by default">
<StarOutlined style={{ color: "#faad14" }} />
</Tooltip>
)}
{isDangerous(permission) && (
<Tooltip title="Dangerous permission">
<ExclamationCircleOutlined style={{ color: "#ff4d4f" }} />
</Tooltip>
)}
</Space>
</Checkbox>
</div>
<div style={{ marginLeft: 24, color: "#888", fontSize: 12 }}>
{permission.description}
</div>
</div>
);
}
/** A group of permissions; includes a title, clear and select all buttons, and the permission items */
function PermissionGroup({
groupName, permissions, canUpdate, baselineState, permissionState, setPermissions,
}: {
groupName: string;
permissions: lib.Permission[];
canUpdate: boolean;
baselineState: Record<string, boolean>;
permissionState: Record<string, boolean>;
setPermissions: (names: string[], value: boolean) => void;
}) {
function setGroup(value: boolean) {
setPermissions(permissions.map(p => p.name), value);
}
return (
<div id={`permission-group-${groupName}`} style={{
border: "1px solid #424242", padding: 12, borderRadius: 6, scrollMarginTop: 80,
}}>
<Space style={{ width: "100%", justifyContent: "space-between" }}>
<strong>{formatGroupTitle(groupName)}</strong>
<Space>
<Button size="small" disabled={!canUpdate} onClick={() => setGroup(true)}>Select all</Button>
<Button size="small" disabled={!canUpdate} onClick={() => setGroup(false)}>Clear</Button>
</Space>
</Space>
<div style={{ marginTop: 8 }}>
{permissions.map((p: any) => (
<PermissionItem
key={p.name}
permission={p}
checked={permissionState[p.name]}
baseline={baselineState[p.name]}
disabled={!canUpdate}
onChange={v => setPermissions([p.name], v)}
/>
))}
</div>
</div>
);
}
/** An entry in the group list; the dot is blue when any permission in the group is granted */
function PermissionGroupNav({ groupName, granted }: { groupName: string; granted: boolean }) {
function scrollToGroup() {
document.getElementById(`permission-group-${groupName}`)?.scrollIntoView({ behavior: "smooth" });
}
return (
<div
role="button"
onClick={scrollToGroup}
style={{ display: "flex", alignItems: "center", gap: 8, padding: "4px 8px", cursor: "pointer" }}
>
<span style={{
width: 8,
height: 8,
borderRadius: "50%",
flexShrink: 0,
background: granted ? "#1677ff" : "#595959",
}} />
{formatGroupTitle(groupName)}
</div>
);
}
/** The role page view */
export default function RoleViewPage() {
const params = useParams();
const roleId = Number(params.id);
const navigate = useNavigate();
const account = useAccount();
const control = useContext(ControlContext);
const [role, synced] = useRole(roleId);
// Local edits on top of the live role
const [nameEdit, setNameEdit] = useState<string>();
const [descriptionEdit, setDescriptionEdit] = useState<string>();
const [permissionEdits, setPermissionEdits] = useState<Record<string, boolean>>({});
const [search, setSearch] = useState("");
const loadedRoleId = useRef<number>(undefined);
// Hidden below lg, where the site sider leaves too little room; undefined until measured
const screens = Grid.useBreakpoint();
const showGroupList = screens.lg !== false;
const allPermissions = useMemo(() => [...lib.permissions.values()], []);
const canUpdate = Boolean(account.hasPermission("core.role.update"));
const baselineState = useMemo(() => Object.fromEntries(
allPermissions.map(p => [p.name, Boolean(role?.permissions.has(p.name))])
), [role, allPermissions]);
// Drop edits that now match the role, and all edits if we can no longer apply them
useEffect(() => {
if (!role || !canUpdate) {
setNameEdit(undefined);
setDescriptionEdit(undefined);
setPermissionEdits(prev => (Object.keys(prev).length ? {} : prev));
return;
}
setNameEdit(prev => (prev === role.name ? undefined : prev));
setDescriptionEdit(prev => (prev === role.description ? undefined : prev));
setPermissionEdits(prev => {
const next = Object.fromEntries(Object.entries(prev).filter(([k, v]) => v !== baselineState[k]));
return Object.keys(next).length === Object.keys(prev).length ? prev : next;
});
}, [role, canUpdate, baselineState]);
useEffect(() => {
setNameEdit(undefined);
setDescriptionEdit(undefined);
setPermissionEdits({});
}, [roleId]);
if (role) {
loadedRoleId.current = roleId;
}
// If no role then display either the loading screen or the not found screen
if (!role) {
const nav = [{ name: "Roles", path: "/roles" }, { name: String(roleId) }];
if (!synced) {
return <PageLayout nav={nav}>
<PageHeader title={String(roleId)} />
<Spin size="large" />
</PageLayout>;
}
return <PageLayout nav={nav}>
<PageHeader title="Role not found" />
<p>Role with id {roleId} {
loadedRoleId.current === roleId ? "has been deleted" : "was not found on the controller"
}.</p>
</PageLayout>;
}
const name = nameEdit ?? role.name;
const description = descriptionEdit ?? role.description;
const permissionState = { ...baselineState, ...permissionEdits };
// Apply the filter by searching all permission fields
const filtered = allPermissions.filter(p => {
const q = search.toLowerCase();
return p.name.toLowerCase().includes(q)
|| p.title.toLowerCase().includes(q)
|| p.description.toLowerCase().includes(q);
});
// Organise the permissions into groups
const groups = new Map<string, typeof filtered>();
for (let p of filtered) {
const g = getGroupName(p.name);
if (!groups.has(g)) {
groups.set(g, []);
}
groups.get(g)!.push(p);
}
// Sort permissions alphabetically within their groups
const sortedGroups = [...groups.entries()].sort(([a], [b]) => a.localeCompare(b));
for (let [, permissions] of sortedGroups) {
permissions.sort((a, b) => a.title.localeCompare(b.title));
}
// Groups with at least one granted permission, including those hidden by the search
const grantedGroups = new Set(allPermissions.filter(p => permissionState[p.name]).map(p => getGroupName(p.name)));
// Check if any changes have been made to the permissions or role metadata
const permissionChanged = Object.keys(permissionEdits).some(k => permissionEdits[k] !== baselineState[k]);
const metaChanged = name !== role.name || description !== role.description;
const edited = permissionChanged || metaChanged;
function setPermissions(names: string[], value: boolean) {
setPermissionEdits(prev => {
const next = { ...prev };
for (let permissionName of names) {
next[permissionName] = value;
}
return next;
});
}
/** Revert any pending changes */
function revertChanges() {
setNameEdit(undefined);
setDescriptionEdit(undefined);
setPermissionEdits({});
}
/** Applies all pending changes */
function applyChanges() {
const newPermissions = Object.entries(permissionState).filter(([, v]) => v).map(([k]) => k);
control.send(new lib.RoleUpdateRequest(roleId, name, description, newPermissions))
.then(revertChanges)
.catch(notifyErrorHandler("Error applying changes"));
}
return (
<PageLayout nav={[{ name: "Roles", path: "/roles" }, { name: role.name }]}>
<PageHeader
title={role.name}
extra={<Space wrap>
{account.hasPermission("core.role.delete") && <Popconfirm
title="Delete role?"
placement="bottomRight"
okText="Delete"
okButtonProps={{ danger: true }}
onConfirm={() => {
control.send(new lib.RoleDeleteRequest(roleId))
.then(() => navigate("/roles"))
.catch(notifyErrorHandler("Error deleting role"));
}}
>
<Button danger><DeleteOutlined /></Button>
</Popconfirm>}
</Space>}
/>
{edited && (
<div style={{
position: "fixed",
bottom: 24,
left: "50%",
transform: "translateX(-50%)",
background: "#2a1912",
borderRadius: 8,
boxShadow: "0 8px 24px rgba(0,0,0,0.12)",
padding: "10px 14px",
display: "flex",
alignItems: "center",
gap: 12,
zIndex: 1000,
}}>
<span style={{ color: "#FFF" }}>
You have unsaved changes
</span>
<Space>
<Button onClick={revertChanges}>
Revert
</Button>
<Button type="primary" onClick={applyChanges}>
Apply
</Button>
</Space>
</div>
)}
<div style={{ marginBottom: 16, display: "flex", alignItems: "center", gap: 12 }}>
<label style={{ width: 110, flexShrink: 0 }}>Name</label>
<Input value={name} disabled={!canUpdate} onChange={e => setNameEdit(e.target.value)} />
</div>
<div style={{ marginBottom: 16, display: "flex", alignItems: "center", gap: 12 }}>
<label style={{ width: 110, flexShrink: 0 }}>Description</label>
<Input value={description} disabled={!canUpdate} onChange={e => setDescriptionEdit(e.target.value)} />
</div>
<ResourceNotes resourceType="role" resourceId={roleId} />
<PluginExtra component="RoleViewPageBeforePermissions" role={role} />
<h3>Permissions</h3>
<Input.Search placeholder="Search permissions" allowClear
onChange={e => setSearch(e.target.value)} style={{ marginBottom: 16 }} />
<Row gutter={16} wrap={false}>
{showGroupList && <Col flex="200px">
{/* Clear of the fixed page header */}
<div style={{
position: "sticky", top: 80, maxHeight: "calc(100vh - 96px)", overflowY: "auto",
// Keep the last groups reachable above the unsaved changes bar
paddingBottom: edited ? 76 : 0,
}}>
{sortedGroups.map(([groupName]) => (
<PermissionGroupNav
key={groupName}
groupName={groupName}
granted={grantedGroups.has(groupName)}
/>
))}
</div>
</Col>}
<Col flex="auto" style={{ minWidth: 0 }}>
<Space direction="vertical" style={{ width: "100%" }}>
{sortedGroups.map(([groupName, permissions]) => (
<PermissionGroup
key={groupName}
groupName={groupName}
permissions={permissions}
permissionState={permissionState}
baselineState={baselineState}
setPermissions={setPermissions}
canUpdate={canUpdate}
/>
))}
</Space>
</Col>
</Row>
<PluginExtra component="RoleViewPage" role={role} search={search}/>
</PageLayout>
);
}