@coffre/ui 0.0.0 → 0.1.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 (168) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +7 -3
  3. package/dist/client/.assetsignore +2 -0
  4. package/dist/client/_coffre/assets/account-C3Zo1l5A.js +1 -0
  5. package/dist/client/_coffre/assets/account-C4bNbeO7.js +2 -0
  6. package/dist/client/_coffre/assets/affordances-DiuJIiR2.js +1 -0
  7. package/dist/client/_coffre/assets/audit-1xLyuxGz.js +2 -0
  8. package/dist/client/_coffre/assets/audit-Dab2GuRj.js +1 -0
  9. package/dist/client/_coffre/assets/auth.device-BfCfeBWe.js +2 -0
  10. package/dist/client/_coffre/assets/auth.device-D2pCLF-X.js +1 -0
  11. package/dist/client/_coffre/assets/coffre-C7qL3HGR.js +1 -0
  12. package/dist/client/_coffre/assets/directory-C2Ou4SQv.js +1 -0
  13. package/dist/client/_coffre/assets/globals-Dgq4Jy0x.css +1 -0
  14. package/dist/client/_coffre/assets/grants-fqTtJ-aW.js +1 -0
  15. package/dist/client/_coffre/assets/index-BpGwbjmM.js +12 -0
  16. package/dist/client/_coffre/assets/inter-cyrillic-ext-opsz-normal-CfYZpDVp.woff2 +0 -0
  17. package/dist/client/_coffre/assets/inter-cyrillic-opsz-normal-BFQXbQ-f.woff2 +0 -0
  18. package/dist/client/_coffre/assets/inter-greek-ext-opsz-normal-BmPQvyvo.woff2 +0 -0
  19. package/dist/client/_coffre/assets/inter-greek-opsz-normal-BM0NlUE_.woff2 +0 -0
  20. package/dist/client/_coffre/assets/inter-latin-ext-opsz-normal-DpK-iCPk.woff2 +0 -0
  21. package/dist/client/_coffre/assets/inter-latin-opsz-normal-BwkfbSeq.woff2 +0 -0
  22. package/dist/client/_coffre/assets/inter-vietnamese-opsz-normal-Ci9T9ieq.woff2 +0 -0
  23. package/dist/client/_coffre/assets/jetbrains-mono-cyrillic-ext-wght-normal-EocZY2iu.woff2 +0 -0
  24. package/dist/client/_coffre/assets/jetbrains-mono-cyrillic-wght-normal-D73BlboJ.woff2 +0 -0
  25. package/dist/client/_coffre/assets/jetbrains-mono-greek-wght-normal-Bw9x6K1M.woff2 +0 -0
  26. package/dist/client/_coffre/assets/jetbrains-mono-latin-ext-wght-normal-DBQx-q_a.woff2 +0 -0
  27. package/dist/client/_coffre/assets/jetbrains-mono-latin-wght-normal-B9CIFXIH.woff2 +0 -0
  28. package/dist/client/_coffre/assets/jetbrains-mono-vietnamese-wght-normal-Bt-aOZkq.woff2 +0 -0
  29. package/dist/client/_coffre/assets/login-CR9CBXlw.js +2 -0
  30. package/dist/client/_coffre/assets/login-DZizFxa4.js +1 -0
  31. package/dist/client/_coffre/assets/offboarding-DOLJQcXU.js +1 -0
  32. package/dist/client/_coffre/assets/page-BxW2MwaX.js +1 -0
  33. package/dist/client/_coffre/assets/preload-helper-BHuP8_FL.js +46 -0
  34. package/dist/client/_coffre/assets/principal-page-xnBZVURr.js +1 -0
  35. package/dist/client/_coffre/assets/project-environments-CfIH5kFM.js +1 -0
  36. package/dist/client/_coffre/assets/projects._project._environment-CrWjQmIA.js +2 -0
  37. package/dist/client/_coffre/assets/projects._project._environment-CwRVG9Ne.js +3 -0
  38. package/dist/client/_coffre/assets/projects._project.index-CCDEuE-z.js +2 -0
  39. package/dist/client/_coffre/assets/projects._project.index-DCJsmnD7.js +1 -0
  40. package/dist/client/_coffre/assets/projects.index-B8eV8JIn.js +1 -0
  41. package/dist/client/_coffre/assets/projects.index-BNEC7Oz8.js +2 -0
  42. package/dist/client/_coffre/assets/settings-2BPsenNM.js +2 -0
  43. package/dist/client/_coffre/assets/settings-BrS0-wEt.js +1 -0
  44. package/dist/client/_coffre/assets/signin-errors-CroZWZgm.js +1 -0
  45. package/dist/client/_coffre/assets/tokens._token-B3-YwuKp.js +2 -0
  46. package/dist/client/_coffre/assets/tokens._token-D_hsbHx6.js +1 -0
  47. package/dist/client/_coffre/assets/tokens.index-BdKE2Jv8.js +2 -0
  48. package/dist/client/_coffre/assets/tokens.index-XvA4XRxh.js +1 -0
  49. package/dist/client/_coffre/assets/unregistered-CTRMtBIy.js +1 -0
  50. package/dist/client/_coffre/assets/use-action-DgAf-tiX.js +1 -0
  51. package/dist/client/_coffre/assets/users._user-BFPXcJ4y.js +2 -0
  52. package/dist/client/_coffre/assets/users._user-DEtMeSoh.js +1 -0
  53. package/dist/client/_coffre/assets/users.index-B03beUHq.js +1 -0
  54. package/dist/client/_coffre/assets/users.index-D8hwzM6P.js +2 -0
  55. package/dist/client/_coffre/assets/validation-9Cl_XS-l.js +1 -0
  56. package/dist/index.d.ts +19 -0
  57. package/dist/server/.vite/manifest.json +572 -0
  58. package/dist/server/_coffre/assets/_tanstack-start-manifest_v-B6CZDfrB.js +133 -0
  59. package/dist/server/_coffre/assets/account-B71_b5xc.js +296 -0
  60. package/dist/server/_coffre/assets/account-BoO6ZqMc.js +176 -0
  61. package/dist/server/_coffre/assets/affordances-BNaXjMOb.js +46 -0
  62. package/dist/server/_coffre/assets/async-local-storage-C0cwgkEr.js +3107 -0
  63. package/dist/server/_coffre/assets/audit-CwRFnSAY.js +74 -0
  64. package/dist/server/_coffre/assets/audit-t1sYjtrf.js +964 -0
  65. package/dist/server/_coffre/assets/auth.device-1IcJ475s.js +208 -0
  66. package/dist/server/_coffre/assets/auth.device-CJwZYYtn.js +29 -0
  67. package/dist/server/_coffre/assets/coffre-1hxT2E_U.js +278 -0
  68. package/dist/server/_coffre/assets/directory-wm_JQUfk.js +396 -0
  69. package/dist/server/_coffre/assets/empty-plugin-adapters-Cq4--YW-.js +5 -0
  70. package/dist/server/_coffre/assets/globals-Dgq4Jy0x.css +1 -0
  71. package/dist/server/_coffre/assets/grants-BmhEY1vz.js +243 -0
  72. package/dist/server/_coffre/assets/inter-cyrillic-ext-opsz-normal-CfYZpDVp.woff2 +0 -0
  73. package/dist/server/_coffre/assets/inter-cyrillic-opsz-normal-BFQXbQ-f.woff2 +0 -0
  74. package/dist/server/_coffre/assets/inter-greek-ext-opsz-normal-BmPQvyvo.woff2 +0 -0
  75. package/dist/server/_coffre/assets/inter-greek-opsz-normal-BM0NlUE_.woff2 +0 -0
  76. package/dist/server/_coffre/assets/inter-latin-ext-opsz-normal-DpK-iCPk.woff2 +0 -0
  77. package/dist/server/_coffre/assets/inter-latin-opsz-normal-BwkfbSeq.woff2 +0 -0
  78. package/dist/server/_coffre/assets/inter-vietnamese-opsz-normal-Ci9T9ieq.woff2 +0 -0
  79. package/dist/server/_coffre/assets/jetbrains-mono-cyrillic-ext-wght-normal-EocZY2iu.woff2 +0 -0
  80. package/dist/server/_coffre/assets/jetbrains-mono-cyrillic-wght-normal-D73BlboJ.woff2 +0 -0
  81. package/dist/server/_coffre/assets/jetbrains-mono-greek-wght-normal-Bw9x6K1M.woff2 +0 -0
  82. package/dist/server/_coffre/assets/jetbrains-mono-latin-ext-wght-normal-DBQx-q_a.woff2 +0 -0
  83. package/dist/server/_coffre/assets/jetbrains-mono-latin-wght-normal-B9CIFXIH.woff2 +0 -0
  84. package/dist/server/_coffre/assets/jetbrains-mono-vietnamese-wght-normal-Bt-aOZkq.woff2 +0 -0
  85. package/dist/server/_coffre/assets/login-B5O5LPXV.js +100 -0
  86. package/dist/server/_coffre/assets/login-Bvc4gp0X.js +22 -0
  87. package/dist/server/_coffre/assets/offboarding-CvSilnSm.js +330 -0
  88. package/dist/server/_coffre/assets/page-BjXk0Ehu.js +2003 -0
  89. package/dist/server/_coffre/assets/principal-page-DkwNGzM2.js +617 -0
  90. package/dist/server/_coffre/assets/project-environments-lEzFHUt4.js +9 -0
  91. package/dist/server/_coffre/assets/projects._project._environment-7grhqlgq.js +2227 -0
  92. package/dist/server/_coffre/assets/projects._project._environment-ByEf6pmz.js +55 -0
  93. package/dist/server/_coffre/assets/projects._project.index-B1bjowP-.js +932 -0
  94. package/dist/server/_coffre/assets/projects._project.index-DU8wtoHD.js +22 -0
  95. package/dist/server/_coffre/assets/projects.index-B0DoHbCz.js +280 -0
  96. package/dist/server/_coffre/assets/projects.index-kMDA6pMy.js +54 -0
  97. package/dist/server/_coffre/assets/react-dom-qCTiwGGR.js +1352 -0
  98. package/dist/server/_coffre/assets/router-ChU_CwCX.js +2028 -0
  99. package/dist/server/_coffre/assets/settings-CM9-h7in.js +18 -0
  100. package/dist/server/_coffre/assets/settings-Cg5qOGij.js +60 -0
  101. package/dist/server/_coffre/assets/signin-errors-BEFlVDbQ.js +28 -0
  102. package/dist/server/_coffre/assets/start-DR3rN0St.js +4 -0
  103. package/dist/server/_coffre/assets/tokens._token-CCpaPMAk.js +268 -0
  104. package/dist/server/_coffre/assets/tokens._token-Di4OZdAA.js +18 -0
  105. package/dist/server/_coffre/assets/tokens.index-Do3OHR31.js +34 -0
  106. package/dist/server/_coffre/assets/tokens.index-tDKq4AFh.js +10 -0
  107. package/dist/server/_coffre/assets/ui-BNzhIAQP.js +15710 -0
  108. package/dist/server/_coffre/assets/unregistered-BqaWYFJK.js +51 -0
  109. package/dist/server/_coffre/assets/use-action-BkJXC5JM.js +953 -0
  110. package/dist/server/_coffre/assets/useStore-DWGPqzXR.js +892 -0
  111. package/dist/server/_coffre/assets/users._user-BenYva93.js +15 -0
  112. package/dist/server/_coffre/assets/users._user-Cp039Va-.js +10 -0
  113. package/dist/server/_coffre/assets/users.index-BrzG1Ecq.js +10 -0
  114. package/dist/server/_coffre/assets/users.index-DQ_45DQl.js +34 -0
  115. package/dist/server/_coffre/assets/validation-BQtqPe2V.js +27 -0
  116. package/dist/server/index.js +13910 -0
  117. package/package.json +58 -5
  118. package/src/components/affordances.ts +80 -0
  119. package/src/components/agentation.tsx +34 -0
  120. package/src/components/command-palette.tsx +271 -0
  121. package/src/components/directory.tsx +448 -0
  122. package/src/components/grants.tsx +204 -0
  123. package/src/components/icons.tsx +283 -0
  124. package/src/components/offboarding.tsx +286 -0
  125. package/src/components/page.tsx +128 -0
  126. package/src/components/principal-page.tsx +549 -0
  127. package/src/components/principal-picker.tsx +168 -0
  128. package/src/components/principal.tsx +53 -0
  129. package/src/components/route-states.tsx +60 -0
  130. package/src/components/service-tokens.tsx +212 -0
  131. package/src/components/shell.tsx +641 -0
  132. package/src/components/syncs.tsx +581 -0
  133. package/src/components/theme.tsx +141 -0
  134. package/src/components/tile.tsx +22 -0
  135. package/src/components/ui.tsx +440 -0
  136. package/src/entry.ts +19 -0
  137. package/src/lib/access-plan.ts +201 -0
  138. package/src/lib/audit-sentences.ts +468 -0
  139. package/src/lib/capabilities.ts +45 -0
  140. package/src/lib/coffre.ts +57 -0
  141. package/src/lib/project-access.ts +95 -0
  142. package/src/lib/project-environments.ts +23 -0
  143. package/src/lib/reveal.ts +21 -0
  144. package/src/lib/secret-edit-batch.ts +181 -0
  145. package/src/lib/signin-errors.ts +33 -0
  146. package/src/lib/use-action.ts +50 -0
  147. package/src/lib/validation.ts +27 -0
  148. package/src/routeTree.gen.ts +367 -0
  149. package/src/router.tsx +84 -0
  150. package/src/routes/__root.tsx +192 -0
  151. package/src/routes/access.tsx +11 -0
  152. package/src/routes/account.tsx +359 -0
  153. package/src/routes/audit.tsx +545 -0
  154. package/src/routes/auth.device.tsx +200 -0
  155. package/src/routes/index.tsx +15 -0
  156. package/src/routes/login.tsx +129 -0
  157. package/src/routes/projects.$project.$environment.tsx +1659 -0
  158. package/src/routes/projects.$project.index.tsx +917 -0
  159. package/src/routes/projects.index.tsx +353 -0
  160. package/src/routes/settings.tsx +72 -0
  161. package/src/routes/tokens.$token.tsx +35 -0
  162. package/src/routes/tokens.index.tsx +37 -0
  163. package/src/routes/unregistered.tsx +59 -0
  164. package/src/routes/users.$user.tsx +13 -0
  165. package/src/routes/users.index.tsx +37 -0
  166. package/src/shared/models.ts +55 -0
  167. package/src/styles/globals.css +4118 -0
  168. package/src/types.ts +21 -0
@@ -0,0 +1,168 @@
1
+ import { useEffect, useId, useState } from 'react';
2
+ import { Link } from '@tanstack/react-router';
3
+ import { useCoffre } from '../lib/coffre';
4
+ import { projectAccessLabel } from '../lib/project-access';
5
+ import type { DirectoryPrincipal, GrantRow } from '../shared/models';
6
+ import { KIND, loadDirectory } from './directory';
7
+ import { PrincipalAvatar } from './principal';
8
+ import { Spinner } from './ui';
9
+ import { Check, Search } from './icons';
10
+
11
+ type PrincipalType = DirectoryPrincipal['principalType'];
12
+ type Directory = Awaited<ReturnType<typeof loadDirectory>>;
13
+
14
+ /**
15
+ * Who a grant is for: picked from the registered users or tokens, each shown
16
+ * with what it already holds on this project, or typed when the list is not
17
+ * yours to read.
18
+ *
19
+ * Only instance owners may list the directory. Asking on anyone else's behalf
20
+ * would write a refusal to the audit log in their name, so they get a text
21
+ * field and the server's answer instead. The list is asked for when the
22
+ * dialog opens rather than with the page, since most visits never open it.
23
+ */
24
+ export function PrincipalPicker({
25
+ principalType,
26
+ canList,
27
+ grants,
28
+ value,
29
+ onChange,
30
+ }: {
31
+ principalType: PrincipalType;
32
+ /** Whether you may list the directory: instance owners only. */
33
+ canList: boolean;
34
+ /** The project's grants, to show what each entry already holds. */
35
+ grants: GrantRow[];
36
+ value: string;
37
+ onChange: (principalId: string) => void;
38
+ }) {
39
+ const coffre = useCoffre();
40
+ const [directory, setDirectory] = useState<Directory | null>(null);
41
+ const [query, setQuery] = useState('');
42
+ const name = useId();
43
+ const kind = KIND[principalType];
44
+ const label = principalType === 'user' ? 'User' : 'Token';
45
+
46
+ useEffect(() => {
47
+ if (!canList) return;
48
+ let live = true;
49
+ void loadDirectory(coffre, true).then((result) => {
50
+ if (live) setDirectory(result);
51
+ });
52
+ return () => {
53
+ live = false;
54
+ };
55
+ }, [canList, coffre]);
56
+
57
+ if (!canList || directory?.ok === false) {
58
+ return (
59
+ <label className="field">
60
+ <span className="label">
61
+ {principalType === 'user' ? 'Email' : 'Service token common name'}
62
+ </span>
63
+ <input
64
+ className="input input-mono"
65
+ autoFocus
66
+ spellCheck={false}
67
+ autoComplete="off"
68
+ placeholder={principalType === 'user' ? 'someone@acme.example' : 'ci-deploy'}
69
+ value={value}
70
+ onChange={(event) => onChange(event.target.value)}
71
+ />
72
+ <span className="hint">
73
+ The {kind} must already be registered under {principalType === 'user' ? 'Users' : 'Tokens'}.
74
+ </span>
75
+ </label>
76
+ );
77
+ }
78
+
79
+ const registered =
80
+ directory?.principals.filter((principal) => principal.principalType === principalType) ?? [];
81
+ const needle = query.trim().toLowerCase();
82
+ const matches = registered.filter((principal) =>
83
+ principal.principalId.toLowerCase().includes(needle),
84
+ );
85
+
86
+ return (
87
+ <fieldset className="field picker">
88
+ <legend className="label">{label}</legend>
89
+
90
+ <label className="input-group">
91
+ <span className="visually-hidden">Filter {kind}s by name</span>
92
+ <Search size={14} className="input-icon" />
93
+ <input
94
+ className="input"
95
+ type="search"
96
+ autoFocus
97
+ spellCheck={false}
98
+ autoComplete="off"
99
+ placeholder={principalType === 'user' ? 'Filter by email…' : 'Filter by name…'}
100
+ value={query}
101
+ onChange={(event) => setQuery(event.target.value)}
102
+ onKeyDown={(event) => {
103
+ // Narrowed to one, Enter picks it rather than sending the form.
104
+ if (event.key === 'Enter' && matches.length === 1) {
105
+ event.preventDefault();
106
+ onChange(matches[0].principalId);
107
+ }
108
+ }}
109
+ />
110
+ </label>
111
+
112
+ <div className="picker-list">
113
+ {directory === null ? (
114
+ <p className="picker-empty">
115
+ <Spinner size={13} />
116
+ Loading {kind}s…
117
+ </p>
118
+ ) : registered.length === 0 ? (
119
+ <p className="picker-empty">
120
+ No {kind}s are registered yet.{' '}
121
+ <Link to={principalType === 'user' ? '/users' : '/tokens'}>Add one</Link> first.
122
+ </p>
123
+ ) : matches.length === 0 ? (
124
+ <p className="picker-empty">No registered {kind} matches “{query.trim()}”.</p>
125
+ ) : (
126
+ matches.map((principal) => {
127
+ const held = grants
128
+ .filter(
129
+ (grant) =>
130
+ grant.principalType === principalType &&
131
+ grant.principalId === principal.principalId,
132
+ )
133
+ .map(projectAccessLabel);
134
+ const chosen = principal.principalId === value;
135
+ return (
136
+ <label
137
+ key={principal.principalId}
138
+ className={`picker-option${chosen ? ' is-chosen' : ''}`}
139
+ >
140
+ <input
141
+ type="radio"
142
+ className="visually-hidden"
143
+ name={name}
144
+ value={principal.principalId}
145
+ checked={chosen}
146
+ onChange={() => onChange(principal.principalId)}
147
+ />
148
+ <PrincipalAvatar type={principalType} id={principal.principalId} />
149
+ <span className="picker-name">{principal.principalId}</span>
150
+ {held.length > 0 && (
151
+ <span className="picker-held">
152
+ <span className="visually-hidden">holds</span>
153
+ {held.map((access) => (
154
+ <span key={access} className="tag">
155
+ {access}
156
+ </span>
157
+ ))}
158
+ </span>
159
+ )}
160
+ <Check size={14} className="picker-check" />
161
+ </label>
162
+ );
163
+ })
164
+ )}
165
+ </div>
166
+ </fieldset>
167
+ );
168
+ }
@@ -0,0 +1,53 @@
1
+ import { Link } from '@tanstack/react-router';
2
+ import type { DirectoryPrincipal } from '../shared/models';
3
+ import { Key } from './icons';
4
+
5
+ type PrincipalType = DirectoryPrincipal['principalType'];
6
+
7
+ /** A person's initial, or a key for a machine. */
8
+ export function PrincipalAvatar({
9
+ type,
10
+ id,
11
+ size = 'sm',
12
+ }: {
13
+ type: PrincipalType;
14
+ id: string;
15
+ size?: 'sm' | 'lg';
16
+ }) {
17
+ const className = `avatar${type === 'service' ? ' avatar-token' : ''}${size === 'lg' ? ' avatar-lg' : ''}`;
18
+ return (
19
+ <span className={className} aria-hidden>
20
+ {type === 'service' ? <Key size={size === 'lg' ? 18 : 13} /> : id.slice(0, 1)}
21
+ </span>
22
+ );
23
+ }
24
+
25
+ /**
26
+ * A user or token, linking to its own page. `stretch` makes the link cover
27
+ * the whole table row it sits in.
28
+ */
29
+ export function PrincipalLink({
30
+ type,
31
+ id,
32
+ stretch = false,
33
+ }: {
34
+ type: PrincipalType;
35
+ id: string;
36
+ stretch?: boolean;
37
+ }) {
38
+ const className = `cell-link${stretch ? ' stretch' : ''}`;
39
+ return (
40
+ <span className="cell-principal">
41
+ <PrincipalAvatar type={type} id={id} />
42
+ {type === 'user' ? (
43
+ <Link className={className} to="/users/$user" params={{ user: id }}>
44
+ {id}
45
+ </Link>
46
+ ) : (
47
+ <Link className={className} to="/tokens/$token" params={{ token: id }}>
48
+ {id}
49
+ </Link>
50
+ )}
51
+ </span>
52
+ );
53
+ }
@@ -0,0 +1,60 @@
1
+ import { Link, useRouter, type ErrorComponentProps } from '@tanstack/react-router';
2
+ import { ClosedDoor } from './page';
3
+ import { AlertTriangle, Search } from './icons';
4
+
5
+ /** A path that matches no page. */
6
+ export function NotFound() {
7
+ return (
8
+ <ClosedDoor
9
+ icon={<Search size={18} />}
10
+ label="404"
11
+ title="Nothing at this address"
12
+ actions={
13
+ <Link className="btn" to="/projects">
14
+ All projects
15
+ </Link>
16
+ }
17
+ >
18
+ There is no page at this address. If you followed a link to a project or
19
+ environment, it may have been renamed or archived since.
20
+ </ClosedDoor>
21
+ );
22
+ }
23
+
24
+ /**
25
+ * A loader or render failure the page itself did not anticipate.
26
+ *
27
+ * Deliberately says nothing about the error's contents: they can include
28
+ * server detail that has no business on screen. Retrying re-runs the loaders.
29
+ */
30
+ export function RouteError({ reset }: ErrorComponentProps) {
31
+ const router = useRouter();
32
+ return (
33
+ <ClosedDoor
34
+ icon={<AlertTriangle size={18} />}
35
+ label="Something went wrong"
36
+ title="This page could not be shown"
37
+ actions={
38
+ <>
39
+ <button
40
+ type="button"
41
+ className="btn btn-primary"
42
+ onClick={() => {
43
+ reset();
44
+ void router.invalidate();
45
+ }}
46
+ >
47
+ Try again
48
+ </button>
49
+ <Link className="btn" to="/projects">
50
+ All projects
51
+ </Link>
52
+ </>
53
+ }
54
+ >
55
+ coffre could not load what this page needs, and nothing was read or written. If you
56
+ typed this address, check it; if it keeps happening, the service or its database may
57
+ be unavailable.
58
+ </ClosedDoor>
59
+ );
60
+ }
@@ -0,0 +1,212 @@
1
+ import type { ServiceTokenRow } from '@coffre/client';
2
+ import { useState } from 'react';
3
+ import { toast } from 'sonner';
4
+ import { memberRef, useCoffre } from '../lib/coffre';
5
+ import { useAction } from '../lib/use-action';
6
+ import { Card } from './page';
7
+ import { ConfirmButton, CopyButton, EmptyState, ErrorLine, Modal, Notice, Spinner, Timestamp } from './ui';
8
+ import { Key, Plus, X } from './icons';
9
+
10
+ const LIFETIMES = [30, 90, 180, 365] as const;
11
+
12
+ /**
13
+ * The bearer tokens a service presents to the API. A value exists in exactly
14
+ * one place, this dialog, for as long as it stays open: coffre keeps a hash.
15
+ */
16
+ export function ServiceTokens({ serviceId, tokens }: { serviceId: string; tokens: ServiceTokenRow[] }) {
17
+ const coffre = useCoffre();
18
+ const { pending, error, run } = useAction();
19
+
20
+ return (
21
+ <Card
22
+ labelledBy="service-tokens"
23
+ title="Credentials"
24
+ description="Bearer tokens this service presents to the API. Each is shown once, when it is issued; coffre keeps only a hash."
25
+ actions={<IssueToken serviceId={serviceId} />}
26
+ >
27
+ {error !== null && (
28
+ <div className="card-body">
29
+ <ErrorLine error={error} />
30
+ </div>
31
+ )}
32
+ {tokens.length === 0 ? (
33
+ <EmptyState title="No live credentials">
34
+ Nothing can act as this service until a token is issued.
35
+ </EmptyState>
36
+ ) : (
37
+ <div className="dt-wrap">
38
+ <table className="dt">
39
+ <thead>
40
+ <tr>
41
+ <th>Token</th>
42
+ <th className="col-shrink">Issued (UTC)</th>
43
+ <th className="col-shrink">Last used</th>
44
+ <th className="col-shrink">Expires</th>
45
+ <th className="col-actions">
46
+ <span className="visually-hidden">Actions</span>
47
+ </th>
48
+ </tr>
49
+ </thead>
50
+ <tbody>
51
+ {tokens.map((token) => (
52
+ <tr key={token.id}>
53
+ <td>
54
+ <span className="cell-account">
55
+ <Key size={15} />
56
+ <span className="cell-stack">
57
+ <span>{token.label ?? 'Unlabelled'}</span>
58
+ <small className="mono">{token.hint}</small>
59
+ </span>
60
+ </span>
61
+ </td>
62
+ <td className="nowrap">
63
+ <span className="cell-stack">
64
+ <Timestamp iso={token.createdAt} />
65
+ <small>by {token.createdBy}</small>
66
+ </span>
67
+ </td>
68
+ <td className="nowrap cell-muted">
69
+ {token.lastUsedAt === null ? (
70
+ 'Never'
71
+ ) : (
72
+ <span className="cell-stack">
73
+ <Timestamp iso={token.lastUsedAt} display="relative" />
74
+ {token.lastUsedIp !== null && <small className="mono">{token.lastUsedIp}</small>}
75
+ </span>
76
+ )}
77
+ </td>
78
+ <td className="nowrap cell-muted">
79
+ <Timestamp iso={token.expiresAt} display="relative" />
80
+ </td>
81
+ <td className="col-actions">
82
+ <ConfirmButton
83
+ trigger={
84
+ <button className="act" disabled={pending}>
85
+ <X size={13} />
86
+ Revoke
87
+ </button>
88
+ }
89
+ title={
90
+ <>
91
+ Revoke <span className="mono">{token.hint}</span>?
92
+ </>
93
+ }
94
+ body="Whatever uses it is refused from its next request. Issue a new token first if the service should keep working."
95
+ confirmLabel="Revoke token"
96
+ onConfirm={() =>
97
+ run(
98
+ () => coffre.tokens.revoke(memberRef('service', serviceId), token.id),
99
+ () => toast.success('Token revoked'),
100
+ )
101
+ }
102
+ />
103
+ </td>
104
+ </tr>
105
+ ))}
106
+ </tbody>
107
+ </table>
108
+ </div>
109
+ )}
110
+ </Card>
111
+ );
112
+ }
113
+
114
+ function IssueToken({ serviceId }: { serviceId: string }) {
115
+ const [open, setOpen] = useState(false);
116
+ const [label, setLabel] = useState('');
117
+ const [days, setDays] = useState<number>(90);
118
+ const [issued, setIssued] = useState<{ token: string; expiresAt: string } | null>(null);
119
+ const coffre = useCoffre();
120
+ const { pending, error, setError, run } = useAction();
121
+
122
+ function close() {
123
+ setOpen(false);
124
+ setIssued(null);
125
+ setLabel('');
126
+ setError(null);
127
+ }
128
+
129
+ return (
130
+ <>
131
+ <button className="btn btn-sm" onClick={() => setOpen(true)}>
132
+ <Plus size={14} />
133
+ Issue token
134
+ </button>
135
+ <Modal
136
+ open={open}
137
+ onOpenChange={(next) => (next ? setOpen(true) : close())}
138
+ title={issued === null ? 'Issue a token' : 'Copy the token now'}
139
+ >
140
+ {issued === null ? (
141
+ <form
142
+ className="form"
143
+ onSubmit={(event) => {
144
+ event.preventDefault();
145
+ run(
146
+ () =>
147
+ coffre.tokens.issue(memberRef('service', serviceId), {
148
+ label: label.trim() === '' ? null : label.trim(),
149
+ expiresInDays: days,
150
+ }),
151
+ (credential) => setIssued(credential),
152
+ );
153
+ }}
154
+ >
155
+ <div className="form-row">
156
+ <label className="field">
157
+ <span className="label">Label</span>
158
+ <input
159
+ className="input"
160
+ value={label}
161
+ maxLength={120}
162
+ placeholder="GitHub Actions deploy"
163
+ onChange={(event) => setLabel(event.target.value)}
164
+ />
165
+ </label>
166
+ <label className="field" style={{ flex: '0 0 9rem' }}>
167
+ <span className="label">Expires after</span>
168
+ <select className="select" value={days} onChange={(event) => setDays(Number(event.target.value))}>
169
+ {LIFETIMES.map((lifetime) => (
170
+ <option key={lifetime} value={lifetime}>
171
+ {lifetime} days
172
+ </option>
173
+ ))}
174
+ </select>
175
+ </label>
176
+ </div>
177
+ <ErrorLine error={error} />
178
+ <div className="dialog-actions">
179
+ <button className="btn" type="button" onClick={close}>
180
+ Cancel
181
+ </button>
182
+ <button className="btn btn-primary" type="submit" disabled={pending}>
183
+ {pending && <Spinner />}
184
+ Issue token
185
+ </button>
186
+ </div>
187
+ </form>
188
+ ) : (
189
+ <div className="form">
190
+ <Notice tone="info">
191
+ This is the only time the value is shown. Store it where the service reads its
192
+ configuration, as <span className="mono">COFFRE_TOKEN</span>.
193
+ </Notice>
194
+ <div className="token-once">
195
+ <code className="mono">{issued.token}</code>
196
+ <CopyButton value={issued.token} label="Copy token" />
197
+ </div>
198
+ <p className="hint">
199
+ Expires <Timestamp iso={issued.expiresAt} />. Send it as{' '}
200
+ <span className="mono">Authorization: Bearer …</span>
201
+ </p>
202
+ <div className="dialog-actions">
203
+ <button className="btn btn-primary" type="button" onClick={close}>
204
+ Done
205
+ </button>
206
+ </div>
207
+ </div>
208
+ )}
209
+ </Modal>
210
+ </>
211
+ );
212
+ }