@rapidmx/web-client 0.3.1 → 0.5.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 (202) hide show
  1. package/apps/admin/audit-log/index.tsx +35 -8
  2. package/apps/admin/branding/index.tsx +39 -404
  3. package/apps/admin/data-requests/index.tsx +480 -485
  4. package/apps/admin/distribution-lists/[uid].tsx +19 -1
  5. package/apps/admin/domains/[uid].tsx +92 -259
  6. package/apps/admin/encryption-policy/index.tsx +19 -0
  7. package/apps/admin/escrow-scopes/[uid].tsx +348 -237
  8. package/apps/admin/escrow-scopes/new/index.tsx +8 -2
  9. package/apps/admin/index.tsx +118 -82
  10. package/apps/admin/ingest-queue/index.tsx +44 -5
  11. package/apps/admin/mailbox-policy/index.tsx +19 -0
  12. package/apps/admin/mailboxes/[uid].tsx +211 -184
  13. package/apps/admin/mailboxes/new/index.tsx +28 -291
  14. package/apps/admin/plugins/index.tsx +15 -0
  15. package/apps/admin/quarantine/index.tsx +46 -14
  16. package/apps/admin/retention-policy/index.tsx +39 -132
  17. package/apps/admin/setup/index.tsx +15 -0
  18. package/apps/admin/transport-rules/[uid].tsx +30 -1
  19. package/apps/admin/transport-rules/_transportRuleConfig.tsx +38 -2
  20. package/apps/admin/transport-rules/index.tsx +24 -6
  21. package/apps/admin/transport-rules/new/index.tsx +30 -1
  22. package/apps/escrow/_layout.tsx +2 -3
  23. package/apps/escrow/audit-log/index.tsx +196 -168
  24. package/apps/escrow/matters/[uid].tsx +621 -521
  25. package/apps/shared/components/admin/escrowScopes/EscrowScopeKeyAndHoldersFields.tsx +22 -1
  26. package/apps/shared/components/admin/layout/AdminShell.tsx +251 -210
  27. package/apps/shared/components/admin/mailboxes/EscrowScopeCard.tsx +152 -0
  28. package/apps/shared/components/admin/mailboxes/ResourceSettingsCard.tsx +3 -3
  29. package/apps/shared/components/admin/mailboxes/ShareAccessCard.tsx +55 -11
  30. package/apps/shared/components/admin/settings/BrandingForm.tsx +385 -0
  31. package/apps/shared/components/admin/settings/DomainDnsSetup.tsx +187 -0
  32. package/apps/shared/components/admin/settings/EncryptionPolicyForm.tsx +112 -0
  33. package/apps/shared/components/admin/settings/LoadedSettingsForm.tsx +34 -0
  34. package/apps/shared/components/admin/settings/MailboxCreateForm.tsx +308 -0
  35. package/apps/shared/components/admin/settings/MailboxPolicyForm.tsx +152 -0
  36. package/apps/shared/components/admin/settings/PluginsManager.tsx +1235 -0
  37. package/apps/shared/components/admin/settings/RetentionPolicyForm.tsx +174 -0
  38. package/apps/shared/components/admin/setup/EscrowSetupStep.tsx +290 -0
  39. package/apps/shared/components/admin/setup/SetupWizard.tsx +416 -0
  40. package/apps/shared/components/admin/signOut.ts +79 -0
  41. package/apps/shared/components/admin/usePagedList.tsx +129 -0
  42. package/apps/shared/components/calendar/CalendarListSidebar.tsx +120 -76
  43. package/apps/shared/components/calendar/EventModal.tsx +717 -513
  44. package/apps/shared/components/calendar/MonthView.tsx +173 -139
  45. package/apps/shared/components/calendar/RecurrenceEditor.tsx +222 -180
  46. package/apps/shared/components/calendar/SplitDayView.tsx +143 -137
  47. package/apps/shared/components/calendar/TimeGridView.tsx +244 -198
  48. package/apps/shared/components/calendar/allDay.ts +124 -0
  49. package/apps/shared/components/calendar/layout/CalendarShell.tsx +80 -99
  50. package/apps/shared/components/contacts/ContactDetailPane.tsx +100 -21
  51. package/apps/shared/components/contacts/ContactForm.tsx +377 -314
  52. package/apps/shared/components/contacts/ContactsSidebar.tsx +6 -2
  53. package/apps/shared/components/contacts/KeyChangeReview.tsx +192 -0
  54. package/apps/shared/components/contacts/contactKeys.ts +78 -0
  55. package/apps/shared/components/escrow/layout/EscrowShell.tsx +148 -134
  56. package/apps/shared/components/layout/AppShell.tsx +270 -169
  57. package/apps/shared/components/layout/BrandingChrome.tsx +48 -10
  58. package/apps/shared/components/layout/KeyEnrollmentGate.tsx +150 -26
  59. package/apps/shared/components/layout/MailboxProvisioning.tsx +19 -3
  60. package/apps/shared/components/layout/RecoveryCodeUnlock.tsx +350 -0
  61. package/apps/shared/components/layout/UnlockPromptProvider.tsx +257 -0
  62. package/apps/shared/components/mail/ConversationThreadPane.tsx +216 -181
  63. package/apps/shared/components/mail/MessageDetailPane.tsx +799 -76
  64. package/apps/shared/components/mail/compose/ComposeContext.tsx +26 -16
  65. package/apps/shared/components/mail/compose/ComposeWindow.tsx +1652 -765
  66. package/apps/shared/components/mail/compose/composeFlushRegistry.ts +60 -0
  67. package/apps/shared/components/mail/layout/MailShell.tsx +418 -302
  68. package/apps/shared/components/mail/pinnedSigners.ts +124 -0
  69. package/apps/shared/components/mail/verificationSeals.ts +125 -0
  70. package/apps/shared/components/mail/writableMailboxes.ts +101 -0
  71. package/apps/shared/components/rules/RuleBuilder.tsx +311 -276
  72. package/apps/shared/components/settings/layout/SettingsShell.tsx +1 -0
  73. package/apps/shared/mail/findWellKnownFolderUid.ts +13 -0
  74. package/apps/shared/mail/listAllPages.ts +39 -0
  75. package/apps/shared/search/LocalIndexLifecycle.tsx +98 -0
  76. package/apps/shared/search/localIndexBlockCipher.ts +83 -0
  77. package/apps/shared/search/localIndexBuilder.ts +481 -0
  78. package/apps/shared/search/localIndexKey.ts +27 -0
  79. package/apps/shared/search/localIndexRpcClient.ts +316 -0
  80. package/apps/shared/search/localIndexSchema.ts +236 -0
  81. package/apps/shared/search/localIndexSizePreference.ts +88 -0
  82. package/apps/shared/search/localIndexStorage.ts +98 -0
  83. package/apps/shared/search/localIndexVFS.ts +266 -0
  84. package/apps/shared/search/localIndexWorker.ts +926 -0
  85. package/apps/shared/search/searchTier2.ts +79 -0
  86. package/apps/shared/search/wa-sqlite-shims.d.ts +44 -0
  87. package/apps/www/calendar/index.tsx +438 -407
  88. package/apps/www/contacts/[uid].tsx +21 -2
  89. package/apps/www/contacts/index.tsx +148 -17
  90. package/apps/www/index.tsx +1314 -486
  91. package/apps/www/messages/[uid].tsx +23 -7
  92. package/apps/www/settings/auto-reply/index.tsx +134 -129
  93. package/apps/www/settings/booking-types/[uid].tsx +306 -300
  94. package/apps/www/settings/encryption/index.tsx +800 -215
  95. package/apps/www/settings/filters/[uid].tsx +171 -155
  96. package/apps/www/settings/filters/new/index.tsx +8 -1
  97. package/apps/www/settings/focused-inbox/index.tsx +150 -148
  98. package/apps/www/settings/privacy/index.tsx +494 -436
  99. package/apps/www/settings/read-receipts/index.tsx +148 -125
  100. package/apps/www/settings/sharing/index.tsx +274 -0
  101. package/apps/www/tasks/index.tsx +638 -530
  102. package/dist/apps/admin/audit-log/index.js +38 -8
  103. package/dist/apps/admin/branding/index.js +4 -98
  104. package/dist/apps/admin/data-requests/index.js +52 -72
  105. package/dist/apps/admin/distribution-lists/[uid].js +1 -1
  106. package/dist/apps/admin/domains/[uid].js +5 -68
  107. package/dist/apps/admin/encryption-policy/index.js +8 -0
  108. package/dist/apps/admin/escrow-scopes/[uid].js +91 -12
  109. package/dist/apps/admin/escrow-scopes/new/index.js +8 -3
  110. package/dist/apps/admin/index.js +17 -1
  111. package/dist/apps/admin/ingest-queue/index.js +24 -6
  112. package/dist/apps/admin/mailbox-policy/index.js +8 -0
  113. package/dist/apps/admin/mailboxes/[uid].js +20 -4
  114. package/dist/apps/admin/mailboxes/new/index.js +4 -89
  115. package/dist/apps/admin/plugins/index.js +6 -0
  116. package/dist/apps/admin/quarantine/index.js +20 -11
  117. package/dist/apps/admin/retention-policy/index.js +3 -36
  118. package/dist/apps/admin/setup/index.js +6 -0
  119. package/dist/apps/admin/transport-rules/[uid].js +18 -2
  120. package/dist/apps/admin/transport-rules/_transportRuleConfig.js +19 -0
  121. package/dist/apps/admin/transport-rules/index.js +22 -6
  122. package/dist/apps/admin/transport-rules/new/index.js +18 -2
  123. package/dist/apps/escrow/_layout.js +3 -3
  124. package/dist/apps/escrow/audit-log/index.js +26 -1
  125. package/dist/apps/escrow/matters/[uid].js +77 -42
  126. package/dist/apps/shared/components/admin/escrowScopes/EscrowScopeKeyAndHoldersFields.js +11 -2
  127. package/dist/apps/shared/components/admin/layout/AdminShell.js +37 -4
  128. package/dist/apps/shared/components/admin/mailboxes/EscrowScopeCard.js +72 -0
  129. package/dist/apps/shared/components/admin/mailboxes/ResourceSettingsCard.js +3 -3
  130. package/dist/apps/shared/components/admin/mailboxes/ShareAccessCard.js +22 -4
  131. package/dist/apps/shared/components/admin/settings/BrandingForm.js +116 -0
  132. package/dist/apps/shared/components/admin/settings/DomainDnsSetup.js +83 -0
  133. package/dist/apps/shared/components/admin/settings/EncryptionPolicyForm.js +62 -0
  134. package/dist/apps/shared/components/admin/settings/LoadedSettingsForm.js +25 -0
  135. package/dist/apps/shared/components/admin/settings/MailboxCreateForm.js +112 -0
  136. package/dist/apps/shared/components/admin/settings/MailboxPolicyForm.js +74 -0
  137. package/dist/apps/shared/components/admin/settings/PluginsManager.js +572 -0
  138. package/dist/apps/shared/components/admin/settings/RetentionPolicyForm.js +73 -0
  139. package/dist/apps/shared/components/admin/setup/EscrowSetupStep.js +147 -0
  140. package/dist/apps/shared/components/admin/setup/SetupWizard.js +208 -0
  141. package/dist/apps/shared/components/admin/signOut.js +75 -0
  142. package/dist/apps/shared/components/admin/usePagedList.js +99 -0
  143. package/dist/apps/shared/components/calendar/CalendarListSidebar.js +24 -13
  144. package/dist/apps/shared/components/calendar/EventModal.js +154 -21
  145. package/dist/apps/shared/components/calendar/MonthView.js +12 -5
  146. package/dist/apps/shared/components/calendar/RecurrenceEditor.js +40 -5
  147. package/dist/apps/shared/components/calendar/SplitDayView.js +10 -4
  148. package/dist/apps/shared/components/calendar/TimeGridView.js +23 -7
  149. package/dist/apps/shared/components/calendar/allDay.js +111 -0
  150. package/dist/apps/shared/components/calendar/layout/CalendarShell.js +47 -39
  151. package/dist/apps/shared/components/contacts/ContactDetailPane.js +43 -5
  152. package/dist/apps/shared/components/contacts/ContactForm.js +45 -12
  153. package/dist/apps/shared/components/contacts/ContactsSidebar.js +2 -2
  154. package/dist/apps/shared/components/contacts/KeyChangeReview.js +63 -0
  155. package/dist/apps/shared/components/contacts/contactKeys.js +64 -0
  156. package/dist/apps/shared/components/escrow/layout/EscrowShell.js +24 -5
  157. package/dist/apps/shared/components/layout/AppShell.js +100 -8
  158. package/dist/apps/shared/components/layout/BrandingChrome.js +45 -10
  159. package/dist/apps/shared/components/layout/KeyEnrollmentGate.js +63 -14
  160. package/dist/apps/shared/components/layout/MailboxProvisioning.js +10 -2
  161. package/dist/apps/shared/components/layout/RecoveryCodeUnlock.js +174 -0
  162. package/dist/apps/shared/components/layout/UnlockPromptProvider.js +136 -0
  163. package/dist/apps/shared/components/mail/ConversationThreadPane.js +50 -12
  164. package/dist/apps/shared/components/mail/MessageDetailPane.js +464 -34
  165. package/dist/apps/shared/components/mail/compose/ComposeContext.js +9 -6
  166. package/dist/apps/shared/components/mail/compose/ComposeWindow.js +807 -116
  167. package/dist/apps/shared/components/mail/compose/composeFlushRegistry.js +50 -0
  168. package/dist/apps/shared/components/mail/layout/MailShell.js +116 -41
  169. package/dist/apps/shared/components/mail/pinnedSigners.js +98 -0
  170. package/dist/apps/shared/components/mail/verificationSeals.js +112 -0
  171. package/dist/apps/shared/components/mail/writableMailboxes.js +87 -0
  172. package/dist/apps/shared/components/rules/RuleBuilder.js +34 -6
  173. package/dist/apps/shared/components/settings/layout/SettingsShell.js +1 -0
  174. package/dist/apps/shared/mail/findWellKnownFolderUid.js +12 -0
  175. package/dist/apps/shared/mail/listAllPages.js +25 -0
  176. package/dist/apps/shared/search/LocalIndexLifecycle.js +80 -0
  177. package/dist/apps/shared/search/localIndexBlockCipher.js +63 -0
  178. package/dist/apps/shared/search/localIndexBuilder.js +408 -0
  179. package/dist/apps/shared/search/localIndexKey.js +25 -0
  180. package/dist/apps/shared/search/localIndexRpcClient.js +241 -0
  181. package/dist/apps/shared/search/localIndexSchema.js +185 -0
  182. package/dist/apps/shared/search/localIndexSizePreference.js +78 -0
  183. package/dist/apps/shared/search/localIndexStorage.js +86 -0
  184. package/dist/apps/shared/search/localIndexVFS.js +258 -0
  185. package/dist/apps/shared/search/localIndexWorker.js +687 -0
  186. package/dist/apps/shared/search/searchTier2.js +38 -0
  187. package/dist/apps/www/calendar/index.js +38 -18
  188. package/dist/apps/www/contacts/[uid].js +13 -2
  189. package/dist/apps/www/contacts/index.js +94 -15
  190. package/dist/apps/www/index.js +770 -137
  191. package/dist/apps/www/messages/[uid].js +23 -7
  192. package/dist/apps/www/settings/auto-reply/index.js +9 -4
  193. package/dist/apps/www/settings/booking-types/[uid].js +17 -12
  194. package/dist/apps/www/settings/encryption/index.js +541 -96
  195. package/dist/apps/www/settings/filters/[uid].js +22 -7
  196. package/dist/apps/www/settings/filters/new/index.js +8 -2
  197. package/dist/apps/www/settings/focused-inbox/index.js +3 -1
  198. package/dist/apps/www/settings/privacy/index.js +57 -22
  199. package/dist/apps/www/settings/read-receipts/index.js +10 -3
  200. package/dist/apps/www/settings/sharing/index.js +124 -0
  201. package/dist/apps/www/tasks/index.js +76 -14
  202. package/package.json +3 -2
@@ -1,302 +1,418 @@
1
- ///////////////////////////////////////////////////////////////////////////////
2
- // Copyright (C) 2026 Jean-Philippe Steinmetz
3
- // SPDX-License-Identifier: MPL-2.0
4
- ///////////////////////////////////////////////////////////////////////////////
5
- import React, { createContext, PropsWithChildren, ReactNode, useContext, useEffect, useMemo, useState } from "react";
6
- import { HiOutlineBars3 } from "react-icons/hi2";
7
- import { ApiRequestError } from "@rapidmx/react-shared/util/api.js";
8
- import Drawer from "@rapidmx/react-shared/components/overlays/Drawer.js";
9
- import { Folder, Mailbox, listFolders, listMailboxes } from "@rapidmx/react-shared/mail/mailApi.js";
10
- import Alert from "@rapidmx/react-shared/components/feedback/Alert.js";
11
- import Skeleton, { SkeletonList } from "@rapidmx/react-shared/components/feedback/Skeleton.js";
12
- import AppShell, { AppShellProps } from "../../layout/AppShell.js";
13
- import KeyEnrollmentGate from "../../layout/KeyEnrollmentGate.js";
14
- import MailboxProvisioning from "../../layout/MailboxProvisioning.js";
15
- import { useCompose } from "../compose/ComposeContext.js";
16
-
17
- export type MailShellProps = Omit<AppShellProps, "active">;
18
-
19
- export interface MailShellContextValue {
20
- /** The mailbox currently selected (`?mailboxUid=`, or the caller's first accessible mailbox). */
21
- mailboxUid?: string;
22
- /** The folder currently selected (`?folderUid=`, or the selected mailbox's Inbox). */
23
- folderUid?: string;
24
- mailboxes: Mailbox[];
25
- folders: Folder[];
26
- }
27
-
28
- const MailShellContext = createContext<MailShellContextValue>({ mailboxes: [], folders: [] });
29
-
30
- /** Reads the mailbox/folder a page is currently showing, as resolved by the enclosing `MailShell`. */
31
- export function useMailShell(): MailShellContextValue {
32
- return useContext(MailShellContext);
33
- }
34
-
35
- const FOLDER_LABELS: Record<string, string> = {
36
- inbox: "Inbox",
37
- sent_items: "Sent Items",
38
- drafts: "Drafts",
39
- outbox: "Outbox",
40
- junk: "Junk Email",
41
- archive: "Archive",
42
- deleted_items: "Deleted Items",
43
- };
44
-
45
- /** Well-known folders sort first, in Gmail/Outlook's conventional order; anything else (incl. `user`) sorts after, alphabetically. */
46
- const FOLDER_ORDER = ["inbox", "drafts", "outbox", "sent_items", "junk", "archive", "deleted_items"];
47
-
48
- /**
49
- * A mailbox's `calendar`/`contacts`/`tasks`/`notes` folders back their own dedicated apps (see
50
- * `CalendarShell`/`ContactsShell`/`TasksShell`), not Mail — `listFolders()` returns every well-known
51
- * folder for the mailbox regardless of which app owns it, so Mail's own folder tree must filter down
52
- * to just the mail ones itself, or those other apps' folders leak into this sidebar.
53
- */
54
- const MAIL_FOLDER_TYPES = new Set([...FOLDER_ORDER, "user"]);
55
-
56
- function folderSortKey(folder: Folder): number {
57
- const idx = FOLDER_ORDER.indexOf(folder.type);
58
- return idx === -1 ? FOLDER_ORDER.length : idx;
59
- }
60
-
61
- type Status = "checking" | "error" | "ready";
62
-
63
- /**
64
- * A separate component (not inlined into `MailShell`'s own render) so `useCompose()` resolves against
65
- * `ComposeProvider` correctly: that provider is rendered *inside* the `AppShell` that `MailShell` itself
66
- * returns, i.e. a descendant of `MailShell`, not an ancestor — a hook call made directly in `MailShell`'s
67
- * own function body would see only whatever context exists *above* `MailShell`, never a provider one of
68
- * its own descendants creates. This button, rendered as part of `AppShell`'s `children`, sits correctly
69
- * inside that subtree.
70
- */
71
- function ComposeButton({ mailboxUid }: { mailboxUid: string }) {
72
- const { openCompose } = useCompose();
73
- return (
74
- <button
75
- type="button"
76
- onClick={() => openCompose({ mailboxUid })}
77
- className="block text-center w-full py-2.5 px-4 rounded-sm font-semibold text-sm bg-primary text-white hover:bg-primary-dark"
78
- >
79
- Compose
80
- </button>
81
- );
82
- }
83
-
84
- /**
85
- * Mail's own contextual sidebar (mailbox switcher + folder tree) + content area, rendered inside the shared
86
- * `AppShell` chrome (icon rail, header, impersonation banner — see that component). There is no client-side
87
- * router in this framework (see `ReactRoute`'s file-convention resolver) — the selected mailbox/folder live
88
- * in the URL's `?mailboxUid=`/`?folderUid=` query params, read once on mount (never during the initial
89
- * render itself, matching every other query-param reader in this codebase — e.g. `apps/admin/quarantine`'s
90
- * `readMailboxUid()` — so the server-rendered and just-hydrated client markup match).
91
- */
92
- export default function MailShell({
93
- userUid,
94
- authServerUrl,
95
- impersonating,
96
- impersonationBaseUrl,
97
- trusted,
98
- children,
99
- }: PropsWithChildren<MailShellProps>) {
100
- const [status, setStatus] = useState<Status>("checking");
101
- const [error, setError] = useState<string | null>(null);
102
- const [mailboxes, setMailboxes] = useState<Mailbox[]>([]);
103
- const [folders, setFolders] = useState<Folder[]>([]);
104
- const [foldersLoading, setFoldersLoading] = useState(true);
105
- const [folderError, setFolderError] = useState<string | null>(null);
106
- const [requestedMailboxUid, setRequestedMailboxUid] = useState<string | null>(null);
107
- const [requestedFolderUid, setRequestedFolderUid] = useState<string | null>(null);
108
- const [drawerOpen, setDrawerOpen] = useState(false);
109
-
110
- useEffect(() => {
111
- const params = new URLSearchParams(window.location.search);
112
- setRequestedMailboxUid(params.get("mailboxUid"));
113
- setRequestedFolderUid(params.get("folderUid"));
114
- }, []);
115
-
116
- useEffect(() => {
117
- if (!userUid) {
118
- return;
119
- }
120
- listMailboxes({ limit: 100 })
121
- .then((result) => {
122
- setMailboxes(result);
123
- setStatus("ready");
124
- })
125
- .catch((err) => {
126
- setError(err instanceof ApiRequestError ? err.message : "Could not load your mailboxes.");
127
- setStatus("error");
128
- });
129
- }, [userUid]);
130
-
131
- const mailboxUid: string | undefined =
132
- (requestedMailboxUid && mailboxes.some((mb) => mb.uid === requestedMailboxUid) ? requestedMailboxUid : undefined) ??
133
- mailboxes[0]?.uid;
134
-
135
- useEffect(() => {
136
- if (!mailboxUid) {
137
- setFolders([]);
138
- return;
139
- }
140
- setFolderError(null);
141
- setFoldersLoading(true);
142
- listFolders(mailboxUid)
143
- .then((result) => setFolders(result.filter((f) => MAIL_FOLDER_TYPES.has(f.type))))
144
- .catch((err) => setFolderError(err instanceof ApiRequestError ? err.message : "Could not load folders."))
145
- .finally(() => setFoldersLoading(false));
146
- }, [mailboxUid]);
147
-
148
- const folderUid: string | undefined =
149
- (requestedFolderUid && folders.some((f) => f.uid === requestedFolderUid) ? requestedFolderUid : undefined) ??
150
- folders.find((f) => f.type === "inbox")?.uid;
151
-
152
- const contextValue = useMemo<MailShellContextValue>(
153
- () => ({ mailboxUid, folderUid, mailboxes, folders }),
154
- [mailboxUid, folderUid, mailboxes, folders],
155
- );
156
-
157
- const sortedFolders = useMemo(
158
- () => [...folders].sort((a, b) => folderSortKey(a) - folderSortKey(b) || a.name.localeCompare(b.name)),
159
- [folders],
160
- );
161
-
162
- // A full-screen takeover, not nested inside the rest of the app's chrome — there's nothing else
163
- // for a mailbox-less caller to do here yet, so the icon rail/header/folder tree don't render at all.
164
- if (userUid && status === "ready" && !mailboxUid) {
165
- return <MailboxProvisioning />;
166
- }
167
-
168
- let inner: ReactNode = null;
169
- if (userUid && status === "checking") {
170
- // Renders immediately (no network round trip needed) so switching into Mail never shows a blank
171
- // pane while `listMailboxes()` is in flight — the outline below mirrors the real sidebar's shape.
172
- inner = (
173
- <aside className="w-64 shrink-0 bg-surface border-r border-border flex flex-col p-3 gap-4">
174
- <Skeleton height="h-9" className="rounded-sm" />
175
- <SkeletonList count={6} />
176
- </aside>
177
- );
178
- } else if (userUid && status === "error") {
179
- inner = (
180
- <div className="flex-1 flex items-center justify-center p-8">
181
- <div className="w-full max-w-md">
182
- <Alert>{error}</Alert>
183
- </div>
184
- </div>
185
- );
186
- } else if (userUid && status === "ready") {
187
- // A function, not a plain JSX constant — it's rendered twice (desktop `<aside>` + mobile
188
- // `Drawer`), possibly *simultaneously* mounted (the aside is only CSS-hidden below `md`, not
189
- // unmounted), so the mailbox-switcher `<select>`'s `id`/its `<label>`'s `htmlFor` need a distinct
190
- // value per instance. Two elements sharing one id breaks label association (and is invalid HTML).
191
- const sidebarContent = (idPrefix: string) => (
192
- <>
193
- <div className="p-3">
194
- <ComposeButton mailboxUid={mailboxUid} />
195
- </div>
196
- {mailboxes.length > 1 && (
197
- <div className="px-3 pb-2">
198
- <label
199
- className="block text-xs font-bold uppercase tracking-wide text-text-muted mb-1"
200
- htmlFor={`${idPrefix}-mailbox-switcher`}
201
- >
202
- Mailbox
203
- </label>
204
- <select
205
- id={`${idPrefix}-mailbox-switcher`}
206
- className="w-full text-sm border border-border rounded-sm py-1.5 px-2 bg-surface"
207
- value={mailboxUid}
208
- onChange={(e) => {
209
- window.location.href = `/?mailboxUid=${encodeURIComponent(e.target.value)}`;
210
- }}
211
- >
212
- {mailboxes.map((mb) => (
213
- <option key={mb.uid} value={mb.uid}>
214
- {mb.displayName}
215
- {mb.ownerUserUid ? "" : " (shared)"}
216
- </option>
217
- ))}
218
- </select>
219
- </div>
220
- )}
221
- {folderError && (
222
- <div className="px-3 pb-2">
223
- <Alert>{folderError}</Alert>
224
- </div>
225
- )}
226
- <nav className="flex-1 overflow-y-auto px-3 pb-3 flex flex-col gap-0.5">
227
- {foldersLoading ? (
228
- <SkeletonList count={5} className="pt-1" />
229
- ) : (
230
- sortedFolders.map((folder) => (
231
- <a
232
- key={folder.uid}
233
- href={`/?mailboxUid=${encodeURIComponent(mailboxUid)}&folderUid=${encodeURIComponent(folder.uid)}`}
234
- className={[
235
- "flex items-center justify-between text-sm rounded-sm py-1.5 px-2.5",
236
- folder.uid === folderUid
237
- ? "bg-primary/10 text-primary-dark font-semibold"
238
- : "text-text hover:bg-surface-alt",
239
- ].join(" ")}
240
- >
241
- <span>{FOLDER_LABELS[folder.type] ?? folder.name}</span>
242
- {folder.unreadCount > 0 && (
243
- <span className="text-xs font-bold rounded-pill py-0.5 px-1.5 bg-surface-alt text-text-muted">
244
- {folder.unreadCount}
245
- </span>
246
- )}
247
- </a>
248
- ))
249
- )}
250
- </nav>
251
- </>
252
- );
253
-
254
- inner = (
255
- <>
256
- <aside className="hidden md:flex w-64 shrink-0 bg-surface border-r border-border flex-col">
257
- {sidebarContent("desktop")}
258
- </aside>
259
- <Drawer open={drawerOpen} onClose={() => setDrawerOpen(false)} title="Folders">
260
- <div className="flex flex-col">{sidebarContent("mobile")}</div>
261
- </Drawer>
262
- <main className="flex-1 min-w-0 overflow-y-auto">
263
- <button
264
- type="button"
265
- className="md:hidden m-3 w-9 h-9 flex items-center justify-center rounded-sm text-text-muted hover:bg-surface-alt hover:text-text"
266
- aria-label="Open folders"
267
- onClick={() => setDrawerOpen(true)}
268
- >
269
- <HiOutlineBars3 size={20} aria-hidden="true" />
270
- </button>
271
- <MailShellContext.Provider value={contextValue}>{children}</MailShellContext.Provider>
272
- </main>
273
- </>
274
- );
275
- }
276
-
277
- // Wraps AppShell unconditionally, at a stable tree position regardless of whether mailboxUid has
278
- // resolved yet - KeyEnrollmentGate itself passes `children` through untouched until a real
279
- // mailboxUid is supplied (see its own doc comment). Wrapping only once ready (a conditional tree
280
- // position) would make AppShell itself remount the moment mailboxUid resolves, tearing down
281
- // whatever state/effects it had already started (confirmed by direct reproduction: the
282
- // impersonation banner's own internal state was lost exactly at that transition).
283
- const selectedMailbox = mailboxes.find((mb) => mb.uid === mailboxUid);
284
- return (
285
- <KeyEnrollmentGate
286
- mailboxUid={mailboxUid}
287
- mailboxAddress={selectedMailbox?.primarySmtpAddress}
288
- mailboxKeys={selectedMailbox?.keys}
289
- >
290
- <AppShell
291
- active="mail"
292
- userUid={userUid}
293
- authServerUrl={authServerUrl}
294
- impersonating={impersonating}
295
- impersonationBaseUrl={impersonationBaseUrl}
296
- trusted={trusted}
297
- >
298
- {inner}
299
- </AppShell>
300
- </KeyEnrollmentGate>
301
- );
302
- }
1
+ ///////////////////////////////////////////////////////////////////////////////
2
+ // Copyright (C) 2026 Jean-Philippe Steinmetz
3
+ // SPDX-License-Identifier: MPL-2.0
4
+ ///////////////////////////////////////////////////////////////////////////////
5
+ import React, { createContext, PropsWithChildren, ReactNode, useContext, useEffect, useMemo, useState } from "react";
6
+ import { HiOutlineBars3 } from "react-icons/hi2";
7
+ import { ApiRequestError } from "@rapidmx/react-shared/util/api.js";
8
+ import Drawer from "@rapidmx/react-shared/components/overlays/Drawer.js";
9
+ import { Folder, Mailbox, listFolders, listMailboxes } from "@rapidmx/react-shared/mail/mailApi.js";
10
+ import Alert from "@rapidmx/react-shared/components/feedback/Alert.js";
11
+ import Skeleton, { SkeletonList } from "@rapidmx/react-shared/components/feedback/Skeleton.js";
12
+ import AppShell, { AppShellProps } from "../../layout/AppShell.js";
13
+ import KeyEnrollmentGate from "../../layout/KeyEnrollmentGate.js";
14
+ import MailboxProvisioning from "../../layout/MailboxProvisioning.js";
15
+ import { useCompose } from "../compose/ComposeContext.js";
16
+ import LocalIndexLifecycle from "../../../search/LocalIndexLifecycle.js";
17
+
18
+ export type MailShellProps = Omit<AppShellProps, "active">;
19
+
20
+ /** One mailbox's own mail folders (already filtered to `MAIL_FOLDER_TYPES` and unsorted) - one entry per
21
+ * mailbox in `mailboxes`, fetched in parallel so every accessible mailbox's folder tree can render
22
+ * simultaneously (see `MailShell`'s own doc comment on why this replaced the old single-mailbox `folders`
23
+ * field). `error` is set (and `folders` left empty) for a mailbox whose own `listFolders()` call failed -
24
+ * one mailbox's fetch failure must never blank out every other mailbox's section. */
25
+ export interface MailboxFolders {
26
+ mailbox: Mailbox;
27
+ folders: Folder[];
28
+ error?: string;
29
+ }
30
+
31
+ /** The well-known mail folder types an "aggregate" pseudo-folder can merge across every accessible
32
+ * mailbox - a fixed, small set: `outbox` is deliberately excluded (transient per-mailbox send-queue
33
+ * state, not a "merge across mailboxes" concept), and there's no aggregate concept for `calendar`/
34
+ * `contacts`/`tasks`/`notes`/`user` folders (Mail's own sidebar already excludes those - see
35
+ * `MAIL_FOLDER_TYPES`). */
36
+ export const AGGREGATE_FOLDER_TYPES = ["inbox", "sent_items", "drafts", "deleted_items", "junk"] as const;
37
+ export type AggregateFolderType = (typeof AGGREGATE_FOLDER_TYPES)[number];
38
+
39
+ function isAggregateFolderType(value: string | null): value is AggregateFolderType {
40
+ return !!value && (AGGREGATE_FOLDER_TYPES as readonly string[]).includes(value);
41
+ }
42
+
43
+ export interface MailShellContextValue {
44
+ /** The mailbox currently selected (`?mailboxUid=`) - `undefined` while an aggregate pseudo-folder is
45
+ * active instead (`aggregateFolderType` set), since there's no single mailbox to speak of then. */
46
+ mailboxUid?: string;
47
+ /** The folder currently selected (`?folderUid=`, or the selected mailbox's Inbox) - `undefined` in
48
+ * aggregate mode, for the same reason. */
49
+ folderUid?: string;
50
+ /** Set only when an aggregate pseudo-folder (`?aggregate=inbox` etc.) is selected instead of a real,
51
+ * single mailbox+folder - mutually exclusive with `mailboxUid`/`folderUid` above. */
52
+ aggregateFolderType?: AggregateFolderType;
53
+ mailboxes: Mailbox[];
54
+ /** Every accessible mailbox's own mail folders - replaces the old single-mailbox `folders: Folder[]`
55
+ * now that every mailbox's tree renders at once (see `MailboxFolders`'s own doc comment). */
56
+ mailboxFolders: MailboxFolders[];
57
+ }
58
+
59
+ const MailShellContext = createContext<MailShellContextValue>({ mailboxes: [], mailboxFolders: [] });
60
+
61
+ /** Reads the mailbox/folder a page is currently showing, as resolved by the enclosing `MailShell`. */
62
+ export function useMailShell(): MailShellContextValue {
63
+ return useContext(MailShellContext);
64
+ }
65
+
66
+ const FOLDER_LABELS: Record<string, string> = {
67
+ inbox: "Inbox",
68
+ sent_items: "Sent Items",
69
+ drafts: "Drafts",
70
+ outbox: "Outbox",
71
+ junk: "Junk Email",
72
+ archive: "Archive",
73
+ deleted_items: "Deleted Items",
74
+ };
75
+
76
+ /** Well-known folders sort first, in Gmail/Outlook's conventional order; anything else (incl. `user`) sorts after, alphabetically. */
77
+ const FOLDER_ORDER = ["inbox", "drafts", "outbox", "sent_items", "junk", "archive", "deleted_items"];
78
+
79
+ /**
80
+ * A mailbox's `calendar`/`contacts`/`tasks`/`notes` folders back their own dedicated apps (see
81
+ * `CalendarShell`/`ContactsShell`/`TasksShell`), not Mail — `listFolders()` returns every well-known
82
+ * folder for the mailbox regardless of which app owns it, so Mail's own folder tree must filter down
83
+ * to just the mail ones itself, or those other apps' folders leak into this sidebar.
84
+ */
85
+ const MAIL_FOLDER_TYPES = new Set([...FOLDER_ORDER, "user"]);
86
+
87
+ function folderSortKey(folder: Folder): number {
88
+ const idx = FOLDER_ORDER.indexOf(folder.type);
89
+ return idx === -1 ? FOLDER_ORDER.length : idx;
90
+ }
91
+
92
+ function sortedFoldersOf(folders: Folder[]): Folder[] {
93
+ return [...folders].sort((a, b) => folderSortKey(a) - folderSortKey(b) || a.name.localeCompare(b.name));
94
+ }
95
+
96
+ function aggregateUnreadCount(mailboxFolders: MailboxFolders[], type: AggregateFolderType): number {
97
+ return mailboxFolders.reduce((total, mf) => total + (mf.folders.find((f) => f.type === type)?.unreadCount ?? 0), 0);
98
+ }
99
+
100
+ type Status = "checking" | "error" | "ready";
101
+
102
+ /** Page size of the one `listMailboxes()` call this shell makes. */
103
+ export const MAILBOX_LIST_LIMIT = 100;
104
+
105
+ /**
106
+ * A separate component (not inlined into `MailShell`'s own render) so `useCompose()` resolves against
107
+ * `ComposeProvider` correctly: that provider is rendered *inside* the `AppShell` that `MailShell` itself
108
+ * returns, i.e. a descendant of `MailShell`, not an ancestor — a hook call made directly in `MailShell`'s
109
+ * own function body would see only whatever context exists *above* `MailShell`, never a provider one of
110
+ * its own descendants creates. This button, rendered as part of `AppShell`'s `children`, sits correctly
111
+ * inside that subtree.
112
+ *
113
+ * Opens with no mailbox: a fresh message defaults to the caller's own mailbox, and the compose window's
114
+ * own From field is where the sender is chosen.
115
+ */
116
+ function ComposeButton() {
117
+ const { openCompose } = useCompose();
118
+ return (
119
+ <button
120
+ type="button"
121
+ onClick={() => openCompose({})}
122
+ className="block text-center w-full py-2.5 px-4 rounded-sm font-semibold text-sm bg-primary text-white hover:bg-primary-dark"
123
+ >
124
+ Compose
125
+ </button>
126
+ );
127
+ }
128
+
129
+ /**
130
+ * Mail's own contextual sidebar (every accessible mailbox's own folder tree, plus a merged "All Mailboxes"
131
+ * aggregate section) + content area, rendered inside the shared `AppShell` chrome (icon rail, header,
132
+ * impersonation banner — see that component). There is no client-side router in this framework (see
133
+ * `ReactRoute`'s file-convention resolver) — the selected mailbox/folder (or aggregate pseudo-folder) live
134
+ * in the URL's `?mailboxUid=`/`?folderUid=`/`?aggregate=` query params, read once on mount (never during
135
+ * the initial render itself, matching every other query-param reader in this codebase — e.g.
136
+ * `apps/admin/quarantine`'s `readMailboxUid()` — so the server-rendered and just-hydrated client markup
137
+ * match).
138
+ *
139
+ * Every accessible mailbox's folder tree renders at once - there is no "switch mailbox" affordance
140
+ * anymore (the mailbox `<select>` this shell used to have is gone) - so a single-mailbox user sees zero
141
+ * behavior change from before this, and a multi-mailbox user sees every mailbox's folders (and the new
142
+ * aggregate section) simultaneously, matching the shared-mailbox feature's own "display as a separate set
143
+ * of folders" requirement.
144
+ */
145
+ export default function MailShell({
146
+ userUid,
147
+ authServerUrl,
148
+ impersonating,
149
+ impersonationBaseUrl,
150
+ trusted,
151
+ children,
152
+ }: PropsWithChildren<MailShellProps>) {
153
+ const [status, setStatus] = useState<Status>("checking");
154
+ const [error, setError] = useState<string | null>(null);
155
+ const [mailboxes, setMailboxes] = useState<Mailbox[]>([]);
156
+ const [mailboxFolders, setMailboxFolders] = useState<MailboxFolders[]>([]);
157
+ const [foldersLoading, setFoldersLoading] = useState(true);
158
+ const [requestedMailboxUid, setRequestedMailboxUid] = useState<string | null>(null);
159
+ const [requestedFolderUid, setRequestedFolderUid] = useState<string | null>(null);
160
+ const [requestedAggregateType, setRequestedAggregateType] = useState<string | null>(null);
161
+ const [drawerOpen, setDrawerOpen] = useState(false);
162
+
163
+ useEffect(() => {
164
+ const params = new URLSearchParams(window.location.search);
165
+ setRequestedMailboxUid(params.get("mailboxUid"));
166
+ setRequestedFolderUid(params.get("folderUid"));
167
+ setRequestedAggregateType(params.get("aggregate"));
168
+ }, []);
169
+
170
+ useEffect(() => {
171
+ if (!userUid) {
172
+ return;
173
+ }
174
+ listMailboxes({ limit: MAILBOX_LIST_LIMIT })
175
+ .then((result) => {
176
+ setMailboxes(result);
177
+ setStatus("ready");
178
+ })
179
+ .catch((err) => {
180
+ setError(err instanceof ApiRequestError ? err.message : "Could not load your mailboxes.");
181
+ setStatus("error");
182
+ });
183
+ }, [userUid]);
184
+
185
+ const aggregateFolderType: AggregateFolderType | undefined = isAggregateFolderType(requestedAggregateType)
186
+ ? requestedAggregateType
187
+ : undefined;
188
+
189
+ const mailboxUid: string | undefined = aggregateFolderType
190
+ ? undefined
191
+ : (requestedMailboxUid && mailboxes.some((mb) => mb.uid === requestedMailboxUid) ? requestedMailboxUid : undefined) ??
192
+ mailboxes[0]?.uid;
193
+
194
+ // Fans out one listFolders() call per accessible mailbox in parallel - each call catches its own
195
+ // failure into an MailboxFolders.error rather than letting Promise.all reject, so one mailbox's fetch
196
+ // failure renders that section's own inline Alert instead of blanking out every other mailbox's
197
+ // folder tree.
198
+ useEffect(() => {
199
+ if (mailboxes.length === 0) {
200
+ // Deliberately leaves foldersLoading as-is: this also runs once before listMailboxes() has
201
+ // resolved, and clearing it here would briefly render an empty sidebar the moment mailboxes
202
+ // arrive, before their folders do. A genuinely mailbox-less caller gets MailboxProvisioning.
203
+ setMailboxFolders([]);
204
+ return;
205
+ }
206
+ setFoldersLoading(true);
207
+ void Promise.all(
208
+ mailboxes.map((mailbox) =>
209
+ listFolders(mailbox.uid)
210
+ .then((result): MailboxFolders => ({ mailbox, folders: result.filter((f) => MAIL_FOLDER_TYPES.has(f.type)) }))
211
+ .catch(
212
+ (err): MailboxFolders => ({
213
+ mailbox,
214
+ folders: [],
215
+ error: err instanceof ApiRequestError ? err.message : "Could not load folders.",
216
+ }),
217
+ ),
218
+ ),
219
+ )
220
+ .then(setMailboxFolders)
221
+ .finally(() => setFoldersLoading(false));
222
+ }, [mailboxes]);
223
+
224
+ const selectedMailboxFolders = mailboxFolders.find((mf) => mf.mailbox.uid === mailboxUid)?.folders ?? [];
225
+ const folderUid: string | undefined = aggregateFolderType
226
+ ? undefined
227
+ : (requestedFolderUid && selectedMailboxFolders.some((f) => f.uid === requestedFolderUid) ? requestedFolderUid : undefined) ??
228
+ selectedMailboxFolders.find((f) => f.type === "inbox")?.uid;
229
+
230
+ // The mailbox `KeyEnrollmentGate`/`LocalIndexLifecycle`/`ComposeButton` treat as "the" mailbox when
231
+ // there's no single selected one to use (aggregate mode) - the caller's own owned mailbox if they
232
+ // have one, else whichever accessible mailbox happens to be first. See MailShell's own doc comment on
233
+ // this being an accepted limitation: an aggregate-view message from a *different*, not-yet-visited
234
+ // mailbox may still need that mailbox's own folder view opened directly to unlock/decrypt it.
235
+ const defaultMailboxUid = mailboxes.find((mb) => mb.ownerUserUid === userUid)?.uid ?? mailboxes[0]?.uid;
236
+ const activeMailboxUid = mailboxUid ?? defaultMailboxUid;
237
+
238
+ const contextValue = useMemo<MailShellContextValue>(
239
+ () => ({ mailboxUid, folderUid, aggregateFolderType, mailboxes, mailboxFolders }),
240
+ [mailboxUid, folderUid, aggregateFolderType, mailboxes, mailboxFolders],
241
+ );
242
+
243
+ // A full-screen takeover, not nested inside the rest of the app's chrome — there's nothing else for a
244
+ // mailbox-less caller to do here yet, so the icon rail/header/folder tree don't render at all. Checks
245
+ // `mailboxes.length` directly (not `!mailboxUid`) since `mailboxUid` is legitimately undefined in
246
+ // aggregate mode even with mailboxes present.
247
+ if (userUid && status === "ready" && mailboxes.length === 0) {
248
+ return <MailboxProvisioning />;
249
+ }
250
+
251
+ let inner: ReactNode = null;
252
+ if (userUid && status === "checking") {
253
+ // Renders immediately (no network round trip needed) so switching into Mail never shows a blank
254
+ // pane while `listMailboxes()` is in flight — the outline below mirrors the real sidebar's shape.
255
+ inner = (
256
+ <aside className="w-64 shrink-0 bg-surface border-r border-border flex flex-col p-3 gap-4">
257
+ <Skeleton height="h-9" className="rounded-sm" />
258
+ <SkeletonList count={6} />
259
+ </aside>
260
+ );
261
+ } else if (userUid && status === "error") {
262
+ inner = (
263
+ <div className="flex-1 flex items-center justify-center p-8">
264
+ <div className="w-full max-w-md">
265
+ <Alert>{error}</Alert>
266
+ </div>
267
+ </div>
268
+ );
269
+ } else if (userUid && status === "ready") {
270
+ // A function, not a plain JSX constant — it's rendered twice (desktop `<aside>` + mobile
271
+ // `Drawer`), possibly *simultaneously* mounted (the aside is only CSS-hidden below `md`, not
272
+ // unmounted).
273
+ const sidebarContent = () => (
274
+ <>
275
+ <div className="p-3">
276
+ <ComposeButton />
277
+ </div>
278
+ {foldersLoading ? (
279
+ <div className="flex-1 overflow-y-auto px-3 pb-3">
280
+ <SkeletonList count={6} className="pt-1" />
281
+ </div>
282
+ ) : (
283
+ <nav className="flex-1 overflow-y-auto px-3 pb-3 flex flex-col gap-3">
284
+ {mailboxes.length > 1 && (
285
+ <div>
286
+ <div className="text-xs font-bold uppercase tracking-wide text-text-muted mb-1 px-2.5">
287
+ All Mailboxes
288
+ </div>
289
+ <div className="flex flex-col gap-0.5">
290
+ {AGGREGATE_FOLDER_TYPES.map((type) => {
291
+ const unread = aggregateUnreadCount(mailboxFolders, type);
292
+ return (
293
+ <a
294
+ key={type}
295
+ href={`/?aggregate=${encodeURIComponent(type)}`}
296
+ className={[
297
+ "flex items-center justify-between text-sm rounded-sm py-1.5 px-2.5",
298
+ aggregateFolderType === type
299
+ ? "bg-primary/10 text-primary-dark font-semibold"
300
+ : "text-text hover:bg-surface-alt",
301
+ ].join(" ")}
302
+ >
303
+ <span>{FOLDER_LABELS[type]}</span>
304
+ {unread > 0 && (
305
+ <span className="text-xs font-bold rounded-pill py-0.5 px-1.5 bg-surface-alt text-text-muted">
306
+ {unread}
307
+ </span>
308
+ )}
309
+ </a>
310
+ );
311
+ })}
312
+ </div>
313
+ </div>
314
+ )}
315
+ {mailboxFolders.map(({ mailbox, folders, error: mailboxError }) => (
316
+ <div key={mailbox.uid}>
317
+ <div className="text-xs font-bold uppercase tracking-wide text-text-muted mb-1 px-2.5 truncate">
318
+ {mailbox.displayName}
319
+ {mailbox.ownerUserUid ? "" : " (shared)"}
320
+ </div>
321
+ {mailboxError && (
322
+ <div className="px-2.5 pb-1">
323
+ <Alert>{mailboxError}</Alert>
324
+ </div>
325
+ )}
326
+ <div className="flex flex-col gap-0.5">
327
+ {sortedFoldersOf(folders).map((folder) => (
328
+ <a
329
+ key={folder.uid}
330
+ href={`/?mailboxUid=${encodeURIComponent(mailbox.uid)}&folderUid=${encodeURIComponent(folder.uid)}`}
331
+ className={[
332
+ "flex items-center justify-between text-sm rounded-sm py-1.5 px-2.5",
333
+ folder.uid === folderUid
334
+ ? "bg-primary/10 text-primary-dark font-semibold"
335
+ : "text-text hover:bg-surface-alt",
336
+ ].join(" ")}
337
+ >
338
+ <span>{FOLDER_LABELS[folder.type] ?? folder.name}</span>
339
+ {folder.unreadCount > 0 && (
340
+ <span className="text-xs font-bold rounded-pill py-0.5 px-1.5 bg-surface-alt text-text-muted">
341
+ {folder.unreadCount}
342
+ </span>
343
+ )}
344
+ </a>
345
+ ))}
346
+ </div>
347
+ </div>
348
+ ))}
349
+ </nav>
350
+ )}
351
+ </>
352
+ );
353
+
354
+ inner = (
355
+ <>
356
+ <aside className="hidden md:flex w-64 shrink-0 bg-surface border-r border-border flex-col">{sidebarContent()}</aside>
357
+ <Drawer open={drawerOpen} onClose={() => setDrawerOpen(false)} title="Folders">
358
+ <div className="flex flex-col">{sidebarContent()}</div>
359
+ </Drawer>
360
+ <main className="flex-1 min-w-0 overflow-y-auto">
361
+ <button
362
+ type="button"
363
+ className="md:hidden m-3 w-9 h-9 flex items-center justify-center rounded-sm text-text-muted hover:bg-surface-alt hover:text-text"
364
+ aria-label="Open folders"
365
+ onClick={() => setDrawerOpen(true)}
366
+ >
367
+ <HiOutlineBars3 size={20} aria-hidden="true" />
368
+ </button>
369
+ <MailShellContext.Provider value={contextValue}>{children}</MailShellContext.Provider>
370
+ </main>
371
+ </>
372
+ );
373
+ }
374
+
375
+ // Wraps AppShell unconditionally, at a stable tree position regardless of whether mailboxUid has
376
+ // resolved yet - KeyEnrollmentGate itself passes `children` through untouched until a real
377
+ // mailboxUid is supplied (see its own doc comment). Wrapping only once ready (a conditional tree
378
+ // position) would make AppShell itself remount the moment mailboxUid resolves, tearing down
379
+ // whatever state/effects it had already started (confirmed by direct reproduction: the
380
+ // impersonation banner's own internal state was lost exactly at that transition).
381
+ const activeMailbox = mailboxes.find((mb) => mb.uid === activeMailboxUid);
382
+ return (
383
+ <KeyEnrollmentGate
384
+ mailboxUid={activeMailboxUid}
385
+ mailboxAddress={activeMailbox?.primarySmtpAddress}
386
+ mailboxKeys={activeMailbox?.keys}
387
+ // Unlocking is only actually required to sign/encrypt a compose, read an already-encrypted
388
+ // message, or change encryption settings - not merely to open Mail. Those specific call sites
389
+ // (ComposeWindow, MessageDetailPane) request an unlock on demand via useUnlockPrompt() instead.
390
+ // First-time provisioning (a mailbox with no vault at all yet) still always blocks - see this
391
+ // prop's own doc comment on KeyEnrollmentGateProps.
392
+ blocking={false}
393
+ // A shared/delegated mailbox the caller doesn't own is never provisioned from here - its keys
394
+ // belong to its owner (or its admins), not to whoever happens to open it first - and neither is
395
+ // the impersonated user's own mailbox: an administrator must never choose its password/recovery codes.
396
+ canProvision={!impersonating && activeMailbox?.ownerUserUid === userUid}
397
+ >
398
+ <LocalIndexLifecycle
399
+ mailboxUid={activeMailboxUid}
400
+ mailboxKeys={activeMailbox?.keys}
401
+ folders={mailboxFolders.find((mf) => mf.mailbox.uid === activeMailboxUid)?.folders ?? []}
402
+ // A full page may be truncated - pruning against it would delete indexes of accessible mailboxes
403
+ // beyond the limit, so it's only trusted as the complete list when it came back short.
404
+ accessibleMailboxUids={status === "ready" && mailboxes.length < MAILBOX_LIST_LIMIT ? mailboxes.map((mb) => mb.uid) : undefined}
405
+ />
406
+ <AppShell
407
+ active="mail"
408
+ userUid={userUid}
409
+ authServerUrl={authServerUrl}
410
+ impersonating={impersonating}
411
+ impersonationBaseUrl={impersonationBaseUrl}
412
+ trusted={trusted}
413
+ >
414
+ {inner}
415
+ </AppShell>
416
+ </KeyEnrollmentGate>
417
+ );
418
+ }