@rapidmx/web-client 0.6.0 → 0.7.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 (79) hide show
  1. package/apps/shared/components/admin/settings/BrandingForm.tsx +398 -385
  2. package/apps/shared/components/admin/settings/EncryptionPolicyForm.tsx +119 -112
  3. package/apps/shared/components/admin/settings/MailboxPolicyForm.tsx +159 -152
  4. package/apps/shared/components/admin/settings/PluginsManager.tsx +1288 -1235
  5. package/apps/shared/components/admin/settings/RetentionPolicyForm.tsx +9 -1
  6. package/apps/shared/components/admin/setup/EscrowSetupStep.tsx +288 -290
  7. package/apps/shared/components/admin/setup/SetupWizard.tsx +443 -416
  8. package/apps/shared/components/layout/AppShell.tsx +288 -288
  9. package/apps/shared/components/mail/ConversationList.tsx +225 -33
  10. package/apps/shared/components/mail/ConversationThreadPane.tsx +434 -216
  11. package/apps/shared/components/mail/MailListToolbar.tsx +265 -0
  12. package/apps/shared/components/mail/MailSelectionBar.tsx +222 -0
  13. package/apps/shared/components/mail/MenuButton.tsx +384 -0
  14. package/apps/shared/components/mail/MessageDetailPane.tsx +280 -215
  15. package/apps/shared/components/mail/MoveToFolderDialog.tsx +263 -0
  16. package/apps/shared/components/mail/compose/ComposeContext.tsx +29 -2
  17. package/apps/shared/components/mail/compose/ComposeWindow.tsx +1700 -1652
  18. package/apps/shared/components/mail/compose/RecipientInput.tsx +356 -0
  19. package/apps/shared/components/mail/compose/RichTextEditor.tsx +38 -3
  20. package/apps/shared/components/mail/compose/quotedBody.ts +100 -0
  21. package/apps/shared/components/mail/compose/recipients.ts +104 -0
  22. package/apps/shared/components/mail/labelMenu.tsx +278 -0
  23. package/apps/shared/components/mail/layout/MailShell.tsx +445 -420
  24. package/apps/shared/components/mail/listPreferences.ts +228 -0
  25. package/apps/shared/components/settings/layout/SettingsShell.tsx +0 -1
  26. package/apps/shared/styles/app.css +28 -5
  27. package/apps/www/index.tsx +1854 -1314
  28. package/apps/www/messages/[uid].tsx +5 -4
  29. package/dist/apps/shared/components/admin/settings/BrandingForm.d.ts +7 -3
  30. package/dist/apps/shared/components/admin/settings/BrandingForm.js +6 -5
  31. package/dist/apps/shared/components/admin/settings/EncryptionPolicyForm.d.ts +4 -1
  32. package/dist/apps/shared/components/admin/settings/EncryptionPolicyForm.js +2 -2
  33. package/dist/apps/shared/components/admin/settings/MailboxPolicyForm.d.ts +4 -1
  34. package/dist/apps/shared/components/admin/settings/MailboxPolicyForm.js +2 -2
  35. package/dist/apps/shared/components/admin/settings/PluginsManager.d.ts +8 -1
  36. package/dist/apps/shared/components/admin/settings/PluginsManager.js +33 -14
  37. package/dist/apps/shared/components/admin/settings/RetentionPolicyForm.d.ts +4 -1
  38. package/dist/apps/shared/components/admin/settings/RetentionPolicyForm.js +2 -2
  39. package/dist/apps/shared/components/admin/setup/EscrowSetupStep.js +1 -1
  40. package/dist/apps/shared/components/admin/setup/SetupWizard.js +14 -10
  41. package/dist/apps/shared/components/mail/ConversationList.d.ts +42 -6
  42. package/dist/apps/shared/components/mail/ConversationList.js +84 -10
  43. package/dist/apps/shared/components/mail/ConversationThreadPane.d.ts +52 -13
  44. package/dist/apps/shared/components/mail/ConversationThreadPane.js +217 -60
  45. package/dist/apps/shared/components/mail/MailListToolbar.d.ts +55 -0
  46. package/dist/apps/shared/components/mail/MailListToolbar.js +149 -0
  47. package/dist/apps/shared/components/mail/MailSelectionBar.d.ts +59 -0
  48. package/dist/apps/shared/components/mail/MailSelectionBar.js +48 -0
  49. package/dist/apps/shared/components/mail/MenuButton.d.ts +71 -0
  50. package/dist/apps/shared/components/mail/MenuButton.js +211 -0
  51. package/dist/apps/shared/components/mail/MessageDetailPane.d.ts +27 -19
  52. package/dist/apps/shared/components/mail/MessageDetailPane.js +126 -129
  53. package/dist/apps/shared/components/mail/MoveToFolderDialog.d.ts +57 -0
  54. package/dist/apps/shared/components/mail/MoveToFolderDialog.js +138 -0
  55. package/dist/apps/shared/components/mail/compose/ComposeContext.d.ts +16 -1
  56. package/dist/apps/shared/components/mail/compose/ComposeContext.js +3 -1
  57. package/dist/apps/shared/components/mail/compose/ComposeWindow.js +52 -20
  58. package/dist/apps/shared/components/mail/compose/RecipientInput.d.ts +36 -0
  59. package/dist/apps/shared/components/mail/compose/RecipientInput.js +203 -0
  60. package/dist/apps/shared/components/mail/compose/RichTextEditor.d.ts +10 -1
  61. package/dist/apps/shared/components/mail/compose/RichTextEditor.js +21 -3
  62. package/dist/apps/shared/components/mail/compose/quotedBody.d.ts +37 -0
  63. package/dist/apps/shared/components/mail/compose/quotedBody.js +85 -0
  64. package/dist/apps/shared/components/mail/compose/recipients.d.ts +24 -0
  65. package/dist/apps/shared/components/mail/compose/recipients.js +87 -0
  66. package/dist/apps/shared/components/mail/labelMenu.d.ts +97 -0
  67. package/dist/apps/shared/components/mail/labelMenu.js +134 -0
  68. package/dist/apps/shared/components/mail/layout/MailShell.d.ts +8 -0
  69. package/dist/apps/shared/components/mail/layout/MailShell.js +15 -3
  70. package/dist/apps/shared/components/mail/listPreferences.d.ts +101 -0
  71. package/dist/apps/shared/components/mail/listPreferences.js +186 -0
  72. package/dist/apps/shared/components/settings/layout/SettingsShell.js +0 -1
  73. package/dist/apps/shared/styles/app.css +28 -5
  74. package/dist/apps/www/index.js +506 -112
  75. package/dist/apps/www/messages/[uid].js +2 -3
  76. package/package.json +2 -2
  77. package/apps/www/settings/focused-inbox/index.tsx +0 -150
  78. package/dist/apps/www/settings/focused-inbox/index.d.ts +0 -4
  79. package/dist/apps/www/settings/focused-inbox/index.js +0 -71
@@ -22,11 +22,15 @@ export default function RetentionPolicyForm({
22
22
  policy,
23
23
  onChange,
24
24
  onDirtyChange,
25
+ embedded = false,
25
26
  }: {
26
27
  policy: RetentionPolicy;
27
28
  onChange: (p: RetentionPolicy) => void;
28
29
  /** Told whether the form has edits that haven't been saved. */
29
30
  onDirtyChange?: (dirty: boolean) => void;
31
+ /** Set when shown as one section of a page with its own heading (the setup wizard), so the title is a smaller,
32
+ * lower-level heading. */
33
+ embedded?: boolean;
30
34
  }) {
31
35
  const [messageRetentionDays, setMessageRetentionDays] = useState(policy.messageRetentionDays?.toString() ?? "");
32
36
  const [auditLogRetentionDays, setAuditLogRetentionDays] = useState(policy.auditLogRetentionDays?.toString() ?? "");
@@ -84,7 +88,11 @@ export default function RetentionPolicyForm({
84
88
 
85
89
  return (
86
90
  <div className="max-w-xl">
87
- <h1 className="text-xl font-bold uppercase tracking-wide mb-1">Retention Policy</h1>
91
+ {embedded ? (
92
+ <h3 className="text-base font-bold uppercase tracking-wide mb-1">Retention policy</h3>
93
+ ) : (
94
+ <h1 className="text-xl font-bold uppercase tracking-wide mb-1">Retention Policy</h1>
95
+ )}
88
96
  <p className="text-sm text-text-muted mb-5">
89
97
  Automatically and permanently deletes mail/audit-log entries older than the configured age.
90
98
  Leave a field blank for no automatic purge. This does not apply to a mailbox that is a
@@ -1,290 +1,288 @@
1
- ///////////////////////////////////////////////////////////////////////////////
2
- // Copyright (C) 2026 Jean-Philippe Steinmetz
3
- // SPDX-License-Identifier: MPL-2.0
4
- ///////////////////////////////////////////////////////////////////////////////
5
- import React, { FormEvent, useEffect, useState } from "react";
6
- import { ApiRequestError } from "@rapidmx/react-shared/util/api.js";
7
- import { createEscrowScope, EscrowScope, listEscrowScopes } from "@rapidmx/react-shared/admin/escrowScopesApi.js";
8
- import { generateEscrowKeyPair, GeneratedEscrowKeys } from "@rapidmx/react-shared/crypto/escrowKeys.js";
9
- import { toDatetimeLocal } from "@rapidmx/react-shared/util/dateInput.js";
10
- import Alert from "@rapidmx/react-shared/components/feedback/Alert.js";
11
- import Button from "@rapidmx/react-shared/components/buttons/Button.js";
12
- import FormField from "@rapidmx/react-shared/components/forms/FormField.js";
13
- import EscrowScopeKeyAndHoldersFields, {
14
- emptyEscrowScopeKeyAndHoldersValue,
15
- EscrowScopeKeyAndHoldersValue,
16
- selfAsHolderError,
17
- } from "../escrowScopes/EscrowScopeKeyAndHoldersFields.js";
18
-
19
- const INPUT_CLASS =
20
- "w-full text-sm py-2.5 px-3 border border-border rounded-sm bg-surface text-text focus:outline-none focus:border-primary";
21
-
22
- type Mode = "none" | "generate" | "existing";
23
-
24
- /** How long a download's object URL is kept, since some browsers are still reading it after the click returns. */
25
- export const DOWNLOAD_URL_LIFETIME_MS = 60_000;
26
-
27
- /** Offers `text` to the browser as a file download. */
28
- export function downloadTextFile(filename: string, text: string, type: string = "application/x-pem-file"): void {
29
- const url = URL.createObjectURL(new Blob([text], { type }));
30
- const link = document.createElement("a");
31
- link.href = url;
32
- link.download = filename;
33
- document.body.appendChild(link);
34
- link.click();
35
- link.remove();
36
- setTimeout(() => URL.revokeObjectURL(url), DOWNLOAD_URL_LIFETIME_MS);
37
- }
38
-
39
- function fileSafe(name: string): string {
40
- return name.trim().replace(/[^A-Za-z0-9._-]+/g, "-").replace(/^-+|-+$/g, "") || "escrow";
41
- }
42
-
43
- /**
44
- * The setup wizard's escrow step. Escrow lets a separate group of holders recover a mailbox's encrypted mail (for
45
- * example for legal discovery) using a key this server never has. It's optional, and the administrator can either
46
- * generate the key pair here (downloading the private key, which never leaves the browser), paste in an existing
47
- * certificate, or go without escrow.
48
- */
49
- export default function EscrowSetupStep({ adminUid }: { adminUid?: string }) {
50
- const [scopes, setScopes] = useState<EscrowScope[] | null>(null);
51
- const [mode, setMode] = useState<Mode>("none");
52
- const [name, setName] = useState("Escrow");
53
- const [validYears, setValidYears] = useState(5);
54
- const [generated, setGenerated] = useState<GeneratedEscrowKeys | null>(null);
55
- const [downloadedPrivateKey, setDownloadedPrivateKey] = useState(false);
56
- const [savedPrivateKey, setSavedPrivateKey] = useState(false);
57
- const [keyAndHolders, setKeyAndHolders] = useState<EscrowScopeKeyAndHoldersValue>(emptyEscrowScopeKeyAndHoldersValue());
58
- const [error, setError] = useState<string | null>(null);
59
- const [busy, setBusy] = useState(false);
60
-
61
- useEffect(() => {
62
- listEscrowScopes({ limit: 100 })
63
- .then(setScopes)
64
- .catch(() => setScopes([]));
65
- }, []);
66
-
67
- function chooseMode(next: Mode) {
68
- setMode(next);
69
- setError(null);
70
- setGenerated(null);
71
- setDownloadedPrivateKey(false);
72
- setSavedPrivateKey(false);
73
- setKeyAndHolders(emptyEscrowScopeKeyAndHoldersValue());
74
- }
75
-
76
- async function generate() {
77
- if (!name.trim()) {
78
- setError("Give the escrow scope a name first.");
79
- return;
80
- }
81
- setBusy(true);
82
- setError(null);
83
- try {
84
- const keys = await generateEscrowKeyPair({ name: name.trim(), validDays: validYears * 365 });
85
- setGenerated(keys);
86
- setKeyAndHolders({
87
- ...emptyEscrowScopeKeyAndHoldersValue(),
88
- publicKey: keys.publicKey.publicKey,
89
- keyType: keys.publicKey.type,
90
- fingerprint: keys.publicKey.fingerprint,
91
- notBefore: toDatetimeLocal(new Date(keys.publicKey.notBefore).toISOString()),
92
- notAfter: toDatetimeLocal(new Date(keys.publicKey.notAfter).toISOString()),
93
- });
94
- } catch {
95
- setError("This browser couldn't generate the escrow keys.");
96
- } finally {
97
- setBusy(false);
98
- }
99
- }
100
-
101
- async function create(e: FormEvent) {
102
- e.preventDefault();
103
- setError(null);
104
- if (!name.trim()) {
105
- setError("A name is required.");
106
- return;
107
- }
108
- // Generated keys are used exactly as generated; only a pasted-in certificate's fields come from the form.
109
- const publicKey = generated
110
- ? generated.publicKey
111
- : {
112
- publicKey: keyAndHolders.publicKey.trim(),
113
- type: keyAndHolders.keyType.trim(),
114
- fingerprint: keyAndHolders.fingerprint.trim(),
115
- notBefore: new Date(keyAndHolders.notBefore).getTime(),
116
- notAfter: new Date(keyAndHolders.notAfter).getTime(),
117
- };
118
- if (!publicKey.publicKey || !publicKey.type || !publicKey.fingerprint) {
119
- setError("The public key, its type, and its fingerprint are all required.");
120
- return;
121
- }
122
- if (keyAndHolders.holderUserUids.length === 0) {
123
- setError("At least one holder is required.");
124
- return;
125
- }
126
- if (keyAndHolders.requiredHolders < 1 || keyAndHolders.requiredHolders > keyAndHolders.holderUserUids.length) {
127
- setError("Required holders must be between 1 and the number of holders.");
128
- return;
129
- }
130
- const selfError = selfAsHolderError(keyAndHolders.holderUserUids, adminUid);
131
- if (selfError) {
132
- setError(selfError);
133
- return;
134
- }
135
- setBusy(true);
136
- try {
137
- const created = await createEscrowScope({
138
- name: name.trim(),
139
- publicKey,
140
- holderUserUids: keyAndHolders.holderUserUids,
141
- requiredHolders: keyAndHolders.requiredHolders,
142
- notifySubjectOnAccess: keyAndHolders.notifySubjectOnAccess,
143
- });
144
- setScopes((prev) => [...(prev ?? []), created]);
145
- chooseMode("none");
146
- setName("Escrow");
147
- } catch (err) {
148
- setError(err instanceof ApiRequestError ? err.message : "Could not create the escrow scope.");
149
- } finally {
150
- setBusy(false);
151
- }
152
- }
153
-
154
- const scopeForm = (
155
- <form onSubmit={create} className="flex flex-col gap-5">
156
- <EscrowScopeKeyAndHoldersFields
157
- value={keyAndHolders}
158
- onChange={setKeyAndHolders}
159
- disabled={mode === "generate" && busy}
160
- keyReadOnly={mode === "generate"}
161
- />
162
- <div>
163
- <Button type="submit" loading={busy} disabled={busy} className="!w-auto">
164
- Create escrow scope
165
- </Button>
166
- </div>
167
- </form>
168
- );
169
-
170
- return (
171
- <div className="flex flex-col gap-5 max-w-3xl">
172
- <div>
173
- <h2 className="text-lg font-bold uppercase tracking-wide mb-1">Escrow</h2>
174
- <p className="text-sm text-text-muted">
175
- Escrow lets a group of trusted holders - for example your legal or compliance team - recover a
176
- mailbox&rsquo;s encrypted mail when they all agree to. It&rsquo;s optional. Without it, mail that
177
- someone encrypts can&rsquo;t be recovered if they lose their keys, by anyone.
178
- </p>
179
- </div>
180
-
181
- {scopes && scopes.length > 0 && (
182
- <div role="status" className="text-sm py-2 px-3 rounded-sm bg-surface-alt text-text">
183
- Escrow scopes set up: {scopes.map((scope) => scope.name).join(", ")}.
184
- </div>
185
- )}
186
-
187
- {error && <Alert>{error}</Alert>}
188
-
189
- <fieldset className="flex flex-col gap-2 text-sm">
190
- <legend className="font-semibold mb-1">How do you want to set up escrow?</legend>
191
- <label className="flex items-center gap-2">
192
- <input type="radio" name="escrow-mode" checked={mode === "generate"} onChange={() => chooseMode("generate")} />
193
- Generate new escrow keys in this browser
194
- </label>
195
- <label className="flex items-center gap-2">
196
- <input type="radio" name="escrow-mode" checked={mode === "existing"} onChange={() => chooseMode("existing")} />
197
- Use a certificate I already have
198
- </label>
199
- <label className="flex items-center gap-2">
200
- <input type="radio" name="escrow-mode" checked={mode === "none"} onChange={() => chooseMode("none")} />
201
- {scopes && scopes.length > 0 ? "Don't add another escrow scope" : "Don't use escrow"}
202
- </label>
203
- </fieldset>
204
-
205
- {mode !== "none" && (
206
- <div className="bg-surface border border-border rounded-md p-6">
207
- <FormField label="Escrow scope name" htmlFor="escrowName">
208
- <input
209
- id="escrowName"
210
- type="text"
211
- className={INPUT_CLASS}
212
- value={name}
213
- disabled={!!generated}
214
- onChange={(e) => setName(e.target.value)}
215
- />
216
- </FormField>
217
- {mode === "generate" && !generated && (
218
- <>
219
- <FormField label="Keys valid for" htmlFor="escrowValidYears">
220
- <select
221
- id="escrowValidYears"
222
- className={INPUT_CLASS}
223
- value={validYears}
224
- onChange={(e) => setValidYears(Number(e.target.value))}
225
- >
226
- {[1, 2, 5, 10].map((years) => (
227
- <option key={years} value={years}>
228
- {years} {years === 1 ? "year" : "years"}
229
- </option>
230
- ))}
231
- </select>
232
- </FormField>
233
- <Button type="button" className="!w-auto" loading={busy} disabled={busy} onClick={() => void generate()}>
234
- Generate keys
235
- </Button>
236
- </>
237
- )}
238
- {mode === "generate" && generated && (
239
- <div className="flex flex-col gap-3 text-sm">
240
- <Alert>
241
- Download the private key now and give it to your escrow holders. It isn&rsquo;t stored
242
- anywhere else - if it&rsquo;s lost, no escrowed mail can ever be recovered.
243
- </Alert>
244
- <Alert>
245
- You&rsquo;re downloading this private key, so you must not also be one of its holders:
246
- pick holders other than yourself, hand the key over, and delete your copy once they
247
- have it. Otherwise one person could both configure escrow and recover mail with it.
248
- </Alert>
249
- <div className="flex flex-wrap gap-2">
250
- <Button
251
- type="button"
252
- className="!w-auto"
253
- onClick={() => {
254
- downloadTextFile(`${fileSafe(name)}-private-key.pem`, generated.privateKeyPem);
255
- setDownloadedPrivateKey(true);
256
- }}
257
- >
258
- Download private key
259
- </Button>
260
- <Button
261
- type="button"
262
- variant="secondary"
263
- className="!w-auto"
264
- onClick={() => downloadTextFile(`${fileSafe(name)}-certificate.pem`, generated.certificatePem)}
265
- >
266
- Download certificate
267
- </Button>
268
- </div>
269
- <label className="flex items-center gap-2">
270
- <input
271
- type="checkbox"
272
- checked={savedPrivateKey}
273
- disabled={!downloadedPrivateKey}
274
- onChange={(e) => setSavedPrivateKey(e.target.checked)}
275
- />
276
- I&rsquo;ve saved the private key somewhere safe
277
- </label>
278
- {!downloadedPrivateKey && (
279
- <p className="text-xs text-text-muted">Download the private key before continuing.</p>
280
- )}
281
- </div>
282
- )}
283
- </div>
284
- )}
285
-
286
- {mode === "existing" && scopeForm}
287
- {mode === "generate" && generated && savedPrivateKey && scopeForm}
288
- </div>
289
- );
290
- }
1
+ ///////////////////////////////////////////////////////////////////////////////
2
+ // Copyright (C) 2026 Jean-Philippe Steinmetz
3
+ // SPDX-License-Identifier: MPL-2.0
4
+ ///////////////////////////////////////////////////////////////////////////////
5
+ import React, { FormEvent, useEffect, useState } from "react";
6
+ import { ApiRequestError } from "@rapidmx/react-shared/util/api.js";
7
+ import { createEscrowScope, EscrowScope, listEscrowScopes } from "@rapidmx/react-shared/admin/escrowScopesApi.js";
8
+ import { generateEscrowKeyPair, GeneratedEscrowKeys } from "@rapidmx/react-shared/crypto/escrowKeys.js";
9
+ import { toDatetimeLocal } from "@rapidmx/react-shared/util/dateInput.js";
10
+ import Alert from "@rapidmx/react-shared/components/feedback/Alert.js";
11
+ import Button from "@rapidmx/react-shared/components/buttons/Button.js";
12
+ import FormField from "@rapidmx/react-shared/components/forms/FormField.js";
13
+ import EscrowScopeKeyAndHoldersFields, {
14
+ emptyEscrowScopeKeyAndHoldersValue,
15
+ EscrowScopeKeyAndHoldersValue,
16
+ selfAsHolderError,
17
+ } from "../escrowScopes/EscrowScopeKeyAndHoldersFields.js";
18
+
19
+ const INPUT_CLASS =
20
+ "w-full text-sm py-2.5 px-3 border border-border rounded-sm bg-surface text-text focus:outline-none focus:border-primary";
21
+
22
+ type Mode = "none" | "generate" | "existing";
23
+
24
+ /** How long a download's object URL is kept, since some browsers are still reading it after the click returns. */
25
+ export const DOWNLOAD_URL_LIFETIME_MS = 60_000;
26
+
27
+ /** Offers `text` to the browser as a file download. */
28
+ export function downloadTextFile(filename: string, text: string, type: string = "application/x-pem-file"): void {
29
+ const url = URL.createObjectURL(new Blob([text], { type }));
30
+ const link = document.createElement("a");
31
+ link.href = url;
32
+ link.download = filename;
33
+ document.body.appendChild(link);
34
+ link.click();
35
+ link.remove();
36
+ setTimeout(() => URL.revokeObjectURL(url), DOWNLOAD_URL_LIFETIME_MS);
37
+ }
38
+
39
+ function fileSafe(name: string): string {
40
+ return name.trim().replace(/[^A-Za-z0-9._-]+/g, "-").replace(/^-+|-+$/g, "") || "escrow";
41
+ }
42
+
43
+ /**
44
+ * The setup wizard's escrow step. Escrow lets a separate group of holders recover a mailbox's encrypted mail (for
45
+ * example for legal discovery) using a key this server never has. It's optional, and the administrator can either
46
+ * generate the key pair here (downloading the private key, which never leaves the browser), paste in an existing
47
+ * certificate, or go without escrow.
48
+ */
49
+ export default function EscrowSetupStep({ adminUid }: { adminUid?: string }) {
50
+ const [scopes, setScopes] = useState<EscrowScope[] | null>(null);
51
+ const [mode, setMode] = useState<Mode>("none");
52
+ const [name, setName] = useState("Escrow");
53
+ const [validYears, setValidYears] = useState(5);
54
+ const [generated, setGenerated] = useState<GeneratedEscrowKeys | null>(null);
55
+ const [downloadedPrivateKey, setDownloadedPrivateKey] = useState(false);
56
+ const [savedPrivateKey, setSavedPrivateKey] = useState(false);
57
+ const [keyAndHolders, setKeyAndHolders] = useState<EscrowScopeKeyAndHoldersValue>(emptyEscrowScopeKeyAndHoldersValue());
58
+ const [error, setError] = useState<string | null>(null);
59
+ const [busy, setBusy] = useState(false);
60
+
61
+ useEffect(() => {
62
+ listEscrowScopes({ limit: 100 })
63
+ .then(setScopes)
64
+ .catch(() => setScopes([]));
65
+ }, []);
66
+
67
+ function chooseMode(next: Mode) {
68
+ setMode(next);
69
+ setError(null);
70
+ setGenerated(null);
71
+ setDownloadedPrivateKey(false);
72
+ setSavedPrivateKey(false);
73
+ setKeyAndHolders(emptyEscrowScopeKeyAndHoldersValue());
74
+ }
75
+
76
+ async function generate() {
77
+ if (!name.trim()) {
78
+ setError("Give the escrow scope a name first.");
79
+ return;
80
+ }
81
+ setBusy(true);
82
+ setError(null);
83
+ try {
84
+ const keys = await generateEscrowKeyPair({ name: name.trim(), validDays: validYears * 365 });
85
+ setGenerated(keys);
86
+ setKeyAndHolders({
87
+ ...emptyEscrowScopeKeyAndHoldersValue(),
88
+ publicKey: keys.publicKey.publicKey,
89
+ keyType: keys.publicKey.type,
90
+ fingerprint: keys.publicKey.fingerprint,
91
+ notBefore: toDatetimeLocal(new Date(keys.publicKey.notBefore).toISOString()),
92
+ notAfter: toDatetimeLocal(new Date(keys.publicKey.notAfter).toISOString()),
93
+ });
94
+ } catch {
95
+ setError("This browser couldn't generate the escrow keys.");
96
+ } finally {
97
+ setBusy(false);
98
+ }
99
+ }
100
+
101
+ async function create(e: FormEvent) {
102
+ e.preventDefault();
103
+ setError(null);
104
+ if (!name.trim()) {
105
+ setError("A name is required.");
106
+ return;
107
+ }
108
+ // Generated keys are used exactly as generated; only a pasted-in certificate's fields come from the form.
109
+ const publicKey = generated
110
+ ? generated.publicKey
111
+ : {
112
+ publicKey: keyAndHolders.publicKey.trim(),
113
+ type: keyAndHolders.keyType.trim(),
114
+ fingerprint: keyAndHolders.fingerprint.trim(),
115
+ notBefore: new Date(keyAndHolders.notBefore).getTime(),
116
+ notAfter: new Date(keyAndHolders.notAfter).getTime(),
117
+ };
118
+ if (!publicKey.publicKey || !publicKey.type || !publicKey.fingerprint) {
119
+ setError("The public key, its type, and its fingerprint are all required.");
120
+ return;
121
+ }
122
+ if (keyAndHolders.holderUserUids.length === 0) {
123
+ setError("At least one holder is required.");
124
+ return;
125
+ }
126
+ if (keyAndHolders.requiredHolders < 1 || keyAndHolders.requiredHolders > keyAndHolders.holderUserUids.length) {
127
+ setError("Required holders must be between 1 and the number of holders.");
128
+ return;
129
+ }
130
+ const selfError = selfAsHolderError(keyAndHolders.holderUserUids, adminUid);
131
+ if (selfError) {
132
+ setError(selfError);
133
+ return;
134
+ }
135
+ setBusy(true);
136
+ try {
137
+ const created = await createEscrowScope({
138
+ name: name.trim(),
139
+ publicKey,
140
+ holderUserUids: keyAndHolders.holderUserUids,
141
+ requiredHolders: keyAndHolders.requiredHolders,
142
+ notifySubjectOnAccess: keyAndHolders.notifySubjectOnAccess,
143
+ });
144
+ setScopes((prev) => [...(prev ?? []), created]);
145
+ chooseMode("none");
146
+ setName("Escrow");
147
+ } catch (err) {
148
+ setError(err instanceof ApiRequestError ? err.message : "Could not create the escrow scope.");
149
+ } finally {
150
+ setBusy(false);
151
+ }
152
+ }
153
+
154
+ const scopeForm = (
155
+ <form onSubmit={create} className="flex flex-col gap-5">
156
+ <EscrowScopeKeyAndHoldersFields
157
+ value={keyAndHolders}
158
+ onChange={setKeyAndHolders}
159
+ disabled={mode === "generate" && busy}
160
+ keyReadOnly={mode === "generate"}
161
+ />
162
+ <div>
163
+ <Button type="submit" loading={busy} disabled={busy} className="!w-auto">
164
+ Create escrow scope
165
+ </Button>
166
+ </div>
167
+ </form>
168
+ );
169
+
170
+ return (
171
+ <div className="flex flex-col gap-5 max-w-3xl">
172
+ {/* The setup wizard heads this step, so it only explains what escrow is. */}
173
+ <p className="text-sm text-text-muted">
174
+ Escrow lets a group of trusted holders - for example your legal or compliance team - recover a
175
+ mailbox&rsquo;s encrypted mail when they all agree to. It&rsquo;s optional. Without it, mail that someone
176
+ encrypts can&rsquo;t be recovered if they lose their keys, by anyone.
177
+ </p>
178
+
179
+ {scopes && scopes.length > 0 && (
180
+ <div role="status" className="text-sm py-2 px-3 rounded-sm bg-surface-alt text-text">
181
+ Escrow scopes set up: {scopes.map((scope) => scope.name).join(", ")}.
182
+ </div>
183
+ )}
184
+
185
+ {error && <Alert>{error}</Alert>}
186
+
187
+ <fieldset className="flex flex-col gap-2 text-sm">
188
+ <legend className="font-semibold mb-1">How do you want to set up escrow?</legend>
189
+ <label className="flex items-center gap-2">
190
+ <input type="radio" name="escrow-mode" checked={mode === "generate"} onChange={() => chooseMode("generate")} />
191
+ Generate new escrow keys in this browser
192
+ </label>
193
+ <label className="flex items-center gap-2">
194
+ <input type="radio" name="escrow-mode" checked={mode === "existing"} onChange={() => chooseMode("existing")} />
195
+ Use a certificate I already have
196
+ </label>
197
+ <label className="flex items-center gap-2">
198
+ <input type="radio" name="escrow-mode" checked={mode === "none"} onChange={() => chooseMode("none")} />
199
+ {scopes && scopes.length > 0 ? "Don't add another escrow scope" : "Don't use escrow"}
200
+ </label>
201
+ </fieldset>
202
+
203
+ {mode !== "none" && (
204
+ <div className="bg-surface border border-border rounded-md p-6">
205
+ <FormField label="Escrow scope name" htmlFor="escrowName">
206
+ <input
207
+ id="escrowName"
208
+ type="text"
209
+ className={INPUT_CLASS}
210
+ value={name}
211
+ disabled={!!generated}
212
+ onChange={(e) => setName(e.target.value)}
213
+ />
214
+ </FormField>
215
+ {mode === "generate" && !generated && (
216
+ <>
217
+ <FormField label="Keys valid for" htmlFor="escrowValidYears">
218
+ <select
219
+ id="escrowValidYears"
220
+ className={INPUT_CLASS}
221
+ value={validYears}
222
+ onChange={(e) => setValidYears(Number(e.target.value))}
223
+ >
224
+ {[1, 2, 5, 10].map((years) => (
225
+ <option key={years} value={years}>
226
+ {years} {years === 1 ? "year" : "years"}
227
+ </option>
228
+ ))}
229
+ </select>
230
+ </FormField>
231
+ <Button type="button" className="!w-auto" loading={busy} disabled={busy} onClick={() => void generate()}>
232
+ Generate keys
233
+ </Button>
234
+ </>
235
+ )}
236
+ {mode === "generate" && generated && (
237
+ <div className="flex flex-col gap-3 text-sm">
238
+ <Alert>
239
+ Download the private key now and give it to your escrow holders. It isn&rsquo;t stored
240
+ anywhere else - if it&rsquo;s lost, no escrowed mail can ever be recovered.
241
+ </Alert>
242
+ <Alert>
243
+ You&rsquo;re downloading this private key, so you must not also be one of its holders:
244
+ pick holders other than yourself, hand the key over, and delete your copy once they
245
+ have it. Otherwise one person could both configure escrow and recover mail with it.
246
+ </Alert>
247
+ <div className="flex flex-wrap gap-2">
248
+ <Button
249
+ type="button"
250
+ className="!w-auto"
251
+ onClick={() => {
252
+ downloadTextFile(`${fileSafe(name)}-private-key.pem`, generated.privateKeyPem);
253
+ setDownloadedPrivateKey(true);
254
+ }}
255
+ >
256
+ Download private key
257
+ </Button>
258
+ <Button
259
+ type="button"
260
+ variant="secondary"
261
+ className="!w-auto"
262
+ onClick={() => downloadTextFile(`${fileSafe(name)}-certificate.pem`, generated.certificatePem)}
263
+ >
264
+ Download certificate
265
+ </Button>
266
+ </div>
267
+ <label className="flex items-center gap-2">
268
+ <input
269
+ type="checkbox"
270
+ checked={savedPrivateKey}
271
+ disabled={!downloadedPrivateKey}
272
+ onChange={(e) => setSavedPrivateKey(e.target.checked)}
273
+ />
274
+ I&rsquo;ve saved the private key somewhere safe
275
+ </label>
276
+ {!downloadedPrivateKey && (
277
+ <p className="text-xs text-text-muted">Download the private key before continuing.</p>
278
+ )}
279
+ </div>
280
+ )}
281
+ </div>
282
+ )}
283
+
284
+ {mode === "existing" && scopeForm}
285
+ {mode === "generate" && generated && savedPrivateKey && scopeForm}
286
+ </div>
287
+ );
288
+ }