@bevel-software/platform-core-frontend 0.9.1 → 0.11.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.
Files changed (40) hide show
  1. package/package.json +2 -2
  2. package/src/core/CoreAppShell.tsx +6 -0
  3. package/src/core/__tests__/AppChrome.test.tsx +3 -0
  4. package/src/core/registry.ts +25 -0
  5. package/src/lib/email.ts +25 -0
  6. package/src/modules/access/__tests__/ManageAccessDialog.placement.test.tsx +1 -1
  7. package/src/modules/access/__tests__/ManageAccessDialog.test.tsx +280 -4
  8. package/src/modules/access/api.ts +47 -13
  9. package/src/modules/access/components/ManageAccessDialog.tsx +119 -49
  10. package/src/modules/admin/__tests__/AdminRolesPage.capabilities.test.tsx +281 -0
  11. package/src/modules/admin/__tests__/AdminRolesPage.suggest.test.tsx +4 -4
  12. package/src/modules/admin/__tests__/DirectoryGroupsPage.test.tsx +438 -0
  13. package/src/modules/admin/components/AdminRolesPage.tsx +203 -411
  14. package/src/modules/admin/components/DirectoryGroupsPage.tsx +610 -0
  15. package/src/modules/admin/hooks/useExclusiveRunner.ts +27 -0
  16. package/src/modules/admin/services/groups.api.ts +151 -0
  17. package/src/modules/admin/services/roles.api.ts +57 -31
  18. package/src/modules/library/__tests__/LibraryRoutes.test.tsx +42 -3
  19. package/src/modules/library/__tests__/PluginPage.test.tsx +29 -0
  20. package/src/modules/library/__tests__/ToolPage.test.tsx +22 -0
  21. package/src/modules/library/__tests__/WorkspaceItemRoute.test.tsx +90 -0
  22. package/src/modules/library/components/LibraryPage.tsx +2 -2
  23. package/src/modules/library/components/PersonalPluginPage.tsx +2 -2
  24. package/src/modules/library/components/PluginPage.tsx +2 -2
  25. package/src/modules/library/components/tool-page/ToolPage.tsx +7 -1
  26. package/src/modules/library/routes/LibraryRoutes.tsx +6 -4
  27. package/src/modules/library/routes/WorkspaceItemRoute.tsx +33 -5
  28. package/src/modules/library/routes/library-paths.ts +30 -0
  29. package/src/modules/onboarding/__tests__/onboarding.test.tsx +63 -17
  30. package/src/modules/onboarding/agent-clients.ts +21 -7
  31. package/src/modules/onboarding/components/WelcomePage.tsx +1 -1
  32. package/src/modules/settings/settings-nav-items.tsx +9 -1
  33. package/src/modules/toolbar/components/ExternalAgentAccessPage.tsx +135 -20
  34. package/src/modules/toolbar/components/__tests__/ExternalAgentAccessPage.test.tsx +113 -14
  35. package/src/modules/update-check/__tests__/UpdateBanner.test.tsx +126 -0
  36. package/src/modules/update-check/components/UpdateBanner.tsx +92 -0
  37. package/src/modules/update-check/services/update-check.api.ts +24 -0
  38. package/src/shared/mcp/__tests__/connect-snippets.test.ts +66 -0
  39. package/src/shared/mcp/connect-snippets.ts +76 -0
  40. package/src/shared/mcp/index.ts +3 -0
@@ -0,0 +1,610 @@
1
+ import { useCallback, useEffect, useState } from 'react';
2
+ import { Check, Loader2, Pencil, Plus, Trash2, X } from 'lucide-react';
3
+ import { PageShell } from '../../../shared/components/PageShell';
4
+ import { Dialog } from '../../../shared/components/Dialog';
5
+ import { useAdmin } from '../state/admin.context';
6
+ import { useAppRegistry } from '../../../core/registry';
7
+ import { useExclusiveRunner, type ExclusiveRunner } from '../hooks/useExclusiveRunner';
8
+ import {
9
+ addGroupMember,
10
+ createGroup,
11
+ deleteGroup,
12
+ getGroupsRoster,
13
+ GroupsApiError,
14
+ removeGroupMember,
15
+ renameGroup,
16
+ type GroupRosterEntry,
17
+ type GroupsRoster,
18
+ } from '../services/groups.api';
19
+ import { EMAIL_RE, isGroupPrefixed } from '../../../lib/email';
20
+
21
+ function errMessage(err: unknown, fallback: string): string {
22
+ return err instanceof Error ? err.message : fallback;
23
+ }
24
+
25
+ /**
26
+ * The Groups page (still routed at `/directory-groups` — the component and
27
+ * route name stayed to avoid churn). Groups are people-sets for access
28
+ * management, with ONE source per deployment:
29
+ *
30
+ * - MANUAL mode: groups are created and edited right here.
31
+ * - IDP mode: an identity provider owns the groups — the roster renders
32
+ * read-only (membership is managed in the IdP).
33
+ *
34
+ * HOW an identity provider gets connected is not core's business: an overlay
35
+ * contributes that UI through the registry's `groupsDirectoryPanel` slot,
36
+ * rendered below the list in both modes. A core-only deployment simply never
37
+ * mentions a directory connection.
38
+ */
39
+ export function DirectoryGroupsPage() {
40
+ const { isAdmin } = useAdmin();
41
+ const { groupsDirectoryPanel: DirectoryPanel } = useAppRegistry();
42
+ const [roster, setRoster] = useState<GroupsRoster | null>(null);
43
+ const [error, setError] = useState<string | null>(null);
44
+ // The broken-MANUAL-groups.yaml state: the roster answers 422
45
+ // (`kind: 'broken-groups'`) with the parse message instead of a group list.
46
+ // Held separately from `error` so the page can render the same loud
47
+ // repair banner the in-roster `groupsHealth` marker gets — a broken groups
48
+ // file means groups are NOT applying, which is worse than a failed fetch.
49
+ const [brokenGroups, setBrokenGroups] = useState<{ file?: string; reason: string } | null>(null);
50
+ const [working, setWorking] = useState(false);
51
+ // Group pending delete confirmation (manual mode).
52
+ const [deleteTarget, setDeleteTarget] = useState<GroupRosterEntry | null>(null);
53
+ // The overlay panel's "a directory connection exists" signal — true between
54
+ // connecting and the first push, when the roster still reads 'manual' but
55
+ // manual edits would go dormant. See GroupsDirectoryPanelProps.
56
+ const [directoryConnected, setDirectoryConnected] = useState(false);
57
+ // All roster mutations queue through here — see useExclusiveRunner.
58
+ const runExclusive = useExclusiveRunner();
59
+
60
+ // Refreshes queue through the SAME exclusive runner as the mutations, so a
61
+ // refresh and a mutation can never interleave OR apply out of order:
62
+ // whichever queued first runs — and settles, and applies its roster — first.
63
+ // (That total order is also why there is no request-id stale-response guard
64
+ // here: the queue's tail only starts the next task after the previous one's
65
+ // handlers ran, so a superseded response can never land after a newer one.)
66
+ const refresh = useCallback(() => {
67
+ runExclusive(getGroupsRoster)
68
+ .then((r) => {
69
+ setRoster(r);
70
+ setError(null);
71
+ setBrokenGroups(null);
72
+ })
73
+ .catch((err) => {
74
+ if (err instanceof GroupsApiError && err.kind === 'broken-groups') {
75
+ // Manual groups.yaml is unreadable: surface the repair banner with
76
+ // the parse message. Not a generic error — groups are not applying.
77
+ setBrokenGroups({ file: err.file, reason: err.reason ?? err.message });
78
+ setError(null);
79
+ // A roster rendered from an EARLIER healthy load no longer describes
80
+ // the file: clear the stale rows (and any pending delete confirm) so
81
+ // no CRUD control can operate on a file that can't be parsed. The
82
+ // banner above is the whole page until the file is repaired.
83
+ setRoster(null);
84
+ setDeleteTarget(null);
85
+ return;
86
+ }
87
+ setError(errMessage(err, "Couldn't load groups."));
88
+ // Keep whatever was already on screen — a failed reload is a banner,
89
+ // not an empty page.
90
+ });
91
+ }, [runExclusive]);
92
+
93
+ useEffect(() => {
94
+ if (isAdmin) refresh();
95
+ }, [isAdmin, refresh]);
96
+
97
+ // Any mutation on the groups roster returns the authoritative roster — set
98
+ // it directly rather than refetching. No requestId bump needed: mutations
99
+ // share the exclusive queue with refresh(), so their responses already apply
100
+ // in a strict total order.
101
+ const applyRoster = useCallback((r: GroupsRoster) => {
102
+ setRoster(r);
103
+ // A stale banner from an earlier failed reload no longer describes what's
104
+ // on screen.
105
+ setError(null);
106
+ setBrokenGroups(null);
107
+ }, []);
108
+
109
+ async function handleDeleteGroup() {
110
+ if (!deleteTarget || working) return;
111
+ setWorking(true);
112
+ setError(null);
113
+ try {
114
+ const target = deleteTarget;
115
+ applyRoster(await runExclusive(() => deleteGroup(target.canonical)));
116
+ setDeleteTarget(null);
117
+ } catch (err) {
118
+ setDeleteTarget(null);
119
+ setError(errMessage(err, "Couldn't delete the group."));
120
+ } finally {
121
+ setWorking(false);
122
+ }
123
+ }
124
+
125
+ if (!isAdmin) {
126
+ return (
127
+ <PageShell title="Groups">
128
+ <div className="text-sm text-ink-muted">Admins only.</div>
129
+ </PageShell>
130
+ );
131
+ }
132
+
133
+ const idpMode = roster?.mode === 'idp';
134
+
135
+ // The broken-source banner has TWO triggers describing the same fact — the
136
+ // groups file cannot be read, so groups are NOT being applied:
137
+ // - a 200 roster whose `groupsHealth` marker is not ok (broken IdP-synced
138
+ // file: mode stays 'idp', roster is empty, resolution degrades), and
139
+ // - the roster 422 `broken-groups` (broken MANUAL file: no roster at all).
140
+ const health = roster?.groupsHealth;
141
+ const broken =
142
+ brokenGroups ?? (health && !health.ok ? { file: health.file, reason: health.reason } : null);
143
+
144
+ return (
145
+ <>
146
+ <PageShell title="Groups">
147
+ <div className="space-y-4">
148
+ {broken && (
149
+ <div
150
+ className="text-xs text-danger bg-danger-soft border border-danger/30 rounded-sm px-2 py-1.5 space-y-0.5"
151
+ role="alert"
152
+ >
153
+ <div className="font-semibold">
154
+ The groups file{broken.file ? ` (${broken.file})` : ''} is broken — groups are
155
+ not being applied.
156
+ </div>
157
+ <div>
158
+ No one gets access through groups until the file is repaired.
159
+ {broken.reason ? <> Reason: {broken.reason}</> : null}
160
+ </div>
161
+ </div>
162
+ )}
163
+
164
+ {error && (
165
+ <div
166
+ className="text-xs text-danger bg-danger-soft border border-danger/30 rounded-sm px-2 py-1.5"
167
+ role="alert"
168
+ >
169
+ {error}
170
+ </div>
171
+ )}
172
+
173
+ {roster === null ? (
174
+ error || broken ? null : <div className="text-xs text-ink-muted">Loading…</div>
175
+ ) : idpMode ? (
176
+ <IdpModeView roster={roster} />
177
+ ) : directoryConnected ? (
178
+ // Connected, but the first provisioning push hasn't landed: the
179
+ // IdP already owns groups, so no manual CRUD — creating one now
180
+ // would go dormant the moment the first push materializes.
181
+ <p className="text-xs text-ink-muted leading-snug">
182
+ An identity provider is connected. Groups appear here after its
183
+ first provisioning push — membership is managed there from now on.
184
+ </p>
185
+ ) : (
186
+ <ManualModeView
187
+ roster={roster}
188
+ onApply={applyRoster}
189
+ onDeleteRequest={setDeleteTarget}
190
+ runExclusive={runExclusive}
191
+ />
192
+ )}
193
+
194
+ {roster !== null && DirectoryPanel && (
195
+ <DirectoryPanel
196
+ mode={roster.mode}
197
+ onDirectoryChanged={refresh}
198
+ onConnectedChange={(connected) => {
199
+ setDirectoryConnected(connected);
200
+ // A connection flipping true unmounts the manual CRUD view —
201
+ // a delete confirm still open would then operate on a roster
202
+ // the IdP now owns. Close it.
203
+ if (connected) setDeleteTarget(null);
204
+ }}
205
+ />
206
+ )}
207
+ </div>
208
+ </PageShell>
209
+
210
+ {/* Delete-group confirm (manual mode), warning on grants that reference it. */}
211
+ <Dialog
212
+ open={deleteTarget !== null}
213
+ onClose={() => setDeleteTarget(null)}
214
+ title={deleteTarget ? `Delete "${deleteTarget.displayName}"?` : 'Delete group'}
215
+ size="sm"
216
+ busy={working}
217
+ footer={
218
+ <>
219
+ <button
220
+ onClick={() => setDeleteTarget(null)}
221
+ disabled={working}
222
+ className="px-3 py-1.5 text-sm rounded-sm text-ink hover:bg-hover border border-line disabled:opacity-50"
223
+ >
224
+ Cancel
225
+ </button>
226
+ <button
227
+ onClick={handleDeleteGroup}
228
+ disabled={working}
229
+ className="px-3 py-1.5 text-sm rounded-sm bg-danger hover:bg-danger/90 text-white disabled:opacity-50"
230
+ >
231
+ {working ? 'Working…' : 'Delete group'}
232
+ </button>
233
+ </>
234
+ }
235
+ >
236
+ {deleteTarget && (
237
+ <div className="space-y-2">
238
+ {deleteTarget.referencedBy.length > 0 ? (
239
+ <>
240
+ <p className="text-xs text-ink leading-snug">
241
+ {deleteTarget.referencedBy.length} access rule
242
+ {deleteTarget.referencedBy.length === 1 ? '' : 's'} reference
243
+ {deleteTarget.referencedBy.length === 1 ? 's' : ''} this group and will stop
244
+ matching anyone after deletion:
245
+ </p>
246
+ <ul className="max-h-40 overflow-auto text-xs text-ink-muted space-y-0.5">
247
+ {deleteTarget.referencedBy.map((ref, i) => (
248
+ <li key={`${ref.path}:${ref.verb}:${i}`} className="truncate">
249
+ {ref.verb} · {ref.path}
250
+ </li>
251
+ ))}
252
+ </ul>
253
+ </>
254
+ ) : (deleteTarget.assignedToRoles?.length ?? 0) === 0 ? (
255
+ <p className="text-xs text-ink leading-snug">
256
+ Nothing is shared with this group. Its members lose nothing else.
257
+ </p>
258
+ ) : null}
259
+ {/* Role assignments are unassigned atomically with the delete —
260
+ each named role loses the members it inherits through this
261
+ group, which the admin must hear BEFORE confirming. */}
262
+ {(deleteTarget.assignedToRoles?.length ?? 0) > 0 && (
263
+ <p className="text-xs text-ink leading-snug">
264
+ Deleting also unassigns this group from the{' '}
265
+ {deleteTarget.assignedToRoles!.length === 1 ? 'role' : 'roles'}{' '}
266
+ <span className="font-semibold">{deleteTarget.assignedToRoles!.join(', ')}</span>
267
+ {' '}— this group's members no longer hold{' '}
268
+ {deleteTarget.assignedToRoles!.length === 1 ? 'that role' : 'those roles'} through it.
269
+ </p>
270
+ )}
271
+ </div>
272
+ )}
273
+ </Dialog>
274
+ </>
275
+ );
276
+ }
277
+
278
+ /** Manual mode: in-app group CRUD. */
279
+ function ManualModeView({
280
+ roster,
281
+ onApply,
282
+ onDeleteRequest,
283
+ runExclusive,
284
+ }: {
285
+ roster: GroupsRoster;
286
+ onApply: (roster: GroupsRoster) => void;
287
+ onDeleteRequest: (group: GroupRosterEntry) => void;
288
+ runExclusive: ExclusiveRunner;
289
+ }) {
290
+ return (
291
+ <>
292
+ <p className="text-xs text-ink-muted leading-snug">
293
+ Groups are sets of people you can share with and assign roles to.
294
+ </p>
295
+
296
+ <CreateGroupForm onApply={onApply} runExclusive={runExclusive} />
297
+
298
+ {roster.groups.length === 0 ? (
299
+ <div className="text-xs text-ink-muted">No groups yet.</div>
300
+ ) : (
301
+ <div className="flex flex-col gap-3 max-w-xl">
302
+ {roster.groups.map((group) => (
303
+ <ManualGroupCard
304
+ key={group.canonical}
305
+ group={group}
306
+ onApply={onApply}
307
+ onDeleteRequest={onDeleteRequest}
308
+ runExclusive={runExclusive}
309
+ />
310
+ ))}
311
+ </div>
312
+ )}
313
+ </>
314
+ );
315
+ }
316
+
317
+ function CreateGroupForm({
318
+ onApply,
319
+ runExclusive,
320
+ }: {
321
+ onApply: (roster: GroupsRoster) => void;
322
+ runExclusive: ExclusiveRunner;
323
+ }) {
324
+ const [name, setName] = useState('');
325
+ const [busy, setBusy] = useState(false);
326
+ const [error, setError] = useState<string | null>(null);
327
+
328
+ const submit = async () => {
329
+ const trimmed = name.trim();
330
+ if (!trimmed) {
331
+ setError('Enter a group name.');
332
+ return;
333
+ }
334
+ setBusy(true);
335
+ setError(null);
336
+ try {
337
+ onApply(await runExclusive(() => createGroup(trimmed)));
338
+ setName('');
339
+ } catch (err) {
340
+ setError(errMessage(err, 'Failed to create group'));
341
+ } finally {
342
+ setBusy(false);
343
+ }
344
+ };
345
+
346
+ return (
347
+ <div className="space-y-1 max-w-xl">
348
+ <div className="flex items-center gap-1.5">
349
+ <input
350
+ value={name}
351
+ onChange={(e) => setName(e.target.value)}
352
+ onKeyDown={(e) => {
353
+ if (e.key === 'Enter') submit();
354
+ }}
355
+ placeholder="New group name"
356
+ disabled={busy}
357
+ className="text-xs px-2 py-1 border border-line rounded-sm focus:outline-none focus:border-accent w-56 max-w-full min-w-0"
358
+ aria-label="New group name"
359
+ />
360
+ <button
361
+ type="button"
362
+ onClick={submit}
363
+ disabled={busy}
364
+ className="px-2 py-1 text-xs rounded-sm bg-accent hover:bg-accent-hover text-white disabled:opacity-50 flex items-center gap-1"
365
+ >
366
+ {busy ? <Loader2 size={12} className="animate-spin" /> : <Plus size={12} />}
367
+ Create group
368
+ </button>
369
+ </div>
370
+ {error && (
371
+ <div className="text-xs text-danger bg-danger-soft border border-danger/30 rounded-sm px-2 py-1">
372
+ {error}
373
+ </div>
374
+ )}
375
+ </div>
376
+ );
377
+ }
378
+
379
+ function ManualGroupCard({
380
+ group,
381
+ onApply,
382
+ onDeleteRequest,
383
+ runExclusive,
384
+ }: {
385
+ group: GroupRosterEntry;
386
+ onApply: (roster: GroupsRoster) => void;
387
+ onDeleteRequest: (group: GroupRosterEntry) => void;
388
+ runExclusive: ExclusiveRunner;
389
+ }) {
390
+ const [email, setEmail] = useState('');
391
+ const [busy, setBusy] = useState(false);
392
+ const [error, setError] = useState<string | null>(null);
393
+ // Inline rename: null = not renaming; otherwise the draft name.
394
+ const [renameDraft, setRenameDraft] = useState<string | null>(null);
395
+
396
+ const submitRename = async () => {
397
+ const trimmed = (renameDraft ?? '').trim();
398
+ if (!trimmed) {
399
+ setError('Enter a group name.');
400
+ return;
401
+ }
402
+ if (trimmed === group.displayName) {
403
+ setRenameDraft(null);
404
+ return;
405
+ }
406
+ setBusy(true);
407
+ setError(null);
408
+ try {
409
+ onApply(await runExclusive(() => renameGroup(group.canonical, trimmed)));
410
+ setRenameDraft(null);
411
+ } catch (err) {
412
+ setError(errMessage(err, 'Failed to rename group'));
413
+ } finally {
414
+ setBusy(false);
415
+ }
416
+ };
417
+
418
+ const submitAdd = async () => {
419
+ const trimmed = email.trim();
420
+ // Mirror the backend's refusal of `group:`-prefixed member values with an
421
+ // inline hint — group members are emails; groups don't contain groups.
422
+ if (isGroupPrefixed(trimmed)) {
423
+ setError("Group members are emails — 'group:' references aren't allowed here.");
424
+ return;
425
+ }
426
+ if (!EMAIL_RE.test(trimmed)) {
427
+ setError('Enter a valid email address.');
428
+ return;
429
+ }
430
+ setBusy(true);
431
+ setError(null);
432
+ try {
433
+ onApply(await runExclusive(() => addGroupMember(group.canonical, trimmed)));
434
+ setEmail('');
435
+ } catch (err) {
436
+ setError(errMessage(err, 'Failed to add member'));
437
+ } finally {
438
+ setBusy(false);
439
+ }
440
+ };
441
+
442
+ const submitRemove = async (member: string) => {
443
+ setBusy(true);
444
+ setError(null);
445
+ try {
446
+ onApply(await runExclusive(() => removeGroupMember(group.canonical, member)));
447
+ } catch (err) {
448
+ setError(errMessage(err, 'Failed to remove member'));
449
+ } finally {
450
+ setBusy(false);
451
+ }
452
+ };
453
+
454
+ return (
455
+ <div className="border border-line rounded-lg p-3">
456
+ <div className="flex items-start gap-3">
457
+ {renameDraft === null ? (
458
+ <h2 className="flex-1 min-w-0 text-sm font-semibold text-ink truncate">
459
+ {group.displayName}
460
+ </h2>
461
+ ) : (
462
+ <div className="flex-1 min-w-0 flex items-center gap-1.5">
463
+ <input
464
+ value={renameDraft}
465
+ onChange={(e) => setRenameDraft(e.target.value)}
466
+ onKeyDown={(e) => {
467
+ if (e.key === 'Enter') submitRename();
468
+ if (e.key === 'Escape') setRenameDraft(null);
469
+ }}
470
+ disabled={busy}
471
+ autoFocus
472
+ className="text-sm font-semibold px-2 py-0.5 border border-line rounded-sm focus:outline-none focus:border-accent flex-1 min-w-0"
473
+ aria-label={`Rename ${group.displayName}`}
474
+ />
475
+ <button
476
+ type="button"
477
+ onClick={submitRename}
478
+ disabled={busy}
479
+ className="p-1.5 rounded-sm hover:bg-hover text-accent disabled:opacity-50 shrink-0"
480
+ title="Save name"
481
+ aria-label={`Save name for ${group.displayName}`}
482
+ >
483
+ {busy ? <Loader2 size={14} className="animate-spin" /> : <Check size={14} />}
484
+ </button>
485
+ <button
486
+ type="button"
487
+ onClick={() => setRenameDraft(null)}
488
+ disabled={busy}
489
+ className="p-1.5 rounded-sm hover:bg-hover text-ink-faint disabled:opacity-50 shrink-0"
490
+ title="Cancel rename"
491
+ aria-label={`Cancel renaming ${group.displayName}`}
492
+ >
493
+ <X size={14} />
494
+ </button>
495
+ </div>
496
+ )}
497
+ {renameDraft === null && (
498
+ <button
499
+ type="button"
500
+ onClick={() => setRenameDraft(group.displayName)}
501
+ disabled={busy}
502
+ className="p-1.5 rounded-sm hover:bg-hover text-ink-muted disabled:opacity-50 shrink-0"
503
+ title="Rename group"
504
+ aria-label={`Rename ${group.displayName}`}
505
+ >
506
+ <Pencil size={14} />
507
+ </button>
508
+ )}
509
+ <button
510
+ type="button"
511
+ onClick={() => onDeleteRequest(group)}
512
+ disabled={busy}
513
+ className="p-1.5 rounded-sm hover:bg-danger-soft text-danger disabled:opacity-50 shrink-0"
514
+ title="Delete group"
515
+ aria-label={`Delete ${group.displayName}`}
516
+ >
517
+ <Trash2 size={14} />
518
+ </button>
519
+ </div>
520
+
521
+ <div className="mt-2 flex flex-wrap gap-2">
522
+ {group.members.length === 0 ? (
523
+ <span className="text-xs text-ink-faint">No members.</span>
524
+ ) : (
525
+ group.members.map((member) => (
526
+ <span
527
+ key={member}
528
+ className="inline-flex max-w-full items-center gap-1.5 px-1.5 py-1 bg-sunken border border-line rounded-full text-xs text-ink"
529
+ >
530
+ <span className="min-w-0 truncate max-w-[14rem]">{member}</span>
531
+ <button
532
+ type="button"
533
+ onClick={() => submitRemove(member)}
534
+ disabled={busy}
535
+ className="shrink-0 rounded-full p-0.5 text-ink-faint hover:text-danger hover:bg-danger-soft disabled:opacity-50"
536
+ title="Remove member"
537
+ aria-label={`Remove ${member}`}
538
+ >
539
+ <X size={12} />
540
+ </button>
541
+ </span>
542
+ ))
543
+ )}
544
+ </div>
545
+
546
+ <div className="mt-2 flex items-center gap-1.5">
547
+ <input
548
+ type="email"
549
+ value={email}
550
+ onChange={(e) => setEmail(e.target.value)}
551
+ onKeyDown={(e) => {
552
+ if (e.key === 'Enter') submitAdd();
553
+ }}
554
+ placeholder="Add member by email"
555
+ disabled={busy}
556
+ className="text-xs px-2 py-1 border border-line rounded-sm focus:outline-none focus:border-accent flex-1 min-w-0 max-w-[16rem]"
557
+ aria-label={`Add member to ${group.displayName}`}
558
+ autoComplete="off"
559
+ />
560
+ <button
561
+ type="button"
562
+ onClick={submitAdd}
563
+ disabled={busy}
564
+ className="shrink-0 px-3 py-1 text-xs rounded-sm border border-line hover:bg-hover disabled:opacity-50 flex items-center gap-1"
565
+ >
566
+ {busy && <Loader2 size={12} className="animate-spin" />}
567
+ Add
568
+ </button>
569
+ </div>
570
+
571
+ {error && (
572
+ <div className="mt-2 text-xs text-danger bg-danger-soft border border-danger/30 rounded-sm px-2 py-1">
573
+ {error}
574
+ </div>
575
+ )}
576
+ </div>
577
+ );
578
+ }
579
+
580
+ /** IdP mode: the synced roster, read-only — membership is managed in the IdP. */
581
+ function IdpModeView({ roster }: { roster: GroupsRoster }) {
582
+ return (
583
+ <>
584
+ <p className="text-xs text-ink-muted leading-snug">
585
+ Groups are synced from your identity provider and are read-only here —
586
+ membership is managed there.
587
+ </p>
588
+
589
+ {roster.groups.length === 0 ? (
590
+ <div className="text-xs text-ink-muted">No groups synced yet.</div>
591
+ ) : (
592
+ <div className="max-w-xl space-y-1">
593
+ <ul className="divide-y divide-line border border-line rounded-sm">
594
+ {roster.groups.map((group) => (
595
+ <li key={group.canonical} className="px-3 py-2 text-sm">
596
+ <div className="font-medium truncate">{group.displayName}</div>
597
+ <div className="text-meta text-ink-muted truncate">
598
+ {group.members.length} {group.members.length === 1 ? 'member' : 'members'}
599
+ </div>
600
+ </li>
601
+ ))}
602
+ </ul>
603
+ <div className="text-meta text-ink-muted">
604
+ Membership is managed in your identity provider.
605
+ </div>
606
+ </div>
607
+ )}
608
+ </>
609
+ );
610
+ }
@@ -0,0 +1,27 @@
1
+ import { useCallback, useRef } from 'react';
2
+
3
+ /** Queues an async task behind every previously queued one. */
4
+ export type ExclusiveRunner = <T>(fn: () => Promise<T>) => Promise<T>;
5
+
6
+ /**
7
+ * Page-level mutation queue. Roster mutations are snapshot-based server-side:
8
+ * two overlapping writes each read the pre-mutation state, so the later commit
9
+ * silently drops the earlier change — and their responses can land out of
10
+ * order, letting a slow response overwrite a fresher roster. `run(fn)` starts
11
+ * `fn` only after every previously queued task has settled, so requests (and
12
+ * the rosters they return) apply in a strict order.
13
+ */
14
+ export function useExclusiveRunner(): ExclusiveRunner {
15
+ // Tail of the queue. The stored tail never rejects — a failed task must not
16
+ // wedge every task queued after it — while callers still observe their own
17
+ // task's rejection through the returned promise.
18
+ const tail = useRef<Promise<unknown>>(Promise.resolve());
19
+ return useCallback(<T,>(fn: () => Promise<T>): Promise<T> => {
20
+ const next = tail.current.then(fn);
21
+ tail.current = next.then(
22
+ () => undefined,
23
+ () => undefined,
24
+ );
25
+ return next;
26
+ }, []);
27
+ }