@terpjs/react-core 0.17.0 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +8 -8
- package/src/Authorized.tsx +20 -5
- package/src/Field.tsx +11 -22
- package/src/TerpProvider.tsx +13 -2
- package/src/admin/AccessAdmin.tsx +118 -0
- package/src/admin/AdminHub.tsx +10 -0
- package/src/admin/GroupDetail.tsx +7 -0
- package/src/admin/ModuleAccessPanel.tsx +349 -0
- package/src/admin/UserCreate.tsx +16 -6
- package/src/admin/UserDetail.tsx +23 -16
- package/src/admin/UsersAdmin.tsx +9 -3
- package/src/admin/access.test.tsx +655 -0
- package/src/admin/accessModel.test.ts +210 -0
- package/src/admin/accessModel.ts +165 -0
- package/src/admin/accessTiles.tsx +65 -0
- package/src/admin/admin.test.tsx +95 -3
- package/src/admin/module.tsx +3 -0
- package/src/admin/roles.ts +29 -15
- package/src/admin/useAccessLadder.ts +102 -0
- package/src/bootstrap.tsx +10 -0
- package/src/capabilities.test.ts +20 -0
- package/src/capabilities.ts +13 -3
- package/src/index.ts +2 -0
- package/src/locale.tsx +29 -0
- package/src/markers.test.ts +7 -0
- package/src/styles.test.ts +12 -5
- package/src/styles.ts +116 -38
- package/src/ui/Checkbox.tsx +56 -22
- package/src/ui/Radio.tsx +25 -1
- package/src/ui/Switch.tsx +57 -23
- package/src/ui/TileGroup.test.tsx +185 -0
- package/src/ui/TileGroup.tsx +220 -0
- package/src/ui/controlMessages.test.tsx +136 -0
- package/src/ui/controlMessages.tsx +81 -0
- package/src/uiText.tsx +70 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@terpjs/react-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Terp React stack core — typed @terpjs/contract client provider, auth session, capability gates, TanStack Router adapter, app shell, page archetypes, DataView and token-styled UI primitives. First frontend stack; see README.md for the component catalog.",
|
|
6
6
|
"exports": {
|
|
@@ -13,8 +13,8 @@
|
|
|
13
13
|
"test": "vitest run"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@tanstack/react-router": "^1.170.
|
|
17
|
-
"@terpjs/contract": "^0.
|
|
16
|
+
"@tanstack/react-router": "^1.170.32",
|
|
17
|
+
"@terpjs/contract": "^0.19.0"
|
|
18
18
|
},
|
|
19
19
|
"peerDependencies": {
|
|
20
20
|
"react": "^19.0.0",
|
|
@@ -22,15 +22,15 @@
|
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@testing-library/jest-dom": "^6.9.1",
|
|
25
|
-
"@testing-library/react": "^16.3.
|
|
26
|
-
"@types/react": "^19.2.
|
|
27
|
-
"@types/react-dom": "^19.2.
|
|
28
|
-
"@vitejs/plugin-react": "^6.
|
|
25
|
+
"@testing-library/react": "^16.3.3",
|
|
26
|
+
"@types/react": "^19.2.18",
|
|
27
|
+
"@types/react-dom": "^19.2.7",
|
|
28
|
+
"@vitejs/plugin-react": "^6.1.1",
|
|
29
29
|
"jsdom": "^29.1.1",
|
|
30
30
|
"react": "^19.2.7",
|
|
31
31
|
"react-dom": "^19.2.7",
|
|
32
32
|
"typescript": "^5.9.3",
|
|
33
|
-
"vite": "^8.
|
|
33
|
+
"vite": "^8.2.2",
|
|
34
34
|
"vitest": "^4.1.9"
|
|
35
35
|
},
|
|
36
36
|
"license": "Apache-2.0",
|
package/src/Authorized.tsx
CHANGED
|
@@ -3,9 +3,16 @@ import type { ReactNode } from "react";
|
|
|
3
3
|
|
|
4
4
|
import { useAuth } from "./TerpProvider";
|
|
5
5
|
|
|
6
|
-
/**
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Whether the current user may perform `action` (the UI gate; the backend re-checks).
|
|
8
|
+
*
|
|
9
|
+
* Pass `module` on a module's own screen: a per-module rung the caller holds there raises the
|
|
10
|
+
* answer exactly as it does at the guard (ADR 0121). Without it this compares the global rank
|
|
11
|
+
* only, which is correct for a screen that belongs to no module and wrong for one that does —
|
|
12
|
+
* it hid a module the caller could reach and rendered none of its controls.
|
|
13
|
+
*/
|
|
14
|
+
export function useCan(action: Action, module?: string): boolean {
|
|
15
|
+
return useAuth().can(action, module);
|
|
9
16
|
}
|
|
10
17
|
|
|
11
18
|
/**
|
|
@@ -32,6 +39,8 @@ export function useHasPermission(permission: string): boolean {
|
|
|
32
39
|
|
|
33
40
|
export interface AuthorizedProps {
|
|
34
41
|
action: Action;
|
|
42
|
+
/** The module this action happens in, so a per-module rung can raise the answer. */
|
|
43
|
+
module?: string;
|
|
35
44
|
children: ReactNode;
|
|
36
45
|
/**
|
|
37
46
|
* Also require this named permission grant.
|
|
@@ -46,8 +55,14 @@ export interface AuthorizedProps {
|
|
|
46
55
|
}
|
|
47
56
|
|
|
48
57
|
/** Render `children` only when the current user may perform `action`, else `fallback`. */
|
|
49
|
-
export function Authorized({
|
|
50
|
-
|
|
58
|
+
export function Authorized({
|
|
59
|
+
action,
|
|
60
|
+
module,
|
|
61
|
+
permission,
|
|
62
|
+
children,
|
|
63
|
+
fallback = null,
|
|
64
|
+
}: AuthorizedProps) {
|
|
65
|
+
const allowedByRank = useCan(action, module);
|
|
51
66
|
const permissions = usePermissions();
|
|
52
67
|
const allowed = allowedByRank && (permission === undefined || permissions.includes(permission));
|
|
53
68
|
return <>{allowed ? children : fallback}</>;
|
package/src/Field.tsx
CHANGED
|
@@ -2,6 +2,7 @@ import { cloneElement, isValidElement, useId } from "react";
|
|
|
2
2
|
import type { ReactNode } from "react";
|
|
3
3
|
|
|
4
4
|
import { injectTerpStyles } from "./styles";
|
|
5
|
+
import { mergeDescribedBy, useControlMessages } from "./ui/controlMessages";
|
|
5
6
|
import { useUiText } from "./uiText";
|
|
6
7
|
import type { UiText } from "./uiText";
|
|
7
8
|
|
|
@@ -42,12 +43,11 @@ export interface FieldProps {
|
|
|
42
43
|
*/
|
|
43
44
|
export function Field({ label, children, error, hint }: FieldProps) {
|
|
44
45
|
const resolve = useUiText();
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
const labelId = `${baseId}-label`;
|
|
46
|
+
const labelId = `${useId()}-label`;
|
|
47
|
+
// Shared with Switch / Checkbox / RadioGroup, which cannot nest inside this component and
|
|
48
|
+
// so carry the same hint/error envelope themselves: one implementation of the ids, the
|
|
49
|
+
// description wiring and the alert, rather than four that drift apart.
|
|
50
|
+
const { hasError, describedBy, messages } = useControlMessages(hint, error);
|
|
51
51
|
|
|
52
52
|
// Only a single element child can be named and described — which is the documented contract
|
|
53
53
|
// ("the control"). Anything else is passed through untouched rather than guessed at.
|
|
@@ -70,12 +70,10 @@ export function Field({ label, children, error, hint }: FieldProps) {
|
|
|
70
70
|
children.props["aria-label"] === undefined
|
|
71
71
|
? (children.props["aria-labelledby"] ?? labelId)
|
|
72
72
|
: undefined,
|
|
73
|
-
"aria-describedby":
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
.join(" ")
|
|
78
|
-
: children.props["aria-describedby"],
|
|
73
|
+
"aria-describedby": mergeDescribedBy(
|
|
74
|
+
children.props["aria-describedby"],
|
|
75
|
+
describedBy,
|
|
76
|
+
),
|
|
79
77
|
"aria-invalid": children.props["aria-invalid"] ?? (hasError ? true : undefined),
|
|
80
78
|
})
|
|
81
79
|
: children;
|
|
@@ -88,16 +86,7 @@ export function Field({ label, children, error, hint }: FieldProps) {
|
|
|
88
86
|
</span>
|
|
89
87
|
{control}
|
|
90
88
|
</label>
|
|
91
|
-
{
|
|
92
|
-
<span id={hintId} data-terp="field-hint">
|
|
93
|
-
{resolve(hint)}
|
|
94
|
-
</span>
|
|
95
|
-
)}
|
|
96
|
-
{hasError && (
|
|
97
|
-
<span id={errorId} role="alert" data-terp="field-error">
|
|
98
|
-
{error}
|
|
99
|
-
</span>
|
|
100
|
-
)}
|
|
89
|
+
{messages}
|
|
101
90
|
</div>
|
|
102
91
|
);
|
|
103
92
|
}
|
package/src/TerpProvider.tsx
CHANGED
|
@@ -249,8 +249,19 @@ export function TerpProvider({
|
|
|
249
249
|
refresh,
|
|
250
250
|
currentUser: () => user,
|
|
251
251
|
loading: () => loading,
|
|
252
|
-
|
|
253
|
-
|
|
252
|
+
// `module` names the module the action happens in, so a per-module rung can raise the
|
|
253
|
+
// answer. Omitted, the gate is the global rank exactly as before — which is the honest
|
|
254
|
+
// default for a screen that is not a module's own, and keeps every existing caller
|
|
255
|
+
// correct rather than silently widened.
|
|
256
|
+
can: (action: Action, module?: string) =>
|
|
257
|
+
user
|
|
258
|
+
? canPerform(
|
|
259
|
+
user.role_rank,
|
|
260
|
+
action,
|
|
261
|
+
thresholds,
|
|
262
|
+
module === undefined ? undefined : user.module_ranks?.[module],
|
|
263
|
+
)
|
|
264
|
+
: false,
|
|
254
265
|
}),
|
|
255
266
|
[login, logout, refresh, user, loading, thresholds],
|
|
256
267
|
);
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
import { Alert } from "../ui/Alert";
|
|
4
|
+
import { Badge } from "../ui/Badge";
|
|
5
|
+
import { Card } from "../ui/Card";
|
|
6
|
+
import { ErrorState } from "../ErrorState";
|
|
7
|
+
import { LoadingState } from "../LoadingState";
|
|
8
|
+
import { Page } from "../Page";
|
|
9
|
+
import { Stack } from "../layout";
|
|
10
|
+
import { TileGroup } from "../ui/TileGroup";
|
|
11
|
+
import { useTerpClient } from "../TerpProvider";
|
|
12
|
+
import { useStrings } from "../uiText";
|
|
13
|
+
import type { TerpStrings } from "../uiText";
|
|
14
|
+
import { unwrap } from "../unwrap";
|
|
15
|
+
|
|
16
|
+
import { adminCrumb, renderAdminCrumb } from "./crumbs";
|
|
17
|
+
import { buildModuleRows } from "./accessModel";
|
|
18
|
+
import type { AccessModel, ModuleRow } from "./accessModel";
|
|
19
|
+
import { tilesFor } from "./accessTiles";
|
|
20
|
+
|
|
21
|
+
function ModuleCard({ row, strings }: { row: ModuleRow; strings: TerpStrings }) {
|
|
22
|
+
return (
|
|
23
|
+
<Card key={row.name}>
|
|
24
|
+
<Stack gap={2}>
|
|
25
|
+
<Stack direction="row" gap={2} align="center">
|
|
26
|
+
<strong>{row.label}</strong>
|
|
27
|
+
<code>{row.name}</code>
|
|
28
|
+
{row.platformReason !== null && (
|
|
29
|
+
<Badge tone="warning">{strings.accessNeverAssignable}</Badge>
|
|
30
|
+
)}
|
|
31
|
+
{row.platformReason === null && !row.assignable && (
|
|
32
|
+
<Badge tone="neutral">{strings.accessNotAssignable}</Badge>
|
|
33
|
+
)}
|
|
34
|
+
</Stack>
|
|
35
|
+
{row.platformReason !== null && (
|
|
36
|
+
// Rendered, not hovered. The reason is the useful part — a module simply absent from
|
|
37
|
+
// the pane leaves a question an administrator cannot stop asking — and an explanation
|
|
38
|
+
// you have to discover by hovering is not one, which is the same argument that puts
|
|
39
|
+
// the delta on the tile instead of in a tooltip.
|
|
40
|
+
<span data-terp="tile-note">{row.platformReason}</span>
|
|
41
|
+
)}
|
|
42
|
+
{row.unexplainedRoutes > 0 && (
|
|
43
|
+
<Alert tone="warning">
|
|
44
|
+
{strings.accessUnexplainedRoutes.replace("{count}", String(row.unexplainedRoutes))}
|
|
45
|
+
</Alert>
|
|
46
|
+
)}
|
|
47
|
+
<span data-terp="tile-note">{strings.accessAdds}</span>
|
|
48
|
+
{/* A description, not a disabled control: this screen explains the declared model, and
|
|
49
|
+
there is nothing here to choose. Choosing a rung for a person happens where the
|
|
50
|
+
person is, which is the only place the choice has a subject — and it writes through
|
|
51
|
+
the same rows this screen describes. */}
|
|
52
|
+
<TileGroup label={row.label} tiles={tilesFor(row, strings)} readOnly />
|
|
53
|
+
</Stack>
|
|
54
|
+
</Card>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* `/admin/access` — which roles exist, and what each one may do per module.
|
|
60
|
+
*
|
|
61
|
+
* The read-only half of the permission surface, and the first screen that answers the question
|
|
62
|
+
* the whole design is for: *these are the roles, and this is what each role gets in this
|
|
63
|
+
* module*. Every allowance shown is the kernel guard's own answer, projected per rung by
|
|
64
|
+
* `GET /api/v1/access/model` (ADR 0121 §4) — this screen groups and diffs those answers and
|
|
65
|
+
* forms no opinion about them, so it cannot disagree with the gate.
|
|
66
|
+
*
|
|
67
|
+
* It deliberately does not show *who holds what*. That question has a subject and belongs where
|
|
68
|
+
* the subject is; conflating the declared model with the assignments against it is how a pane
|
|
69
|
+
* ends up implying that changing a tile here changes someone's access.
|
|
70
|
+
*
|
|
71
|
+
* There is no empty state, deliberately: `create_app` mounts the base-profile capabilities in
|
|
72
|
+
* every app, so a composed application always has modules to describe. A branch no test can
|
|
73
|
+
* reach is a claim nobody checks, and the honest alternative to guarding it is not having it.
|
|
74
|
+
*/
|
|
75
|
+
export function AccessAdmin() {
|
|
76
|
+
const client = useTerpClient();
|
|
77
|
+
const strings = useStrings();
|
|
78
|
+
const [model, setModel] = useState<AccessModel | null>(null);
|
|
79
|
+
const [error, setError] = useState<string | null>(null);
|
|
80
|
+
const [loading, setLoading] = useState(true);
|
|
81
|
+
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
let live = true;
|
|
84
|
+
void (async () => {
|
|
85
|
+
try {
|
|
86
|
+
const loaded = unwrap(await client.GET("/api/v1/access/model", {}));
|
|
87
|
+
if (live) setModel(loaded);
|
|
88
|
+
} catch (cause: unknown) {
|
|
89
|
+
if (live) setError(cause instanceof Error ? cause.message : String(cause));
|
|
90
|
+
} finally {
|
|
91
|
+
if (live) setLoading(false);
|
|
92
|
+
}
|
|
93
|
+
})();
|
|
94
|
+
return () => {
|
|
95
|
+
live = false;
|
|
96
|
+
};
|
|
97
|
+
}, [client]);
|
|
98
|
+
|
|
99
|
+
const rows = model === null ? [] : buildModuleRows(model);
|
|
100
|
+
|
|
101
|
+
return (
|
|
102
|
+
<Page
|
|
103
|
+
title={strings.adminAccess}
|
|
104
|
+
description={strings.adminAccessDescription}
|
|
105
|
+
breadcrumbs={[{ ...adminCrumb(strings), to: "/admin" }]}
|
|
106
|
+
renderLink={renderAdminCrumb}
|
|
107
|
+
>
|
|
108
|
+
<Stack gap={3}>
|
|
109
|
+
<Alert tone="info">{strings.accessDeclaredOnly}</Alert>
|
|
110
|
+
{loading && <LoadingState />}
|
|
111
|
+
{!loading && error !== null && <ErrorState description={error} />}
|
|
112
|
+
{rows.map((row) => (
|
|
113
|
+
<ModuleCard key={row.name} row={row} strings={strings} />
|
|
114
|
+
))}
|
|
115
|
+
</Stack>
|
|
116
|
+
</Page>
|
|
117
|
+
);
|
|
118
|
+
}
|
package/src/admin/AdminHub.tsx
CHANGED
|
@@ -70,6 +70,7 @@ export function AdminHub({ sections }: { sections?: AdminAreaSections } = {}) {
|
|
|
70
70
|
users: sections?.users !== false,
|
|
71
71
|
groups: sections?.groups !== false,
|
|
72
72
|
audit: sections?.audit !== false,
|
|
73
|
+
access: sections?.access !== false,
|
|
73
74
|
};
|
|
74
75
|
const stats = useHubStats(selected);
|
|
75
76
|
return (
|
|
@@ -103,6 +104,15 @@ export function AdminHub({ sections }: { sections?: AdminAreaSections } = {}) {
|
|
|
103
104
|
renderLink={renderLink}
|
|
104
105
|
/>
|
|
105
106
|
)}
|
|
107
|
+
{selected.access && (
|
|
108
|
+
<HubCard
|
|
109
|
+
to="/admin/access"
|
|
110
|
+
title={strings.adminAccess}
|
|
111
|
+
description={strings.adminAccessDescription}
|
|
112
|
+
icon={<NavIcon name="shield" label={strings.adminAccess} />}
|
|
113
|
+
renderLink={renderLink}
|
|
114
|
+
/>
|
|
115
|
+
)}
|
|
106
116
|
</HubPage>
|
|
107
117
|
);
|
|
108
118
|
}
|
|
@@ -21,6 +21,7 @@ import { useFormatDate, useFormatDateTime } from "../format";
|
|
|
21
21
|
import { useStrings } from "../uiText";
|
|
22
22
|
import { ApiError, unwrap } from "../unwrap";
|
|
23
23
|
|
|
24
|
+
import { ModuleAccessPanel } from "./ModuleAccessPanel";
|
|
24
25
|
import { adminCrumb, renderAdminCrumb } from "./crumbs";
|
|
25
26
|
|
|
26
27
|
type GroupRead = components["schemas"]["GroupRead"];
|
|
@@ -373,6 +374,12 @@ export function GroupDetail() {
|
|
|
373
374
|
]}
|
|
374
375
|
/>
|
|
375
376
|
)}
|
|
377
|
+
{record !== null && (
|
|
378
|
+
// No `globalRank`: a group has no role of its own (ADR 0074 gives groups permissions,
|
|
379
|
+
// not roles), so there is no floor under its strips. A rung assigned here reaches the
|
|
380
|
+
// members through the same expansion that already carries the group's grants.
|
|
381
|
+
<ModuleAccessPanel subjectId={record.id} />
|
|
382
|
+
)}
|
|
376
383
|
<Stack gap={3}>
|
|
377
384
|
<h2 data-terp="admin-section-title">
|
|
378
385
|
{strings.members}
|