@coffre/ui 0.9.0 → 0.10.1

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 (109) hide show
  1. package/dist/chunks/{audit-sentence-JrQIUq6j.js → audit-sentence-DpYySuqg.js} +74 -7
  2. package/dist/chunks/audit-sentence-DpYySuqg.js.map +1 -0
  3. package/dist/chunks/{connected-apps-Dr8UgjK7.js → connected-apps-D48SFJsG.js} +4 -4
  4. package/dist/chunks/{connected-apps-Dr8UgjK7.js.map → connected-apps-D48SFJsG.js.map} +1 -1
  5. package/dist/chunks/{delete-place-CQCDHkne.js → delete-place-Cg_M6rAq.js} +4 -4
  6. package/dist/chunks/{delete-place-CQCDHkne.js.map → delete-place-Cg_M6rAq.js.map} +1 -1
  7. package/dist/chunks/{directory-XcSbDRJv.js → directory-_LTgdiIM.js} +11 -9
  8. package/dist/chunks/directory-_LTgdiIM.js.map +1 -0
  9. package/dist/chunks/{folders-Dw240WMJ.js → folders-D3A1NGpt.js} +2 -2
  10. package/dist/chunks/{folders-Dw240WMJ.js.map → folders-D3A1NGpt.js.map} +1 -1
  11. package/dist/chunks/{grants-CmdZjKYU.js → grants-CNM913EY.js} +6 -6
  12. package/dist/chunks/{grants-CmdZjKYU.js.map → grants-CNM913EY.js.map} +1 -1
  13. package/dist/chunks/{multi-combobox-CsIXihuc.js → multi-combobox-B0JzP1QN.js} +2 -2
  14. package/dist/chunks/{multi-combobox-CsIXihuc.js.map → multi-combobox-B0JzP1QN.js.map} +1 -1
  15. package/dist/chunks/{offboarding-CrgwaetA.js → offboarding-BeaNM3sf.js} +3 -3
  16. package/dist/chunks/{offboarding-CrgwaetA.js.map → offboarding-BeaNM3sf.js.map} +1 -1
  17. package/dist/chunks/{personal-tokens-Dvl1FktE.js → personal-tokens-KsXajpx4.js} +4 -4
  18. package/dist/chunks/{personal-tokens-Dvl1FktE.js.map → personal-tokens-KsXajpx4.js.map} +1 -1
  19. package/dist/chunks/{principal-page-bOaEYWJB.js → principal-page-BaiZXh6D.js} +94 -78
  20. package/dist/chunks/principal-page-BaiZXh6D.js.map +1 -0
  21. package/dist/chunks/{project-page-BI--vjt3.js → project-page-CYZ_MkfX.js} +2 -2
  22. package/dist/chunks/{project-page-BI--vjt3.js.map → project-page-CYZ_MkfX.js.map} +1 -1
  23. package/dist/chunks/protected-BL_DsZO6.js +22 -0
  24. package/dist/chunks/protected-BL_DsZO6.js.map +1 -0
  25. package/dist/chunks/{queries-epz4MIuW.js → queries-Danu0o75.js} +99 -31
  26. package/dist/chunks/queries-Danu0o75.js.map +1 -0
  27. package/dist/chunks/{references-DImfw0cz.js → references-BK2aPXae.js} +4 -4
  28. package/dist/chunks/{references-DImfw0cz.js.map → references-BK2aPXae.js.map} +1 -1
  29. package/dist/chunks/{role-field-C1KhNaIR.js → role-field-DIJiRz9B.js} +8 -5
  30. package/dist/chunks/role-field-DIJiRz9B.js.map +1 -0
  31. package/dist/chunks/{row-state-BsIHspy4.js → row-state-xtRGFnDc.js} +2 -2
  32. package/dist/chunks/{row-state-BsIHspy4.js.map → row-state-xtRGFnDc.js.map} +1 -1
  33. package/dist/chunks/{theme-3b4DnCow.js → theme-Ck45wzWB.js} +2 -2
  34. package/dist/chunks/{theme-3b4DnCow.js.map → theme-Ck45wzWB.js.map} +1 -1
  35. package/dist/chunks/{ui-2kwXXfrX.js → ui-D-blFWfx.js} +29 -5
  36. package/dist/chunks/{ui-2kwXXfrX.js.map → ui-D-blFWfx.js.map} +1 -1
  37. package/dist/chunks/{use-action-BB-RcHo1.js → use-action-BafZVwux.js} +2 -2
  38. package/dist/chunks/{use-action-BB-RcHo1.js.map → use-action-BafZVwux.js.map} +1 -1
  39. package/dist/chunks/{use-change-BY6KPmnJ.js → use-change-DhV6i_Ge.js} +218 -101
  40. package/dist/chunks/use-change-DhV6i_Ge.js.map +1 -0
  41. package/dist/chunks/use-roles-Daje0ikO.js +224 -0
  42. package/dist/chunks/use-roles-Daje0ikO.js.map +1 -0
  43. package/dist/chunks/{use-shell-C_NmvCt_.js → use-shell-BX9mzfaL.js} +2 -2
  44. package/dist/chunks/{use-shell-C_NmvCt_.js.map → use-shell-BX9mzfaL.js.map} +1 -1
  45. package/dist/index.d.ts +8 -3
  46. package/dist/index.js +29 -15
  47. package/dist/index.js.map +1 -1
  48. package/dist/pages/account.js +9 -9
  49. package/dist/pages/approval.js +1 -1
  50. package/dist/pages/audit.js +96 -34
  51. package/dist/pages/audit.js.map +1 -1
  52. package/dist/pages/device-login.js +1 -1
  53. package/dist/pages/environment.js +255 -115
  54. package/dist/pages/environment.js.map +1 -1
  55. package/dist/pages/login.js +2 -2
  56. package/dist/pages/oauth-authorize.js +1 -1
  57. package/dist/pages/project-access.js +79 -46
  58. package/dist/pages/project-access.js.map +1 -1
  59. package/dist/pages/project-environments.js +79 -30
  60. package/dist/pages/project-environments.js.map +1 -1
  61. package/dist/pages/project-settings.js +7 -7
  62. package/dist/pages/project.js +2 -2
  63. package/dist/pages/projects.js +26 -14
  64. package/dist/pages/projects.js.map +1 -1
  65. package/dist/pages/service-account.js +22 -15
  66. package/dist/pages/service-account.js.map +1 -1
  67. package/dist/pages/service-accounts.js +4 -4
  68. package/dist/pages/settings.js +580 -8
  69. package/dist/pages/settings.js.map +1 -1
  70. package/dist/pages/unregistered.js +1 -1
  71. package/dist/pages/user.js +5 -5
  72. package/dist/pages/users.js +4 -4
  73. package/package.json +3 -3
  74. package/src/components/directory.tsx +5 -2
  75. package/src/components/grants.tsx +1 -1
  76. package/src/components/principal-page.tsx +38 -23
  77. package/src/components/protected.tsx +17 -0
  78. package/src/components/role-editor.tsx +582 -0
  79. package/src/components/role-field.tsx +7 -3
  80. package/src/components/scope-picker.tsx +19 -9
  81. package/src/components/shell.tsx +7 -1
  82. package/src/components/ui.tsx +25 -2
  83. package/src/lib/access-plan.ts +35 -36
  84. package/src/lib/audit-sentences.ts +75 -9
  85. package/src/lib/changes.ts +46 -8
  86. package/src/lib/project-access.ts +4 -78
  87. package/src/lib/queries.ts +97 -29
  88. package/src/lib/roles.ts +201 -0
  89. package/src/lib/row-reads.ts +12 -0
  90. package/src/lib/secret-edit-batch.ts +147 -56
  91. package/src/lib/sensitivity.ts +50 -0
  92. package/src/lib/service-setup.ts +20 -8
  93. package/src/lib/use-roles.ts +17 -0
  94. package/src/options.ts +2 -3
  95. package/src/pages/audit.tsx +99 -26
  96. package/src/pages/environment.tsx +264 -101
  97. package/src/pages/project-access.tsx +49 -19
  98. package/src/pages/project-environments.tsx +62 -3
  99. package/src/pages/projects.tsx +3 -1
  100. package/src/pages/settings.tsx +2 -0
  101. package/src/parity.ts +42 -5
  102. package/src/shared/models.ts +2 -9
  103. package/src/styles/globals.css +322 -25
  104. package/dist/chunks/audit-sentence-JrQIUq6j.js.map +0 -1
  105. package/dist/chunks/directory-XcSbDRJv.js.map +0 -1
  106. package/dist/chunks/principal-page-bOaEYWJB.js.map +0 -1
  107. package/dist/chunks/queries-epz4MIuW.js.map +0 -1
  108. package/dist/chunks/role-field-C1KhNaIR.js.map +0 -1
  109. package/dist/chunks/use-change-BY6KPmnJ.js.map +0 -1
@@ -17,9 +17,10 @@ import type { DirectoryPrincipal, GrantRow, Me, ProjectSummary } from '../shared
17
17
  * names what it touches (`affects`), so your own changes always show.
18
18
  * Someone else's change shows within that window. Nothing is decided on a
19
19
  * cached read: the server authorizes every call, so a stale screen can offer
20
- * an action the server then refuses, never grant one. The audit log and the
21
- * chain's verification are the exception, read fresh on every visit: their
22
- * whole point is to be current.
20
+ * an action the server then refuses, never grant one. The audit log is the
21
+ * exception, read fresh on every visit: its whole point is to be current.
22
+ * The chain's verification is read once a visit like the rest, and again
23
+ * after any change made here.
23
24
  */
24
25
  export const FRESH_MS = 10_000;
25
26
 
@@ -64,6 +65,7 @@ export const keys = {
64
65
  audit: ['audit'],
65
66
  auditKeys: ['audit', 'keys'],
66
67
  settings: ['settings'],
68
+ roles: ['roles'],
67
69
  } satisfies Record<string, QueryKey | ((...args: never[]) => QueryKey)>;
68
70
 
69
71
  /** Members' grants, as rows, and a row for each one whose instance role reaches the place listed. */
@@ -291,6 +293,14 @@ export const queries = {
291
293
  queryFn: () => (allowed ? uiResult(() => client.settings.get()) : Promise.resolve(null)),
292
294
  }),
293
295
 
296
+ /**
297
+ * The instance's roles and instance roles, as an Owner edited and made
298
+ * them (docs/design/roles.md), and whether the caller edits them. Any
299
+ * member reads them: they are what every grant picker offers.
300
+ */
301
+ roles: (client: CoffreClient) =>
302
+ queryOptions({ queryKey: keys.roles, queryFn: () => uiResult(() => client.roles.list()) }),
303
+
294
304
  /** A page of the log, as filtered: always read fresh. */
295
305
  auditEntries: (client: CoffreClient, search: AuditSearch) =>
296
306
  queryOptions({
@@ -309,12 +319,15 @@ export const queries = {
309
319
  }),
310
320
 
311
321
  /**
312
- * Whether the log holds: verified on every visit, and never waited for.
313
- * It re-reads the whole log, so it takes as long as the log is long; the
314
- * page shows the entries first and the verdict when it comes.
322
+ * Whether the log holds: verified with each visit, and rendered with the
323
+ * page, the loader's read to carry to the browser: it is not asked again on
324
+ * mount, and not for a filter change or a hover, which `FRESH_MS` covers.
325
+ * It reads what was written since the newest checkpoint, so it takes as
326
+ * long as five minutes of the log, however long the log. It never rejects:
327
+ * a check that could not run is its verdict (`unknown`).
315
328
  */
316
329
  auditChain: (client: CoffreClient) =>
317
- queryOptions({ queryKey: [...keys.audit, 'chain'], staleTime: 0, queryFn: () => verifyChain(client) }),
330
+ queryOptions({ queryKey: [...keys.audit, 'chain'], refetchOnMount: false, queryFn: () => verifyChain(client) }),
318
331
  /** What deleting `market` or `market/prod` would take, asked afresh each time it is shown. */
319
332
  deletion: (client: CoffreClient, path: string) =>
320
333
  queryOptions({
@@ -339,32 +352,66 @@ export const queries = {
339
352
  */
340
353
  async function verifyChain(client: CoffreClient) {
341
354
  try {
342
- const result = await client.audit.verify();
343
- return result.ok
344
- ? {
345
- integrity: 'intact' as const,
346
- through: result.through,
347
- entries: result.entries,
348
- checkpoint: result.checkpoint,
349
- pending: result.pending ?? 0,
350
- }
351
- : {
352
- integrity: 'broken' as const,
353
- through: result.through,
354
- failedAtSeq: result.failedAtSeq,
355
- author: result.author,
356
- reason: result.reason,
357
- };
355
+ return chainResult(await client.audit.verify(), false);
358
356
  } catch (error) {
359
- const status = statusOf(error);
360
- if (status === 403) return { integrity: 'owners-only' as const };
361
- return {
362
- integrity: 'unknown' as const,
363
- problem: status === undefined ? 'the request never got an answer' : `the request failed with HTTP ${status}`,
364
- };
357
+ return chainUnknown(error);
365
358
  }
366
359
  }
367
360
 
361
+ /**
362
+ * A verdict as the page shows it: `whole` when it comes from the walk
363
+ * from the first entry, which someone asked for, not from the newest
364
+ * checkpoint's.
365
+ */
366
+ export function chainResult(result: Awaited<ReturnType<CoffreClient['audit']['verify']>>, whole: boolean) {
367
+ return result.ok
368
+ ? {
369
+ integrity: 'intact' as const,
370
+ whole,
371
+ through: result.through,
372
+ entries: result.entries,
373
+ // A server older than 0.10.1 checked from the first entry, and reached the head.
374
+ from: result.from ?? 0,
375
+ remaining: result.remaining ?? 0,
376
+ checkpoint: result.checkpoint,
377
+ pending: result.pending ?? 0,
378
+ }
379
+ : {
380
+ integrity: 'broken' as const,
381
+ through: result.through,
382
+ failedAtSeq: result.failedAtSeq,
383
+ author: result.author,
384
+ reason: result.reason,
385
+ };
386
+ }
387
+
388
+ /**
389
+ * What the page shows, of the check from the newest checkpoint (`quick`)
390
+ * and the walk from the first entry, once someone ran it (`whole`). A break
391
+ * the walk found stands; otherwise the newer check says how the log is now,
392
+ * broken, partly checked or not checked at all; and only while it found the
393
+ * log whole too does the walk's verdict, which read further back, stand
394
+ * over it.
395
+ *
396
+ * whole broken → whole
397
+ * whole intact, quick intact with 0 remaining → whole
398
+ * whole intact, quick anything else → quick
399
+ */
400
+ export function shownChain(quick: ChainResult, whole: ChainResult | null): ChainResult {
401
+ if (whole === null || whole.integrity === 'broken') return whole ?? quick;
402
+ return quick.integrity === 'intact' && quick.remaining === 0 && whole.integrity === 'intact' ? whole : quick;
403
+ }
404
+
405
+ /** Why the log's verdict is not known: closed to the caller, or no answer. */
406
+ export function chainUnknown(error: unknown) {
407
+ const status = statusOf(error);
408
+ if (status === 403) return { integrity: 'owners-only' as const };
409
+ return {
410
+ integrity: 'unknown' as const,
411
+ problem: status === undefined ? 'the request never got an answer' : `the request failed with HTTP ${status}`,
412
+ };
413
+ }
414
+
368
415
  export type ChainResult = Awaited<ReturnType<typeof verifyChain>>;
369
416
 
370
417
  /**
@@ -481,6 +528,20 @@ export async function loadEnvironment(queryClient: QueryClient, client: CoffreCl
481
528
  return secrets;
482
529
  }
483
530
 
531
+ /**
532
+ * What the audit page reads: a page of the log and the verification from
533
+ * the newest checkpoint, asked together and rendered together, so the badge
534
+ * arrives with the page. The verification never fails the load: a check that
535
+ * could not run is its verdict.
536
+ */
537
+ export async function loadAudit(queryClient: QueryClient, client: CoffreClient, search: AuditSearch) {
538
+ const [entries] = await Promise.all([
539
+ queryClient.fetchQuery(queries.auditEntries(client, search)),
540
+ queryClient.fetchQuery(queries.auditChain(client)),
541
+ ]);
542
+ return entries;
543
+ }
544
+
484
545
  /** The directory pages' read: the directory, for owners. */
485
546
  export async function loadDirectory(queryClient: QueryClient, client: CoffreClient) {
486
547
  const shell = await loadShell(queryClient, client);
@@ -494,6 +555,7 @@ export async function loadSettings(queryClient: QueryClient, client: CoffreClien
494
555
  loadDirectory(queryClient, client),
495
556
  queryClient.fetchQuery(queries.settings(client, shell.capabilities.runsInstance)),
496
557
  queryClient.fetchQuery(queries.auditKeys(client, shell.capabilities.runsInstance)),
558
+ queryClient.fetchQuery(queries.roles(client)),
497
559
  ]);
498
560
  }
499
561
 
@@ -674,6 +736,12 @@ export const affects = {
674
736
  * may do, which the directory and `/me` say.
675
737
  */
676
738
  settings: (): QueryKey[] => [keys.settings, keys.me, keys.directory],
739
+ /**
740
+ * A role edited, made, reset or deleted, or an environment protected:
741
+ * what everyone holds may change, so the roles, what you hold, and every
742
+ * grant and environment read with what it holds.
743
+ */
744
+ roles: (): QueryKey[] => [keys.roles, keys.me, keys.projects, ['grants'], ['member-access'], ['secrets']],
677
745
  credentials: (member: string): QueryKey[] => [keys.credentials(member)],
678
746
  bindings: (member: string): QueryKey[] => [keys.bindings(member)],
679
747
  /** Unlinking an account also ends the sessions it signed in. */
@@ -0,0 +1,201 @@
1
+ import type { ImpactLine, RolesView, RoleView } from '@coffre/client';
2
+ import {
3
+ assignableToEnvironment,
4
+ FIXED_ROLES,
5
+ INSTANCE_ROLES,
6
+ PERMISSIONS,
7
+ PROJECT_ONLY_PERMISSIONS,
8
+ ROLES,
9
+ type Permission,
10
+ type RoleDefinition,
11
+ } from '@coffre/core/access';
12
+
13
+ /**
14
+ * The instance's roles as the pages offer and name them (docs/design/roles.md):
15
+ * each in two columns, what it holds in an environment and what a person
16
+ * holds of it in a protected one. The policy is the vault's, read from
17
+ * `GET /api/roles`; until it is, the roles as they ship stand in.
18
+ */
19
+
20
+ /** Every permission in words, in the catalogue's order, and what a short label leaves out. */
21
+ export const PERMISSION_WORDS: Record<Permission, { label: string; note?: string; verb: string }> = {
22
+ 'secret.read': { label: 'Read values', verb: 'read values' },
23
+ 'secret.use': { label: 'Use values', note: 'placeholders, the proxy', verb: 'use values' },
24
+ 'secret.write': { label: 'Write values', verb: 'write values' },
25
+ 'secret.archive': { label: 'Retire values', verb: 'retire values' },
26
+ 'audit.read': { label: 'Read the log', verb: 'read the log' },
27
+ 'environment.manage': { label: 'Manage environments', verb: 'manage environments' },
28
+ 'grant.manage': { label: 'Manage access', verb: 'manage access' },
29
+ 'project.manage': { label: 'Manage the project', verb: 'manage the project' },
30
+ };
31
+
32
+ /** The rows of a role's grid: secrets first, as a role is read, then the log, then what only a project holds. */
33
+ export const GRID_PERMISSIONS: readonly Permission[] = [
34
+ 'secret.read',
35
+ 'secret.use',
36
+ 'secret.write',
37
+ 'secret.archive',
38
+ 'audit.read',
39
+ 'environment.manage',
40
+ 'grant.manage',
41
+ 'project.manage',
42
+ ];
43
+
44
+ export function projectWide(permission: Permission): boolean {
45
+ return PROJECT_ONLY_PERMISSIONS.includes(permission);
46
+ }
47
+
48
+ /** A role's two columns, as the grid edits them. */
49
+ export type Columns = { permissions: Permission[]; protected: Permission[] };
50
+ export type Column = 'normal' | 'protected';
51
+
52
+ /**
53
+ * One checkbox of the grid flipped, and the rules the vault holds a role to
54
+ * kept true by the boxes it takes along (`definitionProblem`):
55
+ *
56
+ * checking Read checks Use unchecking Use unchecks Read
57
+ * checking a protected box checks its unchecking a normal box unchecks
58
+ * normal one its protected one
59
+ *
60
+ * A project-wide permission has no protected box: flipping one is no change.
61
+ */
62
+ export function toggle(columns: Pick<RoleDefinition, 'permissions' | 'protected'>, permission: Permission, column: Column, on: boolean): Columns {
63
+ if (column === 'protected' && projectWide(permission)) return ordered(columns);
64
+ const normal = new Set(columns.permissions);
65
+ const guarded = new Set(columns.protected);
66
+ // Reading takes using; giving up using gives up reading.
67
+ const along: Permission[] = on ? (permission === 'secret.read' ? ['secret.use'] : []) : permission === 'secret.use' ? ['secret.read'] : [];
68
+ for (const each of [permission, ...along]) {
69
+ if (on) {
70
+ normal.add(each);
71
+ if (column === 'protected') guarded.add(each);
72
+ } else {
73
+ guarded.delete(each);
74
+ if (column === 'normal') normal.delete(each);
75
+ }
76
+ }
77
+ return ordered({ permissions: [...normal], protected: [...guarded] });
78
+ }
79
+
80
+ /** Each column in catalogue order, as the vault keeps it, so that comparing two says whether they differ. */
81
+ export function ordered(columns: Pick<RoleDefinition, 'permissions' | 'protected'>): Columns {
82
+ return {
83
+ permissions: PERMISSIONS.filter((permission) => columns.permissions.includes(permission)),
84
+ protected: PERMISSIONS.filter((permission) => columns.protected.includes(permission)),
85
+ };
86
+ }
87
+
88
+ /** Whether two definitions hold the same, named and described the same. */
89
+ export function sameDefinition(a: RoleDefinition, b: RoleDefinition): boolean {
90
+ const [x, y] = [ordered(a), ordered(b)];
91
+ return a.name.trim() === b.name.trim() && a.description === b.description
92
+ && x.permissions.join() === y.permissions.join() && x.protected.join() === y.protected.join();
93
+ }
94
+
95
+ /** A column in words, for a role's row: "read, use and write values", or "nothing". */
96
+ export function columnInWords(permissions: readonly Permission[]): string {
97
+ const values = ['secret.read', 'secret.use', 'secret.write', 'secret.archive'] as const;
98
+ const held = values.filter((permission) => permissions.includes(permission));
99
+ const verbs = { 'secret.read': 'read', 'secret.use': 'use', 'secret.write': 'write', 'secret.archive': 'retire' } as const;
100
+ const parts = [
101
+ ...(held.length === 0 ? [] : [`${list(held.map((permission) => verbs[permission]))} values`]),
102
+ ...GRID_PERMISSIONS.filter((permission) => !(values as readonly string[]).includes(permission) && permissions.includes(permission)).map((permission) => PERMISSION_WORDS[permission].verb),
103
+ ];
104
+ return parts.length === 0 ? 'nothing' : list(parts);
105
+ }
106
+
107
+ function list(words: readonly string[]): string {
108
+ return words.length <= 1 ? (words[0] ?? '') : `${words.slice(0, -1).join(', ')} and ${words.at(-1)}`;
109
+ }
110
+
111
+ /** A role's id, made from its name as it is typed: "Release manager" is `release-manager`. */
112
+ export function roleIdFrom(name: string): string {
113
+ return name
114
+ .toLowerCase()
115
+ .normalize('NFKD')
116
+ .replace(/[\u0300-\u036f]/g, '')
117
+ .replace(/[^a-z0-9]+/g, '-')
118
+ .replace(/^[^a-z]+/, '')
119
+ .replace(/-+$/, '')
120
+ .slice(0, 32)
121
+ .replace(/-+$/, '');
122
+ }
123
+
124
+ /** What is wrong with a new role's id, or null: its shape, and not one the instance has. */
125
+ export function roleIdProblem(id: string, taken: readonly string[]): string | null {
126
+ if (id === '') return 'A role needs an id.';
127
+ if (!/^[a-z0-9-]+$/.test(id)) return 'An id is lowercase letters, digits and dashes.';
128
+ if (!/^[a-z]/.test(id)) return 'An id starts with a letter.';
129
+ if (id.length < 2 || id.length > 32) return 'An id is 2 to 32 characters.';
130
+ if (taken.includes(id)) return `There is a role ${id} already.`;
131
+ return null;
132
+ }
133
+
134
+ /** The roles as they ship: what the pages offer and name until the instance's are read, or where they cannot be. */
135
+ export const SHIPPED_ROLES: RolesView = {
136
+ roles: Object.entries(ROLES).map(([id, definition]) => shippedView(id, definition, FIXED_ROLES.roles.includes(id), assignableToEnvironment(id))),
137
+ instanceRoles: Object.entries(INSTANCE_ROLES).map(([id, definition]) =>
138
+ shippedView(id, definition, (FIXED_ROLES.instanceRoles as readonly string[]).includes(id), false)),
139
+ editable: false,
140
+ };
141
+
142
+ function shippedView(id: string, definition: RoleDefinition, fixed: boolean, assignable: boolean): RoleView {
143
+ const shipped = { name: definition.name, description: definition.description, ...ordered(definition) };
144
+ return { id, ...shipped, builtin: true, edited: false, fixed, default: shipped, assignableToEnvironment: assignable, grants: 0 };
145
+ }
146
+
147
+ /** A role's name by its id, as the instance defines it; one it does not know, as itself. */
148
+ export function roleNameOf(roles: readonly Pick<RoleView, 'id' | 'name'>[], id: string): string {
149
+ return roles.find((role) => role.id === id)?.name ?? id;
150
+ }
151
+
152
+ /**
153
+ * One line of a change's impact, in words, from the dry run's count:
154
+ *
155
+ * widens secret.read, protected, 6 people, 4 environments
156
+ * 6 people can now read values in 4 protected environments
157
+ * narrows secret.write, 2 people, 1 environment, ["ada@… on market/dev", …]
158
+ * 2 people can no longer write values in market/dev
159
+ *
160
+ * One place is named, from its examples; more are counted.
161
+ */
162
+ export function impactInWords(line: ImpactLine): string {
163
+ const people = [
164
+ line.people > 0 ? (line.people === 1 ? '1 person' : `${line.people} people`) : null,
165
+ line.services > 0 ? (line.services === 1 ? '1 service account' : `${line.services} service accounts`) : null,
166
+ ].filter((part) => part !== null).join(' and ');
167
+ const can = line.change === 'widens' ? 'can now' : 'can no longer';
168
+ const wide = projectWide(line.permission);
169
+ const count = wide ? line.projects : line.environments;
170
+ const only = count === 1 ? placeOf(line.examples[0]) : null;
171
+ const where = only !== null
172
+ ? `${wide ? 'on' : 'in'} ${only}${line.protected ? ', protected' : ''}`
173
+ : `${wide ? 'on' : 'in'} ${count} ${line.protected ? 'protected ' : ''}${wide ? (count === 1 ? 'project' : 'projects') : count === 1 ? 'environment' : 'environments'}`;
174
+ return `${people} ${can} ${PERMISSION_WORDS[line.permission].verb} ${where}`;
175
+ }
176
+
177
+ /** The place of an impact's example, `ada@acme.example on market/prod`: `market/prod`. */
178
+ function placeOf(example: string | undefined): string | null {
179
+ const at = example?.lastIndexOf(' on ') ?? -1;
180
+ return example === undefined || at < 0 ? null : example.slice(at + ' on '.length);
181
+ }
182
+
183
+ /** Who an impact line reaches, for its note: `ada@acme.example on market/prod, bo@acme.example on market/prod`. */
184
+ export function impactExamples(line: ImpactLine): string | null {
185
+ return line.examples.length === 0 ? null : `${line.examples.join(', ')}${line.people + line.services > line.examples.length ? ', …' : ''}`;
186
+ }
187
+
188
+ /**
189
+ * What protecting an environment does, said by a role whose two columns
190
+ * differ, Developer first: "Developers use and write values, and read
191
+ * none". Null where no role's do, so protecting changes nothing.
192
+ */
193
+ export function protectionInWords(roles: readonly Pick<RoleView, 'id' | 'name' | 'permissions' | 'protected'>[]): string | null {
194
+ const differs = (role: Pick<RoleView, 'permissions' | 'protected'>) =>
195
+ role.permissions.some((permission) => !projectWide(permission) && !role.protected.includes(permission));
196
+ const role = roles.find((each) => each.id === 'developer' && differs(each)) ?? roles.find(differs);
197
+ if (role === undefined) return null;
198
+ const held = columnInWords(role.protected);
199
+ const loses = role.permissions.includes('secret.read') && !role.protected.includes('secret.read') && role.protected.length > 0;
200
+ return `${role.name}s ${held === 'nothing' ? 'hold nothing' : held}${loses ? ', and read none' : ''}`;
201
+ }
@@ -62,3 +62,15 @@ export function rowReads(state: RowReads, action: RowReadsAction): RowReads {
62
62
  export function revealToggleDisabled(state: RowReads, shown: Reveal | null): boolean {
63
63
  return shown === null && state.revealing;
64
64
  }
65
+
66
+ /**
67
+ * The value an edit starts from: a plain one as listed, one the caller reads
68
+ * as `read` opens it, or none, the editor opening empty to replace it: a
69
+ * sensitive value, a parent's they do not read, or a read refused or failed,
70
+ * whose reason the row says. It always opens: writing a value takes no read
71
+ * of the one it replaces.
72
+ */
73
+ export async function valueToEdit(row: { value: string | null; readable: boolean }, read: () => Promise<string | null>): Promise<string | null> {
74
+ if (row.value !== null) return row.value;
75
+ return row.readable ? read() : null;
76
+ }