// R-P2b — D-P2 "WF-P2 Admin RBAC" S02 "Rollen und Berechtigungen" in David's // Subframe optics. Pure presentational: renders the server-built RolesListVM // and reports mutations through injected actions (route owns the fetcher). // S02 — merged roles tab: search + type/in-use filters, Typ/Mitglieder columns // M04A — "Rolle anlegen" basics step → M02 // M04B — "Rolle duplizieren" basics step (copied permissions) → M02 // M02 — large permission editor (L 1200): section sidebar + search + switches // D03 — "Rolle in Nutzung blockiert" delete blocker // David's "Status" column has no data-model backing (no Role.status) and the // "Bereich/Kategorie" select has no Role.category — both omitted, tracked as // design-nachzug. Role duplication = create with the source's permission set // preloaded into the editor (same audited createWorkspaceRole path). import { useEffect, useMemo, useState } from "react"; import { useTranslation } from "react-i18next"; import * as SubframeCore from "@subframe/core"; import { FeatherCopy, FeatherEdit2, FeatherMoreHorizontal, FeatherPlus, FeatherShield, FeatherTrash, FeatherUserCheck } from "@subframe/core"; import type { RoleEditorInputPermission, RolesListRow, RolesListVM, RoleTypeKey } from "@hivegrid/view-models"; import { buildRoleEditorVM, filterRoleEditorSections, togglePermission, EMPTY_ROLES_FILTER, filterRolesRows, type RolesListFilterState } from "@hivegrid/view-models"; import { permissionDisplayDescription, permissionDisplayName, roleDisplayDescription, roleDisplayName } from "~/components/core/roles/roleDisplay"; import { FacetFilterBar, type ClientFacet } from "../shared/FacetFilterBar"; import { PageIntro } from "../shared/PageShell"; import { useLocaleFormatters } from "~/locale/format"; import { Alert } from "~/ui/components/Alert"; import { Button } from "~/ui/components/Button"; import { FormDialog } from "../shared/FormDialog"; import { DropdownMenu } from "~/ui/components/DropdownMenu"; import { EmptyState } from "~/ui/components/EmptyState"; import { IconButton } from "~/ui/components/IconButton"; import { SearchInput } from "~/ui/components/SearchInput"; import { Switch } from "~/ui/components/Switch"; import { Table } from "~/ui/components/Table"; import { TextArea } from "~/ui/components/TextArea"; import { TextInput } from "~/ui/components/TextInput"; export type RoleMutationInput = { name: string; description: string; permissionNames: string[] }; export type RolesActions = { /** POSTs the existing create contract (roles/new, action=create). */ onCreate: (input: RoleMutationInput) => void; /** POSTs the existing update contract (roles/:id, action=edit). */ onUpdate: (roleId: string, input: RoleMutationInput) => void; /** POSTs the existing delete contract (roles/:id, action=delete). */ onDelete: (roleId: string) => void; }; export type RolesPageProps = { vm: RolesListVM; /** App permission catalog feeding the M02 editor sections. */ editorCatalog: readonly RoleEditorInputPermission[]; /** roleId → assigned permission names (editor prefill + M04B duplication). */ rolePermissionNames: Readonly>; /** Tab bar back to S01 (members). */ actions: RolesActions; submitting: boolean; errorMessage: string | null; /** Increments after a successful mutation — closes any open dialog. */ closeSignal: number; }; /** M04A/M04B basics step. */ type BasicsState = { source: RolesListRow | null; name: string; description: string }; /** M02 editor state. roleId null = create (after M04A/M04B). */ type EditorState = { roleId: string | null; duplicatedFromName: string | null; name: string; description: string; enabled: ReadonlySet; }; export function RolesPage({ vm, editorCatalog, rolePermissionNames, actions, submitting, errorMessage, closeSignal }: RolesPageProps) { const { t } = useTranslation(); const formatters = useLocaleFormatters(); const [filter, setFilter] = useState(EMPTY_ROLES_FILTER); const [basics, setBasics] = useState(null); const [editor, setEditor] = useState(null); const [deleteRow, setDeleteRow] = useState(null); useEffect(() => { if (closeSignal > 0) { setBasics(null); setEditor(null); setDeleteRow(null); } }, [closeSignal]); if (vm.status !== "ready" || !vm.data) { return
; } const data = vm.data; const rows = filterRolesRows(data.rows, filter); const facets: ClientFacet[] = [ { key: "type", label: t("settings.rolesAndPermissions.columns.type"), icon: , tone: "violet-subtle", options: [ { value: "system", label: t("settings.rolesAndPermissions.type.system") }, { value: "custom", label: t("settings.rolesAndPermissions.type.custom") }, ], selected: new Set(filter.type ? [filter.type] : []), }, { key: "inUse", label: t("settings.rolesAndPermissions.filters.inUse"), icon: , tone: "blue-subtle", options: [ { value: "yes", label: t("settings.rolesAndPermissions.filters.inUseYes") }, { value: "no", label: t("settings.rolesAndPermissions.filters.inUseNo") }, ], selected: new Set(filter.inUse === null ? [] : [filter.inUse ? "yes" : "no"]), }, ]; function toggleFacet(facetKey: string, value: string) { if (facetKey === "type") { return setFilter((prev) => ({ ...prev, type: prev.type === value ? null : (value as RoleTypeKey) })); } if (facetKey === "inUse") { const next = value === "yes"; return setFilter((prev) => ({ ...prev, inUse: prev.inUse === next ? null : next })); } return undefined; } function openEditorFor(row: RolesListRow) { setEditor({ roleId: row.id, duplicatedFromName: null, name: row.name, description: row.description, enabled: new Set(rolePermissionNames[row.id] ?? []), }); } function continueToPermissions(state: BasicsState) { setBasics(null); setEditor({ roleId: null, duplicatedFromName: state.source ? state.source.name : null, name: state.name, description: state.description, enabled: new Set(state.source ? (rolePermissionNames[state.source.id] ?? []) : []), }); } return (
{/* The page header (title + subtitle) is rendered by the route above the layout, like Mitglieder. */} {/* AK-SET-01: the Team-Hub renders the tab bar this screen used to carry. */} {/* Intro + create CTA on one row, exactly as the Mitglieder tab next door hosts its own; the hub header above carries only title and subtitle. */}
{t("settings.rolesAndPermissions.subtitle")} {data.canCreate ? ( ) : null}
{/* --- Filter bar: the shared facet strip the sibling tabs and the board lists wear - free-text box, facet buttons, chips for what is active and one reset. Both facets are single-select, so picking the active option again clears it, just as the chip's X does. --- */} setFilter((prev) => ({ ...prev, query: value }))} searchPlaceholder={t("settings.rolesAndPermissions.filters.searchPlaceholder")} onReset={() => setFilter(EMPTY_ROLES_FILTER)} labels={{ reset: t("settings.rolesAndPermissions.filters.reset"), removeChip: t("settings.rolesAndPermissions.filters.removeChip"), }} /> {data.rows.length === 0 ? (
) : (
{t("models.role.object")}{t("models.role.description")}{t("settings.rolesAndPermissions.columns.type")}{t("settings.rolesAndPermissions.columns.members")}{t("models.permission.plural")} } > {rows.map((row) => ( {roleDisplayName({ name: row.name, description: row.description }, t)} {roleDisplayDescription({ name: row.name, description: row.description }, t) || "—"} {t(row.typeLabelKey)} {formatters.number(row.memberCount)} {t("models.permission.count", { count: row.permissionCount, formattedCount: formatters.number(row.permissionCount) })} {row.canEdit || row.canDuplicate || row.canDelete ? ( } aria-label={`${t("shared.actions")}: ${roleDisplayName({ name: row.name, description: row.description }, t)}`} data-ui-id="ui.settings.rolesAndPermissions.roles.rowActions" /> {row.canEdit ? ( } onClick={() => openEditorFor(row)}> {t("shared.edit")} ) : null} {row.canDuplicate ? ( } onClick={() => setBasics({ source: row, name: t("settings.rolesAndPermissions.duplicate.copyName", { name: row.name }), description: row.description, }) } > {t("settings.rolesAndPermissions.duplicate.action")} ) : null} {row.canDelete ? ( } onClick={() => setDeleteRow(row)}> {t("shared.delete")} ) : null} ) : null} ))}
{rows.length === 0 ? (
) : null}
)} {/* M04A / M04B — basics step (S 560) */} {basics ? ( setBasics(null)} /> ) : null} {/* M02 — large permission editor (L 1200) */} {editor ? ( setEditor(null)} onSave={() => { const input = { name: editor.name, description: editor.description, permissionNames: [...editor.enabled] }; if (editor.roleId) actions.onUpdate(editor.roleId, input); else actions.onCreate(input); }} /> ) : null} {/* Delete confirm / D03 blocker (XS 420) */} {deleteRow ? ( (open ? undefined : setDeleteRow(null))} size="xs" uiId="ui.settings.rolesAndPermissions.roles.deleteConfirm" title={deleteRow.deleteBlockedInUse ? t("settings.rolesAndPermissions.deleteBlocked.title") : t("settings.rolesAndPermissions.delete.title")} footer={ <> {!deleteRow.deleteBlockedInUse ? ( ) : null} } > {deleteRow.deleteBlockedInUse ? t("settings.rolesAndPermissions.deleteBlocked.description", { name: deleteRow.name, count: deleteRow.memberCount }) : t("settings.rolesAndPermissions.delete.description", { name: deleteRow.name })} ) : null}
); } function RoleBasicsDialog({ state, onChange, onContinue, onCancel, }: { state: BasicsState; onChange: (state: BasicsState) => void; onContinue: (state: BasicsState) => void; onCancel: () => void; }) { const { t } = useTranslation(); const isDuplicate = state.source !== null; const nameMissing = state.name.trim().length === 0; return ( (open ? undefined : onCancel())} uiId="ui.settings.rolesAndPermissions.roles.basics" title={isDuplicate ? t("settings.rolesAndPermissions.duplicate.title") : t("settings.rolesAndPermissions.createDialog.title")} subtitle={ isDuplicate ? t("settings.rolesAndPermissions.duplicate.hint", { name: state.source?.name ?? "" }) : t("settings.rolesAndPermissions.createDialog.description") } footer={ <> } > onChange({ ...state, name: event.target.value })} required autoFocus /> ); } function RoleEditorDialog({ state, onChange, editorCatalog, submitting, errorMessage, onCancel, onSave, }: { state: EditorState; onChange: (state: EditorState) => void; editorCatalog: readonly RoleEditorInputPermission[]; submitting: boolean; errorMessage: string | null; onCancel: () => void; onSave: () => void; }) { const { t } = useTranslation(); const [query, setQuery] = useState(""); const [selectedSection, setSelectedSection] = useState(null); // Pure VM grouping (scope → David's sections); enabled state comes from the dialog. const sections = useMemo(() => { const vm = buildRoleEditorVM({ role: state.roleId ? { id: state.roleId, name: state.name, description: state.description } : null, duplicatedFromName: state.duplicatedFromName, appPermissions: editorCatalog, assignedPermissionNames: [...state.enabled], }); return vm.data?.sections ?? []; }, [state.roleId, state.name, state.description, state.duplicatedFromName, state.enabled, editorCatalog]); const searched = useMemo(() => filterRoleEditorSections(sections, query), [sections, query]); const visible = query.trim() ? searched : selectedSection ? searched.filter((section) => section.id === selectedSection) : searched; const nameMissing = state.name.trim().length === 0; return ( (open ? undefined : onCancel())} size="large" uiId="ui.settings.rolesAndPermissions.roles.editor" title={state.roleId ? t("settings.rolesAndPermissions.editor.title") : t("settings.rolesAndPermissions.createDialog.title")} subtitle={state.duplicatedFromName ? t("settings.rolesAndPermissions.duplicate.hint", { name: state.duplicatedFromName }) : undefined} // The editor's permission column scrolls on its own, so the body must not // add a second scroll container around it. bodyClassName="max-h-none overflow-visible" footer={ <> } > {errorMessage ? : null}
{/* Sidebar: search + sections (Apple-settings pattern from David's M02) */}
setQuery(event.target.value)} />
{sections.map((section) => ( ))}
{/* Main: role base data + permission rows as switches */}
{t("settings.rolesAndPermissions.editor.roleSection")} onChange({ ...state, name: event.target.value })} required />
{visible.length === 0 ? ( ) : ( visible.map((section) => (
{t(section.labelKey)}
{section.permissions.map((permission) => (
{permissionDisplayName({ name: permission.name, description: permission.description }, t)} {permissionDisplayDescription({ name: permission.name, description: permission.description }, t) ?? permission.name}
onChange({ ...state, enabled: togglePermission(state.enabled, permission.name, checked) })} />
))}
)) )}
); }