@rapidmx/web-client 0.9.0 → 0.10.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 (226) hide show
  1. package/README.md +192 -88
  2. package/apps/shared/auth/accountUrl.ts +9 -0
  3. package/apps/shared/auth/adminAccess.ts +99 -0
  4. package/apps/shared/components/admin/settings/DomainDnsSetup.tsx +2 -1
  5. package/apps/shared/components/calendar/layout/CalendarShell.tsx +8 -2
  6. package/apps/shared/components/contacts/ContactsToolbar.tsx +125 -115
  7. package/apps/shared/components/contacts/layout/ContactsShell.tsx +15 -3
  8. package/apps/shared/components/layout/AppShell.tsx +411 -288
  9. package/apps/shared/components/layout/KeyEnrollmentGate.tsx +398 -389
  10. package/apps/shared/components/layout/MailboxProvisioning.tsx +168 -164
  11. package/apps/shared/components/layout/UserMenu.tsx +303 -185
  12. package/apps/shared/components/mail/ConversationList.tsx +269 -262
  13. package/apps/shared/components/mail/ConversationThreadPane.tsx +475 -434
  14. package/apps/shared/components/mail/LazyReadingPane.tsx +115 -0
  15. package/apps/shared/components/mail/MailSelectionBar.tsx +235 -222
  16. package/apps/shared/components/mail/MessageDetailPane.tsx +1458 -1387
  17. package/apps/shared/components/mail/NewMailToasts.tsx +141 -0
  18. package/apps/shared/components/mail/compose/ComposeContext.tsx +279 -160
  19. package/apps/shared/components/mail/compose/ComposeToolbar.tsx +422 -418
  20. package/apps/shared/components/mail/compose/ComposeWindow.tsx +1842 -1736
  21. package/apps/shared/components/mail/compose/ComposeWindowPlaceholder.tsx +111 -0
  22. package/apps/shared/components/mail/compose/RichTextEditor.tsx +147 -118
  23. package/apps/shared/components/mail/compose/composePerf.ts +46 -0
  24. package/apps/shared/components/mail/compose/quotedBody.ts +161 -100
  25. package/apps/shared/components/mail/layout/MailShell.tsx +476 -472
  26. package/apps/shared/components/mail/unreadStyle.tsx +75 -0
  27. package/apps/shared/components/settings/layout/SettingsShell.tsx +252 -240
  28. package/apps/shared/components/tasks/layout/TasksShell.tsx +15 -3
  29. package/apps/shared/keyboard/GlobalShortcuts.tsx +51 -0
  30. package/apps/shared/keyboard/ShortcutProvider.tsx +62 -0
  31. package/apps/shared/keyboard/ShortcutsDialog.tsx +84 -0
  32. package/apps/shared/keyboard/dispatch.ts +124 -0
  33. package/apps/shared/keyboard/format.ts +89 -0
  34. package/apps/shared/keyboard/keymap.ts +114 -0
  35. package/apps/shared/keyboard/match.ts +44 -0
  36. package/apps/shared/keyboard/parse.ts +136 -0
  37. package/apps/shared/keyboard/platform.ts +34 -0
  38. package/apps/shared/keyboard/registry.ts +65 -0
  39. package/apps/shared/keyboard/targets.ts +79 -0
  40. package/apps/shared/keyboard/useShortcut.ts +50 -0
  41. package/apps/shared/keyboard/useShortcutProps.ts +17 -0
  42. package/apps/shared/mail/folderCounts.ts +302 -0
  43. package/apps/shared/mail/listSnapshots.ts +87 -0
  44. package/apps/shared/mail/messageReadState.ts +85 -0
  45. package/apps/shared/mail/newMailNotifications.ts +183 -0
  46. package/apps/shared/mail/useMailConnection.ts +150 -0
  47. package/apps/shared/mail/useMailLiveUpdates.ts +232 -224
  48. package/apps/shared/mail/useMarkMessageRead.ts +47 -0
  49. package/apps/shared/mail/useNewMailNotifications.ts +163 -0
  50. package/apps/shared/mail/useUnreadTitle.ts +42 -0
  51. package/apps/shared/navigation/AppRouter.tsx +300 -0
  52. package/apps/shared/navigation/appHrefs.ts +23 -0
  53. package/apps/shared/navigation/frameContext.tsx +35 -0
  54. package/apps/shared/navigation/idle.ts +45 -0
  55. package/apps/shared/navigation/routerContext.tsx +83 -0
  56. package/apps/shared/navigation/routes.ts +72 -0
  57. package/apps/shared/search/LocalIndexLifecycle.tsx +114 -98
  58. package/apps/shared/styles/app.css +28 -10
  59. package/apps/www/_routedPage.tsx +24 -0
  60. package/apps/www/_routes.ts +35 -0
  61. package/apps/www/calendar/index.tsx +55 -19
  62. package/apps/www/contacts/[uid].tsx +112 -107
  63. package/apps/www/contacts/index.tsx +584 -567
  64. package/apps/www/index.tsx +2280 -1917
  65. package/apps/www/messages/[uid].tsx +106 -101
  66. package/apps/www/settings/auto-reply/index.tsx +4 -1
  67. package/apps/www/settings/encryption/index.tsx +1252 -1249
  68. package/apps/www/settings/filters/[uid].tsx +4 -1
  69. package/apps/www/settings/filters/index.tsx +102 -99
  70. package/apps/www/settings/filters/new/index.tsx +138 -133
  71. package/apps/www/settings/labels/index.tsx +204 -201
  72. package/apps/www/settings/privacy/index.tsx +4 -1
  73. package/apps/www/settings/read-receipts/index.tsx +4 -1
  74. package/apps/www/settings/sharing/index.tsx +277 -274
  75. package/apps/www/settings/signatures/[uid].tsx +170 -167
  76. package/apps/www/settings/signatures/index.tsx +88 -85
  77. package/apps/www/settings/signatures/new/index.tsx +134 -129
  78. package/apps/www/tasks/index.tsx +18 -2
  79. package/dist/apps/shared/auth/accountUrl.d.ts +2 -0
  80. package/dist/apps/shared/auth/accountUrl.js +8 -0
  81. package/dist/apps/shared/auth/adminAccess.d.ts +30 -0
  82. package/dist/apps/shared/auth/adminAccess.js +89 -0
  83. package/dist/apps/shared/components/admin/settings/DomainDnsSetup.js +1 -1
  84. package/dist/apps/shared/components/calendar/layout/CalendarShell.d.ts +1 -1
  85. package/dist/apps/shared/components/calendar/layout/CalendarShell.js +8 -4
  86. package/dist/apps/shared/components/contacts/ContactsToolbar.d.ts +3 -1
  87. package/dist/apps/shared/components/contacts/ContactsToolbar.js +7 -4
  88. package/dist/apps/shared/components/contacts/layout/ContactsShell.d.ts +1 -1
  89. package/dist/apps/shared/components/contacts/layout/ContactsShell.js +15 -5
  90. package/dist/apps/shared/components/layout/AppShell.d.ts +29 -4
  91. package/dist/apps/shared/components/layout/AppShell.js +73 -14
  92. package/dist/apps/shared/components/layout/KeyEnrollmentGate.js +10 -9
  93. package/dist/apps/shared/components/layout/MailboxProvisioning.js +3 -1
  94. package/dist/apps/shared/components/layout/UserMenu.d.ts +19 -3
  95. package/dist/apps/shared/components/layout/UserMenu.js +52 -5
  96. package/dist/apps/shared/components/mail/ConversationList.js +4 -11
  97. package/dist/apps/shared/components/mail/ConversationThreadPane.d.ts +4 -1
  98. package/dist/apps/shared/components/mail/ConversationThreadPane.js +52 -27
  99. package/dist/apps/shared/components/mail/LazyReadingPane.d.ts +9 -0
  100. package/dist/apps/shared/components/mail/LazyReadingPane.js +82 -0
  101. package/dist/apps/shared/components/mail/MailSelectionBar.d.ts +4 -1
  102. package/dist/apps/shared/components/mail/MailSelectionBar.js +11 -3
  103. package/dist/apps/shared/components/mail/MessageDetailPane.d.ts +7 -1
  104. package/dist/apps/shared/components/mail/MessageDetailPane.js +83 -34
  105. package/dist/apps/shared/components/mail/NewMailToasts.d.ts +19 -0
  106. package/dist/apps/shared/components/mail/NewMailToasts.js +55 -0
  107. package/dist/apps/shared/components/mail/compose/ComposeContext.d.ts +30 -0
  108. package/dist/apps/shared/components/mail/compose/ComposeContext.js +71 -5
  109. package/dist/apps/shared/components/mail/compose/ComposeToolbar.js +4 -3
  110. package/dist/apps/shared/components/mail/compose/ComposeWindow.d.ts +1 -1
  111. package/dist/apps/shared/components/mail/compose/ComposeWindow.js +98 -22
  112. package/dist/apps/shared/components/mail/compose/ComposeWindowPlaceholder.d.ts +19 -0
  113. package/dist/apps/shared/components/mail/compose/ComposeWindowPlaceholder.js +33 -0
  114. package/dist/apps/shared/components/mail/compose/RichTextEditor.d.ts +9 -1
  115. package/dist/apps/shared/components/mail/compose/RichTextEditor.js +20 -2
  116. package/dist/apps/shared/components/mail/compose/composePerf.d.ts +16 -0
  117. package/dist/apps/shared/components/mail/compose/composePerf.js +41 -0
  118. package/dist/apps/shared/components/mail/compose/quotedBody.d.ts +13 -0
  119. package/dist/apps/shared/components/mail/compose/quotedBody.js +67 -11
  120. package/dist/apps/shared/components/mail/layout/MailShell.d.ts +12 -4
  121. package/dist/apps/shared/components/mail/layout/MailShell.js +76 -96
  122. package/dist/apps/shared/components/mail/unreadStyle.d.ts +45 -0
  123. package/dist/apps/shared/components/mail/unreadStyle.js +61 -0
  124. package/dist/apps/shared/components/settings/layout/SettingsShell.d.ts +1 -1
  125. package/dist/apps/shared/components/settings/layout/SettingsShell.js +15 -5
  126. package/dist/apps/shared/components/tasks/layout/TasksShell.d.ts +1 -1
  127. package/dist/apps/shared/components/tasks/layout/TasksShell.js +15 -5
  128. package/dist/apps/shared/keyboard/GlobalShortcuts.d.ts +14 -0
  129. package/dist/apps/shared/keyboard/GlobalShortcuts.js +37 -0
  130. package/dist/apps/shared/keyboard/ShortcutProvider.d.ts +20 -0
  131. package/dist/apps/shared/keyboard/ShortcutProvider.js +52 -0
  132. package/dist/apps/shared/keyboard/ShortcutsDialog.d.ts +14 -0
  133. package/dist/apps/shared/keyboard/ShortcutsDialog.js +42 -0
  134. package/dist/apps/shared/keyboard/dispatch.d.ts +16 -0
  135. package/dist/apps/shared/keyboard/dispatch.js +109 -0
  136. package/dist/apps/shared/keyboard/format.d.ts +12 -0
  137. package/dist/apps/shared/keyboard/format.js +74 -0
  138. package/dist/apps/shared/keyboard/keymap.d.ts +294 -0
  139. package/dist/apps/shared/keyboard/keymap.js +84 -0
  140. package/dist/apps/shared/keyboard/match.d.ts +20 -0
  141. package/dist/apps/shared/keyboard/match.js +31 -0
  142. package/dist/apps/shared/keyboard/parse.d.ts +31 -0
  143. package/dist/apps/shared/keyboard/parse.js +107 -0
  144. package/dist/apps/shared/keyboard/platform.d.ts +15 -0
  145. package/dist/apps/shared/keyboard/platform.js +22 -0
  146. package/dist/apps/shared/keyboard/registry.d.ts +39 -0
  147. package/dist/apps/shared/keyboard/registry.js +33 -0
  148. package/dist/apps/shared/keyboard/targets.d.ts +14 -0
  149. package/dist/apps/shared/keyboard/targets.js +67 -0
  150. package/dist/apps/shared/keyboard/useShortcut.d.ts +19 -0
  151. package/dist/apps/shared/keyboard/useShortcut.js +35 -0
  152. package/dist/apps/shared/keyboard/useShortcutProps.d.ts +10 -0
  153. package/dist/apps/shared/keyboard/useShortcutProps.js +15 -0
  154. package/dist/apps/shared/mail/folderCounts.d.ts +78 -0
  155. package/dist/apps/shared/mail/folderCounts.js +212 -0
  156. package/dist/apps/shared/mail/listSnapshots.d.ts +46 -0
  157. package/dist/apps/shared/mail/listSnapshots.js +43 -0
  158. package/dist/apps/shared/mail/messageReadState.d.ts +32 -0
  159. package/dist/apps/shared/mail/messageReadState.js +63 -0
  160. package/dist/apps/shared/mail/newMailNotifications.d.ts +62 -0
  161. package/dist/apps/shared/mail/newMailNotifications.js +138 -0
  162. package/dist/apps/shared/mail/useMailConnection.d.ts +55 -0
  163. package/dist/apps/shared/mail/useMailConnection.js +96 -0
  164. package/dist/apps/shared/mail/useMailLiveUpdates.d.ts +13 -6
  165. package/dist/apps/shared/mail/useMailLiveUpdates.js +28 -26
  166. package/dist/apps/shared/mail/useMarkMessageRead.d.ts +12 -0
  167. package/dist/apps/shared/mail/useMarkMessageRead.js +44 -0
  168. package/dist/apps/shared/mail/useNewMailNotifications.d.ts +41 -0
  169. package/dist/apps/shared/mail/useNewMailNotifications.js +110 -0
  170. package/dist/apps/shared/mail/useUnreadTitle.d.ts +18 -0
  171. package/dist/apps/shared/mail/useUnreadTitle.js +31 -0
  172. package/dist/apps/shared/navigation/AppRouter.d.ts +52 -0
  173. package/dist/apps/shared/navigation/AppRouter.js +242 -0
  174. package/dist/apps/shared/navigation/appHrefs.d.ts +14 -0
  175. package/dist/apps/shared/navigation/appHrefs.js +20 -0
  176. package/dist/apps/shared/navigation/frameContext.d.ts +19 -0
  177. package/dist/apps/shared/navigation/frameContext.js +24 -0
  178. package/dist/apps/shared/navigation/idle.d.ts +14 -0
  179. package/dist/apps/shared/navigation/idle.js +43 -0
  180. package/dist/apps/shared/navigation/routerContext.d.ts +37 -0
  181. package/dist/apps/shared/navigation/routerContext.js +56 -0
  182. package/dist/apps/shared/navigation/routes.d.ts +32 -0
  183. package/dist/apps/shared/navigation/routes.js +37 -0
  184. package/dist/apps/shared/search/LocalIndexLifecycle.js +17 -3
  185. package/dist/apps/shared/styles/app.css +28 -10
  186. package/dist/apps/www/_routedPage.d.ts +12 -0
  187. package/dist/apps/www/_routedPage.js +19 -0
  188. package/dist/apps/www/_routes.d.ts +11 -0
  189. package/dist/apps/www/_routes.js +29 -0
  190. package/dist/apps/www/calendar/index.d.ts +2 -2
  191. package/dist/apps/www/calendar/index.js +39 -8
  192. package/dist/apps/www/contacts/[uid].d.ts +3 -9
  193. package/dist/apps/www/contacts/[uid].js +6 -2
  194. package/dist/apps/www/contacts/index.d.ts +2 -2
  195. package/dist/apps/www/contacts/index.js +17 -4
  196. package/dist/apps/www/index.d.ts +2 -2
  197. package/dist/apps/www/index.js +301 -35
  198. package/dist/apps/www/messages/[uid].d.ts +3 -7
  199. package/dist/apps/www/messages/[uid].js +7 -4
  200. package/dist/apps/www/settings/auto-reply/index.d.ts +2 -2
  201. package/dist/apps/www/settings/auto-reply/index.js +3 -1
  202. package/dist/apps/www/settings/encryption/index.d.ts +2 -2
  203. package/dist/apps/www/settings/encryption/index.js +3 -1
  204. package/dist/apps/www/settings/filters/[uid].d.ts +2 -2
  205. package/dist/apps/www/settings/filters/[uid].js +3 -1
  206. package/dist/apps/www/settings/filters/index.d.ts +2 -2
  207. package/dist/apps/www/settings/filters/index.js +3 -1
  208. package/dist/apps/www/settings/filters/new/index.d.ts +2 -2
  209. package/dist/apps/www/settings/filters/new/index.js +6 -2
  210. package/dist/apps/www/settings/labels/index.d.ts +2 -2
  211. package/dist/apps/www/settings/labels/index.js +3 -1
  212. package/dist/apps/www/settings/privacy/index.d.ts +2 -2
  213. package/dist/apps/www/settings/privacy/index.js +3 -1
  214. package/dist/apps/www/settings/read-receipts/index.d.ts +2 -2
  215. package/dist/apps/www/settings/read-receipts/index.js +3 -1
  216. package/dist/apps/www/settings/sharing/index.d.ts +2 -2
  217. package/dist/apps/www/settings/sharing/index.js +3 -1
  218. package/dist/apps/www/settings/signatures/[uid].d.ts +2 -2
  219. package/dist/apps/www/settings/signatures/[uid].js +3 -1
  220. package/dist/apps/www/settings/signatures/index.d.ts +2 -2
  221. package/dist/apps/www/settings/signatures/index.js +3 -1
  222. package/dist/apps/www/settings/signatures/new/index.d.ts +2 -2
  223. package/dist/apps/www/settings/signatures/new/index.js +6 -2
  224. package/dist/apps/www/tasks/index.d.ts +2 -2
  225. package/dist/apps/www/tasks/index.js +15 -3
  226. package/package.json +2 -2
@@ -1,164 +1,168 @@
1
- ///////////////////////////////////////////////////////////////////////////////
2
- // Copyright (C) 2026 Jean-Philippe Steinmetz
3
- // SPDX-License-Identifier: MPL-2.0
4
- ///////////////////////////////////////////////////////////////////////////////
5
- import React, { useEffect, useState } from "react";
6
- import { ApiRequestError } from "@rapidmx/react-shared/util/api.js";
7
- import { MailboxAutoProvisionAliasOption, autoProvisionMailbox } from "@rapidmx/react-shared/mail/mailApi.js";
8
- import Alert from "@rapidmx/react-shared/components/feedback/Alert.js";
9
- import Button from "@rapidmx/react-shared/components/buttons/Button.js";
10
-
11
- type Status = "checking" | "needs_selection" | "creating" | "unavailable" | "retryable";
12
-
13
- /** What the "Retry" screen says: the server couldn't read its provisioning policy (503) ... */
14
- const RETRY_POLICY_TEXT = "We couldn\u2019t check whether a mailbox can be set up for you. Please try again.";
15
- /** ... or couldn't reach the identity service (auth-server) it looks the caller's username up in (502). */
16
- const RETRY_IDENTITY_TEXT = "We couldn\u2019t reach the identity service to set up your mailbox. Please try again.";
17
-
18
- /** The longest server message shown as the reason a mailbox isn't available - a real reason is a sentence. */
19
- const MAX_REASON_LENGTH = 200;
20
-
21
- /**
22
- * The reason to show under "No mailbox available": the server's own message, for a refusal it made on purpose - a
23
- * 4xx such as 404 "Automatic mailbox provisioning is not enabled." or 404 "No username is registered for this account."
24
- * Never for a 5xx (whose message is whatever went wrong inside the server) or a failure that never reached it (a network
25
- * error's message is browser jargon), and only its first line, capped in length. `null` when there's nothing safe to show.
26
- */
27
- function unavailableReason(err: unknown): string | null {
28
- if (!(err instanceof ApiRequestError) || err.status < 400 || err.status >= 500) {
29
- return null;
30
- }
31
- const line = err.message.split(/\r?\n/)[0].trim();
32
- return line ? line.slice(0, MAX_REASON_LENGTH) : null;
33
- }
34
-
35
- /**
36
- * Rendered by each app's shell (Mail/Calendar/Contacts/Tasks) *instead of* the normal `AppShell`
37
- * chrome (icon rail, header, folder tree, ...) when the caller has no mailbox — a full-screen,
38
- * 404-style takeover, not a small message nested inside the rest of the client UI, since there's
39
- * nothing else in the app for a mailbox-less caller to do yet. Attempts self-service
40
- * auto-provisioning — see `BaseMailboxRoute.autoProvision()`'s own doc comment in `@rapidmx/restapi`
41
- * for the full contract — which is itself a no-op (a 404) unless an admin has both turned it on
42
- * (`mail:auto_provision:enabled`) and configured at least one domain (`mail:domains`). Safe to render
43
- * unconditionally in that place: a failure ends in one of two screens. A 502 (the identity service couldn't be
44
- * reached) or 503 (the server couldn't read its provisioning policy) is transient and offers "Retry". Anything
45
- * else is "No mailbox available - ask an administrator", now with the server's own reason above that advice when it
46
- * refused deliberately (a 4xx, e.g. "Automatic mailbox provisioning is not enabled.") so the caller and the
47
- * administrator they ask can tell why - see `unavailableReason()`.
48
- */
49
- export default function MailboxProvisioning() {
50
- const [status, setStatus] = useState<Status>("checking");
51
- const [options, setOptions] = useState<MailboxAutoProvisionAliasOption[]>([]);
52
- const [selected, setSelected] = useState("");
53
- const [error, setError] = useState<string | null>(null);
54
- // Why the caller has no mailbox (see `unavailableReason()`), and what the retry screen says.
55
- const [reason, setReason] = useState<string | null>(null);
56
- const [retryText, setRetryText] = useState(RETRY_POLICY_TEXT);
57
-
58
- // Bumped by "Retry" to re-run the check below.
59
- const [attempt, setAttempt] = useState(0);
60
-
61
- useEffect(() => {
62
- setStatus("checking");
63
- autoProvisionMailbox()
64
- .then((result) => {
65
- if (result.status === "needs_selection") {
66
- // `autoProvision()` only ever returns this status with a non-empty cross product of
67
- // the caller's aliases and this server's configured domains (both are checked
68
- // non-empty before it's built) — no fallback needed for an empty list here.
69
- setOptions(result.options);
70
- setSelected(result.options[0].primarySmtpAddress);
71
- setStatus("needs_selection");
72
- } else {
73
- // "created" or "existing" — either way the caller now has a mailbox; a full reload is
74
- // this framework's own convention for picking up new server-side state (no client
75
- // router to instead re-fetch just the shell's own data in place).
76
- window.location.reload();
77
- }
78
- })
79
- // A 503 means the server couldn't read its own provisioning policy right now, and a 502 that it couldn't
80
- // reach the identity service - neither says there's no mailbox to be had - so they get a retry instead of
81
- // the permanent "ask an administrator" message.
82
- .catch((err) => {
83
- if (err instanceof ApiRequestError && (err.status === 503 || err.status === 502)) {
84
- setRetryText(err.status === 502 ? RETRY_IDENTITY_TEXT : RETRY_POLICY_TEXT);
85
- setStatus("retryable");
86
- } else {
87
- setReason(unavailableReason(err));
88
- setStatus("unavailable");
89
- }
90
- });
91
- }, [attempt]);
92
-
93
- async function handleConfirm() {
94
- // `selected` only ever takes a value from `options` itself (the initial default, or the
95
- // <select>'s own onChange, which can only report one of its own rendered <option> values), so
96
- // this is always found — no defensive fallback needed.
97
- const option = options.find((o) => o.primarySmtpAddress === selected)!;
98
- setStatus("creating");
99
- setError(null);
100
- try {
101
- await autoProvisionMailbox({ alias: option.alias, domain: option.domain });
102
- window.location.reload();
103
- } catch (err) {
104
- setError(err instanceof ApiRequestError ? err.message : "Could not create your mailbox.");
105
- setStatus("needs_selection");
106
- }
107
- }
108
-
109
- let content: React.ReactNode;
110
- if (status === "checking") {
111
- content = <p className="text-sm text-text-muted">Setting up your mailbox&hellip;</p>;
112
- } else if (status === "needs_selection" || status === "creating") {
113
- const creating = status === "creating";
114
- content = (
115
- <div className="w-full flex flex-col gap-3">
116
- <p className="text-sm text-text-muted text-center">Choose your mailbox address to get started:</p>
117
- {error && <Alert>{error}</Alert>}
118
- <select
119
- aria-label="Mailbox address"
120
- className="text-sm border border-border rounded-sm py-1.5 px-2 bg-surface"
121
- value={selected}
122
- onChange={(e) => setSelected(e.target.value)}
123
- disabled={creating}
124
- >
125
- {options.map((option) => (
126
- <option key={option.primarySmtpAddress} value={option.primarySmtpAddress}>
127
- {option.primarySmtpAddress}
128
- </option>
129
- ))}
130
- </select>
131
- <Button type="button" onClick={handleConfirm} loading={creating} disabled={creating} className="!w-auto self-center">
132
- Continue
133
- </Button>
134
- </div>
135
- );
136
- } else if (status === "retryable") {
137
- content = (
138
- <>
139
- <h1 className="text-lg font-bold uppercase tracking-wide">Couldn&rsquo;t check right now</h1>
140
- <p className="text-sm text-text-muted">{retryText}</p>
141
- <Button type="button" onClick={() => setAttempt((n) => n + 1)} className="!w-auto self-center">
142
- Retry
143
- </Button>
144
- </>
145
- );
146
- } else {
147
- content = (
148
- <>
149
- <h1 className="text-lg font-bold uppercase tracking-wide">No mailbox available</h1>
150
- {reason && <p className="text-sm">{reason}</p>}
151
- <p className="text-sm text-text-muted">Ask an administrator to create one for you.</p>
152
- </>
153
- );
154
- }
155
-
156
- return (
157
- <div className="min-h-screen flex items-center justify-center p-8 bg-surface-alt">
158
- <div className="w-full max-w-md bg-surface border border-border rounded-md p-8 flex flex-col items-center gap-4 text-center">
159
- <img src="/images/wordmark.png" height="128" alt="" />
160
- {content}
161
- </div>
162
- </div>
163
- );
164
- }
1
+ ///////////////////////////////////////////////////////////////////////////////
2
+ // Copyright (C) 2026 Jean-Philippe Steinmetz
3
+ // SPDX-License-Identifier: MPL-2.0
4
+ ///////////////////////////////////////////////////////////////////////////////
5
+ import React, { useEffect, useState } from "react";
6
+ import { ApiRequestError } from "@rapidmx/react-shared/util/api.js";
7
+ import { MailboxAutoProvisionAliasOption, autoProvisionMailbox } from "@rapidmx/react-shared/mail/mailApi.js";
8
+ import Alert from "@rapidmx/react-shared/components/feedback/Alert.js";
9
+ import Button from "@rapidmx/react-shared/components/buttons/Button.js";
10
+ import { FrameTakeover } from "../../navigation/frameContext.js";
11
+
12
+ type Status = "checking" | "needs_selection" | "creating" | "unavailable" | "retryable";
13
+
14
+ /** What the "Retry" screen says: the server couldn't read its provisioning policy (503) ... */
15
+ const RETRY_POLICY_TEXT = "We couldn\u2019t check whether a mailbox can be set up for you. Please try again.";
16
+ /** ... or couldn't reach the identity service (auth-server) it looks the caller's username up in (502). */
17
+ const RETRY_IDENTITY_TEXT = "We couldn\u2019t reach the identity service to set up your mailbox. Please try again.";
18
+
19
+ /** The longest server message shown as the reason a mailbox isn't available - a real reason is a sentence. */
20
+ const MAX_REASON_LENGTH = 200;
21
+
22
+ /**
23
+ * The reason to show under "No mailbox available": the server's own message, for a refusal it made on purpose - a
24
+ * 4xx such as 404 "Automatic mailbox provisioning is not enabled." or 404 "No username is registered for this account."
25
+ * Never for a 5xx (whose message is whatever went wrong inside the server) or a failure that never reached it (a network
26
+ * error's message is browser jargon), and only its first line, capped in length. `null` when there's nothing safe to show.
27
+ */
28
+ function unavailableReason(err: unknown): string | null {
29
+ if (!(err instanceof ApiRequestError) || err.status < 400 || err.status >= 500) {
30
+ return null;
31
+ }
32
+ const line = err.message.split(/\r?\n/)[0].trim();
33
+ return line ? line.slice(0, MAX_REASON_LENGTH) : null;
34
+ }
35
+
36
+ /**
37
+ * Rendered by each app's shell (Mail/Calendar/Contacts/Tasks) *instead of* the normal `AppShell`
38
+ * chrome (icon rail, header, folder tree, ...) when the caller has no mailbox — a full-screen,
39
+ * 404-style takeover, not a small message nested inside the rest of the client UI, since there's
40
+ * nothing else in the app for a mailbox-less caller to do yet. Attempts self-service
41
+ * auto-provisioning — see `BaseMailboxRoute.autoProvision()`'s own doc comment in `@rapidmx/restapi`
42
+ * for the full contract — which is itself a no-op (a 404) unless an admin has both turned it on
43
+ * (`mail:auto_provision:enabled`) and configured at least one domain (`mail:domains`). Safe to render
44
+ * unconditionally in that place: a failure ends in one of two screens. A 502 (the identity service couldn't be
45
+ * reached) or 503 (the server couldn't read its provisioning policy) is transient and offers "Retry". Anything
46
+ * else is "No mailbox available - ask an administrator", now with the server's own reason above that advice when it
47
+ * refused deliberately (a 4xx, e.g. "Automatic mailbox provisioning is not enabled.") so the caller and the
48
+ * administrator they ask can tell why - see `unavailableReason()`.
49
+ */
50
+ export default function MailboxProvisioning() {
51
+ const [status, setStatus] = useState<Status>("checking");
52
+ const [options, setOptions] = useState<MailboxAutoProvisionAliasOption[]>([]);
53
+ const [selected, setSelected] = useState("");
54
+ const [error, setError] = useState<string | null>(null);
55
+ // Why the caller has no mailbox (see `unavailableReason()`), and what the retry screen says.
56
+ const [reason, setReason] = useState<string | null>(null);
57
+ const [retryText, setRetryText] = useState(RETRY_POLICY_TEXT);
58
+
59
+ // Bumped by "Retry" to re-run the check below.
60
+ const [attempt, setAttempt] = useState(0);
61
+
62
+ useEffect(() => {
63
+ setStatus("checking");
64
+ autoProvisionMailbox()
65
+ .then((result) => {
66
+ if (result.status === "needs_selection") {
67
+ // `autoProvision()` only ever returns this status with a non-empty cross product of
68
+ // the caller's aliases and this server's configured domains (both are checked
69
+ // non-empty before it's built) — no fallback needed for an empty list here.
70
+ setOptions(result.options);
71
+ setSelected(result.options[0].primarySmtpAddress);
72
+ setStatus("needs_selection");
73
+ } else {
74
+ // "created" or "existing" — either way the caller now has a mailbox; a full reload is
75
+ // this framework's own convention for picking up new server-side state (no client
76
+ // router to instead re-fetch just the shell's own data in place).
77
+ window.location.reload();
78
+ }
79
+ })
80
+ // A 503 means the server couldn't read its own provisioning policy right now, and a 502 that it couldn't
81
+ // reach the identity service - neither says there's no mailbox to be had - so they get a retry instead of
82
+ // the permanent "ask an administrator" message.
83
+ .catch((err) => {
84
+ if (err instanceof ApiRequestError && (err.status === 503 || err.status === 502)) {
85
+ setRetryText(err.status === 502 ? RETRY_IDENTITY_TEXT : RETRY_POLICY_TEXT);
86
+ setStatus("retryable");
87
+ } else {
88
+ setReason(unavailableReason(err));
89
+ setStatus("unavailable");
90
+ }
91
+ });
92
+ }, [attempt]);
93
+
94
+ async function handleConfirm() {
95
+ // `selected` only ever takes a value from `options` itself (the initial default, or the
96
+ // <select>'s own onChange, which can only report one of its own rendered <option> values), so
97
+ // this is always found — no defensive fallback needed.
98
+ const option = options.find((o) => o.primarySmtpAddress === selected)!;
99
+ setStatus("creating");
100
+ setError(null);
101
+ try {
102
+ await autoProvisionMailbox({ alias: option.alias, domain: option.domain });
103
+ window.location.reload();
104
+ } catch (err) {
105
+ setError(err instanceof ApiRequestError ? err.message : "Could not create your mailbox.");
106
+ setStatus("needs_selection");
107
+ }
108
+ }
109
+
110
+ let content: React.ReactNode;
111
+ if (status === "checking") {
112
+ content = <p className="text-sm text-text-muted">Setting up your mailbox&hellip;</p>;
113
+ } else if (status === "needs_selection" || status === "creating") {
114
+ const creating = status === "creating";
115
+ content = (
116
+ <div className="w-full flex flex-col gap-3">
117
+ <p className="text-sm text-text-muted text-center">Choose your mailbox address to get started:</p>
118
+ {error && <Alert>{error}</Alert>}
119
+ <select
120
+ aria-label="Mailbox address"
121
+ className="text-sm border border-border rounded-sm py-1.5 px-2 bg-surface"
122
+ value={selected}
123
+ onChange={(e) => setSelected(e.target.value)}
124
+ disabled={creating}
125
+ >
126
+ {options.map((option) => (
127
+ <option key={option.primarySmtpAddress} value={option.primarySmtpAddress}>
128
+ {option.primarySmtpAddress}
129
+ </option>
130
+ ))}
131
+ </select>
132
+ <Button type="button" onClick={handleConfirm} loading={creating} disabled={creating} className="!w-auto self-center">
133
+ Continue
134
+ </Button>
135
+ </div>
136
+ );
137
+ } else if (status === "retryable") {
138
+ content = (
139
+ <>
140
+ <h1 className="text-lg font-bold uppercase tracking-wide">Couldn&rsquo;t check right now</h1>
141
+ <p className="text-sm text-text-muted">{retryText}</p>
142
+ <Button type="button" onClick={() => setAttempt((n) => n + 1)} className="!w-auto self-center">
143
+ Retry
144
+ </Button>
145
+ </>
146
+ );
147
+ } else {
148
+ content = (
149
+ <>
150
+ <h1 className="text-lg font-bold uppercase tracking-wide">No mailbox available</h1>
151
+ {reason && <p className="text-sm">{reason}</p>}
152
+ <p className="text-sm text-text-muted">Ask an administrator to create one for you.</p>
153
+ </>
154
+ );
155
+ }
156
+
157
+ // Inside the client-side router's persistent frame this hides the frame's rail and header, as replacing the whole shell used to.
158
+ return (
159
+ <FrameTakeover>
160
+ <div className="min-h-screen flex items-center justify-center p-8 bg-surface-alt">
161
+ <div className="w-full max-w-md bg-surface border border-border rounded-md p-8 flex flex-col items-center gap-4 text-center">
162
+ <img src="/images/wordmark.png" height="128" alt="" />
163
+ {content}
164
+ </div>
165
+ </div>
166
+ </FrameTakeover>
167
+ );
168
+ }