@alepha/ui 0.25.0 → 0.25.2

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.
Files changed (104) hide show
  1. package/package.json +4 -5
  2. package/src/components/account/__tests__/account-router.spec.ts +201 -0
  3. package/src/components/account/account-connections.tsx +103 -0
  4. package/src/components/account/account-delete-dialog.tsx +184 -0
  5. package/src/components/account/account-header.tsx +57 -0
  6. package/src/components/account/account-keys.tsx +221 -0
  7. package/src/components/account/account-layout.tsx +63 -0
  8. package/src/components/account/account-password-dialog.tsx +152 -0
  9. package/src/components/account/account-profile.tsx +229 -0
  10. package/src/components/account/account-router-options.tsx +74 -0
  11. package/src/components/account/account-router-page.tsx +69 -0
  12. package/src/components/account/account-router.tsx +214 -0
  13. package/src/components/account/account-security.tsx +150 -0
  14. package/src/components/account/account-sessions.tsx +140 -0
  15. package/src/components/admin/__tests__/admin-keys-token-dialog.browser.spec.tsx +62 -0
  16. package/src/components/admin/__tests__/admin-layout.browser.spec.tsx +82 -0
  17. package/src/components/admin/__tests__/admin-router-page.spec.ts +54 -0
  18. package/src/components/admin/__tests__/admin-router.spec.ts +208 -0
  19. package/src/components/admin/__tests__/admin-user-cell.browser.spec.tsx +99 -0
  20. package/src/components/admin/__tests__/admin-user-detail.spec.ts +29 -0
  21. package/src/components/admin/admin-analytics-query-panel.tsx +149 -0
  22. package/src/components/admin/admin-analytics-results.tsx +95 -0
  23. package/src/components/admin/admin-analytics.tsx +104 -0
  24. package/src/components/admin/admin-audits.tsx +12 -15
  25. package/src/components/admin/admin-files.tsx +10 -18
  26. package/src/components/admin/admin-jobs-executions-panel.tsx +2 -1
  27. package/src/components/admin/admin-jobs.tsx +3 -1
  28. package/src/components/admin/admin-keys-token-dialog.tsx +123 -0
  29. package/src/components/admin/admin-keys.tsx +54 -3
  30. package/src/components/admin/admin-layout.tsx +107 -0
  31. package/src/components/admin/admin-payments.tsx +4 -5
  32. package/src/components/admin/admin-router-options.tsx +130 -0
  33. package/src/components/admin/admin-router-page.tsx +74 -0
  34. package/src/components/admin/admin-router.tsx +379 -0
  35. package/src/components/admin/admin-sessions.tsx +2 -16
  36. package/src/components/admin/admin-user-cell.tsx +70 -0
  37. package/src/components/admin/admin-user-detail-overview-tab.tsx +28 -0
  38. package/src/components/admin/admin-user-detail-profile-policy.spec.ts +146 -0
  39. package/src/components/admin/admin-user-detail-profile-policy.ts +147 -0
  40. package/src/components/admin/admin-user-detail-security-tab.tsx +1 -1
  41. package/src/components/admin/admin-user-detail.tsx +76 -32
  42. package/src/components/admin/admin-users-status-filter.tsx +2 -0
  43. package/src/components/admin/admin-workflows-execution-sheet.tsx +277 -0
  44. package/src/components/admin/admin-workflows-executions-panel.tsx +170 -0
  45. package/src/components/admin/admin-workflows-status-badge.tsx +40 -0
  46. package/src/components/admin/admin-workflows.tsx +207 -0
  47. package/src/components/alepha-table/alepha-table.tsx +73 -17
  48. package/src/components/app-actions/__tests__/app-actions.browser.spec.tsx +79 -0
  49. package/src/components/app-actions/app-actions.tsx +113 -0
  50. package/src/components/app-shell/app-shell.tsx +122 -0
  51. package/src/components/auth/auth-reset-password.tsx +81 -8
  52. package/src/components/button-user/button-user.tsx +58 -1
  53. package/src/components/control/control.tsx +14 -71
  54. package/src/components/control-password/control-password.tsx +87 -0
  55. package/src/components/control-select/__tests__/control-select-clear-label.browser.spec.tsx +101 -0
  56. package/src/components/control-select/__tests__/control-select-deselect.browser.spec.tsx +126 -0
  57. package/src/components/control-select/__tests__/control-select-searchable.browser.spec.tsx +142 -0
  58. package/src/components/control-select/control-select.tsx +133 -111
  59. package/src/components/control-upload/control-upload.tsx +27 -1
  60. package/src/components/markdown-view/markdown-view.tsx +6 -0
  61. package/src/components/nav-shell/nav-page.tsx +3 -3
  62. package/src/components/settings/__tests__/settings-nav.browser.spec.tsx +123 -0
  63. package/src/components/settings/__tests__/settings-section.browser.spec.tsx +130 -0
  64. package/src/components/settings/settings-danger-section.tsx +59 -0
  65. package/src/components/settings/settings-layout.tsx +56 -0
  66. package/src/components/settings/settings-nav.tsx +131 -0
  67. package/src/components/settings/settings-row.tsx +81 -0
  68. package/src/components/settings/settings-section.tsx +64 -0
  69. package/src/components/ui/alert-dialog.tsx +1 -1
  70. package/src/components/ui/avatar.tsx +2 -0
  71. package/src/components/ui/breadcrumb.tsx +1 -1
  72. package/src/components/ui/calendar.tsx +11 -6
  73. package/src/components/ui/card.tsx +5 -5
  74. package/src/components/ui/carousel.tsx +4 -4
  75. package/src/components/ui/chart.tsx +2 -0
  76. package/src/components/ui/collapsible.tsx +2 -0
  77. package/src/components/ui/combobox.tsx +3 -1
  78. package/src/components/ui/context-menu.tsx +3 -3
  79. package/src/components/ui/dialog.tsx +6 -1
  80. package/src/components/ui/direction.tsx +2 -0
  81. package/src/components/ui/drawer.tsx +134 -37
  82. package/src/components/ui/dropdown-menu.tsx +13 -3
  83. package/src/components/ui/empty.tsx +4 -1
  84. package/src/components/ui/input-otp.tsx +2 -0
  85. package/src/components/ui/menubar.tsx +2 -2
  86. package/src/components/ui/pagination.tsx +2 -2
  87. package/src/components/ui/popover.tsx +2 -0
  88. package/src/components/ui/radio-group.tsx +2 -0
  89. package/src/components/ui/scroll-area.tsx +2 -0
  90. package/src/components/ui/select.tsx +1 -1
  91. package/src/components/ui/separator.tsx +2 -0
  92. package/src/components/ui/sheet.tsx +4 -1
  93. package/src/components/ui/sidebar.tsx +1 -1
  94. package/src/components/ui/spinner.tsx +1 -0
  95. package/src/components/ui/switch.tsx +2 -0
  96. package/src/components/ui/tabs.tsx +2 -0
  97. package/src/components/ui/toggle-group.tsx +2 -0
  98. package/src/components/ui/tooltip.tsx +2 -0
  99. package/src/lib/__tests__/rehype-safe-img.spec.ts +121 -0
  100. package/src/lib/i18n-fr.ts +68 -0
  101. package/src/lib/rehype-safe-img.ts +150 -0
  102. package/src/lib/resize-image.ts +135 -0
  103. package/src/styles.css +126 -1
  104. /package/src/components/{admin/admin-user-detail-provider-labels.ts → auth/provider-labels.ts} +0 -0
@@ -0,0 +1,379 @@
1
+ import { $pageNav } from "@alepha/ui/components/nav-shell/nav-page";
2
+ import { $store, z } from "alepha";
3
+ import type { AdminAnalyticsController } from "alepha/api/analytics";
4
+ import type { AdminAuditController } from "alepha/api/audits";
5
+ import type { FileController } from "alepha/api/files";
6
+ import type { AdminJobController } from "alepha/api/jobs";
7
+ import type { AdminApiKeyController } from "alepha/api/keys";
8
+ import type { AdminNotificationController } from "alepha/api/notifications";
9
+ import type { AdminParameterController } from "alepha/api/parameters";
10
+ import type { AdminPaymentController } from "alepha/api/payments";
11
+ import type {
12
+ AdminSessionController,
13
+ AdminUserController,
14
+ } from "alepha/api/users";
15
+ import type { AdminWorkflowController } from "alepha/api/workflows";
16
+ import { $page, Redirection } from "alepha/react/router";
17
+ import { $secure } from "alepha/security";
18
+ import { $client } from "alepha/server/links";
19
+ import {
20
+ Bell,
21
+ ChartLine,
22
+ CreditCard,
23
+ Files,
24
+ KeyRound,
25
+ ShieldAlert,
26
+ ShieldCheck,
27
+ SlidersHorizontal,
28
+ Timer,
29
+ UsersIcon,
30
+ Workflow,
31
+ } from "lucide-react";
32
+ import { createElement } from "react";
33
+ import { adminRouterOptionsAtom } from "./admin-router-options.tsx";
34
+
35
+ /**
36
+ * The whole `/admin` surface — twelve pages and their shell — mounted and wired.
37
+ *
38
+ * ⚠️ **Nav icons are `createElement(Icon)`, never `<Icon />`. Do not "fix"
39
+ * them back to JSX.** This module is evaluated *eagerly* in the server graph:
40
+ * `main.server.ts` → `$module` → `alepha.inject(AdminRouter)` runs these class
41
+ * fields at import time, unlike every page below, which is behind `lazy()`.
42
+ * `alepha db migrations create` imports that graph in a child process under
43
+ * `tsx`, and `tsx` applies a single tsconfig resolved from the **cwd** — the
44
+ * app root. A file outside that tsconfig's `include` (which this one is, being
45
+ * in a sibling workspace) gets esbuild's defaults instead, and the default JSX
46
+ * transform is the *classic* one. The emitted `React.createElement` then fails
47
+ * with `ReferenceError: React is not defined`, breaking migration generation
48
+ * for the whole app — with a stack that points here and explains none of this.
49
+ *
50
+ * ```ts
51
+ * import { AdminRouter } from "@alepha/ui/components/admin/admin-router";
52
+ *
53
+ * export const MyWeb = $module({
54
+ * name: "my.web",
55
+ * services: [MyRouter, AuthRouter, AdminRouter],
56
+ * });
57
+ * ```
58
+ *
59
+ * ### Every page is mounted; none is conditionally registered
60
+ *
61
+ * A page whose permission the signed-in admin does not hold hides itself, and
62
+ * so does a page whose module is not registered — but through two separate
63
+ * gates, not one. Each `$pageNav` here declares `permission`, and its `can`
64
+ * calls a typed `$client` action, and the entry is hidden when either fails.
65
+ *
66
+ * `permission` alone cannot cover "the module is not registered": `$pageNav`
67
+ * wires it into `use: [$secure({ permissions })]` on the page itself, and
68
+ * `$secure` registers that permission into `SecurityProvider` eagerly, at
69
+ * definition time — so the permission exists whether or not any controller
70
+ * backing the page does, and an admin holding the `*` wildcard is granted it
71
+ * regardless. `can` closes that gap with an action instead of a permission:
72
+ * `this.userApi.findUsers.can()` is `LinkProvider.can("findUsers")` against
73
+ * `/api/_links`, a registry built from the actions the server actually
74
+ * registered, so an action nothing declared never appears there for anyone
75
+ * to be granted. The action is read off a `$client<AdminUserController>()`
76
+ * field, so a rename on the controller is a compile error here rather than a
77
+ * silently dead sidebar entry — the failure mode a plain string name would
78
+ * have.
79
+ *
80
+ * This is why there is no `pages: [...]` allowlist. A second gate on top of
81
+ * two that already work goes stale: an application that later turns on
82
+ * `features.audits` would still not see the Audits page until someone
83
+ * remembered to edit the list.
84
+ *
85
+ * ### Extending the shell
86
+ *
87
+ * {@link $pageAdmin} (`@alepha/ui/components/admin/admin-router-page`) is the
88
+ * way to add a page to this shell — the one-call form an application, or a
89
+ * satellite package such as `@alepha/commerce/admin`, whose pages
90
+ * deliberately live outside this design system so it never depends on a
91
+ * domain, reaches for:
92
+ *
93
+ * ```tsx
94
+ * class ShopAdminRouter {
95
+ * products = $pageAdmin({
96
+ * path: "/products",
97
+ * nav: { label: "Catalogue", group: "Commerce", order: 100 },
98
+ * lazy: () => import("./AdminProducts.tsx"),
99
+ * });
100
+ * }
101
+ * ```
102
+ *
103
+ * See `$pageAdmin`'s own JSDoc for the rules this composition must follow —
104
+ * the `AdminRouter` registration it triggers, the `order` / `nav.group`
105
+ * contract, and gating with `can` rather than `permission` alone.
106
+ *
107
+ * `layout` stays public as the seam underneath: `$pageAdmin` is exactly
108
+ * `$inject(AdminRouter)` plus `parent: this.admin.layout`, done for you. An
109
+ * application that wants that composition without going through `$pageAdmin`
110
+ * — for instance to control itself whether and when `AdminRouter` gets
111
+ * registered, rather than as an unconditional side effect of a field
112
+ * initializer — injects `AdminRouter` and sets `parent: this.admin.layout`
113
+ * directly, the same way `$pageAdmin` does internally.
114
+ *
115
+ * ### Group order is a contract
116
+ *
117
+ * The built-in pages occupy `Identity` (orders 1-3) and `Operations`
118
+ * (orders 4-11). An application's page should either join one of those with an
119
+ * `order` of 100 or more, or declare a group of its own. `useNavEntries` sorts
120
+ * groups by their smallest member, so a custom page at `order: 2` would
121
+ * silently reshuffle the built-in sidebar.
122
+ *
123
+ * ### These twelve route names are claimed globally
124
+ *
125
+ * `users`, `userDetail`, `sessions`, `keys`, `jobs`, `notifications`,
126
+ * `audits`, `files`, `parameters`, `payments`, `analytics` and `workflows`
127
+ * each carry an
128
+ * explicit `name:` so a future rename of the field itself (done for
129
+ * readability, without touching the string) never silently changes the public
130
+ * route name — the same reason `AuthRouter`'s pages all carry one too.
131
+ *
132
+ * Route names live in one process-wide namespace, and a duplicate does not
133
+ * throw: `ReactPageProvider.page()` returns the first match. An adopter that
134
+ * registers its own page named `files` (or any of the other ten) either
135
+ * shadows this one or is shadowed by it, silently, depending on mount order.
136
+ * Treat these eleven names as reserved when hanging pages off `layout`.
137
+ */
138
+ export class AdminRouter {
139
+ protected readonly options = $store(adminRouterOptionsAtom);
140
+
141
+ protected readonly userApi = $client<AdminUserController>();
142
+ protected readonly sessionApi = $client<AdminSessionController>();
143
+ protected readonly apiKeyApi = $client<AdminApiKeyController>();
144
+ protected readonly jobApi = $client<AdminJobController>();
145
+ protected readonly notificationApi = $client<AdminNotificationController>();
146
+ protected readonly auditApi = $client<AdminAuditController>();
147
+ protected readonly fileApi = $client<FileController>();
148
+ protected readonly parameterApi = $client<AdminParameterController>();
149
+ protected readonly paymentApi = $client<AdminPaymentController>();
150
+ protected readonly analyticsApi = $client<AdminAnalyticsController>();
151
+ protected readonly workflowApi = $client<AdminWorkflowController>();
152
+
153
+ /**
154
+ * Anchors the shell and the first breadcrumb. Not itself a nav entry — a
155
+ * shell root is excluded from its own sidebar.
156
+ *
157
+ * Named `admin` because `NavShell root="admin"` and `Spotlight root="admin"`
158
+ * both resolve the subtree by this name.
159
+ */
160
+ layout = $page({
161
+ name: "admin",
162
+ path: "/admin",
163
+ use: [$secure({ permissions: ["admin:ui"] })],
164
+ nav: { label: "Admin" },
165
+ loader: async ({ url }) => {
166
+ if (url.pathname === "/admin" || url.pathname === "/admin/") {
167
+ throw new Redirection(this.options.indexPath ?? "/admin/users");
168
+ }
169
+ return {};
170
+ },
171
+ lazy: () => import("./admin-layout.tsx"),
172
+ });
173
+
174
+ users = $pageNav({
175
+ parent: this.layout,
176
+ path: "/users",
177
+ name: "users",
178
+ head: { title: "Users" },
179
+ permission: "admin:user:read",
180
+ can: () => this.userApi.findUsers.can(),
181
+ nav: {
182
+ label: "Users",
183
+ icon: createElement(UsersIcon),
184
+ group: "Identity",
185
+ order: 1,
186
+ },
187
+ lazy: () => import("./admin-users.tsx"),
188
+ props: () => this.options.pages?.users ?? {},
189
+ });
190
+
191
+ /**
192
+ * No `nav` — a secured route that is not a sidebar entry. The breadcrumb
193
+ * label falls back to `head.title`.
194
+ */
195
+ userDetail = $pageNav({
196
+ parent: this.layout,
197
+ path: "/users/:userId",
198
+ name: "userDetail",
199
+ head: { title: "User" },
200
+ permission: "admin:user:read",
201
+ can: () => this.userApi.getUser.can(),
202
+ schema: {
203
+ params: z.object({
204
+ userId: z.uuid(),
205
+ }),
206
+ },
207
+ lazy: () => import("./admin-user-detail.tsx"),
208
+ props: () => this.options.pages?.userDetail ?? {},
209
+ });
210
+
211
+ sessions = $pageNav({
212
+ parent: this.layout,
213
+ path: "/sessions",
214
+ name: "sessions",
215
+ head: { title: "Sessions" },
216
+ permission: "admin:session:read",
217
+ can: () => this.sessionApi.findSessions.can(),
218
+ nav: {
219
+ label: "Sessions",
220
+ icon: createElement(ShieldCheck),
221
+ group: "Identity",
222
+ order: 2,
223
+ },
224
+ lazy: () => import("./admin-sessions.tsx"),
225
+ });
226
+
227
+ keys = $pageNav({
228
+ parent: this.layout,
229
+ path: "/keys",
230
+ name: "keys",
231
+ head: { title: "API keys" },
232
+ permission: "admin:api-key:read",
233
+ can: () => this.apiKeyApi.findApiKeys.can(),
234
+ nav: {
235
+ label: "API keys",
236
+ icon: createElement(KeyRound),
237
+ group: "Identity",
238
+ order: 3,
239
+ keywords: ["tokens", "credentials"],
240
+ },
241
+ lazy: () => import("./admin-keys.tsx"),
242
+ });
243
+
244
+ jobs = $pageNav({
245
+ parent: this.layout,
246
+ path: "/jobs",
247
+ name: "jobs",
248
+ head: { title: "Jobs" },
249
+ permission: "admin:job:read",
250
+ can: () => this.jobApi.listJobs.can(),
251
+ nav: {
252
+ label: "Jobs",
253
+ icon: createElement(Timer),
254
+ group: "Operations",
255
+ order: 4,
256
+ },
257
+ lazy: () => import("./admin-jobs.tsx"),
258
+ });
259
+
260
+ notifications = $pageNav({
261
+ parent: this.layout,
262
+ path: "/notifications",
263
+ name: "notifications",
264
+ head: { title: "Notifications" },
265
+ permission: "admin:notification:read",
266
+ can: () => this.notificationApi.findNotifications.can(),
267
+ nav: {
268
+ label: "Notifications",
269
+ icon: createElement(Bell),
270
+ group: "Operations",
271
+ order: 5,
272
+ },
273
+ lazy: () => import("./admin-notifications.tsx"),
274
+ });
275
+
276
+ audits = $pageNav({
277
+ parent: this.layout,
278
+ path: "/audits",
279
+ name: "audits",
280
+ head: { title: "Audit log" },
281
+ permission: "admin:audit:read",
282
+ can: () => this.auditApi.findAudits.can(),
283
+ nav: {
284
+ label: "Audit log",
285
+ icon: createElement(ShieldAlert),
286
+ group: "Operations",
287
+ order: 6,
288
+ },
289
+ lazy: () => import("./admin-audits.tsx"),
290
+ });
291
+
292
+ files = $pageNav({
293
+ parent: this.layout,
294
+ path: "/files",
295
+ name: "files",
296
+ head: { title: "Files" },
297
+ permission: "admin:file:read",
298
+ can: () => this.fileApi.findFiles.can(),
299
+ nav: {
300
+ label: "Files",
301
+ icon: createElement(Files),
302
+ group: "Operations",
303
+ order: 7,
304
+ },
305
+ lazy: () => import("./admin-files.tsx"),
306
+ });
307
+
308
+ parameters = $pageNav({
309
+ parent: this.layout,
310
+ path: "/parameters",
311
+ name: "parameters",
312
+ head: { title: "Parameters" },
313
+ permission: "admin:parameter:read",
314
+ can: () => this.parameterApi.getParameterTree.can(),
315
+ nav: {
316
+ label: "Parameters",
317
+ icon: createElement(SlidersHorizontal),
318
+ group: "Operations",
319
+ order: 8,
320
+ keywords: ["settings", "config", "configuration"],
321
+ },
322
+ lazy: () => import("./admin-parameters.tsx"),
323
+ props: () => this.options.pages?.parameters ?? {},
324
+ });
325
+
326
+ /**
327
+ * Both permissions are required, matching `AdminPaymentController`, which
328
+ * gates every read with `["admin:payment:read", "payments:read"]` — and
329
+ * `$pageNav` treats an array as AND, the same way `$secure` does.
330
+ */
331
+ payments = $pageNav({
332
+ parent: this.layout,
333
+ path: "/payments",
334
+ name: "payments",
335
+ head: { title: "Payments" },
336
+ permission: ["admin:payment:read", "payments:read"],
337
+ can: () => this.paymentApi.listIntents.can(),
338
+ nav: {
339
+ label: "Payments",
340
+ icon: createElement(CreditCard),
341
+ group: "Operations",
342
+ order: 9,
343
+ },
344
+ lazy: () => import("./admin-payments.tsx"),
345
+ });
346
+
347
+ analytics = $pageNav({
348
+ parent: this.layout,
349
+ path: "/analytics",
350
+ name: "analytics",
351
+ head: { title: "Analytics" },
352
+ permission: "admin:analytics:read",
353
+ can: () => this.analyticsApi.listDatasets.can(),
354
+ nav: {
355
+ label: "Analytics",
356
+ icon: createElement(ChartLine),
357
+ group: "Operations",
358
+ order: 10,
359
+ },
360
+ lazy: () => import("./admin-analytics.tsx"),
361
+ });
362
+
363
+ workflows = $pageNav({
364
+ parent: this.layout,
365
+ path: "/workflows",
366
+ name: "workflows",
367
+ head: { title: "Workflows" },
368
+ permission: "admin:workflow:read",
369
+ can: () => this.workflowApi.getWorkflowRegistry.can(),
370
+ nav: {
371
+ label: "Workflows",
372
+ icon: createElement(Workflow),
373
+ group: "Operations",
374
+ order: 11,
375
+ keywords: ["saga", "steps", "executions"],
376
+ },
377
+ lazy: () => import("./admin-workflows.tsx"),
378
+ });
379
+ }
@@ -3,6 +3,7 @@ import * as React from "react";
3
3
  void React;
4
4
 
5
5
  import { AdminPage } from "@alepha/ui/components/admin/admin-page";
6
+ import { AdminUserCell } from "@alepha/ui/components/admin/admin-user-cell";
6
7
  import { useConfirmedAction } from "@alepha/ui/components/admin/use-confirmed-action";
7
8
  import { AlephaTable } from "@alepha/ui/components/alepha-table/alepha-table";
8
9
  import { useDialog } from "@alepha/ui/components/use-dialog/use-dialog";
@@ -96,22 +97,7 @@ export function AdminSessions() {
96
97
  columns={{
97
98
  user: {
98
99
  label: tr("admin.sessions.colUser", { default: "User" }),
99
- cell: (s) => {
100
- const label =
101
- s.user?.email ||
102
- s.user?.username ||
103
- [s.user?.firstName, s.user?.lastName]
104
- .filter(Boolean)
105
- .join(" ") ||
106
- null;
107
- return label ? (
108
- <span className="font-medium">{label}</span>
109
- ) : (
110
- <span className="text-muted-foreground font-mono text-xs">
111
- {s.userId?.slice(0, 8) ?? "—"}
112
- </span>
113
- );
114
- },
100
+ cell: (s) => <AdminUserCell userId={s.userId} user={s.user} />,
115
101
  },
116
102
  ip: {
117
103
  label: tr("admin.sessions.colIp", { default: "IP" }),
@@ -0,0 +1,70 @@
1
+ import * as React from "react";
2
+
3
+ void React;
4
+
5
+ import { Link } from "alepha/react/router";
6
+
7
+ export interface AdminUserCellProps {
8
+ /**
9
+ * The user's id — the link target. Without it the cell renders unlinked:
10
+ * there is no detail page to point at.
11
+ */
12
+ userId?: string | null;
13
+ /**
14
+ * Slim user summary as embedded by admin listings (sessions, API keys,
15
+ * files, payments). When absent the cell falls back to `fallbackLabel`,
16
+ * then the truncated id — a deleted account is real data, and showing
17
+ * nothing would read as a rendering bug.
18
+ */
19
+ user?: {
20
+ email?: string;
21
+ username?: string;
22
+ firstName?: string;
23
+ lastName?: string;
24
+ };
25
+ /**
26
+ * Last-resort display label before the truncated id — for rows that carry
27
+ * a denormalized snapshot instead of (or besides) a live summary, like the
28
+ * audit log's `userEmail` or a file's `creatorName`.
29
+ */
30
+ fallbackLabel?: string;
31
+ }
32
+
33
+ /**
34
+ * The one way an admin table cell names a user: best available identifier
35
+ * (email, then username, then real name, then the caller's snapshot label),
36
+ * linked to the user detail page whenever an id is present.
37
+ *
38
+ * A real `<a href>` rather than a click handler, so the cell keeps
39
+ * cmd-click, middle-click and "copy link address" — same reasoning as the
40
+ * owner column on Lore's admin projects page.
41
+ */
42
+ export const AdminUserCell = (props: AdminUserCellProps) => {
43
+ const label =
44
+ props.user?.email ||
45
+ props.user?.username ||
46
+ [props.user?.firstName, props.user?.lastName].filter(Boolean).join(" ") ||
47
+ props.fallbackLabel ||
48
+ null;
49
+
50
+ if (!props.userId) {
51
+ return label ? (
52
+ <span className="truncate text-sm">{label}</span>
53
+ ) : (
54
+ <span className="text-muted-foreground text-xs">—</span>
55
+ );
56
+ }
57
+
58
+ return (
59
+ <Link
60
+ href={`/admin/users/${props.userId}`}
61
+ className="hover:text-foreground underline-offset-4 hover:underline"
62
+ >
63
+ {label ?? (
64
+ <code className="text-muted-foreground font-mono text-xs">
65
+ {props.userId.slice(0, 8)}
66
+ </code>
67
+ )}
68
+ </Link>
69
+ );
70
+ };
@@ -6,6 +6,7 @@ import { AutoForm } from "@alepha/ui/components/auto-form/auto-form";
6
6
  import type { FormModel } from "alepha/react/form";
7
7
  import { useI18n } from "alepha/react/i18n";
8
8
  import { ShieldCheck } from "lucide-react";
9
+ import type { ProfilePolicy } from "./admin-user-detail-profile-policy.ts";
9
10
  import type { profileSchema } from "./admin-user-detail-profile-schema.ts";
10
11
 
11
12
  export interface AdminUserDetailOverviewTabProps {
@@ -15,6 +16,12 @@ export interface AdminUserDetailOverviewTabProps {
15
16
  * but not togglable.
16
17
  */
17
18
  availableRoles: ReadonlyArray<{ name: string; default?: boolean }>;
19
+ /**
20
+ * Which identity fields the realm collects. A field the realm has turned
21
+ * off is not rendered at all — an Email input on a username-only realm is
22
+ * an invitation to write a value nothing can use.
23
+ */
24
+ policy: ProfilePolicy;
18
25
  }
19
26
 
20
27
  /**
@@ -25,11 +32,32 @@ export const AdminUserDetailOverviewTab = (
25
32
  ) => {
26
33
  const { tr } = useI18n();
27
34
 
35
+ // AutoForm renders every key of the schema unless it is handed groups, so
36
+ // one group carrying the offered fields is how a field is left out.
37
+ const fields = (
38
+ [
39
+ "username",
40
+ "email",
41
+ "emailVerified",
42
+ "firstName",
43
+ "lastName",
44
+ "roles",
45
+ ] as const
46
+ ).filter((name) => {
47
+ if (name === "username") return props.policy.username.visible;
48
+ // The verified flag is meaningless without the address it qualifies.
49
+ if (name === "email" || name === "emailVerified") {
50
+ return props.policy.email.visible;
51
+ }
52
+ return true;
53
+ });
54
+
28
55
  return (
29
56
  <div className="min-h-0 flex-1 overflow-auto">
30
57
  <div className="flex max-w-6xl flex-col gap-6 p-6">
31
58
  <AutoForm
32
59
  form={props.form}
60
+ groups={[{ fields: [...fields] }]}
33
61
  card
34
62
  icon="user"
35
63
  title={tr("admin.userDetail.profile", { default: "Profile" })}
@@ -0,0 +1,146 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import {
3
+ profileIssues,
4
+ profilePolicy,
5
+ profileUpdateBody,
6
+ } from "./admin-user-detail-profile-policy.ts";
7
+
8
+ /**
9
+ * A realm that signs users in by username only: no email is ever collected,
10
+ * so every user row has an empty one.
11
+ */
12
+ const usernameOnly = { username: "required", email: "none" } as const;
13
+
14
+ /**
15
+ * The framework default: email-based accounts, no username at all.
16
+ */
17
+ const emailOnly = { username: "none", email: "required" } as const;
18
+
19
+ describe("profilePolicy", () => {
20
+ it("hides a field the realm does not collect", () => {
21
+ const policy = profilePolicy(usernameOnly, { username: "neo" });
22
+ expect(policy.email).toEqual({ visible: false, required: false });
23
+ expect(policy.username).toEqual({ visible: true, required: true });
24
+ });
25
+
26
+ it("still shows a field the realm dropped when the user carries a value", () => {
27
+ const policy = profilePolicy(usernameOnly, {
28
+ username: "neo",
29
+ email: "neo@example.com",
30
+ });
31
+ expect(policy.email).toEqual({ visible: true, required: false });
32
+ });
33
+
34
+ it("treats an unreadable realm config as optional, never required", () => {
35
+ const policy = profilePolicy(undefined, {});
36
+ expect(policy.username).toEqual({ visible: true, required: false });
37
+ expect(policy.email).toEqual({ visible: true, required: false });
38
+ });
39
+
40
+ it("does not require a username the realm derives from the email", () => {
41
+ const policy = profilePolicy({ username: "email", email: "required" }, {});
42
+ expect(policy.username).toEqual({ visible: true, required: false });
43
+ });
44
+ });
45
+
46
+ describe("profileIssues", () => {
47
+ it("accepts an empty email on a username-only realm", () => {
48
+ const policy = profilePolicy(usernameOnly, { username: "neo" });
49
+ const issues = profileIssues(
50
+ { username: "neo", email: "", roles: ["user", "admin"] },
51
+ policy,
52
+ { username: "neo" },
53
+ );
54
+ expect(issues).toEqual([]);
55
+ });
56
+
57
+ it("reports a missing email when the realm requires one", () => {
58
+ const policy = profilePolicy(emailOnly, { email: "neo@example.com" });
59
+ const issues = profileIssues({ email: "" }, policy, {
60
+ email: "neo@example.com",
61
+ });
62
+ expect(issues).toEqual([{ field: "email", reason: "required" }]);
63
+ });
64
+
65
+ it("refuses to blank a value that cannot be unset", () => {
66
+ const policy = profilePolicy(
67
+ { username: "optional", email: "optional" },
68
+ { username: "neo", email: "neo@example.com" },
69
+ );
70
+ const issues = profileIssues({ username: "neo", email: "" }, policy, {
71
+ username: "neo",
72
+ email: "neo@example.com",
73
+ });
74
+ expect(issues).toEqual([{ field: "email", reason: "cannot-clear" }]);
75
+ });
76
+
77
+ it("says nothing about a field that was already empty", () => {
78
+ const policy = profilePolicy(
79
+ { username: "optional", email: "optional" },
80
+ {
81
+ username: "neo",
82
+ },
83
+ );
84
+ const issues = profileIssues({ username: "neo", email: "" }, policy, {
85
+ username: "neo",
86
+ });
87
+ expect(issues).toEqual([]);
88
+ });
89
+ });
90
+
91
+ describe("profileUpdateBody", () => {
92
+ it("omits the email entirely on a username-only realm", () => {
93
+ const user = { username: "neo" };
94
+ const policy = profilePolicy(usernameOnly, user);
95
+ const body = profileUpdateBody(
96
+ { username: "neo", email: "", roles: ["user", "admin"] },
97
+ policy,
98
+ user,
99
+ );
100
+ // `users` is uniquely indexed on (realm, email) — sending "" writes a
101
+ // value that the next user to be saved collides with.
102
+ expect(body).not.toHaveProperty("email");
103
+ expect(body).not.toHaveProperty("emailVerified");
104
+ expect(body.username).toBe("neo");
105
+ expect(body.roles).toEqual(["user", "admin"]);
106
+ });
107
+
108
+ it("keeps the verified flag when the email is unchanged", () => {
109
+ const user = { email: "neo@example.com" };
110
+ const policy = profilePolicy(emailOnly, user);
111
+ const body = profileUpdateBody(
112
+ { email: "neo@example.com", emailVerified: true },
113
+ policy,
114
+ user,
115
+ );
116
+ expect(body.email).toBe("neo@example.com");
117
+ expect(body.emailVerified).toBe(true);
118
+ });
119
+
120
+ it("drops the verified flag when the email changes", () => {
121
+ const user = { email: "neo@example.com" };
122
+ const policy = profilePolicy(emailOnly, user);
123
+ const body = profileUpdateBody(
124
+ { email: "trinity@example.com", emailVerified: true },
125
+ policy,
126
+ user,
127
+ );
128
+ expect(body.email).toBe("trinity@example.com");
129
+ expect(body.emailVerified).toBe(false);
130
+ });
131
+
132
+ it("trims names and defaults roles to an empty list", () => {
133
+ const policy = profilePolicy(usernameOnly, { username: "neo" });
134
+ const body = profileUpdateBody(
135
+ { username: " neo ", firstName: " Thomas ", lastName: "" },
136
+ policy,
137
+ { username: "neo" },
138
+ );
139
+ expect(body).toEqual({
140
+ username: "neo",
141
+ firstName: "Thomas",
142
+ lastName: "",
143
+ roles: [],
144
+ });
145
+ });
146
+ });