@rapidmx/web-client 0.18.0 → 0.20.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 (131) hide show
  1. package/README.md +381 -381
  2. package/apps/admin/branding/index.tsx +39 -39
  3. package/apps/admin/data-requests/index.tsx +482 -482
  4. package/apps/admin/domains/[uid].tsx +166 -166
  5. package/apps/admin/escrow-scopes/[uid].tsx +350 -350
  6. package/apps/admin/index.tsx +129 -129
  7. package/apps/admin/mailboxes/[uid].tsx +271 -271
  8. package/apps/admin/mailboxes/new/index.tsx +30 -30
  9. package/apps/admin/plugins/index.tsx +15 -15
  10. package/apps/admin/retention-policy/index.tsx +39 -39
  11. package/apps/admin/signing-certificates/index.tsx +343 -343
  12. package/apps/escrow/audit-log/index.tsx +196 -196
  13. package/apps/escrow/matters/[uid].tsx +621 -621
  14. package/apps/shared/auth/adminAccess.ts +99 -99
  15. package/apps/shared/components/admin/diagnostics/HostCard.tsx +2 -0
  16. package/apps/shared/components/admin/diagnostics/PressureTiles.tsx +108 -0
  17. package/apps/shared/components/admin/diagnostics/PvcTable.tsx +20 -6
  18. package/apps/shared/components/admin/diagnostics/diagnosticsApi.ts +21 -2
  19. package/apps/shared/components/admin/layout/AdminShell.tsx +384 -384
  20. package/apps/shared/components/admin/mailboxes/EraseLeftoverDataDialog.tsx +238 -238
  21. package/apps/shared/components/admin/mailboxes/EscrowScopeCard.tsx +152 -152
  22. package/apps/shared/components/admin/mailboxes/LeftoverMailboxesSection.tsx +194 -194
  23. package/apps/shared/components/admin/settings/BrandingForm.tsx +423 -423
  24. package/apps/shared/components/admin/settings/EncryptionPolicyForm.tsx +119 -119
  25. package/apps/shared/components/admin/settings/MailboxPolicyForm.tsx +198 -198
  26. package/apps/shared/components/admin/settings/PluginsManager.tsx +2093 -1545
  27. package/apps/shared/components/admin/settings/RetentionPolicyForm.tsx +182 -182
  28. package/apps/shared/components/admin/settings/pluginPreferences.ts +34 -0
  29. package/apps/shared/components/admin/setup/EscrowSetupStep.tsx +288 -288
  30. package/apps/shared/components/admin/setup/SetupWizard.tsx +446 -446
  31. package/apps/shared/components/admin/usePagedList.tsx +129 -129
  32. package/apps/shared/components/calendar/EventModal.tsx +206 -206
  33. package/apps/shared/components/calendar/MonthView.tsx +185 -185
  34. package/apps/shared/components/calendar/RecurrenceEditor.tsx +227 -227
  35. package/apps/shared/components/calendar/SplitDayView.tsx +144 -144
  36. package/apps/shared/components/calendar/TimeGridView.tsx +246 -246
  37. package/apps/shared/components/calendar/allDay.ts +124 -124
  38. package/apps/shared/components/contacts/ContactForm.tsx +383 -383
  39. package/apps/shared/components/contacts/ContactsToolbar.tsx +103 -103
  40. package/apps/shared/components/escrow/layout/EscrowShell.tsx +155 -155
  41. package/apps/shared/components/layout/AppShell.tsx +461 -461
  42. package/apps/shared/components/layout/KeyEnrollmentGate.tsx +398 -398
  43. package/apps/shared/components/layout/MailboxProvisioning.tsx +168 -168
  44. package/apps/shared/components/layout/ResponsiveToolbar.tsx +315 -315
  45. package/apps/shared/components/layout/ThemeSwitch.tsx +84 -84
  46. package/apps/shared/components/layout/UserMenu.tsx +439 -439
  47. package/apps/shared/components/mail/ConversationList.tsx +332 -323
  48. package/apps/shared/components/mail/ConversationThreadPane.tsx +613 -613
  49. package/apps/shared/components/mail/MailSelectionBar.tsx +240 -240
  50. package/apps/shared/components/mail/MenuButton.tsx +404 -404
  51. package/apps/shared/components/mail/MessageDetailPane.tsx +1757 -1757
  52. package/apps/shared/components/mail/compose/ComposeContext.tsx +312 -312
  53. package/apps/shared/components/mail/compose/ComposeToolbar.tsx +422 -422
  54. package/apps/shared/components/mail/compose/ComposeWindow.tsx +1681 -1681
  55. package/apps/shared/components/mail/compose/RichTextEditor.tsx +147 -147
  56. package/apps/shared/components/mail/compose/composeFlushRegistry.ts +60 -60
  57. package/apps/shared/components/mail/compose/quotedBody.ts +161 -161
  58. package/apps/shared/components/mail/listPreferences.ts +3 -3
  59. package/apps/shared/components/mail/reading/MessageMoreMenu.tsx +258 -258
  60. package/apps/shared/components/mail/reading/MessageSourceDialog.tsx +79 -79
  61. package/apps/shared/components/mail/reading/messageExport.ts +59 -59
  62. package/apps/shared/components/mail/reading/printMessage.ts +99 -99
  63. package/apps/shared/components/mail/reading/useMessageActions.ts +443 -443
  64. package/apps/shared/components/mail/verificationSeals.ts +125 -125
  65. package/apps/shared/components/rules/RuleBuilder.tsx +311 -311
  66. package/apps/shared/components/settings/SigningCertificateCard.tsx +344 -344
  67. package/apps/shared/keyboard/GlobalShortcuts.tsx +51 -51
  68. package/apps/shared/keyboard/ShortcutProvider.tsx +62 -62
  69. package/apps/shared/keyboard/ShortcutsDialog.tsx +84 -84
  70. package/apps/shared/keyboard/dispatch.ts +124 -124
  71. package/apps/shared/keyboard/format.ts +89 -89
  72. package/apps/shared/keyboard/keymap.ts +114 -114
  73. package/apps/shared/keyboard/registry.ts +65 -65
  74. package/apps/shared/keyboard/targets.ts +79 -79
  75. package/apps/shared/mail/folderOfType.ts +49 -49
  76. package/apps/shared/mail/folderTree.ts +143 -143
  77. package/apps/shared/mail/listAllPages.ts +39 -39
  78. package/apps/shared/mail/newMailNotifications.ts +171 -171
  79. package/apps/shared/mail/outbox/sendJob.ts +445 -445
  80. package/apps/shared/mail/outbox/sendOutcomes.ts +155 -155
  81. package/apps/shared/mail/reportNotices.ts +66 -66
  82. package/apps/shared/mail/senderBlocking.ts +141 -141
  83. package/apps/shared/mail/useMailConnection.ts +205 -205
  84. package/apps/shared/mail/useMailLiveUpdates.ts +277 -277
  85. package/apps/shared/mail/useMailboxUpdateAccess.ts +60 -60
  86. package/apps/shared/mail/useMarkMessageRead.ts +47 -47
  87. package/apps/shared/mail/useNewMailNotifications.ts +178 -178
  88. package/apps/shared/notifications/store.ts +560 -560
  89. package/apps/shared/search/LocalIndexLifecycle.tsx +114 -114
  90. package/apps/shared/search/localIndexBuilder.ts +481 -481
  91. package/apps/shared/signing/enrollmentStorage.ts +33 -33
  92. package/apps/shared/signing/enrollmentTracker.ts +385 -385
  93. package/apps/shared/signing/enrollmentView.ts +251 -251
  94. package/apps/shared/signing/useNow.ts +19 -19
  95. package/apps/shared/signing/useSigningEnrollmentWatcher.ts +90 -90
  96. package/apps/shared/styles/app.css +396 -396
  97. package/apps/www/calendar/index.tsx +581 -581
  98. package/apps/www/contacts/[uid].tsx +112 -112
  99. package/apps/www/index.tsx +3012 -2962
  100. package/apps/www/messages/[uid].tsx +139 -139
  101. package/apps/www/settings/auto-reply/index.tsx +136 -136
  102. package/apps/www/settings/blocked-senders/index.tsx +303 -303
  103. package/apps/www/settings/encryption/index.tsx +1290 -1290
  104. package/apps/www/settings/filters/[uid].tsx +179 -179
  105. package/apps/www/settings/filters/index.tsx +105 -105
  106. package/apps/www/settings/filters/new/index.tsx +165 -165
  107. package/apps/www/settings/labels/index.tsx +207 -207
  108. package/apps/www/settings/privacy/index.tsx +495 -495
  109. package/apps/www/settings/profile/index.tsx +251 -251
  110. package/apps/www/settings/read-receipts/index.tsx +150 -150
  111. package/apps/www/settings/sharing/index.tsx +259 -259
  112. package/apps/www/settings/signatures/[uid].tsx +175 -175
  113. package/apps/www/settings/signatures/index.tsx +91 -91
  114. package/apps/www/settings/signatures/new/index.tsx +138 -138
  115. package/apps/www/tasks/index.tsx +654 -654
  116. package/dist/apps/shared/components/admin/diagnostics/HostCard.js +2 -1
  117. package/dist/apps/shared/components/admin/diagnostics/PressureTiles.d.ts +22 -0
  118. package/dist/apps/shared/components/admin/diagnostics/PressureTiles.js +52 -0
  119. package/dist/apps/shared/components/admin/diagnostics/PvcTable.js +9 -4
  120. package/dist/apps/shared/components/admin/diagnostics/diagnosticsApi.d.ts +35 -2
  121. package/dist/apps/shared/components/admin/settings/BrandingForm.js +3 -3
  122. package/dist/apps/shared/components/admin/settings/PluginsManager.js +328 -48
  123. package/dist/apps/shared/components/admin/settings/pluginPreferences.d.ts +2 -0
  124. package/dist/apps/shared/components/admin/settings/pluginPreferences.js +35 -0
  125. package/dist/apps/shared/components/mail/ConversationList.d.ts +10 -3
  126. package/dist/apps/shared/components/mail/ConversationList.js +9 -5
  127. package/dist/apps/shared/components/mail/listPreferences.d.ts +1 -1
  128. package/dist/apps/shared/components/mail/reading/printMessage.js +11 -11
  129. package/dist/apps/shared/styles/app.css +396 -396
  130. package/dist/apps/www/index.js +51 -10
  131. package/package.json +2 -2
@@ -1,446 +1,446 @@
1
- ///////////////////////////////////////////////////////////////////////////////
2
- // Copyright (C) 2026 Jean-Philippe Steinmetz
3
- // SPDX-License-Identifier: MPL-2.0
4
- ///////////////////////////////////////////////////////////////////////////////
5
- import React, { FormEvent, useEffect, useRef, useState } from "react";
6
- import { useRouter } from "@rapidrest/react/client";
7
- import { HiCheck } from "react-icons/hi2";
8
- import { ApiRequestError } from "@rapidmx/react-shared/util/api.js";
9
- import { getRetentionPolicy } from "@rapidmx/react-shared/admin/retentionPolicyApi.js";
10
- import { getMailboxPolicy } from "@rapidmx/react-shared/admin/mailboxPolicyApi.js";
11
- import { createDomain, Domain, listDomains } from "@rapidmx/react-shared/admin/domainsApi.js";
12
- import { completeSetup, getSetupStatus, saveSetupStep, SetupStatus } from "@rapidmx/react-shared/admin/setupApi.js";
13
- import { getBranding } from "@rapidmx/react-shared/branding/brandingApi.js";
14
- import { EncryptionPolicy, getEncryptionPolicy } from "@rapidmx/react-shared/crypto/keyvaultApi.js";
15
- import { listMailboxes, Mailbox } from "@rapidmx/react-shared/mail/mailApi.js";
16
- import Alert from "@rapidmx/react-shared/components/feedback/Alert.js";
17
- import Button from "@rapidmx/react-shared/components/buttons/Button.js";
18
- import FormField from "@rapidmx/react-shared/components/forms/FormField.js";
19
- import Modal from "@rapidmx/react-shared/components/overlays/Modal.js";
20
- import BrandingForm from "../settings/BrandingForm.js";
21
- import DomainDnsSetup from "../settings/DomainDnsSetup.js";
22
- import EncryptionPolicyForm, { isEncryptionEnabled } from "../settings/EncryptionPolicyForm.js";
23
- import LoadedSettingsForm from "../settings/LoadedSettingsForm.js";
24
- import MailboxCreateForm from "../settings/MailboxCreateForm.js";
25
- import MailboxPolicyForm from "../settings/MailboxPolicyForm.js";
26
- import PluginsManager from "../settings/PluginsManager.js";
27
- import RetentionPolicyForm from "../settings/RetentionPolicyForm.js";
28
- import EscrowSetupStep from "./EscrowSetupStep.js";
29
-
30
- const INPUT_CLASS =
31
- "w-full text-sm py-2.5 px-3 border border-border rounded-sm bg-surface text-text focus:outline-none focus:border-primary";
32
-
33
- /** The bordered panel the wizard's forms sit in. */
34
- const CARD_CLASS = "bg-surface border border-border rounded-md p-6";
35
-
36
- export const SETUP_STEPS = [
37
- { id: "plugins", title: "Plugins", intro: "Choose which protocols and features this server runs. The recommended plugins are already installed." },
38
- { id: "domain", title: "Domain", intro: "Add the domain this server receives mail for, then publish the DNS records below." },
39
- { id: "settings", title: "Server settings", intro: "Set the rules for encryption, how long mail is kept, and new mailboxes." },
40
- { id: "escrow", title: "Escrow", intro: "Decide whether encrypted mail can be recovered by a trusted group of holders." },
41
- { id: "branding", title: "Branding", intro: "Make the sign-in page and apps look like your organization." },
42
- { id: "mailboxes", title: "Mailboxes", intro: "Create the first mailboxes, starting with your own." },
43
- ] as const;
44
-
45
- export type SetupStepId = (typeof SETUP_STEPS)[number]["id"];
46
-
47
- function isStepId(value: string | undefined): value is SetupStepId {
48
- return SETUP_STEPS.some((step) => step.id === value);
49
- }
50
-
51
- function errorMessage(err: unknown, fallback: string): string {
52
- return err instanceof ApiRequestError ? err.message : fallback;
53
- }
54
-
55
- export interface SetupWizardProps {
56
- userUid: string;
57
- }
58
-
59
- /**
60
- * The first-run setup wizard. Each step saves as the administrator works through it; the wizard itself only records
61
- * which step they're on (so leaving and coming back resumes there) and, at the end, that setup is finished. Every step
62
- * except the domain can be passed over and configured later from the admin console.
63
- */
64
- export default function SetupWizard({ userUid }: SetupWizardProps) {
65
- const { navigate } = useRouter();
66
- const [step, setStep] = useState<SetupStepId | null>(null);
67
- const [status, setStatus] = useState<SetupStatus | undefined>();
68
- const [domains, setDomains] = useState<Domain[]>([]);
69
- const [domainsError, setDomainsError] = useState<string | null>(null);
70
- const [encryption, setEncryption] = useState<EncryptionPolicy | null>(null);
71
- const [finishing, setFinishing] = useState(false);
72
- const [error, setError] = useState<string | null>(null);
73
- /** Which of the settings step's forms have edits that haven't been saved. */
74
- const [unsaved, setUnsaved] = useState<Record<string, boolean>>({});
75
- /** The step the administrator asked to go to while the settings step had unsaved edits. */
76
- const [leavingTo, setLeavingTo] = useState<SetupStepId | null>(null);
77
- const [progressFailed, setProgressFailed] = useState(false);
78
- /** Step saves run one at a time, and only the latest step still waiting is saved. */
79
- const progress = useRef<{ running: Promise<void> | null; next: SetupStepId | null }>({ running: null, next: null });
80
-
81
- function loadDomains(): Promise<void> {
82
- return listDomains({ limit: 25 })
83
- .then((list) => {
84
- setDomains(list);
85
- setDomainsError(null);
86
- })
87
- .catch((err) => setDomainsError(errorMessage(err, "Could not load your domains.")));
88
- }
89
-
90
- useEffect(() => {
91
- void Promise.all([getSetupStatus().catch(() => undefined), loadDomains()]).then(([loaded]) => {
92
- setStatus(loaded);
93
- setStep(isStepId(loaded?.currentStep) ? loaded.currentStep : "plugins");
94
- });
95
- }, []);
96
-
97
- useEffect(() => {
98
- if (step === "escrow" || step === "settings") {
99
- getEncryptionPolicy()
100
- .then(setEncryption)
101
- .catch(() => setEncryption(null));
102
- }
103
- }, [step]);
104
-
105
- if (step === null) {
106
- return <p className="text-sm text-text-muted">Loading&hellip;</p>;
107
- }
108
-
109
- const index = SETUP_STEPS.findIndex((s) => s.id === step);
110
- const current = SETUP_STEPS[index];
111
- const last = index === SETUP_STEPS.length - 1;
112
-
113
- /** Records the step the administrator is on. Progress is a convenience for resuming, so a failure is only noted. */
114
- function saveProgress(next: SetupStepId) {
115
- const queue = progress.current;
116
- queue.next = next;
117
- if (queue.running) {
118
- return;
119
- }
120
- queue.running = (async () => {
121
- while (queue.next) {
122
- const target: SetupStepId = queue.next;
123
- queue.next = null;
124
- try {
125
- await saveSetupStep(target);
126
- setProgressFailed(false);
127
- } catch (err) {
128
- console.warn("Could not save setup progress", err);
129
- setProgressFailed(true);
130
- }
131
- }
132
- queue.running = null;
133
- })();
134
- }
135
-
136
- function goTo(next: SetupStepId) {
137
- setError(null);
138
- setLeavingTo(null);
139
- setUnsaved({});
140
- setStep(next);
141
- saveProgress(next);
142
- window.scrollTo?.(0, 0);
143
- }
144
-
145
- /** Goes to `next`, first asking whether to discard any unsaved server settings. */
146
- function requestGoTo(next: SetupStepId) {
147
- // Going to the step already shown would clear the unsaved-edit tracking while the forms keep their edits.
148
- if (next === step) {
149
- return;
150
- }
151
- if (step === "settings" && Object.values(unsaved).some(Boolean)) {
152
- setLeavingTo(next);
153
- return;
154
- }
155
- goTo(next);
156
- }
157
-
158
- const trackUnsaved = (form: string) => (dirty: boolean) => setUnsaved((prev) => ({ ...prev, [form]: dirty }));
159
-
160
- async function finish() {
161
- setFinishing(true);
162
- setError(null);
163
- try {
164
- // Let a step still being recorded land first, so it can't arrive after setup is marked finished.
165
- await progress.current.running;
166
- await completeSetup();
167
- void navigate("/admin");
168
- } catch (err) {
169
- setError(errorMessage(err, "Could not finish setup."));
170
- setFinishing(false);
171
- }
172
- }
173
-
174
- const noDomain: boolean = domains.length === 0;
175
- const blocked = (step === "domain" || last) && noDomain;
176
-
177
- return (
178
- <div className="flex flex-col gap-6">
179
- <div className="flex items-start justify-between gap-4">
180
- <div>
181
- <h1 className="text-xl font-bold uppercase tracking-wide mb-1">Set up your server</h1>
182
- <p className="text-sm text-text-muted">You can change any of these settings later from the admin console.</p>
183
- </div>
184
- {status && !status.required && (
185
- <a href="/admin" className="text-sm text-primary-dark font-medium hover:underline whitespace-nowrap">
186
- Exit setup
187
- </a>
188
- )}
189
- </div>
190
-
191
- {domainsError && (
192
- <Alert>
193
- <span className="flex flex-wrap items-center gap-2">
194
- {domainsError}
195
- <Button type="button" variant="text" onClick={() => void loadDomains()}>
196
- Retry
197
- </Button>
198
- </span>
199
- </Alert>
200
- )}
201
-
202
- <ol aria-label="Setup steps" className="flex flex-wrap gap-2">
203
- {SETUP_STEPS.map((s, i) => (
204
- <li key={s.id}>
205
- <button
206
- type="button"
207
- aria-current={s.id === step ? "step" : undefined}
208
- // Steps after the domain need somewhere to put mail first.
209
- disabled={i > 1 && noDomain}
210
- onClick={() => requestGoTo(s.id)}
211
- className={[
212
- "inline-flex items-center gap-1.5 text-sm py-1.5 px-3 rounded-pill border transition-colors",
213
- "disabled:opacity-50 disabled:cursor-not-allowed",
214
- s.id === step
215
- ? "bg-primary/10 border-primary text-primary-dark font-semibold"
216
- : i < index
217
- ? "bg-surface border-border text-text hover:not-disabled:border-primary"
218
- : "bg-surface border-border text-text-muted hover:not-disabled:border-primary hover:not-disabled:text-text",
219
- ].join(" ")}
220
- >
221
- {i < index && <HiCheck aria-hidden="true" className="w-4 h-4 text-success" />}
222
- {i + 1}. {s.title}
223
- {i < index && <span className="sr-only"> (done)</span>}
224
- </button>
225
- </li>
226
- ))}
227
- </ol>
228
-
229
- <section aria-labelledby="setup-step-title" className="flex flex-col gap-5">
230
- <div className="flex flex-col gap-1">
231
- <h2 id="setup-step-title" className="flex flex-col gap-0.5">
232
- <span className="text-xs font-bold uppercase tracking-wide text-text-muted">
233
- Step {index + 1} of {SETUP_STEPS.length}
234
- <span className="sr-only">:</span>
235
- </span>{" "}
236
- <span className="text-lg font-bold uppercase tracking-wide">{current.title}</span>
237
- </h2>
238
- <p className="text-sm text-text-muted max-w-3xl">{current.intro}</p>
239
- </div>
240
-
241
- {step === "plugins" && (
242
- <div>
243
- <PluginsManager embedded />
244
- </div>
245
- )}
246
- {step === "domain" && <DomainStep domains={domains} onCreated={(domain) => setDomains((prev) => [...prev, domain])} />}
247
- {step === "settings" && (
248
- <div className="flex flex-col gap-6 max-w-3xl">
249
- <div className={CARD_CLASS}>
250
- <LoadedSettingsForm load={getEncryptionPolicy} loadErrorMessage="Could not load the encryption policy.">
251
- {(policy, onChange) => (
252
- <EncryptionPolicyForm
253
- embedded
254
- policy={policy}
255
- onDirtyChange={trackUnsaved("encryption")}
256
- onChange={(updated) => {
257
- onChange(updated);
258
- setEncryption(updated);
259
- }}
260
- />
261
- )}
262
- </LoadedSettingsForm>
263
- </div>
264
- <div className={CARD_CLASS}>
265
- <LoadedSettingsForm load={getRetentionPolicy} loadErrorMessage="Could not load the retention policy.">
266
- {(policy, onChange) => (
267
- <RetentionPolicyForm embedded policy={policy} onChange={onChange} onDirtyChange={trackUnsaved("retention")} />
268
- )}
269
- </LoadedSettingsForm>
270
- </div>
271
- <div className={CARD_CLASS}>
272
- <LoadedSettingsForm load={getMailboxPolicy} loadErrorMessage="Could not load the mailbox policy.">
273
- {(policy, onChange) => (
274
- <MailboxPolicyForm embedded policy={policy} onChange={onChange} onDirtyChange={trackUnsaved("mailbox")} />
275
- )}
276
- </LoadedSettingsForm>
277
- </div>
278
- </div>
279
- )}
280
- {step === "escrow" &&
281
- (encryption && !isEncryptionEnabled(encryption) ? (
282
- <p role="status" className="text-sm py-2 px-3 rounded-sm bg-surface-alt text-text max-w-3xl">
283
- End-to-end encryption is turned off, so escrow isn&rsquo;t needed. You can set it up later if you
284
- turn encryption on.
285
- </p>
286
- ) : (
287
- <EscrowSetupStep adminUid={userUid} />
288
- ))}
289
- {step === "branding" && (
290
- <div className={`${CARD_CLASS} max-w-3xl`}>
291
- <LoadedSettingsForm load={getBranding} loadErrorMessage="Could not load branding.">
292
- {(branding, onChange) => <BrandingForm embedded branding={branding} onChange={onChange} />}
293
- </LoadedSettingsForm>
294
- </div>
295
- )}
296
- {step === "mailboxes" && <MailboxesStep userUid={userUid} domain={domains.find((d) => d.verified)?.name ?? domains[0]?.name} />}
297
- </section>
298
-
299
- {error && <Alert>{error}</Alert>}
300
-
301
- <div className="flex items-center gap-3 border-t border-border pt-5">
302
- {index > 0 && (
303
- <Button type="button" variant="secondary" className="!w-auto" onClick={() => requestGoTo(SETUP_STEPS[index - 1].id)}>
304
- Back
305
- </Button>
306
- )}
307
- <div className="flex-1" />
308
- {progressFailed && <span className="text-xs text-text-muted">Your place in setup couldn&rsquo;t be saved.</span>}
309
- {blocked && <span className="text-sm text-text-muted">{last ? "Add a domain before finishing setup." : "Add a domain to continue."}</span>}
310
- {last ? (
311
- <Button type="button" className="!w-auto" loading={finishing} disabled={finishing || blocked} onClick={() => void finish()}>
312
- Finish setup
313
- </Button>
314
- ) : (
315
- <Button type="button" className="!w-auto" disabled={blocked} onClick={() => requestGoTo(SETUP_STEPS[index + 1].id)}>
316
- Continue
317
- </Button>
318
- )}
319
- </div>
320
-
321
- {leavingTo && (
322
- <Modal open onClose={() => setLeavingTo(null)} title="Discard unsaved changes?">
323
- <p className="text-sm mb-4">Some server settings on this step have changes that haven&rsquo;t been saved.</p>
324
- <div className="flex gap-2 justify-end">
325
- <Button type="button" variant="secondary" className="!w-auto" onClick={() => setLeavingTo(null)}>
326
- Keep editing
327
- </Button>
328
- <Button type="button" className="!w-auto" onClick={() => goTo(leavingTo)}>
329
- Discard changes
330
- </Button>
331
- </div>
332
- </Modal>
333
- )}
334
- </div>
335
- );
336
- }
337
-
338
- function DomainStep({ domains, onCreated }: { domains: Domain[]; onCreated: (domain: Domain) => void }) {
339
- const [name, setName] = useState("");
340
- const [error, setError] = useState<string | null>(null);
341
- const [saving, setSaving] = useState(false);
342
-
343
- async function handleSubmit(e: FormEvent) {
344
- e.preventDefault();
345
- setError(null);
346
- if (!name.trim()) {
347
- setError("A domain name is required.");
348
- return;
349
- }
350
- setSaving(true);
351
- try {
352
- onCreated(await createDomain({ name: name.trim() }));
353
- setName("");
354
- } catch (err) {
355
- setError(errorMessage(err, "Could not add the domain."));
356
- } finally {
357
- setSaving(false);
358
- }
359
- }
360
-
361
- return (
362
- <div className="flex flex-col gap-5 max-w-3xl">
363
- {domains.map((domain) => (
364
- <div key={domain.uid} className="flex flex-col gap-3">
365
- <h3 className="text-base font-bold">{domain.name}</h3>
366
- <DomainDnsSetup uid={domain.uid} />
367
- </div>
368
- ))}
369
- {error && <Alert>{error}</Alert>}
370
- <form onSubmit={handleSubmit} className={CARD_CLASS}>
371
- <FormField label={domains.length === 0 ? "Domain name" : "Add another domain"} htmlFor="setupDomainName">
372
- <input
373
- id="setupDomainName"
374
- type="text"
375
- className={INPUT_CLASS}
376
- value={name}
377
- onChange={(e) => setName(e.target.value)}
378
- placeholder="example.com"
379
- />
380
- </FormField>
381
- <Button type="submit" loading={saving} disabled={saving} className="!w-auto">
382
- Add domain
383
- </Button>
384
- </form>
385
- </div>
386
- );
387
- }
388
-
389
- function MailboxesStep({ userUid, domain }: { userUid: string; domain?: string }) {
390
- const [mailboxes, setMailboxes] = useState<Mailbox[]>([]);
391
- const [loaded, setLoaded] = useState(false);
392
- const [error, setError] = useState<string | null>(null);
393
- const [formKey, setFormKey] = useState(0);
394
-
395
- useEffect(() => {
396
- // The administration scope: every mailbox that exists (administrative metadata), not only the administrator's own.
397
- listMailboxes({ limit: 100, scope: "admin" })
398
- .then((list) =>
399
- // Keep any mailbox created here while the list was loading.
400
- setMailboxes((prev) => [...list, ...prev.filter((mine) => !list.some((mailbox) => mailbox.uid === mine.uid))]),
401
- )
402
- .catch((err) =>
403
- setError(
404
- `${errorMessage(err, "Could not load the existing mailboxes.")} Check the Mailboxes page before creating your own, so you don't create it twice.`,
405
- ),
406
- )
407
- .finally(() => setLoaded(true));
408
- }, []);
409
-
410
- const hasOwn = mailboxes.some((mailbox) => mailbox.ownerUserUid === userUid);
411
-
412
- return (
413
- <div className="flex flex-col gap-5 max-w-xl">
414
- {error && <Alert>{error}</Alert>}
415
- {mailboxes.length > 0 && (
416
- <div>
417
- <h3 className="text-sm font-bold uppercase tracking-wide mb-2">Mailboxes created</h3>
418
- <ul className="text-sm flex flex-col gap-1">
419
- {mailboxes.map((mailbox) => (
420
- <li key={mailbox.uid}>
421
- {mailbox.displayName} &lt;{mailbox.primarySmtpAddress}&gt;
422
- {mailbox.ownerUserUid === userUid && <span className="text-text-muted"> (yours)</span>}
423
- </li>
424
- ))}
425
- </ul>
426
- </div>
427
- )}
428
- {!loaded ? (
429
- <p className="text-sm text-text-muted">Loading&hellip;</p>
430
- ) : (
431
- <>
432
- <h3 className="text-base font-bold">{hasOwn ? "Add another mailbox" : "Your mailbox"}</h3>
433
- <MailboxCreateForm
434
- key={formKey}
435
- submitLabel="Create mailbox"
436
- defaults={hasOwn ? { domain } : { localPart: "admin", domain, displayName: "Administrator", ownerUserUid: userUid }}
437
- onCreated={(mailbox) => {
438
- setMailboxes((prev) => [...prev, mailbox]);
439
- setFormKey((k) => k + 1);
440
- }}
441
- />
442
- </>
443
- )}
444
- </div>
445
- );
446
- }
1
+ ///////////////////////////////////////////////////////////////////////////////
2
+ // Copyright (C) 2026 Jean-Philippe Steinmetz
3
+ // SPDX-License-Identifier: MPL-2.0
4
+ ///////////////////////////////////////////////////////////////////////////////
5
+ import React, { FormEvent, useEffect, useRef, useState } from "react";
6
+ import { useRouter } from "@rapidrest/react/client";
7
+ import { HiCheck } from "react-icons/hi2";
8
+ import { ApiRequestError } from "@rapidmx/react-shared/util/api.js";
9
+ import { getRetentionPolicy } from "@rapidmx/react-shared/admin/retentionPolicyApi.js";
10
+ import { getMailboxPolicy } from "@rapidmx/react-shared/admin/mailboxPolicyApi.js";
11
+ import { createDomain, Domain, listDomains } from "@rapidmx/react-shared/admin/domainsApi.js";
12
+ import { completeSetup, getSetupStatus, saveSetupStep, SetupStatus } from "@rapidmx/react-shared/admin/setupApi.js";
13
+ import { getBranding } from "@rapidmx/react-shared/branding/brandingApi.js";
14
+ import { EncryptionPolicy, getEncryptionPolicy } from "@rapidmx/react-shared/crypto/keyvaultApi.js";
15
+ import { listMailboxes, Mailbox } from "@rapidmx/react-shared/mail/mailApi.js";
16
+ import Alert from "@rapidmx/react-shared/components/feedback/Alert.js";
17
+ import Button from "@rapidmx/react-shared/components/buttons/Button.js";
18
+ import FormField from "@rapidmx/react-shared/components/forms/FormField.js";
19
+ import Modal from "@rapidmx/react-shared/components/overlays/Modal.js";
20
+ import BrandingForm from "../settings/BrandingForm.js";
21
+ import DomainDnsSetup from "../settings/DomainDnsSetup.js";
22
+ import EncryptionPolicyForm, { isEncryptionEnabled } from "../settings/EncryptionPolicyForm.js";
23
+ import LoadedSettingsForm from "../settings/LoadedSettingsForm.js";
24
+ import MailboxCreateForm from "../settings/MailboxCreateForm.js";
25
+ import MailboxPolicyForm from "../settings/MailboxPolicyForm.js";
26
+ import PluginsManager from "../settings/PluginsManager.js";
27
+ import RetentionPolicyForm from "../settings/RetentionPolicyForm.js";
28
+ import EscrowSetupStep from "./EscrowSetupStep.js";
29
+
30
+ const INPUT_CLASS =
31
+ "w-full text-sm py-2.5 px-3 border border-border rounded-sm bg-surface text-text focus:outline-none focus:border-primary";
32
+
33
+ /** The bordered panel the wizard's forms sit in. */
34
+ const CARD_CLASS = "bg-surface border border-border rounded-md p-6";
35
+
36
+ export const SETUP_STEPS = [
37
+ { id: "plugins", title: "Plugins", intro: "Choose which protocols and features this server runs. The recommended plugins are already installed." },
38
+ { id: "domain", title: "Domain", intro: "Add the domain this server receives mail for, then publish the DNS records below." },
39
+ { id: "settings", title: "Server settings", intro: "Set the rules for encryption, how long mail is kept, and new mailboxes." },
40
+ { id: "escrow", title: "Escrow", intro: "Decide whether encrypted mail can be recovered by a trusted group of holders." },
41
+ { id: "branding", title: "Branding", intro: "Make the sign-in page and apps look like your organization." },
42
+ { id: "mailboxes", title: "Mailboxes", intro: "Create the first mailboxes, starting with your own." },
43
+ ] as const;
44
+
45
+ export type SetupStepId = (typeof SETUP_STEPS)[number]["id"];
46
+
47
+ function isStepId(value: string | undefined): value is SetupStepId {
48
+ return SETUP_STEPS.some((step) => step.id === value);
49
+ }
50
+
51
+ function errorMessage(err: unknown, fallback: string): string {
52
+ return err instanceof ApiRequestError ? err.message : fallback;
53
+ }
54
+
55
+ export interface SetupWizardProps {
56
+ userUid: string;
57
+ }
58
+
59
+ /**
60
+ * The first-run setup wizard. Each step saves as the administrator works through it; the wizard itself only records
61
+ * which step they're on (so leaving and coming back resumes there) and, at the end, that setup is finished. Every step
62
+ * except the domain can be passed over and configured later from the admin console.
63
+ */
64
+ export default function SetupWizard({ userUid }: SetupWizardProps) {
65
+ const { navigate } = useRouter();
66
+ const [step, setStep] = useState<SetupStepId | null>(null);
67
+ const [status, setStatus] = useState<SetupStatus | undefined>();
68
+ const [domains, setDomains] = useState<Domain[]>([]);
69
+ const [domainsError, setDomainsError] = useState<string | null>(null);
70
+ const [encryption, setEncryption] = useState<EncryptionPolicy | null>(null);
71
+ const [finishing, setFinishing] = useState(false);
72
+ const [error, setError] = useState<string | null>(null);
73
+ /** Which of the settings step's forms have edits that haven't been saved. */
74
+ const [unsaved, setUnsaved] = useState<Record<string, boolean>>({});
75
+ /** The step the administrator asked to go to while the settings step had unsaved edits. */
76
+ const [leavingTo, setLeavingTo] = useState<SetupStepId | null>(null);
77
+ const [progressFailed, setProgressFailed] = useState(false);
78
+ /** Step saves run one at a time, and only the latest step still waiting is saved. */
79
+ const progress = useRef<{ running: Promise<void> | null; next: SetupStepId | null }>({ running: null, next: null });
80
+
81
+ function loadDomains(): Promise<void> {
82
+ return listDomains({ limit: 25 })
83
+ .then((list) => {
84
+ setDomains(list);
85
+ setDomainsError(null);
86
+ })
87
+ .catch((err) => setDomainsError(errorMessage(err, "Could not load your domains.")));
88
+ }
89
+
90
+ useEffect(() => {
91
+ void Promise.all([getSetupStatus().catch(() => undefined), loadDomains()]).then(([loaded]) => {
92
+ setStatus(loaded);
93
+ setStep(isStepId(loaded?.currentStep) ? loaded.currentStep : "plugins");
94
+ });
95
+ }, []);
96
+
97
+ useEffect(() => {
98
+ if (step === "escrow" || step === "settings") {
99
+ getEncryptionPolicy()
100
+ .then(setEncryption)
101
+ .catch(() => setEncryption(null));
102
+ }
103
+ }, [step]);
104
+
105
+ if (step === null) {
106
+ return <p className="text-sm text-text-muted">Loading&hellip;</p>;
107
+ }
108
+
109
+ const index = SETUP_STEPS.findIndex((s) => s.id === step);
110
+ const current = SETUP_STEPS[index];
111
+ const last = index === SETUP_STEPS.length - 1;
112
+
113
+ /** Records the step the administrator is on. Progress is a convenience for resuming, so a failure is only noted. */
114
+ function saveProgress(next: SetupStepId) {
115
+ const queue = progress.current;
116
+ queue.next = next;
117
+ if (queue.running) {
118
+ return;
119
+ }
120
+ queue.running = (async () => {
121
+ while (queue.next) {
122
+ const target: SetupStepId = queue.next;
123
+ queue.next = null;
124
+ try {
125
+ await saveSetupStep(target);
126
+ setProgressFailed(false);
127
+ } catch (err) {
128
+ console.warn("Could not save setup progress", err);
129
+ setProgressFailed(true);
130
+ }
131
+ }
132
+ queue.running = null;
133
+ })();
134
+ }
135
+
136
+ function goTo(next: SetupStepId) {
137
+ setError(null);
138
+ setLeavingTo(null);
139
+ setUnsaved({});
140
+ setStep(next);
141
+ saveProgress(next);
142
+ window.scrollTo?.(0, 0);
143
+ }
144
+
145
+ /** Goes to `next`, first asking whether to discard any unsaved server settings. */
146
+ function requestGoTo(next: SetupStepId) {
147
+ // Going to the step already shown would clear the unsaved-edit tracking while the forms keep their edits.
148
+ if (next === step) {
149
+ return;
150
+ }
151
+ if (step === "settings" && Object.values(unsaved).some(Boolean)) {
152
+ setLeavingTo(next);
153
+ return;
154
+ }
155
+ goTo(next);
156
+ }
157
+
158
+ const trackUnsaved = (form: string) => (dirty: boolean) => setUnsaved((prev) => ({ ...prev, [form]: dirty }));
159
+
160
+ async function finish() {
161
+ setFinishing(true);
162
+ setError(null);
163
+ try {
164
+ // Let a step still being recorded land first, so it can't arrive after setup is marked finished.
165
+ await progress.current.running;
166
+ await completeSetup();
167
+ void navigate("/admin");
168
+ } catch (err) {
169
+ setError(errorMessage(err, "Could not finish setup."));
170
+ setFinishing(false);
171
+ }
172
+ }
173
+
174
+ const noDomain: boolean = domains.length === 0;
175
+ const blocked = (step === "domain" || last) && noDomain;
176
+
177
+ return (
178
+ <div className="flex flex-col gap-6">
179
+ <div className="flex items-start justify-between gap-4">
180
+ <div>
181
+ <h1 className="text-xl font-bold uppercase tracking-wide mb-1">Set up your server</h1>
182
+ <p className="text-sm text-text-muted">You can change any of these settings later from the admin console.</p>
183
+ </div>
184
+ {status && !status.required && (
185
+ <a href="/admin" className="text-sm text-primary-dark font-medium hover:underline whitespace-nowrap">
186
+ Exit setup
187
+ </a>
188
+ )}
189
+ </div>
190
+
191
+ {domainsError && (
192
+ <Alert>
193
+ <span className="flex flex-wrap items-center gap-2">
194
+ {domainsError}
195
+ <Button type="button" variant="text" onClick={() => void loadDomains()}>
196
+ Retry
197
+ </Button>
198
+ </span>
199
+ </Alert>
200
+ )}
201
+
202
+ <ol aria-label="Setup steps" className="flex flex-wrap gap-2">
203
+ {SETUP_STEPS.map((s, i) => (
204
+ <li key={s.id}>
205
+ <button
206
+ type="button"
207
+ aria-current={s.id === step ? "step" : undefined}
208
+ // Steps after the domain need somewhere to put mail first.
209
+ disabled={i > 1 && noDomain}
210
+ onClick={() => requestGoTo(s.id)}
211
+ className={[
212
+ "inline-flex items-center gap-1.5 text-sm py-1.5 px-3 rounded-pill border transition-colors",
213
+ "disabled:opacity-50 disabled:cursor-not-allowed",
214
+ s.id === step
215
+ ? "bg-primary/10 border-primary text-primary-dark font-semibold"
216
+ : i < index
217
+ ? "bg-surface border-border text-text hover:not-disabled:border-primary"
218
+ : "bg-surface border-border text-text-muted hover:not-disabled:border-primary hover:not-disabled:text-text",
219
+ ].join(" ")}
220
+ >
221
+ {i < index && <HiCheck aria-hidden="true" className="w-4 h-4 text-success" />}
222
+ {i + 1}. {s.title}
223
+ {i < index && <span className="sr-only"> (done)</span>}
224
+ </button>
225
+ </li>
226
+ ))}
227
+ </ol>
228
+
229
+ <section aria-labelledby="setup-step-title" className="flex flex-col gap-5">
230
+ <div className="flex flex-col gap-1">
231
+ <h2 id="setup-step-title" className="flex flex-col gap-0.5">
232
+ <span className="text-xs font-bold uppercase tracking-wide text-text-muted">
233
+ Step {index + 1} of {SETUP_STEPS.length}
234
+ <span className="sr-only">:</span>
235
+ </span>{" "}
236
+ <span className="text-lg font-bold uppercase tracking-wide">{current.title}</span>
237
+ </h2>
238
+ <p className="text-sm text-text-muted max-w-3xl">{current.intro}</p>
239
+ </div>
240
+
241
+ {step === "plugins" && (
242
+ <div>
243
+ <PluginsManager embedded />
244
+ </div>
245
+ )}
246
+ {step === "domain" && <DomainStep domains={domains} onCreated={(domain) => setDomains((prev) => [...prev, domain])} />}
247
+ {step === "settings" && (
248
+ <div className="flex flex-col gap-6 max-w-3xl">
249
+ <div className={CARD_CLASS}>
250
+ <LoadedSettingsForm load={getEncryptionPolicy} loadErrorMessage="Could not load the encryption policy.">
251
+ {(policy, onChange) => (
252
+ <EncryptionPolicyForm
253
+ embedded
254
+ policy={policy}
255
+ onDirtyChange={trackUnsaved("encryption")}
256
+ onChange={(updated) => {
257
+ onChange(updated);
258
+ setEncryption(updated);
259
+ }}
260
+ />
261
+ )}
262
+ </LoadedSettingsForm>
263
+ </div>
264
+ <div className={CARD_CLASS}>
265
+ <LoadedSettingsForm load={getRetentionPolicy} loadErrorMessage="Could not load the retention policy.">
266
+ {(policy, onChange) => (
267
+ <RetentionPolicyForm embedded policy={policy} onChange={onChange} onDirtyChange={trackUnsaved("retention")} />
268
+ )}
269
+ </LoadedSettingsForm>
270
+ </div>
271
+ <div className={CARD_CLASS}>
272
+ <LoadedSettingsForm load={getMailboxPolicy} loadErrorMessage="Could not load the mailbox policy.">
273
+ {(policy, onChange) => (
274
+ <MailboxPolicyForm embedded policy={policy} onChange={onChange} onDirtyChange={trackUnsaved("mailbox")} />
275
+ )}
276
+ </LoadedSettingsForm>
277
+ </div>
278
+ </div>
279
+ )}
280
+ {step === "escrow" &&
281
+ (encryption && !isEncryptionEnabled(encryption) ? (
282
+ <p role="status" className="text-sm py-2 px-3 rounded-sm bg-surface-alt text-text max-w-3xl">
283
+ End-to-end encryption is turned off, so escrow isn&rsquo;t needed. You can set it up later if you
284
+ turn encryption on.
285
+ </p>
286
+ ) : (
287
+ <EscrowSetupStep adminUid={userUid} />
288
+ ))}
289
+ {step === "branding" && (
290
+ <div className={`${CARD_CLASS} max-w-3xl`}>
291
+ <LoadedSettingsForm load={getBranding} loadErrorMessage="Could not load branding.">
292
+ {(branding, onChange) => <BrandingForm embedded branding={branding} onChange={onChange} />}
293
+ </LoadedSettingsForm>
294
+ </div>
295
+ )}
296
+ {step === "mailboxes" && <MailboxesStep userUid={userUid} domain={domains.find((d) => d.verified)?.name ?? domains[0]?.name} />}
297
+ </section>
298
+
299
+ {error && <Alert>{error}</Alert>}
300
+
301
+ <div className="flex items-center gap-3 border-t border-border pt-5">
302
+ {index > 0 && (
303
+ <Button type="button" variant="secondary" className="!w-auto" onClick={() => requestGoTo(SETUP_STEPS[index - 1].id)}>
304
+ Back
305
+ </Button>
306
+ )}
307
+ <div className="flex-1" />
308
+ {progressFailed && <span className="text-xs text-text-muted">Your place in setup couldn&rsquo;t be saved.</span>}
309
+ {blocked && <span className="text-sm text-text-muted">{last ? "Add a domain before finishing setup." : "Add a domain to continue."}</span>}
310
+ {last ? (
311
+ <Button type="button" className="!w-auto" loading={finishing} disabled={finishing || blocked} onClick={() => void finish()}>
312
+ Finish setup
313
+ </Button>
314
+ ) : (
315
+ <Button type="button" className="!w-auto" disabled={blocked} onClick={() => requestGoTo(SETUP_STEPS[index + 1].id)}>
316
+ Continue
317
+ </Button>
318
+ )}
319
+ </div>
320
+
321
+ {leavingTo && (
322
+ <Modal open onClose={() => setLeavingTo(null)} title="Discard unsaved changes?">
323
+ <p className="text-sm mb-4">Some server settings on this step have changes that haven&rsquo;t been saved.</p>
324
+ <div className="flex gap-2 justify-end">
325
+ <Button type="button" variant="secondary" className="!w-auto" onClick={() => setLeavingTo(null)}>
326
+ Keep editing
327
+ </Button>
328
+ <Button type="button" className="!w-auto" onClick={() => goTo(leavingTo)}>
329
+ Discard changes
330
+ </Button>
331
+ </div>
332
+ </Modal>
333
+ )}
334
+ </div>
335
+ );
336
+ }
337
+
338
+ function DomainStep({ domains, onCreated }: { domains: Domain[]; onCreated: (domain: Domain) => void }) {
339
+ const [name, setName] = useState("");
340
+ const [error, setError] = useState<string | null>(null);
341
+ const [saving, setSaving] = useState(false);
342
+
343
+ async function handleSubmit(e: FormEvent) {
344
+ e.preventDefault();
345
+ setError(null);
346
+ if (!name.trim()) {
347
+ setError("A domain name is required.");
348
+ return;
349
+ }
350
+ setSaving(true);
351
+ try {
352
+ onCreated(await createDomain({ name: name.trim() }));
353
+ setName("");
354
+ } catch (err) {
355
+ setError(errorMessage(err, "Could not add the domain."));
356
+ } finally {
357
+ setSaving(false);
358
+ }
359
+ }
360
+
361
+ return (
362
+ <div className="flex flex-col gap-5 max-w-3xl">
363
+ {domains.map((domain) => (
364
+ <div key={domain.uid} className="flex flex-col gap-3">
365
+ <h3 className="text-base font-bold">{domain.name}</h3>
366
+ <DomainDnsSetup uid={domain.uid} />
367
+ </div>
368
+ ))}
369
+ {error && <Alert>{error}</Alert>}
370
+ <form onSubmit={handleSubmit} className={CARD_CLASS}>
371
+ <FormField label={domains.length === 0 ? "Domain name" : "Add another domain"} htmlFor="setupDomainName">
372
+ <input
373
+ id="setupDomainName"
374
+ type="text"
375
+ className={INPUT_CLASS}
376
+ value={name}
377
+ onChange={(e) => setName(e.target.value)}
378
+ placeholder="example.com"
379
+ />
380
+ </FormField>
381
+ <Button type="submit" loading={saving} disabled={saving} className="!w-auto">
382
+ Add domain
383
+ </Button>
384
+ </form>
385
+ </div>
386
+ );
387
+ }
388
+
389
+ function MailboxesStep({ userUid, domain }: { userUid: string; domain?: string }) {
390
+ const [mailboxes, setMailboxes] = useState<Mailbox[]>([]);
391
+ const [loaded, setLoaded] = useState(false);
392
+ const [error, setError] = useState<string | null>(null);
393
+ const [formKey, setFormKey] = useState(0);
394
+
395
+ useEffect(() => {
396
+ // The administration scope: every mailbox that exists (administrative metadata), not only the administrator's own.
397
+ listMailboxes({ limit: 100, scope: "admin" })
398
+ .then((list) =>
399
+ // Keep any mailbox created here while the list was loading.
400
+ setMailboxes((prev) => [...list, ...prev.filter((mine) => !list.some((mailbox) => mailbox.uid === mine.uid))]),
401
+ )
402
+ .catch((err) =>
403
+ setError(
404
+ `${errorMessage(err, "Could not load the existing mailboxes.")} Check the Mailboxes page before creating your own, so you don't create it twice.`,
405
+ ),
406
+ )
407
+ .finally(() => setLoaded(true));
408
+ }, []);
409
+
410
+ const hasOwn = mailboxes.some((mailbox) => mailbox.ownerUserUid === userUid);
411
+
412
+ return (
413
+ <div className="flex flex-col gap-5 max-w-xl">
414
+ {error && <Alert>{error}</Alert>}
415
+ {mailboxes.length > 0 && (
416
+ <div>
417
+ <h3 className="text-sm font-bold uppercase tracking-wide mb-2">Mailboxes created</h3>
418
+ <ul className="text-sm flex flex-col gap-1">
419
+ {mailboxes.map((mailbox) => (
420
+ <li key={mailbox.uid}>
421
+ {mailbox.displayName} &lt;{mailbox.primarySmtpAddress}&gt;
422
+ {mailbox.ownerUserUid === userUid && <span className="text-text-muted"> (yours)</span>}
423
+ </li>
424
+ ))}
425
+ </ul>
426
+ </div>
427
+ )}
428
+ {!loaded ? (
429
+ <p className="text-sm text-text-muted">Loading&hellip;</p>
430
+ ) : (
431
+ <>
432
+ <h3 className="text-base font-bold">{hasOwn ? "Add another mailbox" : "Your mailbox"}</h3>
433
+ <MailboxCreateForm
434
+ key={formKey}
435
+ submitLabel="Create mailbox"
436
+ defaults={hasOwn ? { domain } : { localPart: "admin", domain, displayName: "Administrator", ownerUserUid: userUid }}
437
+ onCreated={(mailbox) => {
438
+ setMailboxes((prev) => [...prev, mailbox]);
439
+ setFormKey((k) => k + 1);
440
+ }}
441
+ />
442
+ </>
443
+ )}
444
+ </div>
445
+ );
446
+ }