// 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 { Link } from "react-router"; import { useTranslation } from "react-i18next"; import * as SubframeCore from "@subframe/core"; import { FeatherCopy, FeatherEdit2, FeatherMoreHorizontal, FeatherPlus, FeatherTrash } 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 { useLocaleFormatters } from "~/locale/format"; import { Alert } from "~/ui/components/Alert"; import { Button } from "~/ui/components/Button"; import { Dialog } from "~/ui/components/Dialog"; 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 { Select } from "~/ui/components/Select"; import { Switch } from "~/ui/components/Switch"; import { Table } from "~/ui/components/Table"; import { Tabs } from "~/ui/components/Tabs"; import { TextArea } from "~/ui/components/TextArea"; import { TextInput } from "~/ui/components/TextInput"; const FILTER_ALL = "__all__"; 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). */ membersTabHref: string; 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, membersTabHref, 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); 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 (
{/* Header: title + subtitle (0px rhythm) + create CTA (M04A) */}
{t("settings.members.tabs.rolesAndPermissions")} {t("settings.rolesAndPermissions.subtitle")}
{data.canCreate ? ( ) : null}
{/* Tabs: Mitarbeiter | Rollen und Berechtigungen (shared with S01) */} {t("settings.members.tabs.members")} {t("settings.members.tabs.rolesAndPermissions")} {/* Toolbar: search (name/description) + type/in-use filters */}
setFilter((prev) => ({ ...prev, query: event.target.value }))} />
{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 ? ( } 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) */} (open ? undefined : setBasics(null))}> {basics ? setBasics(null)} /> :
}
{/* M02 — large permission editor (L 1200) */} (open ? undefined : setEditor(null))}> {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); }} /> ) : (
)}
{/* Delete confirm / D03 blocker (XS 420) */} (open ? undefined : setDeleteRow(null))}> {deleteRow ? (
{deleteRow.deleteBlockedInUse ? t("settings.rolesAndPermissions.deleteBlocked.title") : t("settings.rolesAndPermissions.delete.title")} {deleteRow.deleteBlockedInUse ? t("settings.rolesAndPermissions.deleteBlocked.description", { name: deleteRow.name, count: deleteRow.memberCount }) : t("settings.rolesAndPermissions.delete.description", { name: deleteRow.name })}
{!deleteRow.deleteBlockedInUse ? ( ) : 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 (
{isDuplicate ? t("settings.rolesAndPermissions.duplicate.title") : t("settings.rolesAndPermissions.createDialog.title")} {isDuplicate ? t("settings.rolesAndPermissions.duplicate.hint", { name: state.source?.name ?? "" }) : t("settings.rolesAndPermissions.createDialog.description")}
onChange({ ...state, name: event.target.value })} required />
); } 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 (
{state.roleId ? t("settings.rolesAndPermissions.editor.title") : t("settings.rolesAndPermissions.createDialog.title")} {state.duplicatedFromName ? ( {t("settings.rolesAndPermissions.duplicate.hint", { name: state.duplicatedFromName })} ) : null} {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) })} />
))}
)) )}
); }