@coffre/ui 0.0.0-stage → 0.1.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 (168) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +8 -2
  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 -3
  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,581 @@
1
+ import {
2
+ type CoffreClient,
3
+ configFromForm,
4
+ firstMissing,
5
+ initialValues,
6
+ isAsked,
7
+ type FormValues,
8
+ type SyncProviderInfo,
9
+ } from '@coffre/client';
10
+ import { Fragment, useEffect, useState, type ReactNode } from 'react';
11
+ import { useRouter } from '@tanstack/react-router';
12
+ import { DropdownMenu } from 'radix-ui';
13
+ import { toast } from 'sonner';
14
+ import { uiResult, useCoffre } from '../lib/coffre';
15
+ import { useAction } from '../lib/use-action';
16
+ import type { RunOutcome, SyncView } from '../shared/models';
17
+ import { Card } from './page';
18
+ import { ConfirmDialog, EmptyState, ErrorLine, Modal, Notice, Spinner, Timestamp, Toggletip } from './ui';
19
+ import { AlertTriangle, MoreHorizontal, Pause, Play, Plus, Sync, SyncMark, X } from './icons';
20
+
21
+ /** How often the card refreshes while a run is in flight. */
22
+ const RUNNING_POLL_MS = 2000;
23
+ /** How long a new sync's toast waits for its first push to end. */
24
+ const FIRST_RUN_WAIT_MS = 60_000;
25
+
26
+ type SyncsResult = { ok: true; syncs: SyncView[]; canManage: boolean } | { ok: false; error: string };
27
+
28
+ /**
29
+ * Where this environment's secrets are pushed.
30
+ *
31
+ * Hidden from people who can neither see a sync nor add one; everyone who can
32
+ * write a secret sees them, since saving one sends it on.
33
+ */
34
+ export function Syncs({
35
+ project,
36
+ environment,
37
+ result,
38
+ canRun,
39
+ }: {
40
+ project: string;
41
+ environment: string;
42
+ result: SyncsResult;
43
+ canRun: boolean;
44
+ }) {
45
+ const router = useRouter();
46
+ const running = result.ok && result.syncs.some((sync) => sync.running);
47
+
48
+ // A save starts a run in the background; watch it land.
49
+ useEffect(() => {
50
+ if (!running) return;
51
+ const timer = setInterval(() => void router.invalidate(), RUNNING_POLL_MS);
52
+ return () => clearInterval(timer);
53
+ }, [running, router]);
54
+
55
+ if (result.ok && result.syncs.length === 0 && !result.canManage) return null;
56
+ const canManage = result.ok && result.canManage;
57
+
58
+ return (
59
+ <Card
60
+ labelledBy="syncs"
61
+ title="Syncs"
62
+ description="Destinations these secrets are pushed to. A change goes out when it is saved, and every hour coffre checks each destination and repairs what drifted."
63
+ actions={canManage ? <AddSync project={project} environment={environment} /> : undefined}
64
+ >
65
+ {!result.ok ? (
66
+ <div className="card-body">
67
+ <ErrorLine error={result.error} />
68
+ </div>
69
+ ) : result.syncs.length === 0 ? (
70
+ <EmptyState title="Not synced anywhere">
71
+ Push these secrets where your code runs, such as GitHub Actions or Vercel, and keep
72
+ them current there. Anything else can read them with <code>coffre run</code>.
73
+ </EmptyState>
74
+ ) : (
75
+ <div className="dt-wrap">
76
+ <table className="dt syncs stacks">
77
+ <thead>
78
+ <tr>
79
+ <th>Destination</th>
80
+ <th className="col-shrink">Status</th>
81
+ <th className="col-shrink col-hide-narrow">Token</th>
82
+ <th className="col-actions">
83
+ <span className="visually-hidden">Actions</span>
84
+ </th>
85
+ </tr>
86
+ </thead>
87
+ <tbody>
88
+ {result.syncs.map((sync) => (
89
+ <SyncRow key={sync.id} sync={sync} canRun={canRun} canManage={canManage} />
90
+ ))}
91
+ </tbody>
92
+ </table>
93
+ </div>
94
+ )}
95
+ </Card>
96
+ );
97
+ }
98
+
99
+ function SyncRow({ sync, canRun, canManage }: { sync: SyncView; canRun: boolean; canManage: boolean }) {
100
+ const [removing, setRemoving] = useState(false);
101
+ const coffre = useCoffre();
102
+ // Apart, so pausing does not spin the Run now button.
103
+ const pushing = useAction();
104
+ const managing = useAction();
105
+ const pending = pushing.pending || managing.pending;
106
+ const error = pushing.error ?? managing.error;
107
+
108
+ function runNow() {
109
+ pushing.run(
110
+ () => coffre.syncs.run(sync.id),
111
+ (result) => announce(sync, result.outcome),
112
+ );
113
+ }
114
+
115
+ function setPaused(paused: boolean) {
116
+ managing.run(
117
+ () => coffre.syncs.update(sync.id, { paused }),
118
+ () =>
119
+ toast.success(paused ? `Paused ${sync.destination}` : `Resumed ${sync.destination}`, {
120
+ action: { label: 'Undo', onClick: () => setPaused(!paused) },
121
+ }),
122
+ );
123
+ }
124
+
125
+ // A sync whose provider the deployment stopped listing cannot run, and
126
+ // its last error predates that: what it is now says it all.
127
+ const shownError = error ?? (sync.offered ? sync.lastError : null);
128
+
129
+ return (
130
+ <>
131
+ <tr className={shownError !== null && !sync.running ? 'has-error' : undefined}>
132
+ <td className="col-destination">
133
+ <span className="cell-account">
134
+ <SyncMark brand={sync.brand} size={15} />
135
+ <span className="cell-stack">
136
+ <span>{sync.providerLabel}</span>
137
+ {sync.offered ? (
138
+ <small className="mono">{sync.destination}</small>
139
+ ) : (
140
+ <small>No longer offered by this deployment</small>
141
+ )}
142
+ </span>
143
+ </span>
144
+ </td>
145
+ <td className="nowrap col-status">
146
+ <span className="cell-stack">
147
+ <SyncState sync={sync} />
148
+ <small>
149
+ <Counts sync={sync} />
150
+ </small>
151
+ </span>
152
+ </td>
153
+ <td className="col-shrink col-hide-narrow cell-mono cell-muted">{sync.credential}</td>
154
+ <td className="col-actions">
155
+ <div className="acts">
156
+ {canRun && !sync.paused && (
157
+ <button
158
+ className="act"
159
+ onClick={runNow}
160
+ disabled={pending || sync.running || !sync.offered}
161
+ title={sync.offered ? undefined : 'This deployment no longer offers this provider'}
162
+ >
163
+ {pushing.pending ? <Spinner size={13} /> : <Sync size={13} />}
164
+ Run now
165
+ </button>
166
+ )}
167
+ {canManage && (
168
+ <DropdownMenu.Root>
169
+ <DropdownMenu.Trigger asChild>
170
+ <button
171
+ className="act act-icon act-quiet"
172
+ aria-label={`More for ${sync.destination}`}
173
+ disabled={pending}
174
+ >
175
+ <MoreHorizontal size={16} />
176
+ </button>
177
+ </DropdownMenu.Trigger>
178
+ <DropdownMenu.Portal>
179
+ <DropdownMenu.Content className="menu" sideOffset={6} align="end">
180
+ <DropdownMenu.Item className="menu-item" onSelect={() => setPaused(!sync.paused)}>
181
+ {sync.paused ? <Play size={14} /> : <Pause size={14} />}
182
+ {sync.paused ? 'Resume' : 'Pause'}
183
+ </DropdownMenu.Item>
184
+ <DropdownMenu.Separator className="menu-sep" />
185
+ <DropdownMenu.Item
186
+ className="menu-item menu-item-danger"
187
+ onSelect={() => setRemoving(true)}
188
+ >
189
+ <X size={14} />
190
+ Remove
191
+ </DropdownMenu.Item>
192
+ </DropdownMenu.Content>
193
+ </DropdownMenu.Portal>
194
+ </DropdownMenu.Root>
195
+ )}
196
+ </div>
197
+ <ConfirmDialog
198
+ open={removing}
199
+ onOpenChange={setRemoving}
200
+ title={
201
+ <>
202
+ Stop syncing to <span className="mono">{sync.destination}</span>?
203
+ </>
204
+ }
205
+ body={`Nothing more is pushed to ${sync.providerLabel}. Keys coffre already pushed stay there; remove them at ${sync.providerLabel} if they should go too.`}
206
+ confirmLabel="Remove sync"
207
+ onConfirm={() =>
208
+ managing.run(
209
+ () => coffre.syncs.remove(sync.id),
210
+ () => toast.success(`No longer syncing to ${sync.destination}`),
211
+ )
212
+ }
213
+ />
214
+ </td>
215
+ </tr>
216
+ {shownError !== null && !sync.running && (
217
+ <tr className="row-error">
218
+ <td colSpan={4}>
219
+ <ErrorLine error={shownError} />
220
+ </td>
221
+ </tr>
222
+ )}
223
+ </>
224
+ );
225
+ }
226
+
227
+ /** One tag for the state that matters most right now. */
228
+ function SyncState({ sync }: { sync: SyncView }) {
229
+ if (!sync.offered) return <span className="tag tag-outline">Unavailable</span>;
230
+ if (sync.running) {
231
+ return (
232
+ <span className="tag tag-blue">
233
+ <Spinner size={11} />
234
+ Pushing
235
+ </span>
236
+ );
237
+ }
238
+ if (sync.paused) return <span className="tag tag-outline">Paused</span>;
239
+ if (sync.lastStatus === 'failed') return <span className="tag tag-red">Failed</span>;
240
+ if (sync.pending > 0) return <span className="tag tag-amber">{sync.pending} pending</span>;
241
+ if (sync.lastStatus === 'partial') return <span className="tag tag-amber">Partial</span>;
242
+ if (sync.lastRunAt === null) return <span className="tag">Not run yet</span>;
243
+ return <span className="tag tag-green">In sync</span>;
244
+ }
245
+
246
+ function Counts({ sync }: { sync: SyncView }) {
247
+ const skipped = sync.skipped.length;
248
+ const parts: ReactNode[] = [];
249
+ if (sync.synced > 0 || sync.pending === 0) parts.push(`${sync.synced} synced`);
250
+ if (sync.pending > 0) parts.push(`${sync.pending} pending`);
251
+ if (skipped > 0) {
252
+ parts.push(
253
+ <Toggletip
254
+ label={
255
+ <ul style={{ margin: 0, paddingLeft: '1rem' }}>
256
+ {sync.skipped.map((entry) => (
257
+ <li key={entry.key}>
258
+ <span className="mono">{entry.key}</span>: {entry.reason}
259
+ </li>
260
+ ))}
261
+ </ul>
262
+ }
263
+ >
264
+ <button type="button" className="tag tag-amber tag-button">
265
+ <AlertTriangle size={11} />
266
+ {skipped} skipped
267
+ </button>
268
+ </Toggletip>,
269
+ );
270
+ }
271
+ if (sync.lastRunAt !== null) parts.push(<Timestamp iso={sync.lastRunAt} display="relative" />);
272
+ return (
273
+ <>
274
+ {parts.map((part, index) => (
275
+ <Fragment key={index}>
276
+ {index > 0 && ' · '}
277
+ {part}
278
+ </Fragment>
279
+ ))}
280
+ </>
281
+ );
282
+ }
283
+
284
+ function announce(sync: SyncView, outcome: RunOutcome) {
285
+ if (outcome.status === 'busy') {
286
+ toast('A run is already under way', { description: 'Its result appears here when it ends.' });
287
+ return;
288
+ }
289
+ const changed = outcome.upserted.length + outcome.deleted.length;
290
+ if (outcome.status === 'failed') {
291
+ toast.error(`Could not sync to ${sync.destination}`, { description: outcome.error ?? undefined });
292
+ } else if (outcome.status === 'partial') {
293
+ toast.warning(`${outcome.failed.length} of ${changed + outcome.failed.length} changes failed`, {
294
+ description: outcome.failed[0]?.message,
295
+ });
296
+ } else if (changed === 0) {
297
+ toast.success(`${sync.destination} was already current`);
298
+ } else {
299
+ const parts = [
300
+ outcome.upserted.length > 0 && `pushed ${outcome.upserted.length}`,
301
+ outcome.deleted.length > 0 && `removed ${outcome.deleted.length}`,
302
+ ].filter(Boolean);
303
+ toast.success(`${capitalize(parts.join(', '))} to ${sync.destination}`);
304
+ }
305
+ }
306
+
307
+ /**
308
+ * A new sync pushes in the background once it is made. Say so while it does,
309
+ * then say how it went: a green toast beside a row already marked Failed
310
+ * would be telling two stories.
311
+ */
312
+ async function followFirstRun(
313
+ coffre: CoffreClient,
314
+ path: string,
315
+ sync: SyncView,
316
+ refresh: () => Promise<void>,
317
+ ) {
318
+ const id = toast.loading(`Pushing to ${sync.destination}`, { description: 'The first push is under way.' });
319
+ const deadline = Date.now() + FIRST_RUN_WAIT_MS;
320
+ while (Date.now() < deadline) {
321
+ await new Promise((resolve) => setTimeout(resolve, RUNNING_POLL_MS));
322
+ const listed = await uiResult(() => coffre.syncs.list(path));
323
+ const now = listed.ok ? listed.syncs.find((other) => other.id === sync.id) : undefined;
324
+ if (now === undefined || now.running || now.lastRunAt === null) continue;
325
+ await refresh();
326
+ if (now.lastStatus === 'failed') {
327
+ toast.error(`Could not sync to ${sync.destination}`, { id, description: now.lastError ?? undefined });
328
+ } else if (now.lastStatus === 'partial') {
329
+ toast.warning(`Synced to ${sync.destination}, but not every key`, { id, description: now.lastError ?? undefined });
330
+ } else {
331
+ toast.success(`Synced to ${sync.destination}`, {
332
+ id,
333
+ description: `${now.synced} ${now.synced === 1 ? 'key is' : 'keys are'} there now.`,
334
+ });
335
+ }
336
+ return;
337
+ }
338
+ await refresh();
339
+ toast(`Still pushing to ${sync.destination}`, { id, description: 'Its result appears in the list when it ends.' });
340
+ }
341
+
342
+ function capitalize(text: string): string {
343
+ return text.charAt(0).toUpperCase() + text.slice(1);
344
+ }
345
+
346
+ function AddSync({ project, environment }: { project: string; environment: string }) {
347
+ const [open, setOpen] = useState(false);
348
+
349
+ return (
350
+ <>
351
+ <button className="btn btn-sm" onClick={() => setOpen(true)}>
352
+ <Plus size={14} />
353
+ Add sync
354
+ </button>
355
+ <Modal
356
+ open={open}
357
+ onOpenChange={setOpen}
358
+ title={
359
+ <>
360
+ Sync <span className="mono">{project}/{environment}</span>
361
+ </>
362
+ }
363
+ description="Every key in this environment is pushed to the destination and kept current. coffre only ever removes keys it pushed itself."
364
+ wide
365
+ >
366
+ {/* Mounted while open, so every opening starts from a blank form. */}
367
+ <SyncProviders>
368
+ {(providers) => (
369
+ <AddSyncForm
370
+ project={project}
371
+ environment={environment}
372
+ providers={providers}
373
+ onDone={() => setOpen(false)}
374
+ />
375
+ )}
376
+ </SyncProviders>
377
+ </Modal>
378
+ </>
379
+ );
380
+ }
381
+
382
+ type Offered = { ok: true; providers: SyncProviderInfo[] } | { ok: false; error: string };
383
+
384
+ /** Where this instance can sync to, as the deployment lists it. */
385
+ function SyncProviders({ children }: { children: (providers: SyncProviderInfo[]) => ReactNode }) {
386
+ const coffre = useCoffre();
387
+ const [offered, setOffered] = useState<Offered | null>(null);
388
+
389
+ useEffect(() => {
390
+ let live = true;
391
+ void uiResult(() => coffre.syncs.providers()).then((result) => {
392
+ if (live) setOffered(result);
393
+ });
394
+ return () => {
395
+ live = false;
396
+ };
397
+ }, [coffre]);
398
+
399
+ if (offered === null) {
400
+ return (
401
+ <p className="picker-empty sync-providers-state">
402
+ <Spinner size={13} />
403
+ Loading where coffre can sync to…
404
+ </p>
405
+ );
406
+ }
407
+ if (!offered.ok) {
408
+ return (
409
+ <div className="sync-providers-state">
410
+ <ErrorLine error={offered.error} />
411
+ </div>
412
+ );
413
+ }
414
+ if (offered.providers.length === 0) {
415
+ return (
416
+ <p className="picker-empty sync-providers-state">
417
+ This instance offers nowhere to sync to. Whoever runs coffre lists the providers in its{' '}
418
+ <span className="mono">syncs</span> settings.
419
+ </p>
420
+ );
421
+ }
422
+ return children(offered.providers);
423
+ }
424
+
425
+ function AddSyncForm({
426
+ project,
427
+ environment,
428
+ providers,
429
+ onDone,
430
+ }: {
431
+ project: string;
432
+ environment: string;
433
+ providers: SyncProviderInfo[];
434
+ onDone: () => void;
435
+ }) {
436
+ const [provider, setProvider] = useState(providers[0]);
437
+ const [values, setValues] = useState<FormValues>(() => initialValues(providers[0]));
438
+ const [credential, setCredential] = useState('');
439
+ const coffre = useCoffre();
440
+ const router = useRouter();
441
+ const { pending, error, setError, run } = useAction();
442
+ const missing = firstMissing(provider, values);
443
+
444
+ function pick(next: SyncProviderInfo) {
445
+ setProvider(next);
446
+ setValues(initialValues(next));
447
+ setError(null);
448
+ }
449
+
450
+ function set(name: string, value: string | string[]) {
451
+ setValues((current) => ({ ...current, [name]: value }));
452
+ }
453
+
454
+ return (
455
+ <form
456
+ className="form"
457
+ style={{ marginTop: '1.25rem' }}
458
+ onSubmit={(event) => {
459
+ event.preventDefault();
460
+ run(
461
+ () =>
462
+ coffre.syncs.add(`${project}/${environment}`, {
463
+ provider: provider.id,
464
+ config: configFromForm(provider, values),
465
+ credential: credential.trim(),
466
+ }),
467
+ (sync) => {
468
+ onDone();
469
+ void followFirstRun(coffre, `${project}/${environment}`, sync, () => router.invalidate());
470
+ },
471
+ );
472
+ }}
473
+ >
474
+ {providers.length > 1 && (
475
+ <div className="choice-grid sync-providers" role="group" aria-label="Destination">
476
+ {providers.map((option) => (
477
+ <button
478
+ key={option.id}
479
+ type="button"
480
+ className="choice"
481
+ aria-pressed={option.id === provider.id}
482
+ onClick={() => pick(option)}
483
+ >
484
+ <SyncMark brand={option.brand} />
485
+ {option.label}
486
+ </button>
487
+ ))}
488
+ </div>
489
+ )}
490
+
491
+ <div className="form-row">
492
+ {provider.fields.map((field) => {
493
+ if (!isAsked(field, values)) return null;
494
+ if (field.type === 'text') {
495
+ return (
496
+ <label key={`${provider.id}.${field.name}`} className="field">
497
+ <span className="label">
498
+ {field.label}
499
+ {field.optional === true && <span className="hint"> (optional)</span>}
500
+ </span>
501
+ <input
502
+ className="input input-mono"
503
+ value={values[field.name] as string}
504
+ placeholder={field.placeholder}
505
+ spellCheck={false}
506
+ autoComplete="off"
507
+ onChange={(event) => set(field.name, event.target.value)}
508
+ />
509
+ {field.hint !== undefined && <span className="hint">{field.hint}</span>}
510
+ </label>
511
+ );
512
+ }
513
+ const picked = values[field.name] as string[];
514
+ return (
515
+ <div key={`${provider.id}.${field.name}`} className="field field-options" role="group" aria-label={field.label}>
516
+ <span className="label">{field.label}</span>
517
+ <div className="segmented">
518
+ {field.options.map((option) => (
519
+ <button
520
+ key={option.value}
521
+ type="button"
522
+ aria-pressed={picked.includes(option.value)}
523
+ onClick={() =>
524
+ set(
525
+ field.name,
526
+ !field.multiple
527
+ ? [option.value]
528
+ : picked.includes(option.value)
529
+ ? picked.filter((value) => value !== option.value)
530
+ : [...picked, option.value],
531
+ )
532
+ }
533
+ >
534
+ {option.label}
535
+ </button>
536
+ ))}
537
+ </div>
538
+ {field.hint !== undefined && <span className="hint">{field.hint}</span>}
539
+ </div>
540
+ );
541
+ })}
542
+ </div>
543
+
544
+ <label className="field">
545
+ <span className="label">Token</span>
546
+ <input
547
+ className="input input-mono"
548
+ value={credential}
549
+ placeholder={provider.credential.placeholder}
550
+ spellCheck={false}
551
+ autoComplete="off"
552
+ onChange={(event) => setCredential(event.target.value)}
553
+ />
554
+ <span className="hint">
555
+ The secret that holds it, as <code>project/environment/KEY</code>. An environment of
556
+ its own, such as <code>ops/sync</code>, keeps it from everyone who reads these
557
+ secrets. You need read access to it.
558
+ </span>
559
+ </label>
560
+
561
+ <Notice tone="info">{provider.credential.hint}</Notice>
562
+
563
+ <ErrorLine error={error} />
564
+
565
+ <div className="dialog-actions">
566
+ <button className="btn" type="button" onClick={onDone}>
567
+ Cancel
568
+ </button>
569
+ <button
570
+ className="btn btn-primary"
571
+ type="submit"
572
+ disabled={pending || missing !== null || credential.trim() === ''}
573
+ title={missing !== null ? `${missing} is required` : undefined}
574
+ >
575
+ {pending && <Spinner />}
576
+ Start syncing
577
+ </button>
578
+ </div>
579
+ </form>
580
+ );
581
+ }