@rapidmx/web-client 0.3.0 → 0.4.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 (102) hide show
  1. package/apps/admin/branding/index.tsx +39 -404
  2. package/apps/admin/domains/[uid].tsx +92 -259
  3. package/apps/admin/encryption-policy/index.tsx +19 -0
  4. package/apps/admin/index.tsx +100 -82
  5. package/apps/admin/mailbox-policy/index.tsx +19 -0
  6. package/apps/admin/mailboxes/new/index.tsx +28 -291
  7. package/apps/admin/plugins/index.tsx +15 -0
  8. package/apps/admin/retention-policy/index.tsx +39 -132
  9. package/apps/admin/setup/index.tsx +15 -0
  10. package/apps/shared/components/admin/layout/AdminShell.tsx +249 -210
  11. package/apps/shared/components/admin/settings/BrandingForm.tsx +374 -0
  12. package/apps/shared/components/admin/settings/DomainDnsSetup.tsx +176 -0
  13. package/apps/shared/components/admin/settings/EncryptionPolicyForm.tsx +104 -0
  14. package/apps/shared/components/admin/settings/LoadedSettingsForm.tsx +34 -0
  15. package/apps/shared/components/admin/settings/MailboxCreateForm.tsx +302 -0
  16. package/apps/shared/components/admin/settings/MailboxPolicyForm.tsx +119 -0
  17. package/apps/shared/components/admin/settings/PluginsManager.tsx +595 -0
  18. package/apps/shared/components/admin/settings/RetentionPolicyForm.tsx +98 -0
  19. package/apps/shared/components/admin/setup/EscrowSetupStep.tsx +254 -0
  20. package/apps/shared/components/admin/setup/SetupWizard.tsx +300 -0
  21. package/apps/shared/components/calendar/CalendarListSidebar.tsx +120 -76
  22. package/apps/shared/components/calendar/EventModal.tsx +40 -4
  23. package/apps/shared/components/calendar/layout/CalendarShell.tsx +80 -99
  24. package/apps/shared/components/contacts/ContactForm.tsx +38 -4
  25. package/apps/shared/components/layout/AppShell.tsx +198 -169
  26. package/apps/shared/components/layout/KeyEnrollmentGate.tsx +298 -265
  27. package/apps/shared/components/layout/UnlockPromptProvider.tsx +128 -0
  28. package/apps/shared/components/mail/MessageDetailPane.tsx +630 -595
  29. package/apps/shared/components/mail/compose/ComposeContext.tsx +8 -3
  30. package/apps/shared/components/mail/compose/ComposeWindow.tsx +930 -765
  31. package/apps/shared/components/mail/layout/MailShell.tsx +408 -302
  32. package/apps/shared/components/settings/layout/SettingsShell.tsx +1 -0
  33. package/apps/shared/mail/findWellKnownFolderUid.ts +13 -0
  34. package/apps/shared/search/LocalIndexLifecycle.tsx +89 -0
  35. package/apps/shared/search/localIndexBlockCipher.ts +83 -0
  36. package/apps/shared/search/localIndexBuilder.ts +179 -0
  37. package/apps/shared/search/localIndexKey.ts +27 -0
  38. package/apps/shared/search/localIndexRpcClient.ts +116 -0
  39. package/apps/shared/search/localIndexSchema.ts +227 -0
  40. package/apps/shared/search/localIndexSizePreference.ts +88 -0
  41. package/apps/shared/search/localIndexVFS.ts +235 -0
  42. package/apps/shared/search/localIndexWorker.ts +464 -0
  43. package/apps/shared/search/searchTier2.ts +79 -0
  44. package/apps/shared/search/wa-sqlite-shims.d.ts +44 -0
  45. package/apps/www/calendar/index.tsx +31 -21
  46. package/apps/www/contacts/index.tsx +33 -6
  47. package/apps/www/index.tsx +661 -106
  48. package/apps/www/messages/[uid].tsx +5 -1
  49. package/apps/www/settings/encryption/index.tsx +63 -1
  50. package/apps/www/settings/sharing/index.tsx +271 -0
  51. package/apps/www/tasks/index.tsx +53 -4
  52. package/dist/apps/admin/branding/index.js +4 -98
  53. package/dist/apps/admin/domains/[uid].js +5 -68
  54. package/dist/apps/admin/encryption-policy/index.js +8 -0
  55. package/dist/apps/admin/index.js +14 -1
  56. package/dist/apps/admin/mailbox-policy/index.js +8 -0
  57. package/dist/apps/admin/mailboxes/new/index.js +4 -89
  58. package/dist/apps/admin/plugins/index.js +6 -0
  59. package/dist/apps/admin/retention-policy/index.js +3 -36
  60. package/dist/apps/admin/setup/index.js +6 -0
  61. package/dist/apps/shared/components/admin/layout/AdminShell.js +34 -3
  62. package/dist/apps/shared/components/admin/settings/BrandingForm.js +105 -0
  63. package/dist/apps/shared/components/admin/settings/DomainDnsSetup.js +77 -0
  64. package/dist/apps/shared/components/admin/settings/EncryptionPolicyForm.js +57 -0
  65. package/dist/apps/shared/components/admin/settings/LoadedSettingsForm.js +25 -0
  66. package/dist/apps/shared/components/admin/settings/MailboxCreateForm.js +106 -0
  67. package/dist/apps/shared/components/admin/settings/MailboxPolicyForm.js +52 -0
  68. package/dist/apps/shared/components/admin/settings/PluginsManager.js +260 -0
  69. package/dist/apps/shared/components/admin/settings/RetentionPolicyForm.js +44 -0
  70. package/dist/apps/shared/components/admin/setup/EscrowSetupStep.js +131 -0
  71. package/dist/apps/shared/components/admin/setup/SetupWizard.js +142 -0
  72. package/dist/apps/shared/components/calendar/CalendarListSidebar.js +24 -13
  73. package/dist/apps/shared/components/calendar/EventModal.js +16 -4
  74. package/dist/apps/shared/components/calendar/layout/CalendarShell.js +47 -39
  75. package/dist/apps/shared/components/contacts/ContactForm.js +16 -5
  76. package/dist/apps/shared/components/layout/AppShell.js +30 -7
  77. package/dist/apps/shared/components/layout/KeyEnrollmentGate.js +15 -3
  78. package/dist/apps/shared/components/layout/UnlockPromptProvider.js +77 -0
  79. package/dist/apps/shared/components/mail/MessageDetailPane.js +27 -2
  80. package/dist/apps/shared/components/mail/compose/ComposeContext.js +2 -2
  81. package/dist/apps/shared/components/mail/compose/ComposeWindow.js +134 -14
  82. package/dist/apps/shared/components/mail/layout/MailShell.js +106 -40
  83. package/dist/apps/shared/components/settings/layout/SettingsShell.js +1 -0
  84. package/dist/apps/shared/mail/findWellKnownFolderUid.js +12 -0
  85. package/dist/apps/shared/search/LocalIndexLifecycle.js +76 -0
  86. package/dist/apps/shared/search/localIndexBlockCipher.js +63 -0
  87. package/dist/apps/shared/search/localIndexBuilder.js +153 -0
  88. package/dist/apps/shared/search/localIndexKey.js +25 -0
  89. package/dist/apps/shared/search/localIndexRpcClient.js +78 -0
  90. package/dist/apps/shared/search/localIndexSchema.js +179 -0
  91. package/dist/apps/shared/search/localIndexSizePreference.js +78 -0
  92. package/dist/apps/shared/search/localIndexVFS.js +230 -0
  93. package/dist/apps/shared/search/localIndexWorker.js +341 -0
  94. package/dist/apps/shared/search/searchTier2.js +38 -0
  95. package/dist/apps/www/calendar/index.js +14 -12
  96. package/dist/apps/www/contacts/index.js +15 -6
  97. package/dist/apps/www/index.js +496 -94
  98. package/dist/apps/www/messages/[uid].js +5 -1
  99. package/dist/apps/www/settings/encryption/index.js +30 -2
  100. package/dist/apps/www/settings/sharing/index.js +128 -0
  101. package/dist/apps/www/tasks/index.js +27 -5
  102. package/package.json +3 -2
@@ -1,765 +1,930 @@
1
- ///////////////////////////////////////////////////////////////////////////////
2
- // Copyright (C) 2026 Jean-Philippe Steinmetz
3
- // SPDX-License-Identifier: MPL-2.0
4
- ///////////////////////////////////////////////////////////////////////////////
5
- import React, { ChangeEvent, useEffect, useRef, useState } from "react";
6
- import {
7
- HiOutlineArrowsPointingIn,
8
- HiOutlineArrowsPointingOut,
9
- HiOutlineChevronDown,
10
- HiOutlineMinus,
11
- HiOutlinePaperClip,
12
- HiOutlineTrash,
13
- HiOutlineXMark,
14
- } from "react-icons/hi2";
15
- import { ApiRequestError } from "@rapidmx/react-shared/util/api.js";
16
- import {
17
- Attachment,
18
- ComposeRecipientInput,
19
- Mailbox,
20
- Message,
21
- assembleDraft,
22
- assembleDraftRaw,
23
- attachmentContentUrl,
24
- createDraft,
25
- getMailbox,
26
- listFolders,
27
- sendMessage,
28
- setMessageRequestReceipt,
29
- setMessageScheduledSendTime,
30
- uploadAttachment,
31
- } from "@rapidmx/react-shared/mail/mailApi.js";
32
- import { listMailSignatures } from "@rapidmx/react-shared/mail/mailSignaturesApi.js";
33
- import { decideMessageEncryption, resolveRecipientEncryption, RecipientEncryptionStatus } from "@rapidmx/react-shared/crypto/composeSecurity.js";
34
- import { getUnlockedKeys } from "@rapidmx/react-shared/crypto/keySession.js";
35
- import { EncryptionPolicy, getEncryptionPolicy, lookupKeys } from "@rapidmx/react-shared/crypto/keyvaultApi.js";
36
- import { fromBase64 } from "@rapidmx/react-shared/crypto/encoding.js";
37
- import { ProtectedHeaders, applyBaselineOuterHeaders, assembleOutboundMime, buildEncryptedMessage, buildSignedOnlyMessage } from "@rapidmx/react-shared/crypto/smimeMessage.js";
38
- import useIsMobile from "@rapidmx/react-shared/util/useIsMobile.js";
39
- import type { ComposeSession } from "./ComposeContext.js";
40
- import RichTextEditor from "./RichTextEditor.js";
41
- import ScheduleSendPicker from "./ScheduleSendPicker.js";
42
- import Alert from "@rapidmx/react-shared/components/feedback/Alert.js";
43
- import Button from "@rapidmx/react-shared/components/buttons/Button.js";
44
-
45
- export interface ComposeWindowProps {
46
- session: ComposeSession;
47
- onClose: () => void;
48
- onToggleMinimize: () => void;
49
- }
50
-
51
- const FIELD_ROW = "flex items-center gap-2 px-3 py-1.5 border-b border-border";
52
- const FIELD_INPUT = "flex-1 min-w-0 text-sm bg-transparent outline-none";
53
-
54
- const MIN_WIDTH = 320;
55
- const MIN_HEIGHT = 320;
56
- // Kept well clear of the viewport edge, not flush against it — this is a resize *ceiling* (the drag
57
- // handles below clamp to it), not the "expanded" preset's own size, which stays a plain `85vh`
58
- // Tailwind class precisely so it never needs `window.innerHeight` at all: that'd have to be read at
59
- // module-evaluation time to live alongside these other constants, and `window` doesn't exist yet
60
- // during this app's SSR pass (see `ReactRoute`) — referencing it here would crash every page render,
61
- // not just this component's.
62
- const VIEWPORT_MARGIN = 24;
63
-
64
- interface Size {
65
- width: number;
66
- height: number;
67
- }
68
-
69
- type ResizeEdge = "left" | "top" | "corner";
70
-
71
- function parseAddresses(value: string): ComposeRecipientInput[] {
72
- return value
73
- .split(",")
74
- .map((s) => s.trim())
75
- .filter((s) => s.length > 0)
76
- .map((address) => ({ address }));
77
- }
78
-
79
- function HeaderButton({ label, onClick, icon: Icon }: { label: string; onClick: () => void; icon: React.ComponentType<{ size?: number }> }) {
80
- return (
81
- <button
82
- type="button"
83
- aria-label={label}
84
- title={label}
85
- onClick={onClick}
86
- className="w-6 h-6 flex items-center justify-center rounded-sm text-white/80 hover:bg-white/15 hover:text-white"
87
- >
88
- <Icon size={14} />
89
- </button>
90
- );
91
- }
92
-
93
- /**
94
- * A floating, Gmail-style compose window — overlays whatever app is currently showing instead of
95
- * navigating to a dedicated page (see `ComposeContext.tsx`'s doc comment for why: a full-page compose
96
- * form meant losing your place in the inbox/calendar/contacts view behind it, and every navigation to
97
- * `/compose` re-ran this whole app's full mailbox/folder resolution waterfall from scratch). Owns its
98
- * own draft lifecycle end to end (unlike the old page, which read the Drafts folder from `MailShell`'s
99
- * context — this window can be opened from apps that never mount `MailShell` at all, e.g. Contacts'
100
- * "Email" action, so it resolves its own Drafts folder from just a `mailboxUid`).
101
- *
102
- * Below the `md` breakpoint, a non-minimized session instead renders as a full-screen sheet (there's no
103
- * room for a floating window, and the click-drag resize handles below become meaningless full-screen) —
104
- * see `ComposeContext.tsx`'s own doc comment for how that interacts with several sessions being open at
105
- * once.
106
- */
107
- export default function ComposeWindow({ session, onClose, onToggleMinimize }: ComposeWindowProps) {
108
- const { id, mailboxUid, initialTo, initialCc, initialSubject, initialQuotedHtml, signatureContext, suppressSigning, minimized } = session;
109
- const isMobile = useIsMobile();
110
-
111
- const windowRef = useRef<HTMLDivElement>(null);
112
- const [draftsFolderUid, setDraftsFolderUid] = useState<string | undefined>();
113
- const [folderError, setFolderError] = useState<string | null>(null);
114
- const [draft, setDraft] = useState<Message | null>(null);
115
- const [draftError, setDraftError] = useState<string | null>(null);
116
- const [expanded, setExpanded] = useState(false);
117
- const [manualSize, setManualSize] = useState<Size | null>(null);
118
- const [to, setTo] = useState(initialTo ?? "");
119
- const [cc, setCc] = useState(initialCc ?? "");
120
- const [bcc, setBcc] = useState("");
121
- const [showCcBcc, setShowCcBcc] = useState(!!initialCc);
122
- const [subject, setSubject] = useState(initialSubject ?? "");
123
- const [html, setHtml] = useState("");
124
- const [contentReady, setContentReady] = useState(false);
125
- const [attachments, setAttachments] = useState<Attachment[]>([]);
126
- const [attachError, setAttachError] = useState<string | null>(null);
127
- const [sendError, setSendError] = useState<string | null>(null);
128
- const [sending, setSending] = useState(false);
129
- const [requestReceipt, setRequestReceipt] = useState(false);
130
- const [schedulePickerOpen, setSchedulePickerOpen] = useState(false);
131
- const scheduleButtonRef = useRef<HTMLButtonElement>(null);
132
- const [mailbox, setMailbox] = useState<Mailbox | null>(null);
133
- const [encryptionPolicy, setEncryptionPolicy] = useState<EncryptionPolicy | null>(null);
134
- // Defaults off when replying to a detected mailing list (spec's own "Mailing lists" note under
135
- // Digital Signatures - a list that appends a footer after signing invalidates the signature) - see
136
- // `MessageDetailPane.tsx`'s handleReply()/handleReplyAll() for where this is computed. The user can
137
- // still turn it back on; this only changes the default.
138
- const [signEnabled, setSignEnabled] = useState(!suppressSigning);
139
- const [encryptRequested, setEncryptRequested] = useState(false);
140
- const [encryptionBlocked, setEncryptionBlocked] = useState<RecipientEncryptionStatus[] | null>(null);
141
- // Compose-time discovery (spec: "Discovery occurs ... when the user addresses a new message to a
142
- // recipient", never on receipt) - keyed by address so a recipient already looked up isn't re-fetched
143
- // just because the user re-focuses the field. Only ever grows via `checkRecipientDiscovery()` below;
144
- // never cleared, so switching focus between To/Cc/Bcc repeatedly doesn't re-trigger lookups.
145
- const [recipientStatuses, setRecipientStatuses] = useState<Record<string, RecipientEncryptionStatus>>({});
146
-
147
- // Best-effort, same as the signature-list fetch below: a mailbox with no keys enrolled yet (or a
148
- // failed fetch) just means sign/encrypt stay unavailable for this compose session, never a blocking
149
- // error - encryption is optional and gradual by design (see `KeyEnrollmentGate`'s own doc comment).
150
- // `cryptoContextReady` (below) gates Send/Send-later until both calls have settled either way, so a
151
- // send that happens to race this fetch can't silently skip encryption the spec says should apply.
152
- const [cryptoContextReady, setCryptoContextReady] = useState(false);
153
- useEffect(() => {
154
- let cancelled = false;
155
- setCryptoContextReady(false);
156
- let mailboxSettled = false;
157
- let policySettled = false;
158
- function checkReady() {
159
- if (!cancelled && mailboxSettled && policySettled) {
160
- setCryptoContextReady(true);
161
- }
162
- }
163
- getMailbox(mailboxUid)
164
- .then((result) => {
165
- if (!cancelled) {
166
- setMailbox(result);
167
- }
168
- })
169
- .catch(() => undefined)
170
- .finally(() => {
171
- mailboxSettled = true;
172
- checkReady();
173
- });
174
- getEncryptionPolicy()
175
- .then((result) => {
176
- if (!cancelled) {
177
- setEncryptionPolicy(result);
178
- }
179
- })
180
- .catch(() => undefined)
181
- .finally(() => {
182
- policySettled = true;
183
- checkReady();
184
- });
185
- return () => {
186
- cancelled = true;
187
- };
188
- }, [mailboxUid]);
189
-
190
- useEffect(() => {
191
- listFolders(mailboxUid)
192
- .then((folders) => setDraftsFolderUid(folders.find((f) => f.type === "drafts")?.uid))
193
- .catch((err) => setFolderError(err instanceof ApiRequestError ? err.message : "Could not load your Drafts folder."));
194
- }, [mailboxUid]);
195
-
196
- /**
197
- * Runs discovery for whichever of `addresses` haven't already been looked up this compose session,
198
- * caching the result so the small per-recipient indicator below the recipient fields can update
199
- * without waiting for Send. Called from the To/Cc/Bcc fields' own `onBlur` - not on every keystroke,
200
- * and not the same lookup `assembleForSend()` performs again at send time (deliberately: this is a
201
- * best-effort UI hint, not something a stale value here should be trusted to skip at send time).
202
- * A failed lookup or a mailbox/policy fetch that hasn't resolved yet just leaves that address with no
203
- * indicator rather than surfacing an error over what's a cosmetic nicety.
204
- */
205
- function checkRecipientDiscovery(addresses: ComposeRecipientInput[]) {
206
- if (!mailbox || !encryptionPolicy) {
207
- return;
208
- }
209
- const ownPrefersMutual = mailbox.encryptPreference?.preferEncrypt === "mutual";
210
- const unchecked = addresses.filter((r) => !(r.address in recipientStatuses));
211
- for (const recipient of unchecked) {
212
- lookupKeys(mailboxUid, recipient.address)
213
- .catch(() => undefined)
214
- .then((lookup) => {
215
- const status = resolveRecipientEncryption(mailbox.primarySmtpAddress, ownPrefersMutual, encryptionPolicy, recipient.address, lookup);
216
- setRecipientStatuses((prev) => ({ ...prev, [recipient.address]: status }));
217
- });
218
- }
219
- }
220
-
221
- // Resolves the mailbox's default signature (if any) for `signatureContext` and seeds `html` with it
222
- // plus any quoted original message, before `RichTextEditor` ever mounts (gated by `contentReady`
223
- // below) — `RichTextEditor`'s own doc comment is explicit that `value` only seeds its *initial*
224
- // content and never re-syncs from a later prop change, so this has to resolve before that first
225
- // mount, not after. A signature-list failure is best-effort, same as every other supplementary,
226
- // non-blocking fetch in this codebase (e.g. `ConversationThreadPane`'s attachment fetch) — no
227
- // signature is a completely legitimate outcome, so this falls back to just the quoted content
228
- // (if any) rather than surfacing an error over what's a cosmetic nicety.
229
- useEffect(() => {
230
- listMailSignatures(mailboxUid)
231
- .then((signatures) => {
232
- const signature = signatures.find((s) =>
233
- signatureContext === "new" ? s.isDefaultForNewMessages : s.isDefaultForReplyForward,
234
- );
235
- const signatureHtml = signature?.contentHtml ? `${signature.contentHtml}<p></p>` : "";
236
- setHtml(`${signatureHtml}${initialQuotedHtml ?? ""}`);
237
- })
238
- .catch(() => setHtml(initialQuotedHtml ?? ""))
239
- .finally(() => setContentReady(true));
240
- }, [mailboxUid, signatureContext, initialQuotedHtml]);
241
-
242
- useEffect(() => {
243
- if (!draftsFolderUid || draft) {
244
- return;
245
- }
246
- createDraft(mailboxUid, draftsFolderUid)
247
- .then(setDraft)
248
- .catch((err) => setDraftError(err instanceof ApiRequestError ? err.message : "Could not start a new draft."));
249
- }, [mailboxUid, draftsFolderUid, draft]);
250
-
251
- /**
252
- * Click-and-drag resize, from the window's own top/left edges (and the top-left corner, for both
253
- * at once) — the window is anchored bottom-right (see `ComposeContext.tsx`'s stack), so growing it
254
- * always means extending up and/or left, never down/right. `onMove`/`onUp` are fresh closures
255
- * created per drag gesture (captured over this gesture's own start position/size) rather than
256
- * stable component-level functions, specifically so `removeEventListener` in `onUp` always removes
257
- * the exact listener `onMove`/`onUp` themselves just added — a version defined once per render and
258
- * reused across gestures would need extra ref-juggling to avoid removing a stale (already-replaced)
259
- * listener instead of the current one.
260
- */
261
- function handleResizeStart(edge: ResizeEdge) {
262
- return (e: React.PointerEvent) => {
263
- e.preventDefault();
264
- const rect = windowRef.current!.getBoundingClientRect();
265
- const startX = e.clientX;
266
- const startY = e.clientY;
267
- const startWidth = rect.width;
268
- const startHeight = rect.height;
269
-
270
- function onMove(ev: PointerEvent) {
271
- const maxWidth = window.innerWidth - VIEWPORT_MARGIN;
272
- const maxHeight = window.innerHeight - VIEWPORT_MARGIN;
273
- const width = edge === "top" ? startWidth : Math.min(Math.max(startWidth - (ev.clientX - startX), MIN_WIDTH), maxWidth);
274
- const height = edge === "left" ? startHeight : Math.min(Math.max(startHeight - (ev.clientY - startY), MIN_HEIGHT), maxHeight);
275
- setManualSize({ width, height });
276
- }
277
- function onUp() {
278
- window.removeEventListener("pointermove", onMove);
279
- window.removeEventListener("pointerup", onUp);
280
- }
281
- window.addEventListener("pointermove", onMove);
282
- window.addEventListener("pointerup", onUp);
283
- };
284
- }
285
-
286
- function toggleExpanded() {
287
- setExpanded((v) => !v);
288
- setManualSize(null);
289
- }
290
-
291
- /** Uploads `file` as an attachment on the current draft, resolving to a URL the editor can preview
292
- * it at immediately — see `BaseMailComposeRoute.rewriteInlineImageSources()`'s doc comment for how
293
- * that URL gets swapped for the message's real inline `cid:` reference at send time. Resolves to
294
- * `null` (surfacing `attachError` itself, reusing the same state "Attach files" already has) rather
295
- * than throwing: unlike "Attach files" (`disabled={!draft}`), "Insert image" in the formatting
296
- * toolbar is only gated on the *editor* being mounted, not on the draft existing yet — those two
297
- * things load in parallel, so a real (if narrow) window exists where the button is clickable before
298
- * `draft` resolves, and this needs to fail gracefully rather than crash on a null draft.
299
- */
300
- async function handleUploadImage(file: File): Promise<string | null> {
301
- if (!draft) {
302
- setAttachError("Please wait for the draft to finish loading before inserting an image.");
303
- return null;
304
- }
305
- try {
306
- const attachment = await uploadAttachment(draft.uid, file);
307
- return attachmentContentUrl(attachment.uid);
308
- } catch (err) {
309
- setAttachError(err instanceof ApiRequestError ? err.message : "Could not upload image.");
310
- return null;
311
- }
312
- }
313
-
314
- // The "Attach files" input is itself `disabled` until `draft` resolves (see its `disabled={!draft}`
315
- // below), so this can only ever fire once `draft` is set — no defensive null check needed, matching
316
- // this codebase's established pattern for the same class of "always non-null by the time it's
317
- // called" value (e.g. `ComposeToolbar.run()`). No separate empty-`files` guard either: the loop
318
- // below is already a no-op when there's nothing to iterate.
319
- async function handleFilesSelected(e: ChangeEvent<HTMLInputElement>) {
320
- const files = Array.from(e.target.files ?? []);
321
- e.target.value = "";
322
- setAttachError(null);
323
- for (const file of files) {
324
- try {
325
- const attachment = await uploadAttachment(draft!.uid, file);
326
- setAttachments((prev) => [...prev, attachment]);
327
- } catch (err) {
328
- setAttachError(err instanceof ApiRequestError ? err.message : "Could not upload attachment.");
329
- }
330
- }
331
- }
332
-
333
- /**
334
- * Builds this draft's final body — plaintext, signed-only, or signed+encrypted per
335
- * `specs/end-to-end_encryption.md` — then stores it via `assembleDraft()`/`assembleDraftRaw()`.
336
- * Shared by `handleSend()`/`handleScheduleSend()` (and the "Send without encryption" conflict button
337
- * rendered below, via `forcePlaintext`) so the sign/encrypt decision lives in exactly one place.
338
- *
339
- * Returns `"blocked"` (never throws for this case) when encryption was requested but not every
340
- * recipient can currently be encrypted to — the spec's "Multiple Recipients" all-or-nothing rule.
341
- * Sets `encryptionBlocked` as a side effect so the window can render that prompt; callers must stop
342
- * (not send) when they get this back.
343
- *
344
- * Discovery (`lookupKeys()`) and the resulting sign/encrypt decision only run at this, the final
345
- * pre-send step — not live as recipients are typed. A compose-time recipient-entry indicator (key
346
- * icons, live discovery as each address is entered) is Phase 4's "Discovery & contacts UI" work, not
347
- * this pass; this still satisfies the spec's "MUST be called lazily at compose time, not on receipt"
348
- * requirement, just without a live UI reflecting it before Send is clicked.
349
- */
350
- async function assembleForSend(
351
- toRecipients: ComposeRecipientInput[],
352
- ccRecipients: ComposeRecipientInput[],
353
- bccRecipients: ComposeRecipientInput[],
354
- forcePlaintext: boolean,
355
- ): Promise<Message | "blocked"> {
356
- const unlocked = getUnlockedKeys(mailboxUid);
357
- // `mailbox` is required to build protected headers (own From address) whenever signing or
358
- // encrypting - not just guarded on the encryption branch below, since a signed-only message
359
- // needs it too. A mailbox fetch failure just means no crypto for this send, never a crash.
360
- const canSign = signEnabled && !!mailbox && !!unlocked?.signingPrivateKey && !!unlocked.signingCertDer;
361
- const canEncryptSelf = !!unlocked?.encryptionPrivateKey && !!unlocked.encryptionCertDer;
362
- const allRecipients = [...toRecipients, ...ccRecipients, ...bccRecipients];
363
-
364
- let wantEncrypt = false;
365
- let recipientCertDers: Uint8Array[] = [];
366
- if (!forcePlaintext && canEncryptSelf && mailbox && encryptionPolicy && allRecipients.length > 0) {
367
- const ownPrefersMutual = mailbox.encryptPreference?.preferEncrypt === "mutual";
368
- const lookups = await Promise.all(allRecipients.map((r) => lookupKeys(mailboxUid, r.address).catch(() => undefined)));
369
- const statuses = allRecipients.map((r, i) =>
370
- resolveRecipientEncryption(mailbox.primarySmtpAddress, ownPrefersMutual, encryptionPolicy, r.address, lookups[i]),
371
- );
372
- const decision = decideMessageEncryption(statuses);
373
- wantEncrypt = encryptRequested || decision.autoEncrypt;
374
- if (wantEncrypt) {
375
- if (!decision.canEncryptAll) {
376
- setEncryptionBlocked(decision.blockedRecipients);
377
- return "blocked";
378
- }
379
- recipientCertDers = [unlocked!.encryptionCertDer!, ...statuses.map((s) => fromBase64(s.encryptCert!.publicKey))];
380
- }
381
- }
382
-
383
- if ((wantEncrypt || canSign) && attachments.length > 0) {
384
- // Matches BaseMailComposeRoute.assembleRaw()'s own server-side rejection, surfaced here with a
385
- // clearer explanation than that route's generic 400 rather than via a round trip.
386
- throw new ApiRequestError("A signed or encrypted message cannot include file attachments yet - remove them before sending.", 400);
387
- }
388
-
389
- if (!wantEncrypt && !canSign) {
390
- // See the old compose page's identical note: `sanitize-html` is Node-oriented and the server-side
391
- // gate in `BaseMailComposeRoute.assemble()` is the sole authoritative sanitizer regardless, so no
392
- // client-side pass is done here either.
393
- return assembleDraft(draft!.uid, { to: toRecipients, cc: ccRecipients, bcc: bccRecipients, subject, html });
394
- }
395
-
396
- const domain = mailbox!.primarySmtpAddress.split("@")[1] ?? "localhost";
397
- const protectedHeaders: ProtectedHeaders = {
398
- from: mailbox!.displayName
399
- ? `"${mailbox!.displayName.replace(/"/g, '\\"')}" <${mailbox!.primarySmtpAddress}>`
400
- : mailbox!.primarySmtpAddress,
401
- to: toRecipients.map((r) => r.address).join(", "),
402
- cc: ccRecipients.length > 0 ? ccRecipients.map((r) => r.address).join(", ") : undefined,
403
- date: new Date().toUTCString(),
404
- subject,
405
- messageId: `<${crypto.randomUUID()}@${domain}>`,
406
- };
407
- const bodyContentType = 'text/html; charset="utf-8"';
408
- const signing = canSign ? { certDer: unlocked!.signingCertDer!, privateKey: unlocked!.signingPrivateKey! } : undefined;
409
-
410
- const outerHeaders = wantEncrypt ? applyBaselineOuterHeaders(protectedHeaders) : protectedHeaders;
411
- const mimePart = wantEncrypt
412
- ? await buildEncryptedMessage(bodyContentType, html, protectedHeaders, outerHeaders, recipientCertDers, signing)
413
- : await buildSignedOnlyMessage(bodyContentType, html, protectedHeaders, signing!.certDer, signing!.privateKey);
414
- const rawMime = assembleOutboundMime(outerHeaders, mimePart);
415
- return assembleDraftRaw(draft!.uid, { to: toRecipients, cc: ccRecipients, bcc: bccRecipients, subject: outerHeaders.subject, rawMime });
416
- }
417
-
418
- // Same reasoning as `handleFilesSelected` above: the Send button is itself `disabled` until `draft`
419
- // resolves, so this is never reachable with a null `draft`.
420
- async function handleSend(forcePlaintext = false) {
421
- const toRecipients = parseAddresses(to);
422
- if (toRecipients.length === 0) {
423
- setSendError("At least one recipient is required.");
424
- return;
425
- }
426
-
427
- setSending(true);
428
- setSendError(null);
429
- try {
430
- const assembled = await assembleForSend(toRecipients, parseAddresses(cc), parseAddresses(bcc), forcePlaintext);
431
- if (assembled === "blocked") {
432
- return;
433
- }
434
- const withReceipt = requestReceipt ? await setMessageRequestReceipt(assembled, true) : assembled;
435
- await sendMessage(withReceipt.uid);
436
- onClose();
437
- } catch (err) {
438
- setSendError(err instanceof ApiRequestError ? err.message : "Could not send this message.");
439
- } finally {
440
- setSending(false);
441
- }
442
- }
443
-
444
- // Same reasoning as `handleSend` above (and the "Send later" caret is itself `disabled` alongside
445
- // Send until `draft` resolves) — never reachable with a null `draft`. Assembles the draft first
446
- // (same as an immediate send), then sets `scheduledSendTime` on the *freshly assembled* copy before
447
- // calling `sendMessage()` — `send()` itself is what reads the field and defers relay into Outbox
448
- // instead of sending immediately (see `mailApi.ts`'s own doc comment on `setMessageScheduledSendTime`).
449
- async function handleScheduleSend(scheduledSendTimeIso: string) {
450
- const toRecipients = parseAddresses(to);
451
- setSchedulePickerOpen(false);
452
- if (toRecipients.length === 0) {
453
- setSendError("At least one recipient is required.");
454
- return;
455
- }
456
-
457
- setSending(true);
458
- setSendError(null);
459
- try {
460
- const assembled = await assembleForSend(toRecipients, parseAddresses(cc), parseAddresses(bcc), false);
461
- if (assembled === "blocked") {
462
- return;
463
- }
464
- const withReceipt = requestReceipt ? await setMessageRequestReceipt(assembled, true) : assembled;
465
- const scheduled = await setMessageScheduledSendTime(withReceipt, scheduledSendTimeIso);
466
- await sendMessage(scheduled.uid);
467
- onClose();
468
- } catch (err) {
469
- setSendError(err instanceof ApiRequestError ? err.message : "Could not schedule this message.");
470
- } finally {
471
- setSending(false);
472
- }
473
- }
474
-
475
- // A plain read from keySession.ts's module-level session store, not React state - see that module's
476
- // own doc comment. Cheap enough to read fresh on every render rather than caching in state.
477
- const unlockedKeys = getUnlockedKeys(mailboxUid);
478
-
479
- const title = subject.trim() || "New Message";
480
- const titleId = `compose-title-${id}`;
481
-
482
- if (minimized) {
483
- return (
484
- <div role="dialog" aria-label={title} className="w-64 shrink-0 bg-surface border border-border border-b-0 rounded-t-md shadow-modal">
485
- <div
486
- className="h-10 flex items-center justify-between gap-2 px-3 rounded-t-md bg-primary-darker text-white cursor-pointer"
487
- onClick={onToggleMinimize}
488
- >
489
- <span className="text-sm font-medium truncate">{title}</span>
490
- <div className="flex items-center gap-0.5" onClick={(e) => e.stopPropagation()}>
491
- <HeaderButton label="Restore" onClick={onToggleMinimize} icon={HiOutlineArrowsPointingOut} />
492
- <HeaderButton
493
- label="Discard draft"
494
- onClick={onClose}
495
- icon={HiOutlineXMark}
496
- />
497
- </div>
498
- </div>
499
- </div>
500
- );
501
- }
502
-
503
- return (
504
- <div
505
- ref={windowRef}
506
- role="dialog"
507
- aria-labelledby={titleId}
508
- style={!isMobile && manualSize ? { width: manualSize.width, height: manualSize.height } : undefined}
509
- className={[
510
- "relative shrink-0 flex flex-col bg-surface border border-border shadow-modal overflow-hidden",
511
- isMobile
512
- ? "fixed inset-0 w-full h-full rounded-none border-0"
513
- : ["border-b-0 rounded-t-md", manualSize ? "" : expanded ? "w-[720px] h-[85vh]" : "w-[480px] h-[520px]"].join(" "),
514
- ].join(" ")}
515
- >
516
- {!isMobile && (
517
- <>
518
- <div
519
- onPointerDown={handleResizeStart("top")}
520
- aria-hidden="true"
521
- className="absolute top-0 left-0 right-0 h-1.5 cursor-ns-resize z-20"
522
- />
523
- <div
524
- onPointerDown={handleResizeStart("left")}
525
- aria-hidden="true"
526
- className="absolute top-0 left-0 bottom-0 w-1.5 cursor-ew-resize z-20"
527
- />
528
- <div
529
- onPointerDown={handleResizeStart("corner")}
530
- role="separator"
531
- aria-label="Resize"
532
- className="absolute top-0 left-0 w-3 h-3 cursor-nwse-resize z-30"
533
- />
534
- </>
535
- )}
536
-
537
- <div
538
- className="h-10 shrink-0 flex items-center justify-between gap-2 px-3 bg-primary-darker text-white cursor-pointer"
539
- onClick={onToggleMinimize}
540
- >
541
- <span id={titleId} className="text-sm font-medium truncate">
542
- {title}
543
- </span>
544
- <div className="flex items-center gap-0.5" onClick={(e) => e.stopPropagation()}>
545
- <HeaderButton label="Minimize" onClick={onToggleMinimize} icon={HiOutlineMinus} />
546
- {!isMobile && (
547
- <HeaderButton
548
- label={expanded ? "Collapse" : "Expand"}
549
- onClick={toggleExpanded}
550
- icon={expanded ? HiOutlineArrowsPointingIn : HiOutlineArrowsPointingOut}
551
- />
552
- )}
553
- <HeaderButton label="Close" onClick={onClose} icon={HiOutlineXMark} />
554
- </div>
555
- </div>
556
-
557
- <div className="flex-1 min-h-0 flex flex-col">
558
- {(folderError || draftError || sendError || attachError) && (
559
- <div className="px-3 pt-2">
560
- {folderError && <Alert>{folderError}</Alert>}
561
- {draftError && <Alert>{draftError}</Alert>}
562
- {sendError && <Alert>{sendError}</Alert>}
563
- {attachError && <Alert>{attachError}</Alert>}
564
- </div>
565
- )}
566
-
567
- {encryptionBlocked && (
568
- <div className="px-3 pt-2">
569
- <Alert>
570
- <p className="mb-2">
571
- This message can&rsquo;t be encrypted for everyone: {encryptionBlocked.map((r) => r.address).join(", ")}
572
- {encryptionBlocked[0]?.prohibitedReason ? ` — ${encryptionBlocked[0].prohibitedReason}` : " has no encryption key on file"}.
573
- Remove {encryptionBlocked.length > 1 ? "these recipients" : "this recipient"} from To/Cc/Bcc, or send the
574
- whole message in plaintext.
575
- </p>
576
- <Button
577
- type="button"
578
- onClick={() => {
579
- setEncryptionBlocked(null);
580
- void handleSend(true);
581
- }}
582
- >
583
- Send without encryption
584
- </Button>
585
- </Alert>
586
- </div>
587
- )}
588
-
589
- <div className={FIELD_ROW}>
590
- <label htmlFor={`compose-to-${id}`} className="text-xs text-text-muted shrink-0">
591
- To
592
- </label>
593
- <input
594
- id={`compose-to-${id}`}
595
- type="text"
596
- className={FIELD_INPUT}
597
- value={to}
598
- onChange={(e) => setTo(e.target.value)}
599
- onBlur={() => checkRecipientDiscovery(parseAddresses(to))}
600
- />
601
- {!showCcBcc && (
602
- <button
603
- type="button"
604
- onClick={() => setShowCcBcc(true)}
605
- className="text-xs text-text-muted hover:text-text shrink-0"
606
- >
607
- Cc Bcc
608
- </button>
609
- )}
610
- </div>
611
-
612
- {showCcBcc && (
613
- <>
614
- <div className={FIELD_ROW}>
615
- <label htmlFor={`compose-cc-${id}`} className="text-xs text-text-muted shrink-0">
616
- Cc
617
- </label>
618
- <input
619
- id={`compose-cc-${id}`}
620
- type="text"
621
- className={FIELD_INPUT}
622
- value={cc}
623
- onChange={(e) => setCc(e.target.value)}
624
- onBlur={() => checkRecipientDiscovery(parseAddresses(cc))}
625
- />
626
- </div>
627
- <div className={FIELD_ROW}>
628
- <label htmlFor={`compose-bcc-${id}`} className="text-xs text-text-muted shrink-0">
629
- Bcc
630
- </label>
631
- <input
632
- id={`compose-bcc-${id}`}
633
- type="text"
634
- className={FIELD_INPUT}
635
- value={bcc}
636
- onChange={(e) => setBcc(e.target.value)}
637
- onBlur={() => checkRecipientDiscovery(parseAddresses(bcc))}
638
- />
639
- </div>
640
- </>
641
- )}
642
-
643
- {(() => {
644
- const knownRecipients = [...parseAddresses(to), ...parseAddresses(cc), ...parseAddresses(bcc)]
645
- .map((r) => recipientStatuses[r.address])
646
- .filter((status): status is RecipientEncryptionStatus => !!status);
647
- if (knownRecipients.length === 0) {
648
- return null;
649
- }
650
- return (
651
- <ul className="flex flex-wrap gap-1.5 px-3 pt-1.5" aria-label="Recipient encryption availability">
652
- {knownRecipients.map((status) => (
653
- <li
654
- key={status.address}
655
- className={`text-xs font-medium py-0.5 px-2 rounded-pill ${
656
- status.canEncrypt ? "bg-primary/10 text-primary-dark" : "bg-surface-alt text-text-muted"
657
- }`}
658
- >
659
- {status.address} {status.canEncrypt ? "supports encryption" : "no encryption key found"}
660
- </li>
661
- ))}
662
- </ul>
663
- );
664
- })()}
665
-
666
- <div className={FIELD_ROW}>
667
- <input
668
- aria-label="Subject"
669
- type="text"
670
- placeholder="Subject"
671
- className={FIELD_INPUT}
672
- value={subject}
673
- onChange={(e) => setSubject(e.target.value)}
674
- />
675
- </div>
676
-
677
- <div className="flex-1 min-h-0 p-2">
678
- {contentReady && <RichTextEditor value={html} onChange={setHtml} fill onUploadImage={handleUploadImage} />}
679
- </div>
680
-
681
- {attachments.length > 0 && (
682
- <ul className="flex flex-wrap gap-2 px-3 pb-2">
683
- {attachments.map((attachment) => (
684
- <li key={attachment.uid} className="text-xs font-medium py-1 px-2.5 rounded-pill bg-surface-alt text-text-muted">
685
- {attachment.filename}
686
- </li>
687
- ))}
688
- </ul>
689
- )}
690
-
691
- <label className="flex items-center gap-1.5 px-3 pb-1 text-xs text-text-muted">
692
- <input type="checkbox" checked={requestReceipt} onChange={(e) => setRequestReceipt(e.target.checked)} />
693
- Request a read receipt
694
- </label>
695
-
696
- {/* Only rendered once this session actually has a usable signing/encryption key - see
697
- `assembleForSend()`'s own doc comment for why signing rarely shows today (no signing
698
- certificate exists until RFC 8823 ACME enrollment lands). */}
699
- {unlockedKeys?.signingPrivateKey && (
700
- <label className="flex items-center gap-1.5 px-3 pb-1 text-xs text-text-muted">
701
- <input type="checkbox" checked={signEnabled} onChange={(e) => setSignEnabled(e.target.checked)} />
702
- Digitally sign this message
703
- </label>
704
- )}
705
- {unlockedKeys?.encryptionPrivateKey && (
706
- <label className="flex items-center gap-1.5 px-3 pb-1 text-xs text-text-muted">
707
- <input type="checkbox" checked={encryptRequested} onChange={(e) => setEncryptRequested(e.target.checked)} />
708
- Encrypt this message
709
- </label>
710
- )}
711
-
712
- <div className="shrink-0 flex items-center gap-1 px-3 py-2 border-t border-border">
713
- <div className="flex items-center rounded-pill bg-primary text-white overflow-hidden">
714
- <button
715
- type="button"
716
- onClick={() => handleSend()}
717
- disabled={!draft || sending || !cryptoContextReady}
718
- className="py-1.5 pl-5 pr-3 font-semibold text-sm hover:not-disabled:bg-primary-dark disabled:opacity-55 disabled:cursor-not-allowed"
719
- >
720
- {sending ? "Sending…" : "Send"}
721
- </button>
722
- <button
723
- ref={scheduleButtonRef}
724
- type="button"
725
- aria-label="Send later"
726
- aria-haspopup="true"
727
- aria-expanded={schedulePickerOpen}
728
- disabled={!draft || sending || !cryptoContextReady}
729
- onClick={() => setSchedulePickerOpen((o) => !o)}
730
- className="py-1.5 px-2 border-l border-white/30 hover:not-disabled:bg-primary-dark disabled:opacity-55 disabled:cursor-not-allowed"
731
- >
732
- <HiOutlineChevronDown size={14} />
733
- </button>
734
- </div>
735
- {schedulePickerOpen && (
736
- <ScheduleSendPicker
737
- anchorRef={scheduleButtonRef}
738
- onClose={() => setSchedulePickerOpen(false)}
739
- onSchedule={handleScheduleSend}
740
- />
741
- )}
742
-
743
- <label
744
- aria-label="Attach files"
745
- title="Attach files"
746
- className="w-8 h-8 flex items-center justify-center rounded-full text-text-muted hover:bg-surface-alt hover:text-text cursor-pointer has-[:disabled]:opacity-40 has-[:disabled]:cursor-not-allowed"
747
- >
748
- <HiOutlinePaperClip size={18} />
749
- <input type="file" multiple disabled={!draft} onChange={handleFilesSelected} className="sr-only" />
750
- </label>
751
-
752
- <button
753
- type="button"
754
- aria-label="Discard draft"
755
- title="Discard draft"
756
- onClick={onClose}
757
- className="ml-auto w-8 h-8 flex items-center justify-center rounded-full text-text-muted hover:bg-surface-alt hover:text-text"
758
- >
759
- <HiOutlineTrash size={18} />
760
- </button>
761
- </div>
762
- </div>
763
- </div>
764
- );
765
- }
1
+ ///////////////////////////////////////////////////////////////////////////////
2
+ // Copyright (C) 2026 Jean-Philippe Steinmetz
3
+ // SPDX-License-Identifier: MPL-2.0
4
+ ///////////////////////////////////////////////////////////////////////////////
5
+ import React, { ChangeEvent, useEffect, useRef, useState } from "react";
6
+ import {
7
+ HiOutlineArrowsPointingIn,
8
+ HiOutlineArrowsPointingOut,
9
+ HiOutlineChevronDown,
10
+ HiOutlineLockClosed,
11
+ HiOutlineMinus,
12
+ HiOutlinePaperClip,
13
+ HiOutlineTrash,
14
+ HiOutlineXMark,
15
+ } from "react-icons/hi2";
16
+ import { ApiRequestError } from "@rapidmx/react-shared/util/api.js";
17
+ import {
18
+ Attachment,
19
+ ComposeRecipientInput,
20
+ Mailbox,
21
+ Message,
22
+ assembleDraft,
23
+ assembleDraftRaw,
24
+ attachmentContentUrl,
25
+ createDraft,
26
+ deleteMessage,
27
+ getMailbox,
28
+ listFolders,
29
+ listMailboxes,
30
+ sendMessage,
31
+ setMessageRequestReceipt,
32
+ setMessageScheduledSendTime,
33
+ uploadAttachment,
34
+ } from "@rapidmx/react-shared/mail/mailApi.js";
35
+ import { listMailSignatures } from "@rapidmx/react-shared/mail/mailSignaturesApi.js";
36
+ import { decideMessageEncryption, resolveRecipientEncryption, RecipientEncryptionStatus } from "@rapidmx/react-shared/crypto/composeSecurity.js";
37
+ import { getUnlockedKeys } from "@rapidmx/react-shared/crypto/keySession.js";
38
+ import { EncryptionPolicy, findActivePublicKey, getEncryptionPolicy, lookupKeys } from "@rapidmx/react-shared/crypto/keyvaultApi.js";
39
+ import { useUnlockPrompt } from "../../layout/UnlockPromptProvider.js";
40
+ import { fromBase64 } from "@rapidmx/react-shared/crypto/encoding.js";
41
+ import { ProtectedHeaders, applyBaselineOuterHeaders, assembleOutboundMime, buildEncryptedMessage, buildSignedOnlyMessage } from "@rapidmx/react-shared/crypto/smimeMessage.js";
42
+ import useIsMobile from "@rapidmx/react-shared/util/useIsMobile.js";
43
+ import type { ComposeSession } from "./ComposeContext.js";
44
+ import RichTextEditor from "./RichTextEditor.js";
45
+ import ScheduleSendPicker from "./ScheduleSendPicker.js";
46
+ import Alert from "@rapidmx/react-shared/components/feedback/Alert.js";
47
+ import Button from "@rapidmx/react-shared/components/buttons/Button.js";
48
+
49
+ export interface ComposeWindowProps {
50
+ session: ComposeSession;
51
+ onClose: () => void;
52
+ onToggleMinimize: () => void;
53
+ /** The signed-in user - identifies their own ("primary") mailbox, the default From when the session
54
+ * doesn't name a mailbox. */
55
+ userUid?: string;
56
+ }
57
+
58
+ const FIELD_ROW = "flex items-center gap-2 px-3 py-1.5 border-b border-border";
59
+ const FIELD_INPUT = "flex-1 min-w-0 text-sm bg-transparent outline-none";
60
+
61
+ const MIN_WIDTH = 320;
62
+ const MIN_HEIGHT = 320;
63
+ // Kept well clear of the viewport edge, not flush against it — this is a resize *ceiling* (the drag
64
+ // handles below clamp to it), not the "expanded" preset's own size, which stays a plain `85vh`
65
+ // Tailwind class precisely so it never needs `window.innerHeight` at all: that'd have to be read at
66
+ // module-evaluation time to live alongside these other constants, and `window` doesn't exist yet
67
+ // during this app's SSR pass (see `ReactRoute`) — referencing it here would crash every page render,
68
+ // not just this component's.
69
+ const VIEWPORT_MARGIN = 24;
70
+
71
+ interface Size {
72
+ width: number;
73
+ height: number;
74
+ }
75
+
76
+ type ResizeEdge = "left" | "top" | "corner";
77
+
78
+ function parseAddresses(value: string): ComposeRecipientInput[] {
79
+ return value
80
+ .split(",")
81
+ .map((s) => s.trim())
82
+ .filter((s) => s.length > 0)
83
+ .map((address) => ({ address }));
84
+ }
85
+
86
+ function HeaderButton({ label, onClick, icon: Icon }: { label: string; onClick: () => void; icon: React.ComponentType<{ size?: number }> }) {
87
+ return (
88
+ <button
89
+ type="button"
90
+ aria-label={label}
91
+ title={label}
92
+ onClick={onClick}
93
+ className="w-6 h-6 flex items-center justify-center rounded-sm text-white/80 hover:bg-white/15 hover:text-white"
94
+ >
95
+ <Icon size={14} />
96
+ </button>
97
+ );
98
+ }
99
+
100
+ /**
101
+ * A floating, Gmail-style compose window — overlays whatever app is currently showing instead of
102
+ * navigating to a dedicated page (see `ComposeContext.tsx`'s doc comment for why: a full-page compose
103
+ * form meant losing your place in the inbox/calendar/contacts view behind it, and every navigation to
104
+ * `/compose` re-ran this whole app's full mailbox/folder resolution waterfall from scratch). Owns its
105
+ * own draft lifecycle end to end (unlike the old page, which read the Drafts folder from `MailShell`'s
106
+ * context — this window can be opened from apps that never mount `MailShell` at all, e.g. Contacts'
107
+ * "Email" action, so it resolves its own Drafts folder from just a `mailboxUid`).
108
+ *
109
+ * Below the `md` breakpoint, a non-minimized session instead renders as a full-screen sheet (there's no
110
+ * room for a floating window, and the click-drag resize handles below become meaningless full-screen) —
111
+ * see `ComposeContext.tsx`'s own doc comment for how that interacts with several sessions being open at
112
+ * once.
113
+ */
114
+ export default function ComposeWindow({ session, onClose, onToggleMinimize, userUid }: ComposeWindowProps) {
115
+ const { id, initialTo, initialCc, initialSubject, initialQuotedHtml, signatureContext, suppressSigning, minimized } = session;
116
+ // The sending ("From") mailbox. A reply/forward session names the original message's mailbox; a fresh
117
+ // compose leaves it unset and defaults to the caller's own mailbox once `listMailboxes()` resolves.
118
+ // Everything mailbox-scoped below (Drafts folder, draft, signatures, crypto context) keys off this.
119
+ const [fromMailboxUid, setFromMailboxUid] = useState<string | undefined>(session.mailboxUid);
120
+ const mailboxUid = fromMailboxUid;
121
+ const [mailboxes, setMailboxes] = useState<Mailbox[]>([]);
122
+ // Attachments and inline images upload onto the current draft's uid, which lives in one mailbox's
123
+ // Drafts folder - so once anything has been uploaded the sender can no longer be switched.
124
+ const [hasUploads, setHasUploads] = useState(false);
125
+ const isMobile = useIsMobile();
126
+ const { requestUnlock } = useUnlockPrompt();
127
+ // Bumped after a successful on-demand unlock to force a re-render - `getUnlockedKeys()` below is a
128
+ // plain read from a module-level store, not React state, so nothing else would pick up the change.
129
+ const [, setUnlockRefresh] = useState(0);
130
+
131
+ const windowRef = useRef<HTMLDivElement>(null);
132
+ const [draftsFolderUid, setDraftsFolderUid] = useState<string | undefined>();
133
+ const [folderError, setFolderError] = useState<string | null>(null);
134
+ const [draft, setDraft] = useState<Message | null>(null);
135
+ const [draftError, setDraftError] = useState<string | null>(null);
136
+ const [expanded, setExpanded] = useState(false);
137
+ const [manualSize, setManualSize] = useState<Size | null>(null);
138
+ const [to, setTo] = useState(initialTo ?? "");
139
+ const [cc, setCc] = useState(initialCc ?? "");
140
+ const [bcc, setBcc] = useState("");
141
+ const [showCcBcc, setShowCcBcc] = useState(!!initialCc);
142
+ const [subject, setSubject] = useState(initialSubject ?? "");
143
+ const [html, setHtml] = useState("");
144
+ const [contentReady, setContentReady] = useState(false);
145
+ const [attachments, setAttachments] = useState<Attachment[]>([]);
146
+ const [attachError, setAttachError] = useState<string | null>(null);
147
+ const [sendError, setSendError] = useState<string | null>(null);
148
+ const [sending, setSending] = useState(false);
149
+ const [requestReceipt, setRequestReceipt] = useState(false);
150
+ const [schedulePickerOpen, setSchedulePickerOpen] = useState(false);
151
+ const scheduleButtonRef = useRef<HTMLButtonElement>(null);
152
+ const [mailbox, setMailbox] = useState<Mailbox | null>(null);
153
+ const [encryptionPolicy, setEncryptionPolicy] = useState<EncryptionPolicy | null>(null);
154
+ // Defaults off when replying to a detected mailing list (spec's own "Mailing lists" note under
155
+ // Digital Signatures - a list that appends a footer after signing invalidates the signature) - see
156
+ // `MessageDetailPane.tsx`'s handleReply()/handleReplyAll() for where this is computed. The user can
157
+ // still turn it back on; this only changes the default.
158
+ const [signEnabled, setSignEnabled] = useState(!suppressSigning);
159
+ const [encryptRequested, setEncryptRequested] = useState(false);
160
+ const [encryptionBlocked, setEncryptionBlocked] = useState<RecipientEncryptionStatus[] | null>(null);
161
+ // Compose-time discovery (spec: "Discovery occurs ... when the user addresses a new message to a
162
+ // recipient", never on receipt) - keyed by address so a recipient already looked up isn't re-fetched
163
+ // just because the user re-focuses the field. Only ever grows via `checkRecipientDiscovery()` below;
164
+ // never cleared, so switching focus between To/Cc/Bcc repeatedly doesn't re-trigger lookups.
165
+ const [recipientStatuses, setRecipientStatuses] = useState<Record<string, RecipientEncryptionStatus>>({});
166
+
167
+ // Best-effort, same as the signature-list fetch below: a mailbox with no keys enrolled yet (or a
168
+ // failed fetch) just means sign/encrypt stay unavailable for this compose session, never a blocking
169
+ // error - encryption is optional and gradual by design (see `KeyEnrollmentGate`'s own doc comment).
170
+ // `cryptoContextReady` (below) gates Send/Send-later until both calls have settled either way, so a
171
+ // send that happens to race this fetch can't silently skip encryption the spec says should apply.
172
+ const [cryptoContextReady, setCryptoContextReady] = useState(false);
173
+
174
+ useEffect(() => {
175
+ let cancelled = false;
176
+ listMailboxes({ limit: 100 })
177
+ .then((result) => {
178
+ if (cancelled) {
179
+ return;
180
+ }
181
+ setMailboxes(result);
182
+ setFromMailboxUid((current) => current ?? (result.find((mb) => mb.ownerUserUid === userUid) ?? result[0])?.uid);
183
+ })
184
+ .catch((err) => {
185
+ // Only fatal when there's no mailbox to fall back on - a session that already names one just
186
+ // loses the From picker.
187
+ if (!cancelled && !session.mailboxUid) {
188
+ setFolderError(err instanceof ApiRequestError ? err.message : "Could not load your mailboxes.");
189
+ }
190
+ });
191
+ return () => {
192
+ cancelled = true;
193
+ };
194
+ // eslint-disable-next-line react-hooks/exhaustive-deps
195
+ }, []);
196
+
197
+ useEffect(() => {
198
+ if (!mailboxUid) {
199
+ return;
200
+ }
201
+ let cancelled = false;
202
+ setCryptoContextReady(false);
203
+ let mailboxSettled = false;
204
+ let policySettled = false;
205
+ function checkReady() {
206
+ if (!cancelled && mailboxSettled && policySettled) {
207
+ setCryptoContextReady(true);
208
+ }
209
+ }
210
+ getMailbox(mailboxUid)
211
+ .then((result) => {
212
+ if (!cancelled) {
213
+ setMailbox(result);
214
+ }
215
+ })
216
+ .catch(() => undefined)
217
+ .finally(() => {
218
+ mailboxSettled = true;
219
+ checkReady();
220
+ });
221
+ getEncryptionPolicy()
222
+ .then((result) => {
223
+ if (!cancelled) {
224
+ setEncryptionPolicy(result);
225
+ }
226
+ })
227
+ .catch(() => undefined)
228
+ .finally(() => {
229
+ policySettled = true;
230
+ checkReady();
231
+ });
232
+ return () => {
233
+ cancelled = true;
234
+ };
235
+ }, [mailboxUid]);
236
+
237
+ useEffect(() => {
238
+ if (!mailboxUid) {
239
+ return;
240
+ }
241
+ // Cancellable so a slow response for a mailbox the user has since switched away from can't point
242
+ // the new draft at the wrong mailbox's Drafts folder.
243
+ let cancelled = false;
244
+ listFolders(mailboxUid)
245
+ .then((folders) => {
246
+ if (!cancelled) {
247
+ setDraftsFolderUid(folders.find((f) => f.type === "drafts")?.uid);
248
+ }
249
+ })
250
+ .catch((err) => {
251
+ if (!cancelled) {
252
+ setFolderError(err instanceof ApiRequestError ? err.message : "Could not load your Drafts folder.");
253
+ }
254
+ });
255
+ return () => {
256
+ cancelled = true;
257
+ };
258
+ }, [mailboxUid]);
259
+
260
+ /**
261
+ * Runs discovery for whichever of `addresses` haven't already been looked up this compose session,
262
+ * caching the result so the small per-recipient indicator below the recipient fields can update
263
+ * without waiting for Send. Called from the To/Cc/Bcc fields' own `onBlur` - not on every keystroke,
264
+ * and not the same lookup `assembleForSend()` performs again at send time (deliberately: this is a
265
+ * best-effort UI hint, not something a stale value here should be trusted to skip at send time).
266
+ * A failed lookup or a mailbox/policy fetch that hasn't resolved yet just leaves that address with no
267
+ * indicator rather than surfacing an error over what's a cosmetic nicety.
268
+ */
269
+ function checkRecipientDiscovery(addresses: ComposeRecipientInput[]) {
270
+ if (!mailbox || !encryptionPolicy) {
271
+ return;
272
+ }
273
+ const ownPrefersMutual = mailbox.encryptPreference?.preferEncrypt === "mutual";
274
+ const unchecked = addresses.filter((r) => !(r.address in recipientStatuses));
275
+ for (const recipient of unchecked) {
276
+ lookupKeys(mailbox.uid, recipient.address)
277
+ .catch(() => undefined)
278
+ .then((lookup) => {
279
+ const status = resolveRecipientEncryption(mailbox.primarySmtpAddress, ownPrefersMutual, encryptionPolicy, recipient.address, lookup);
280
+ setRecipientStatuses((prev) => ({ ...prev, [recipient.address]: status }));
281
+ });
282
+ }
283
+ }
284
+
285
+ // Resolves the mailbox's default signature (if any) for `signatureContext` and seeds `html` with it
286
+ // plus any quoted original message, before `RichTextEditor` ever mounts (gated by `contentReady`
287
+ // below) — `RichTextEditor`'s own doc comment is explicit that `value` only seeds its *initial*
288
+ // content and never re-syncs from a later prop change, so this has to resolve before that first
289
+ // mount, not after. A signature-list failure is best-effort, same as every other supplementary,
290
+ // non-blocking fetch in this codebase (e.g. `ConversationThreadPane`'s attachment fetch) — no
291
+ // signature is a completely legitimate outcome, so this falls back to just the quoted content
292
+ // (if any) rather than surfacing an error over what's a cosmetic nicety.
293
+ useEffect(() => {
294
+ // Seeds the body once, from whichever mailbox is the sender when compose opens - switching From
295
+ // later must not overwrite what the user has already written (the editor itself never re-syncs).
296
+ if (!mailboxUid || contentReady) {
297
+ return;
298
+ }
299
+ listMailSignatures(mailboxUid)
300
+ .then((signatures) => {
301
+ const signature = signatures.find((s) =>
302
+ signatureContext === "new" ? s.isDefaultForNewMessages : s.isDefaultForReplyForward,
303
+ );
304
+ const signatureHtml = signature?.contentHtml ? `${signature.contentHtml}<p></p>` : "";
305
+ setHtml(`${signatureHtml}${initialQuotedHtml ?? ""}`);
306
+ })
307
+ .catch(() => setHtml(initialQuotedHtml ?? ""))
308
+ .finally(() => setContentReady(true));
309
+ }, [mailboxUid, signatureContext, initialQuotedHtml]);
310
+
311
+ useEffect(() => {
312
+ if (!mailboxUid || !draftsFolderUid || draft) {
313
+ return;
314
+ }
315
+ let cancelled = false;
316
+ createDraft(mailboxUid, draftsFolderUid)
317
+ .then((created) => {
318
+ if (cancelled) {
319
+ // The sender changed while this was in flight - discard the now-orphaned draft.
320
+ void deleteMessage(created.uid, created.version).catch(() => undefined);
321
+ return;
322
+ }
323
+ setDraft(created);
324
+ })
325
+ .catch((err) => {
326
+ if (!cancelled) {
327
+ setDraftError(err instanceof ApiRequestError ? err.message : "Could not start a new draft.");
328
+ }
329
+ });
330
+ return () => {
331
+ cancelled = true;
332
+ };
333
+ }, [mailboxUid, draftsFolderUid, draft]);
334
+
335
+ /** Switches the sending mailbox: discards the current (unsent, upload-free) draft and resets the
336
+ * per-mailbox state, so the effects above start a fresh draft and crypto context in the new mailbox.
337
+ * Recipients, subject, and body are kept. */
338
+ function handleFromChange(nextMailboxUid: string) {
339
+ if (nextMailboxUid === mailboxUid || hasUploads) {
340
+ return;
341
+ }
342
+ if (draft) {
343
+ void deleteMessage(draft.uid, draft.version).catch(() => undefined);
344
+ }
345
+ setDraft(null);
346
+ setDraftsFolderUid(undefined);
347
+ setDraftError(null);
348
+ setFolderError(null);
349
+ setMailbox(null);
350
+ setRecipientStatuses({});
351
+ setEncryptRequested(false);
352
+ setEncryptionBlocked(null);
353
+ setFromMailboxUid(nextMailboxUid);
354
+ }
355
+
356
+ /**
357
+ * Click-and-drag resize, from the window's own top/left edges (and the top-left corner, for both
358
+ * at once) — the window is anchored bottom-right (see `ComposeContext.tsx`'s stack), so growing it
359
+ * always means extending up and/or left, never down/right. `onMove`/`onUp` are fresh closures
360
+ * created per drag gesture (captured over this gesture's own start position/size) rather than
361
+ * stable component-level functions, specifically so `removeEventListener` in `onUp` always removes
362
+ * the exact listener `onMove`/`onUp` themselves just added — a version defined once per render and
363
+ * reused across gestures would need extra ref-juggling to avoid removing a stale (already-replaced)
364
+ * listener instead of the current one.
365
+ */
366
+ function handleResizeStart(edge: ResizeEdge) {
367
+ return (e: React.PointerEvent) => {
368
+ e.preventDefault();
369
+ const rect = windowRef.current!.getBoundingClientRect();
370
+ const startX = e.clientX;
371
+ const startY = e.clientY;
372
+ const startWidth = rect.width;
373
+ const startHeight = rect.height;
374
+
375
+ function onMove(ev: PointerEvent) {
376
+ const maxWidth = window.innerWidth - VIEWPORT_MARGIN;
377
+ const maxHeight = window.innerHeight - VIEWPORT_MARGIN;
378
+ const width = edge === "top" ? startWidth : Math.min(Math.max(startWidth - (ev.clientX - startX), MIN_WIDTH), maxWidth);
379
+ const height = edge === "left" ? startHeight : Math.min(Math.max(startHeight - (ev.clientY - startY), MIN_HEIGHT), maxHeight);
380
+ setManualSize({ width, height });
381
+ }
382
+ function onUp() {
383
+ window.removeEventListener("pointermove", onMove);
384
+ window.removeEventListener("pointerup", onUp);
385
+ }
386
+ window.addEventListener("pointermove", onMove);
387
+ window.addEventListener("pointerup", onUp);
388
+ };
389
+ }
390
+
391
+ function toggleExpanded() {
392
+ setExpanded((v) => !v);
393
+ setManualSize(null);
394
+ }
395
+
396
+ /** Uploads `file` as an attachment on the current draft, resolving to a URL the editor can preview
397
+ * it at immediately — see `BaseMailComposeRoute.rewriteInlineImageSources()`'s doc comment for how
398
+ * that URL gets swapped for the message's real inline `cid:` reference at send time. Resolves to
399
+ * `null` (surfacing `attachError` itself, reusing the same state "Attach files" already has) rather
400
+ * than throwing: unlike "Attach files" (`disabled={!draft}`), "Insert image" in the formatting
401
+ * toolbar is only gated on the *editor* being mounted, not on the draft existing yet — those two
402
+ * things load in parallel, so a real (if narrow) window exists where the button is clickable before
403
+ * `draft` resolves, and this needs to fail gracefully rather than crash on a null draft.
404
+ */
405
+ async function handleUploadImage(file: File): Promise<string | null> {
406
+ if (!draft) {
407
+ setAttachError("Please wait for the draft to finish loading before inserting an image.");
408
+ return null;
409
+ }
410
+ try {
411
+ const attachment = await uploadAttachment(draft.uid, file);
412
+ setHasUploads(true);
413
+ return attachmentContentUrl(attachment.uid);
414
+ } catch (err) {
415
+ setAttachError(err instanceof ApiRequestError ? err.message : "Could not upload image.");
416
+ return null;
417
+ }
418
+ }
419
+
420
+ // The "Attach files" input is itself `disabled` until `draft` resolves (see its `disabled={!draft}`
421
+ // below), so this can only ever fire once `draft` is set — no defensive null check needed, matching
422
+ // this codebase's established pattern for the same class of "always non-null by the time it's
423
+ // called" value (e.g. `ComposeToolbar.run()`). No separate empty-`files` guard either: the loop
424
+ // below is already a no-op when there's nothing to iterate.
425
+ async function handleFilesSelected(e: ChangeEvent<HTMLInputElement>) {
426
+ const files = Array.from(e.target.files ?? []);
427
+ e.target.value = "";
428
+ setAttachError(null);
429
+ for (const file of files) {
430
+ try {
431
+ const attachment = await uploadAttachment(draft!.uid, file);
432
+ setHasUploads(true);
433
+ setAttachments((prev) => [...prev, attachment]);
434
+ } catch (err) {
435
+ setAttachError(err instanceof ApiRequestError ? err.message : "Could not upload attachment.");
436
+ }
437
+ }
438
+ }
439
+
440
+ /**
441
+ * Builds this draft's final body — plaintext, signed-only, or signed+encrypted per
442
+ * `specs/end-to-end_encryption.md` — then stores it via `assembleDraft()`/`assembleDraftRaw()`.
443
+ * Shared by `handleSend()`/`handleScheduleSend()` (and the "Send without encryption" conflict button
444
+ * rendered below, via `forcePlaintext`) so the sign/encrypt decision lives in exactly one place.
445
+ *
446
+ * Returns `"blocked"` (never throws for this case) when encryption was requested but not every
447
+ * recipient can currently be encrypted to — the spec's "Multiple Recipients" all-or-nothing rule.
448
+ * Sets `encryptionBlocked` as a side effect so the window can render that prompt; callers must stop
449
+ * (not send) when they get this back.
450
+ *
451
+ * Discovery (`lookupKeys()`) and the resulting sign/encrypt decision only run at this, the final
452
+ * pre-send step — not live as recipients are typed. A compose-time recipient-entry indicator (key
453
+ * icons, live discovery as each address is entered) is Phase 4's "Discovery & contacts UI" work, not
454
+ * this pass; this still satisfies the spec's "MUST be called lazily at compose time, not on receipt"
455
+ * requirement, just without a live UI reflecting it before Send is clicked.
456
+ */
457
+ async function assembleForSend(
458
+ toRecipients: ComposeRecipientInput[],
459
+ ccRecipients: ComposeRecipientInput[],
460
+ bccRecipients: ComposeRecipientInput[],
461
+ forcePlaintext: boolean,
462
+ ): Promise<Message | "blocked"> {
463
+ // Only reachable once a draft exists, which itself requires `mailboxUid` to have resolved.
464
+ const unlocked = getUnlockedKeys(mailboxUid!);
465
+ // `mailbox` is required to build protected headers (own From address) whenever signing or
466
+ // encrypting - not just guarded on the encryption branch below, since a signed-only message
467
+ // needs it too. A mailbox fetch failure just means no crypto for this send, never a crash.
468
+ const canSign = signEnabled && !!mailbox && !!unlocked?.signingPrivateKey && !!unlocked.signingCertDer;
469
+ const canEncryptSelf = !!unlocked?.encryptionPrivateKey && !!unlocked.encryptionCertDer;
470
+ const allRecipients = [...toRecipients, ...ccRecipients, ...bccRecipients];
471
+
472
+ let wantEncrypt = false;
473
+ let recipientCertDers: Uint8Array[] = [];
474
+ if (!forcePlaintext && canEncryptSelf && mailbox && encryptionPolicy && allRecipients.length > 0) {
475
+ const ownPrefersMutual = mailbox.encryptPreference?.preferEncrypt === "mutual";
476
+ const lookups = await Promise.all(allRecipients.map((r) => lookupKeys(mailboxUid!, r.address).catch(() => undefined)));
477
+ const statuses = allRecipients.map((r, i) =>
478
+ resolveRecipientEncryption(mailbox.primarySmtpAddress, ownPrefersMutual, encryptionPolicy, r.address, lookups[i]),
479
+ );
480
+ const decision = decideMessageEncryption(statuses);
481
+ wantEncrypt = encryptRequested || decision.autoEncrypt;
482
+ if (wantEncrypt) {
483
+ if (!decision.canEncryptAll) {
484
+ setEncryptionBlocked(decision.blockedRecipients);
485
+ return "blocked";
486
+ }
487
+ recipientCertDers = [unlocked!.encryptionCertDer!, ...statuses.map((s) => fromBase64(s.encryptCert!.publicKey))];
488
+ }
489
+ }
490
+
491
+ if ((wantEncrypt || canSign) && attachments.length > 0) {
492
+ // Matches BaseMailComposeRoute.assembleRaw()'s own server-side rejection, surfaced here with a
493
+ // clearer explanation than that route's generic 400 rather than via a round trip.
494
+ throw new ApiRequestError("A signed or encrypted message cannot include file attachments yet - remove them before sending.", 400);
495
+ }
496
+
497
+ if (!wantEncrypt && !canSign) {
498
+ // See the old compose page's identical note: `sanitize-html` is Node-oriented and the server-side
499
+ // gate in `BaseMailComposeRoute.assemble()` is the sole authoritative sanitizer regardless, so no
500
+ // client-side pass is done here either.
501
+ return assembleDraft(draft!.uid, { to: toRecipients, cc: ccRecipients, bcc: bccRecipients, subject, html });
502
+ }
503
+
504
+ const domain = mailbox!.primarySmtpAddress.split("@")[1] ?? "localhost";
505
+ const protectedHeaders: ProtectedHeaders = {
506
+ from: mailbox!.displayName
507
+ ? `"${mailbox!.displayName.replace(/"/g, '\\"')}" <${mailbox!.primarySmtpAddress}>`
508
+ : mailbox!.primarySmtpAddress,
509
+ to: toRecipients.map((r) => r.address).join(", "),
510
+ cc: ccRecipients.length > 0 ? ccRecipients.map((r) => r.address).join(", ") : undefined,
511
+ date: new Date().toUTCString(),
512
+ subject,
513
+ messageId: `<${crypto.randomUUID()}@${domain}>`,
514
+ };
515
+ const bodyContentType = 'text/html; charset="utf-8"';
516
+ const signing = canSign ? { certDer: unlocked!.signingCertDer!, privateKey: unlocked!.signingPrivateKey! } : undefined;
517
+
518
+ const outerHeaders = wantEncrypt ? applyBaselineOuterHeaders(protectedHeaders) : protectedHeaders;
519
+ const mimePart = wantEncrypt
520
+ ? await buildEncryptedMessage(bodyContentType, html, protectedHeaders, outerHeaders, recipientCertDers, signing)
521
+ : await buildSignedOnlyMessage(bodyContentType, html, protectedHeaders, signing!.certDer, signing!.privateKey);
522
+ const rawMime = assembleOutboundMime(outerHeaders, mimePart);
523
+ return assembleDraftRaw(draft!.uid, { to: toRecipients, cc: ccRecipients, bcc: bccRecipients, subject: outerHeaders.subject, rawMime });
524
+ }
525
+
526
+ // Same reasoning as `handleFilesSelected` above: the Send button is itself `disabled` until `draft`
527
+ // resolves, so this is never reachable with a null `draft`.
528
+ async function handleSend(forcePlaintext = false) {
529
+ const toRecipients = parseAddresses(to);
530
+ if (toRecipients.length === 0) {
531
+ setSendError("At least one recipient is required.");
532
+ return;
533
+ }
534
+
535
+ setSending(true);
536
+ setSendError(null);
537
+ try {
538
+ const assembled = await assembleForSend(toRecipients, parseAddresses(cc), parseAddresses(bcc), forcePlaintext);
539
+ if (assembled === "blocked") {
540
+ return;
541
+ }
542
+ const withReceipt = requestReceipt ? await setMessageRequestReceipt(assembled, true) : assembled;
543
+ await sendMessage(withReceipt.uid);
544
+ onClose();
545
+ } catch (err) {
546
+ setSendError(err instanceof ApiRequestError ? err.message : "Could not send this message.");
547
+ } finally {
548
+ setSending(false);
549
+ }
550
+ }
551
+
552
+ // Same reasoning as `handleSend` above (and the "Send later" caret is itself `disabled` alongside
553
+ // Send until `draft` resolves) — never reachable with a null `draft`. Assembles the draft first
554
+ // (same as an immediate send), then sets `scheduledSendTime` on the *freshly assembled* copy before
555
+ // calling `sendMessage()` — `send()` itself is what reads the field and defers relay into Outbox
556
+ // instead of sending immediately (see `mailApi.ts`'s own doc comment on `setMessageScheduledSendTime`).
557
+ async function handleScheduleSend(scheduledSendTimeIso: string) {
558
+ const toRecipients = parseAddresses(to);
559
+ setSchedulePickerOpen(false);
560
+ if (toRecipients.length === 0) {
561
+ setSendError("At least one recipient is required.");
562
+ return;
563
+ }
564
+
565
+ setSending(true);
566
+ setSendError(null);
567
+ try {
568
+ const assembled = await assembleForSend(toRecipients, parseAddresses(cc), parseAddresses(bcc), false);
569
+ if (assembled === "blocked") {
570
+ return;
571
+ }
572
+ const withReceipt = requestReceipt ? await setMessageRequestReceipt(assembled, true) : assembled;
573
+ const scheduled = await setMessageScheduledSendTime(withReceipt, scheduledSendTimeIso);
574
+ await sendMessage(scheduled.uid);
575
+ onClose();
576
+ } catch (err) {
577
+ setSendError(err instanceof ApiRequestError ? err.message : "Could not schedule this message.");
578
+ } finally {
579
+ setSending(false);
580
+ }
581
+ }
582
+
583
+ async function handleUnlockForCrypto() {
584
+ try {
585
+ // Only reachable via the button below, which never renders unless needsUnlockForCrypto is
586
+ // true - which itself requires mailbox.keys to already contain a real enrolled key (see
587
+ // hasEnrolledSigningKey/hasEnrolledEncryptionKey), so it's never empty/undefined here either.
588
+ await requestUnlock(mailbox!.uid, mailbox!.keys!);
589
+ setUnlockRefresh((n) => n + 1);
590
+ } catch {
591
+ // User dismissed the unlock dialog - nothing to do, the toggles below simply stay hidden.
592
+ }
593
+ }
594
+
595
+ // A plain read from keySession.ts's module-level session store, not React state - see that module's
596
+ // own doc comment. Cheap enough to read fresh on every render rather than caching in state.
597
+ const unlockedKeys = mailboxUid ? getUnlockedKeys(mailboxUid) : undefined;
598
+ const hasEnrolledSigningKey = !!findActivePublicKey(mailbox?.keys ?? [], "sign");
599
+ const hasEnrolledEncryptionKey = !!findActivePublicKey(mailbox?.keys ?? [], "encrypt");
600
+ // This mailbox has a real signing/encryption key on file, but this session hasn't unlocked it yet -
601
+ // the sign/encrypt toggles below stay hidden until it has, so this is the only way to reach them
602
+ // without first stumbling into Mail generally (which no longer force-prompts on its own - see
603
+ // `MailShell`'s `blocking={false}`). `getUnlockedKeys()` is re-read as a plain module value above, so
604
+ // bumping `unlockRefresh` after a successful unlock is what makes this line (and the toggles it
605
+ // gates) reflect it on the next render.
606
+ const needsUnlockForCrypto = (hasEnrolledSigningKey || hasEnrolledEncryptionKey) && !unlockedKeys;
607
+
608
+ const title = subject.trim() || "New Message";
609
+ const titleId = `compose-title-${id}`;
610
+
611
+ if (minimized) {
612
+ return (
613
+ <div role="dialog" aria-label={title} className="w-64 shrink-0 bg-surface border border-border border-b-0 rounded-t-md shadow-modal">
614
+ <div
615
+ className="h-10 flex items-center justify-between gap-2 px-3 rounded-t-md bg-primary-darker text-white cursor-pointer"
616
+ onClick={onToggleMinimize}
617
+ >
618
+ <span className="text-sm font-medium truncate">{title}</span>
619
+ <div className="flex items-center gap-0.5" onClick={(e) => e.stopPropagation()}>
620
+ <HeaderButton label="Restore" onClick={onToggleMinimize} icon={HiOutlineArrowsPointingOut} />
621
+ <HeaderButton
622
+ label="Discard draft"
623
+ onClick={onClose}
624
+ icon={HiOutlineXMark}
625
+ />
626
+ </div>
627
+ </div>
628
+ </div>
629
+ );
630
+ }
631
+
632
+ return (
633
+ <div
634
+ ref={windowRef}
635
+ role="dialog"
636
+ aria-labelledby={titleId}
637
+ style={!isMobile && manualSize ? { width: manualSize.width, height: manualSize.height } : undefined}
638
+ className={[
639
+ "relative shrink-0 flex flex-col bg-surface border border-border shadow-modal overflow-hidden",
640
+ isMobile
641
+ ? "fixed inset-0 w-full h-full rounded-none border-0"
642
+ : ["border-b-0 rounded-t-md", manualSize ? "" : expanded ? "w-[720px] h-[85vh]" : "w-[480px] h-[520px]"].join(" "),
643
+ ].join(" ")}
644
+ >
645
+ {!isMobile && (
646
+ <>
647
+ <div
648
+ onPointerDown={handleResizeStart("top")}
649
+ aria-hidden="true"
650
+ className="absolute top-0 left-0 right-0 h-1.5 cursor-ns-resize z-20"
651
+ />
652
+ <div
653
+ onPointerDown={handleResizeStart("left")}
654
+ aria-hidden="true"
655
+ className="absolute top-0 left-0 bottom-0 w-1.5 cursor-ew-resize z-20"
656
+ />
657
+ <div
658
+ onPointerDown={handleResizeStart("corner")}
659
+ role="separator"
660
+ aria-label="Resize"
661
+ className="absolute top-0 left-0 w-3 h-3 cursor-nwse-resize z-30"
662
+ />
663
+ </>
664
+ )}
665
+
666
+ <div
667
+ className="h-10 shrink-0 flex items-center justify-between gap-2 px-3 bg-primary-darker text-white cursor-pointer"
668
+ onClick={onToggleMinimize}
669
+ >
670
+ <span id={titleId} className="text-sm font-medium truncate">
671
+ {title}
672
+ </span>
673
+ <div className="flex items-center gap-0.5" onClick={(e) => e.stopPropagation()}>
674
+ <HeaderButton label="Minimize" onClick={onToggleMinimize} icon={HiOutlineMinus} />
675
+ {!isMobile && (
676
+ <HeaderButton
677
+ label={expanded ? "Collapse" : "Expand"}
678
+ onClick={toggleExpanded}
679
+ icon={expanded ? HiOutlineArrowsPointingIn : HiOutlineArrowsPointingOut}
680
+ />
681
+ )}
682
+ <HeaderButton label="Close" onClick={onClose} icon={HiOutlineXMark} />
683
+ </div>
684
+ </div>
685
+
686
+ <div className="flex-1 min-h-0 flex flex-col">
687
+ {(folderError || draftError || sendError || attachError) && (
688
+ <div className="px-3 pt-2">
689
+ {folderError && <Alert>{folderError}</Alert>}
690
+ {draftError && <Alert>{draftError}</Alert>}
691
+ {sendError && <Alert>{sendError}</Alert>}
692
+ {attachError && <Alert>{attachError}</Alert>}
693
+ </div>
694
+ )}
695
+
696
+ {encryptionBlocked && (
697
+ <div className="px-3 pt-2">
698
+ <Alert>
699
+ <p className="mb-2">
700
+ This message can&rsquo;t be encrypted for everyone: {encryptionBlocked.map((r) => r.address).join(", ")}
701
+ {encryptionBlocked[0]?.prohibitedReason ? ` — ${encryptionBlocked[0].prohibitedReason}` : " has no encryption key on file"}.
702
+ Remove {encryptionBlocked.length > 1 ? "these recipients" : "this recipient"} from To/Cc/Bcc, or send the
703
+ whole message in plaintext.
704
+ </p>
705
+ <Button
706
+ type="button"
707
+ onClick={() => {
708
+ setEncryptionBlocked(null);
709
+ void handleSend(true);
710
+ }}
711
+ >
712
+ Send without encryption
713
+ </Button>
714
+ </Alert>
715
+ </div>
716
+ )}
717
+
718
+ {mailboxes.length > 1 && (
719
+ <div className={FIELD_ROW}>
720
+ <label htmlFor={`compose-from-${id}`} className="text-xs text-text-muted shrink-0">
721
+ From
722
+ </label>
723
+ <select
724
+ id={`compose-from-${id}`}
725
+ className={`${FIELD_INPUT} disabled:opacity-55`}
726
+ value={mailboxUid ?? ""}
727
+ disabled={hasUploads || sending}
728
+ title={hasUploads ? "The sender can't be changed after adding attachments or images." : undefined}
729
+ onChange={(e) => handleFromChange(e.target.value)}
730
+ >
731
+ {mailboxes.map((mb) => (
732
+ <option key={mb.uid} value={mb.uid}>
733
+ {mb.displayName ? `${mb.displayName} <${mb.primarySmtpAddress}>` : mb.primarySmtpAddress}
734
+ {mb.ownerUserUid ? "" : " (shared)"}
735
+ </option>
736
+ ))}
737
+ </select>
738
+ </div>
739
+ )}
740
+
741
+ <div className={FIELD_ROW}>
742
+ <label htmlFor={`compose-to-${id}`} className="text-xs text-text-muted shrink-0">
743
+ To
744
+ </label>
745
+ <input
746
+ id={`compose-to-${id}`}
747
+ type="text"
748
+ className={FIELD_INPUT}
749
+ value={to}
750
+ onChange={(e) => setTo(e.target.value)}
751
+ onBlur={() => checkRecipientDiscovery(parseAddresses(to))}
752
+ />
753
+ {!showCcBcc && (
754
+ <button
755
+ type="button"
756
+ onClick={() => setShowCcBcc(true)}
757
+ className="text-xs text-text-muted hover:text-text shrink-0"
758
+ >
759
+ Cc Bcc
760
+ </button>
761
+ )}
762
+ </div>
763
+
764
+ {showCcBcc && (
765
+ <>
766
+ <div className={FIELD_ROW}>
767
+ <label htmlFor={`compose-cc-${id}`} className="text-xs text-text-muted shrink-0">
768
+ Cc
769
+ </label>
770
+ <input
771
+ id={`compose-cc-${id}`}
772
+ type="text"
773
+ className={FIELD_INPUT}
774
+ value={cc}
775
+ onChange={(e) => setCc(e.target.value)}
776
+ onBlur={() => checkRecipientDiscovery(parseAddresses(cc))}
777
+ />
778
+ </div>
779
+ <div className={FIELD_ROW}>
780
+ <label htmlFor={`compose-bcc-${id}`} className="text-xs text-text-muted shrink-0">
781
+ Bcc
782
+ </label>
783
+ <input
784
+ id={`compose-bcc-${id}`}
785
+ type="text"
786
+ className={FIELD_INPUT}
787
+ value={bcc}
788
+ onChange={(e) => setBcc(e.target.value)}
789
+ onBlur={() => checkRecipientDiscovery(parseAddresses(bcc))}
790
+ />
791
+ </div>
792
+ </>
793
+ )}
794
+
795
+ {(() => {
796
+ const knownRecipients = [...parseAddresses(to), ...parseAddresses(cc), ...parseAddresses(bcc)]
797
+ .map((r) => recipientStatuses[r.address])
798
+ .filter((status): status is RecipientEncryptionStatus => !!status);
799
+ if (knownRecipients.length === 0) {
800
+ return null;
801
+ }
802
+ return (
803
+ <ul className="flex flex-wrap gap-1.5 px-3 pt-1.5" aria-label="Recipient encryption availability">
804
+ {knownRecipients.map((status) => (
805
+ <li
806
+ key={status.address}
807
+ className={`text-xs font-medium py-0.5 px-2 rounded-pill ${
808
+ status.canEncrypt ? "bg-primary/10 text-primary-dark" : "bg-surface-alt text-text-muted"
809
+ }`}
810
+ >
811
+ {status.address} {status.canEncrypt ? "supports encryption" : "no encryption key found"}
812
+ </li>
813
+ ))}
814
+ </ul>
815
+ );
816
+ })()}
817
+
818
+ <div className={FIELD_ROW}>
819
+ <input
820
+ aria-label="Subject"
821
+ type="text"
822
+ placeholder="Subject"
823
+ className={FIELD_INPUT}
824
+ value={subject}
825
+ onChange={(e) => setSubject(e.target.value)}
826
+ />
827
+ </div>
828
+
829
+ <div className="flex-1 min-h-0 p-2">
830
+ {contentReady && <RichTextEditor value={html} onChange={setHtml} fill onUploadImage={handleUploadImage} />}
831
+ </div>
832
+
833
+ {attachments.length > 0 && (
834
+ <ul className="flex flex-wrap gap-2 px-3 pb-2">
835
+ {attachments.map((attachment) => (
836
+ <li key={attachment.uid} className="text-xs font-medium py-1 px-2.5 rounded-pill bg-surface-alt text-text-muted">
837
+ {attachment.filename}
838
+ </li>
839
+ ))}
840
+ </ul>
841
+ )}
842
+
843
+ <label className="flex items-center gap-1.5 px-3 pb-1 text-xs text-text-muted">
844
+ <input type="checkbox" checked={requestReceipt} onChange={(e) => setRequestReceipt(e.target.checked)} />
845
+ Request a read receipt
846
+ </label>
847
+
848
+ {needsUnlockForCrypto && (
849
+ <div className="px-3 pb-1">
850
+ <button
851
+ type="button"
852
+ onClick={handleUnlockForCrypto}
853
+ className="inline-flex items-center gap-1 text-xs font-medium text-primary-dark hover:underline"
854
+ >
855
+ <HiOutlineLockClosed size={12} aria-hidden="true" />
856
+ Unlock to sign or encrypt this message
857
+ </button>
858
+ </div>
859
+ )}
860
+
861
+ {/* Only rendered once this session actually has a usable signing/encryption key - see
862
+ `assembleForSend()`'s own doc comment for why signing rarely shows today (no signing
863
+ certificate exists until RFC 8823 ACME enrollment lands). */}
864
+ {unlockedKeys?.signingPrivateKey && (
865
+ <label className="flex items-center gap-1.5 px-3 pb-1 text-xs text-text-muted">
866
+ <input type="checkbox" checked={signEnabled} onChange={(e) => setSignEnabled(e.target.checked)} />
867
+ Digitally sign this message
868
+ </label>
869
+ )}
870
+ {unlockedKeys?.encryptionPrivateKey && (
871
+ <label className="flex items-center gap-1.5 px-3 pb-1 text-xs text-text-muted">
872
+ <input type="checkbox" checked={encryptRequested} onChange={(e) => setEncryptRequested(e.target.checked)} />
873
+ Encrypt this message
874
+ </label>
875
+ )}
876
+
877
+ <div className="shrink-0 flex items-center gap-1 px-3 py-2 border-t border-border">
878
+ <div className="flex items-center rounded-pill bg-primary text-white overflow-hidden">
879
+ <button
880
+ type="button"
881
+ onClick={() => handleSend()}
882
+ disabled={!draft || sending || !cryptoContextReady}
883
+ className="py-1.5 pl-5 pr-3 font-semibold text-sm hover:not-disabled:bg-primary-dark disabled:opacity-55 disabled:cursor-not-allowed"
884
+ >
885
+ {sending ? "Sending…" : "Send"}
886
+ </button>
887
+ <button
888
+ ref={scheduleButtonRef}
889
+ type="button"
890
+ aria-label="Send later"
891
+ aria-haspopup="true"
892
+ aria-expanded={schedulePickerOpen}
893
+ disabled={!draft || sending || !cryptoContextReady}
894
+ onClick={() => setSchedulePickerOpen((o) => !o)}
895
+ className="py-1.5 px-2 border-l border-white/30 hover:not-disabled:bg-primary-dark disabled:opacity-55 disabled:cursor-not-allowed"
896
+ >
897
+ <HiOutlineChevronDown size={14} />
898
+ </button>
899
+ </div>
900
+ {schedulePickerOpen && (
901
+ <ScheduleSendPicker
902
+ anchorRef={scheduleButtonRef}
903
+ onClose={() => setSchedulePickerOpen(false)}
904
+ onSchedule={handleScheduleSend}
905
+ />
906
+ )}
907
+
908
+ <label
909
+ aria-label="Attach files"
910
+ title="Attach files"
911
+ className="w-8 h-8 flex items-center justify-center rounded-full text-text-muted hover:bg-surface-alt hover:text-text cursor-pointer has-[:disabled]:opacity-40 has-[:disabled]:cursor-not-allowed"
912
+ >
913
+ <HiOutlinePaperClip size={18} />
914
+ <input type="file" multiple disabled={!draft} onChange={handleFilesSelected} className="sr-only" />
915
+ </label>
916
+
917
+ <button
918
+ type="button"
919
+ aria-label="Discard draft"
920
+ title="Discard draft"
921
+ onClick={onClose}
922
+ className="ml-auto w-8 h-8 flex items-center justify-center rounded-full text-text-muted hover:bg-surface-alt hover:text-text"
923
+ >
924
+ <HiOutlineTrash size={18} />
925
+ </button>
926
+ </div>
927
+ </div>
928
+ </div>
929
+ );
930
+ }