@open-workspace/email 0.2.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 (223) hide show
  1. package/README.md +23 -20
  2. package/alchemy/index.ts +273 -0
  3. package/drizzle/mailbox/0000_puzzling_dracula.sql +149 -0
  4. package/drizzle/mailbox/0001_mute_jack_flag.sql +12 -0
  5. package/drizzle/mailbox/meta/0000_snapshot.json +1002 -0
  6. package/drizzle/mailbox/meta/0001_snapshot.json +1002 -0
  7. package/drizzle/mailbox/meta/_journal.json +20 -0
  8. package/drizzle/mailbox/migrations.js +12 -0
  9. package/index.ts +3 -1
  10. package/migrations/0000_dry_moon_knight.sql +77 -0
  11. package/migrations/meta/0000_snapshot.json +248 -106
  12. package/migrations/meta/_journal.json +2 -2
  13. package/package.json +74 -68
  14. package/shared/attachment-preview.ts +37 -27
  15. package/shared/folder-order.ts +106 -0
  16. package/shared/mail-authentication.ts +8 -3
  17. package/shared/mail-feedback.ts +18 -25
  18. package/shared/mail-headers.ts +114 -39
  19. package/shared/mail-import.ts +38 -0
  20. package/shared/mail-path.ts +42 -0
  21. package/shared/mail-received.ts +174 -31
  22. package/shared/mail-remote.ts +59 -2
  23. package/shared/mail-sender-intel.ts +22 -4
  24. package/shared/mail.ts +78 -26
  25. package/shared/module.ts +33 -0
  26. package/shared/office-preview.ts +14 -2
  27. package/shared/pdfjs-assets.ts +35 -0
  28. package/shared/permissions.ts +73 -0
  29. package/shared/webhooks.ts +41 -0
  30. package/src/admin/blocked-senders.tsx +189 -0
  31. package/src/client.tsx +97 -0
  32. package/src/lib/mail-api.ts +9 -0
  33. package/src/mail/attachment-preview-dialog.tsx +161 -0
  34. package/src/mail/blocked-senders.tsx +138 -0
  35. package/src/mail/compose-window-chrome.tsx +194 -0
  36. package/src/mail/compose-window.tsx +486 -0
  37. package/src/mail/composer-asset-bookmarks.ts +409 -0
  38. package/src/mail/composer-asset.tsx +229 -0
  39. package/src/mail/composer-attachment-list.tsx +138 -0
  40. package/src/mail/composer-editor-document.ts +65 -0
  41. package/src/mail/composer-editor.tsx +284 -0
  42. package/src/mail/composer-linked-attachment-text.ts +12 -0
  43. package/src/mail/composer-recipient-fields.tsx +165 -0
  44. package/src/mail/composer-recipients.ts +208 -0
  45. package/src/mail/composer-session.ts +562 -0
  46. package/src/mail/composer-toolbar.tsx +303 -0
  47. package/src/mail/composer-upload-client.ts +105 -0
  48. package/src/mail/composer-upload-progress.ts +53 -0
  49. package/src/mail/conversation-list.tsx +550 -0
  50. package/src/mail/conversation-loading-skeleton.tsx +119 -0
  51. package/src/mail/conversation-message.tsx +346 -0
  52. package/src/mail/conversation-reply.tsx +470 -0
  53. package/src/mail/conversation-selection-toolbar.tsx +322 -0
  54. package/src/mail/conversation-view.tsx +437 -0
  55. package/src/mail/delivery-indicator.tsx +272 -0
  56. package/src/mail/email-html-body.tsx +203 -0
  57. package/src/mail/folder-glyph.tsx +46 -0
  58. package/src/mail/folder-manager.tsx +532 -0
  59. package/src/mail/format-bytes.ts +7 -0
  60. package/src/mail/mail-app-bar.tsx +253 -0
  61. package/src/mail/mail-background.tsx +12 -0
  62. package/src/mail/mail-dock.tsx +137 -0
  63. package/src/mail/mail-folders.ts +44 -0
  64. package/src/mail/mail-layout.css +236 -0
  65. package/src/mail/mail-location.ts +73 -0
  66. package/src/mail/mail-mailbox-sheet.tsx +275 -0
  67. package/src/mail/mail-nav-rail.tsx +129 -0
  68. package/src/mail/mail-nav.tsx +324 -0
  69. package/src/mail/mail-prefetch.ts +59 -0
  70. package/src/mail/mail-primitives.tsx +66 -0
  71. package/src/mail/mail-queries.ts +209 -0
  72. package/src/mail/mail-query-cache.ts +486 -0
  73. package/src/mail/mail-seed.ts +47 -0
  74. package/src/mail/mail-shell.tsx +126 -0
  75. package/src/mail/mailbox-ai-settings.tsx +185 -0
  76. package/src/mail/mailbox-realtime.tsx +251 -0
  77. package/src/mail/mailbox-route.tsx +15 -0
  78. package/src/mail/message-actions.tsx +353 -0
  79. package/src/mail/message-attachments.tsx +200 -0
  80. package/src/mail/message-format.ts +75 -0
  81. package/src/mail/message-header.tsx +260 -0
  82. package/src/mail/message-translation.tsx +225 -0
  83. package/src/mail/original-message-sheet.tsx +619 -0
  84. package/src/mail/original-message.ts +191 -0
  85. package/src/mail/outbound-notification.ts +53 -0
  86. package/src/mail/pane-splitter.tsx +117 -0
  87. package/src/mail/pdf-preview-toolbar.tsx +152 -0
  88. package/src/mail/pdf-preview.css +44 -0
  89. package/src/mail/pdf-preview.tsx +156 -0
  90. package/src/mail/pdf-translation-layout.ts +95 -0
  91. package/src/mail/print-message.ts +92 -0
  92. package/src/mail/reader-pane.tsx +535 -0
  93. package/src/mail/recipient-combobox.tsx +335 -0
  94. package/src/mail/recipient-field-row.tsx +34 -0
  95. package/src/mail/reply-to-warning.tsx +50 -0
  96. package/src/mail/sanitize-email-html.ts +430 -0
  97. package/src/mail/sender-actions.tsx +149 -0
  98. package/src/mail/sender-authentication.tsx +485 -0
  99. package/src/mail/sender-dialogs.tsx +384 -0
  100. package/src/mail/sender-security.tsx +545 -0
  101. package/src/mail/toolbar-tooltip.tsx +29 -0
  102. package/src/mail/translation-html.ts +49 -0
  103. package/src/mail/translation-language.ts +21 -0
  104. package/src/mail/types.ts +53 -0
  105. package/src/mail/unread-document-indicator.tsx +60 -0
  106. package/src/mail/use-compose-submission.ts +208 -0
  107. package/src/mail/use-composer-attachment-preflight.ts +184 -0
  108. package/src/mail/use-composer-submission.ts +117 -0
  109. package/src/mail/use-conversation-selection.ts +186 -0
  110. package/src/mail/use-mail-controller.ts +427 -0
  111. package/src/mail/use-mail-data.ts +279 -0
  112. package/src/mail/use-mail-safety.ts +155 -0
  113. package/src/mail/use-mail-send.ts +142 -0
  114. package/src/mail/use-pdf-translation-overlay.ts +156 -0
  115. package/src/mail/use-pdf-translation.ts +100 -0
  116. package/src/mail/use-pdf-viewer.ts +215 -0
  117. package/src/mail/use-reply-submission.ts +114 -0
  118. package/src/mail/use-sanitized-email-html.ts +20 -0
  119. package/src/mail/use-visible-message-read.ts +138 -0
  120. package/src/pwa/push-subscription.ts +151 -0
  121. package/src/settings/mail-apps-page.tsx +235 -0
  122. package/src/settings/notifications-page.tsx +291 -0
  123. package/worker/admin/blocked-sender-routes.ts +166 -0
  124. package/worker/admin/schemas.ts +11 -0
  125. package/worker/bindings.d.ts +14 -10
  126. package/worker/db/schema.ts +90 -34
  127. package/worker/jmap/eventsource.ts +169 -0
  128. package/worker/jmap/methods.ts +1716 -361
  129. package/worker/jmap/push-types.ts +27 -0
  130. package/worker/jmap/push.ts +207 -0
  131. package/worker/jmap/routes.ts +448 -86
  132. package/worker/mail/access.ts +258 -21
  133. package/worker/mail/ai-classification.ts +36 -20
  134. package/worker/mail/ai-translation.ts +13 -6
  135. package/worker/mail/auth-headers.ts +395 -137
  136. package/worker/mail/bimi.ts +200 -73
  137. package/worker/mail/bootstrap.ts +79 -0
  138. package/worker/mail/capped-body.ts +36 -0
  139. package/worker/mail/classification-signals.ts +35 -25
  140. package/worker/mail/conversation-cursor.ts +30 -19
  141. package/worker/mail/delivery-events.ts +88 -62
  142. package/worker/mail/delivery-facts.ts +46 -10
  143. package/worker/mail/demo-routes.ts +34 -10
  144. package/worker/mail/doh.ts +62 -23
  145. package/worker/mail/downloads.ts +75 -40
  146. package/worker/mail/edge-cache.ts +155 -0
  147. package/worker/mail/header-metadata.ts +84 -52
  148. package/worker/mail/impersonation.ts +160 -38
  149. package/worker/mail/import-routes.ts +313 -0
  150. package/worker/mail/inbound-delivery.ts +19 -9
  151. package/worker/mail/inbound-threading.ts +18 -8
  152. package/worker/mail/inbound.ts +82 -40
  153. package/worker/mail/ip-intel.ts +110 -27
  154. package/worker/mail/isolate-cache.ts +35 -0
  155. package/worker/mail/link-domains.ts +29 -7
  156. package/worker/mail/mailbox-deletion.ts +10 -4
  157. package/worker/mail/mailbox-directory.ts +16 -6
  158. package/worker/mail/mailing-list.ts +94 -39
  159. package/worker/mail/message-security.ts +90 -15
  160. package/worker/mail/message-view.ts +18 -6
  161. package/worker/mail/office-conversion.ts +31 -9
  162. package/worker/mail/office-preview-container.ts +40 -13
  163. package/worker/mail/office-preview-queue.ts +76 -32
  164. package/worker/mail/org-domain.ts +8 -1
  165. package/worker/mail/outbound-content.ts +71 -26
  166. package/worker/mail/outbound-delivery.ts +31 -17
  167. package/worker/mail/outbound-service.ts +48 -30
  168. package/worker/mail/outbound-threading.ts +30 -17
  169. package/worker/mail/outbound.ts +97 -70
  170. package/worker/mail/outgoing-context.ts +25 -15
  171. package/worker/mail/participants.ts +44 -29
  172. package/worker/mail/push-notifications.ts +283 -171
  173. package/worker/mail/quote-parser.ts +21 -10
  174. package/worker/mail/r2-presigned-urls.ts +31 -25
  175. package/worker/mail/receive.ts +23 -11
  176. package/worker/mail/recipients.ts +6 -1
  177. package/worker/mail/remote-proxy.ts +187 -31
  178. package/worker/mail/remote-routes.ts +87 -0
  179. package/worker/mail/remote-token.ts +104 -0
  180. package/worker/mail/repeat.ts +19 -0
  181. package/worker/mail/reply-plan.ts +62 -33
  182. package/worker/mail/rfc.ts +32 -7
  183. package/worker/mail/routes.ts +871 -542
  184. package/worker/mail/safety-routes.ts +329 -202
  185. package/worker/mail/schemas.ts +84 -105
  186. package/worker/mail/search.ts +1 -0
  187. package/worker/mail/security-settings.ts +41 -9
  188. package/worker/mail/sender-intel.ts +206 -51
  189. package/worker/mail/spamhaus.ts +92 -45
  190. package/worker/mail/unsubscribe.ts +76 -18
  191. package/worker/mail/uploads.ts +153 -91
  192. package/worker/mail/webhook-source.ts +35 -12
  193. package/worker/mail/workspace-safety.ts +97 -38
  194. package/worker/mailbox/demo-html.ts +7 -7
  195. package/worker/mailbox/demo-seed.ts +10 -44
  196. package/worker/mailbox/demo.ts +597 -227
  197. package/worker/mailbox/email-index.ts +53 -0
  198. package/worker/mailbox/folder-model.ts +53 -17
  199. package/worker/mailbox/import-placement.ts +88 -0
  200. package/worker/mailbox/inbox-counts.ts +106 -0
  201. package/worker/mailbox/index.ts +1761 -737
  202. package/worker/mailbox/jmap-store.ts +139 -35
  203. package/worker/mailbox/model.ts +7 -14
  204. package/worker/mailbox/office-preview-jobs.ts +56 -25
  205. package/worker/mailbox/schema.ts +87 -66
  206. package/worker/mailbox/senders.ts +176 -70
  207. package/worker/mcp/mail-action-tools.ts +128 -87
  208. package/worker/mcp/mail-compose-tools.ts +133 -82
  209. package/worker/mcp/mail-query-tools.ts +120 -71
  210. package/worker/mcp/mail-safety-tools.ts +321 -248
  211. package/worker/mcp/schemas.ts +43 -0
  212. package/worker/mcp/tools.ts +13 -10
  213. package/worker/module.ts +43 -18
  214. package/worker/sql.d.ts +8 -0
  215. package/alchemy.ts +0 -126
  216. package/containers/office-preview/Dockerfile +0 -10
  217. package/migrations/0000_email.sql +0 -53
  218. package/shared/mail-rules.ts +0 -156
  219. package/worker/admin/report-routes.ts +0 -164
  220. package/worker/mail/reported-messages.ts +0 -68
  221. package/worker/mailbox/legacy-migrations.ts +0 -41
  222. package/worker/mailbox/migrations.ts +0 -10
  223. package/worker/mailbox/rules.ts +0 -110
@@ -0,0 +1,486 @@
1
+ import {
2
+ useEffect,
3
+ useEffectEvent,
4
+ useMemo,
5
+ useRef,
6
+ useState,
7
+ useSyncExternalStore,
8
+ } from "react";
9
+ import type { DragEvent, RefObject } from "react";
10
+ import { toast } from "sonner";
11
+
12
+ import { forwardSubject } from "../../shared/mail";
13
+ import {
14
+ ComposeWindowFooter,
15
+ ComposeWindowHeader,
16
+ ForwardedMessageNotice,
17
+ MinimizedComposer,
18
+ } from "./compose-window-chrome";
19
+ import { ComposerAttachmentList } from "./composer-attachment-list";
20
+ import { ComposerEditor } from "./composer-editor";
21
+ import type { ComposerContent, ComposerEditorHandle } from "./composer-editor";
22
+ import { ComposerRecipientFields } from "./composer-recipient-fields";
23
+ import { validateComposerRecipients } from "./composer-recipients";
24
+ import type { RecipientFieldValue } from "./composer-recipients";
25
+ import { useComposerSession } from "./composer-session";
26
+ import { isInlineComposerImage } from "./composer-upload-client";
27
+ import { ReplyToWarning } from "./reply-to-warning";
28
+ import type { Mailbox, MessageDetail } from "./types";
29
+ import { useComposeSubmission } from "./use-compose-submission";
30
+ import { useComposerAttachmentPreflight } from "./use-composer-attachment-preflight";
31
+
32
+ const EMPTY_RECIPIENT_FIELD: RecipientFieldValue = {
33
+ recipients: [],
34
+ input: "",
35
+ };
36
+
37
+ const EMPTY_CONTENT: ComposerContent = {
38
+ bodyHtml: "",
39
+ bodyText: "",
40
+ inlineAssetIds: [],
41
+ linkedAssetIds: [],
42
+ };
43
+
44
+ /** Below Tailwind's `sm`, the composer covers the screen. */
45
+ const PHONE_QUERY = "(max-width: 639.98px)";
46
+
47
+ function usePhoneLayout() {
48
+ return useSyncExternalStore(
49
+ (onChange) => {
50
+ const media = matchMedia(PHONE_QUERY);
51
+ media.addEventListener("change", onChange);
52
+
53
+ return () => media.removeEventListener("change", onChange);
54
+ },
55
+ () => matchMedia(PHONE_QUERY).matches,
56
+ () => false
57
+ );
58
+ }
59
+
60
+ /**
61
+ * Makes everything outside `element` inert: each sibling of the element and
62
+ * of its ancestors up to <body>. Returns the undo. Elements that were inert
63
+ * already are left alone.
64
+ */
65
+ function inertOutside(element: HTMLElement) {
66
+ const changed: HTMLElement[] = [];
67
+
68
+ for (
69
+ let node: HTMLElement | null = element;
70
+ node && node !== document.body;
71
+ node = node.parentElement
72
+ ) {
73
+ for (const sibling of node.parentElement?.children ?? []) {
74
+ if (
75
+ sibling === node ||
76
+ !(sibling instanceof HTMLElement) ||
77
+ sibling.inert
78
+ ) {
79
+ continue;
80
+ }
81
+
82
+ if (
83
+ sibling instanceof HTMLScriptElement ||
84
+ sibling instanceof HTMLStyleElement
85
+ ) {
86
+ continue;
87
+ }
88
+
89
+ sibling.inert = true;
90
+ changed.push(sibling);
91
+ }
92
+ }
93
+
94
+ return () => {
95
+ for (const sibling of changed) sibling.inert = false;
96
+ };
97
+ }
98
+
99
+ /**
100
+ * On a phone the composer is a full-screen modal: the page behind is inert,
101
+ * focus starts inside, Escape closes it (unless a list or popup inside is
102
+ * open and takes the key), and focus goes back to what opened it. The
103
+ * floating desktop window stays non-modal.
104
+ */
105
+ function useFullScreenComposer(
106
+ section: RefObject<HTMLElement | null>,
107
+ active: boolean,
108
+ onEscape: () => void
109
+ ) {
110
+ const escape = useEffectEvent(onEscape);
111
+ // What had focus before the composer opened, to go back to when it closes.
112
+ const opener = useRef(document.activeElement);
113
+ useEffect(() => {
114
+ const element = section.current;
115
+
116
+ if (!active || !element) return;
117
+ const undo = inertOutside(element);
118
+
119
+ if (!element.contains(document.activeElement)) element.focus();
120
+
121
+ const onKeyDown = (event: KeyboardEvent) => {
122
+ if (event.key !== "Escape" || event.defaultPrevented) return;
123
+
124
+ if (
125
+ event.target instanceof Element &&
126
+ event.target.closest('[aria-expanded="true"]')
127
+ ) {
128
+ return;
129
+ }
130
+
131
+ event.preventDefault();
132
+ escape();
133
+ };
134
+
135
+ element.addEventListener("keydown", onKeyDown);
136
+
137
+ return () => {
138
+ element.removeEventListener("keydown", onKeyDown);
139
+ undo();
140
+ // Once the composer is gone, focus would fall to <body>.
141
+ const { current: returnTo } = opener;
142
+
143
+ requestAnimationFrame(() => {
144
+ const current = document.activeElement;
145
+
146
+ if (
147
+ (!current || current === document.body) &&
148
+ returnTo instanceof HTMLElement &&
149
+ returnTo.isConnected
150
+ ) {
151
+ returnTo.focus();
152
+ }
153
+ });
154
+ };
155
+ }, [section, active]);
156
+ }
157
+
158
+ export function ComposeWindow({
159
+ mailbox,
160
+ forwardedMessage,
161
+ onClose,
162
+ }: {
163
+ mailbox: Mailbox;
164
+ forwardedMessage?: Pick<
165
+ MessageDetail,
166
+ | "id"
167
+ | "subject"
168
+ | "fromAddress"
169
+ | "fromName"
170
+ | "preview"
171
+ | "attachments"
172
+ | "security"
173
+ >;
174
+ onClose: () => void;
175
+ }) {
176
+ const form = useRef<HTMLFormElement>(null);
177
+ const editor = useRef<ComposerEditorHandle>(null);
178
+ const fileInput = useRef<HTMLInputElement>(null);
179
+ const inlineFileInput = useRef<HTMLInputElement>(null);
180
+ const [to, setTo] = useState<RecipientFieldValue>(EMPTY_RECIPIENT_FIELD);
181
+ const [cc, setCc] = useState<RecipientFieldValue>(EMPTY_RECIPIENT_FIELD);
182
+ const [bcc, setBcc] = useState<RecipientFieldValue>(EMPTY_RECIPIENT_FIELD);
183
+ const [replyTo, setReplyTo] = useState("");
184
+
185
+ const [subject, setSubject] = useState(() =>
186
+ forwardedMessage ? forwardSubject(forwardedMessage.subject) : ""
187
+ );
188
+
189
+ const [content, setContent] = useState<ComposerContent>(EMPTY_CONTENT);
190
+ const [documentAssetIds, setDocumentAssetIds] = useState<string[]>([]);
191
+ const [minimized, setMinimized] = useState(false);
192
+ const [dragging, setDragging] = useState(false);
193
+ const section = useRef<HTMLElement>(null);
194
+ const minimizeButton = useRef<HTMLButtonElement>(null);
195
+ const restoreButton = useRef<HTMLButtonElement>(null);
196
+ // Minimizing moves focus to Restore, and restoring back to Minimize.
197
+ const focusAfterToggle = useRef(false);
198
+ const phone = usePhoneLayout();
199
+
200
+ const { session, snapshot } = useComposerSession(
201
+ mailbox.id,
202
+ forwardedMessage?.attachments
203
+ );
204
+
205
+ const recipients = validateComposerRecipients({
206
+ to,
207
+ cc,
208
+ bcc,
209
+ replyTo,
210
+ });
211
+
212
+ const recipientCount = recipients.count;
213
+
214
+ const documentAssetIdSet = useMemo(
215
+ () => new Set(documentAssetIds),
216
+ [documentAssetIds]
217
+ );
218
+
219
+ const activeAssets = useMemo(
220
+ () => snapshot.assets.filter((asset) => documentAssetIdSet.has(asset.id)),
221
+ [documentAssetIdSet, snapshot.assets]
222
+ );
223
+
224
+ useComposerAttachmentPreflight(
225
+ session,
226
+ forwardedMessage
227
+ ? {
228
+ kind: "forward",
229
+ mailboxId: mailbox.id,
230
+ sourceEmailId: forwardedMessage.id,
231
+ bodyText: content.bodyText,
232
+ bodyHtml: content.bodyHtml || undefined,
233
+ }
234
+ : {
235
+ kind: "compose",
236
+ mailboxId: mailbox.id,
237
+ subject,
238
+ bodyText: content.bodyText,
239
+ bodyHtml: content.bodyHtml || undefined,
240
+ },
241
+ activeAssets,
242
+ snapshot.phase === "editing"
243
+ );
244
+ const linkedIds = snapshot.linkedAssetIds;
245
+ const attachmentError = session.limitError();
246
+
247
+ const uploadsPending = activeAssets.some(
248
+ (asset) => asset.status === "uploading"
249
+ );
250
+
251
+ const uploadsFailed = activeAssets.some((asset) => asset.status === "error");
252
+
253
+ const submission = useComposeSubmission({
254
+ session,
255
+ phase: snapshot.phase,
256
+ mailbox,
257
+ forwardedMessage,
258
+ recipients,
259
+ subject,
260
+ content,
261
+ editor,
262
+ activeAssets,
263
+ uploadLimitError: attachmentError,
264
+ uploadsPending,
265
+ uploadsFailed,
266
+ onClose,
267
+ });
268
+
269
+ const { busy } = submission;
270
+ // Sending takes over the window: no drop target while it is busy.
271
+ const dropTarget = dragging && !busy;
272
+
273
+ function addInlineFiles(files: File[], position?: number) {
274
+ const assets = session.addFiles(files, "inline");
275
+
276
+ if (!assets) return;
277
+ editor.current?.insertAssets(assets, position);
278
+ }
279
+
280
+ function addAttachmentFiles(files: File[], position?: number) {
281
+ const assets = session.addFiles(files, "attachment");
282
+
283
+ if (!assets) return;
284
+ editor.current?.insertAssets(assets, position);
285
+ }
286
+
287
+ function handleComposerDrop(event: DragEvent<HTMLFormElement>) {
288
+ if (!event.dataTransfer.types.includes("Files")) return;
289
+ event.preventDefault();
290
+ setDragging(false);
291
+
292
+ if (busy) return;
293
+
294
+ // ProseMirror owns editor-body drops so it can preserve the insertion
295
+ // position. The form catches files dropped on every other composer area.
296
+ if (
297
+ event.target instanceof Element &&
298
+ event.target.closest("[data-composer-editor-content]")
299
+ ) {
300
+ return;
301
+ }
302
+
303
+ const inline: File[] = [];
304
+ const attachments: File[] = [];
305
+
306
+ for (const file of Array.from(event.dataTransfer.files)) {
307
+ (isInlineComposerImage(file) ? inline : attachments).push(file);
308
+ }
309
+
310
+ if (inline.length) addInlineFiles(inline);
311
+
312
+ if (attachments.length) addAttachmentFiles(attachments);
313
+ }
314
+
315
+ function closeComposer() {
316
+ if (busy) {
317
+ toast.error("Wait for the message to finish sending");
318
+
319
+ return;
320
+ }
321
+
322
+ session.close();
323
+ onClose();
324
+ }
325
+
326
+ function toggleMinimized(next: boolean) {
327
+ focusAfterToggle.current = true;
328
+ setMinimized(next);
329
+ }
330
+
331
+ useEffect(() => {
332
+ if (!focusAfterToggle.current) return;
333
+ focusAfterToggle.current = false;
334
+ (minimized ? restoreButton : minimizeButton).current?.focus();
335
+ }, [minimized]);
336
+
337
+ useFullScreenComposer(section, phone && !minimized, closeComposer);
338
+
339
+ if (minimized) {
340
+ return (
341
+ <MinimizedComposer
342
+ subject={subject}
343
+ restoreRef={restoreButton}
344
+ onRestore={() => toggleMinimized(false)}
345
+ onClose={closeComposer}
346
+ />
347
+ );
348
+ }
349
+
350
+ return (
351
+ <section
352
+ ref={section}
353
+ role="dialog"
354
+ aria-label="New message"
355
+ aria-modal={phone || undefined}
356
+ tabIndex={-1}
357
+ className="animate-rise bg-surface ring-border fixed inset-0 z-50 flex flex-col shadow-2xl ring-1 outline-none sm:inset-auto sm:right-6 sm:bottom-6 sm:h-[min(600px,calc(100dvh-3rem))] sm:w-[min(560px,calc(100vw-2.5rem))] sm:rounded-xl"
358
+ >
359
+ {/* oxlint-disable-next-line jsx-a11y/no-noninteractive-element-interactions -- files can be dropped anywhere on the form */}
360
+ <form
361
+ ref={form}
362
+ className="flex min-h-0 flex-1 flex-col"
363
+ aria-busy={busy}
364
+ onSubmit={submission.submit}
365
+ onDragEnter={(event) => {
366
+ if (!event.dataTransfer.types.includes("Files")) return;
367
+ event.preventDefault();
368
+
369
+ if (!busy) setDragging(true);
370
+ }}
371
+ onDragOver={(event) => {
372
+ if (!event.dataTransfer.types.includes("Files")) return;
373
+ event.preventDefault();
374
+ event.dataTransfer.dropEffect = busy ? "none" : "copy";
375
+ }}
376
+ onDragLeave={(event) => {
377
+ if (
378
+ !(
379
+ event.relatedTarget instanceof Node &&
380
+ event.currentTarget.contains(event.relatedTarget)
381
+ )
382
+ ) {
383
+ setDragging(false);
384
+ }
385
+ }}
386
+ onDragEnd={() => setDragging(false)}
387
+ onDrop={handleComposerDrop}
388
+ >
389
+ <ComposeWindowHeader
390
+ from={mailbox.address}
391
+ minimizeRef={minimizeButton}
392
+ onMinimize={() => toggleMinimized(true)}
393
+ onClose={closeComposer}
394
+ />
395
+
396
+ <ComposerRecipientFields
397
+ mailboxId={mailbox.id}
398
+ to={to}
399
+ cc={cc}
400
+ bcc={bcc}
401
+ replyTo={replyTo}
402
+ subject={subject}
403
+ subjectReadOnly={Boolean(forwardedMessage)}
404
+ disabled={busy}
405
+ onToChange={setTo}
406
+ onCcChange={setCc}
407
+ onBccChange={setBcc}
408
+ onReplyToChange={setReplyTo}
409
+ onSubjectChange={setSubject}
410
+ />
411
+
412
+ {forwardedMessage ? (
413
+ <>
414
+ <ForwardedMessageNotice message={forwardedMessage} />
415
+ <ReplyToWarning
416
+ message={forwardedMessage}
417
+ recipients={[
418
+ ...recipients.to,
419
+ ...recipients.cc,
420
+ ...recipients.bcc,
421
+ ]}
422
+ />
423
+ </>
424
+ ) : null}
425
+
426
+ <ComposerEditor
427
+ ref={editor}
428
+ session={session}
429
+ onChange={setContent}
430
+ onAssetsChange={setDocumentAssetIds}
431
+ onAddInlineFiles={(files, position) => {
432
+ addInlineFiles(files, position);
433
+ }}
434
+ onAddAttachmentFiles={(files, position) => {
435
+ addAttachmentFiles(files, position);
436
+ }}
437
+ onChooseInlineImage={() => inlineFileInput.current?.click()}
438
+ onSubmitShortcut={() => form.current?.requestSubmit()}
439
+ dragging={dropTarget}
440
+ disabled={busy}
441
+ />
442
+
443
+ <ComposerAttachmentList
444
+ uploads={activeAssets.filter(
445
+ (asset) => !linkedIds.has(asset.id) || asset.status !== "uploaded"
446
+ )}
447
+ linkedIds={linkedIds}
448
+ progress={session.progress}
449
+ disabled={busy}
450
+ onRemove={(asset) => {
451
+ if (editor.current?.removeAsset(asset.id)) return;
452
+ session.remove(asset.id);
453
+ }}
454
+ onRetry={(asset) => {
455
+ void session.retry(asset.id);
456
+ }}
457
+ />
458
+
459
+ <ComposeWindowFooter
460
+ fileInput={fileInput}
461
+ inlineFileInput={inlineFileInput}
462
+ uploads={activeAssets}
463
+ error={
464
+ attachmentError ??
465
+ (activeAssets.length > 0 && snapshot.planError
466
+ ? "Could not check attachment delivery"
467
+ : null)
468
+ }
469
+ sending={busy}
470
+ sendDisabled={
471
+ busy ||
472
+ recipientCount === 0 ||
473
+ !mailbox.canSend ||
474
+ Boolean(attachmentError) ||
475
+ uploadsPending ||
476
+ uploadsFailed
477
+ }
478
+ onAddAttachments={addAttachmentFiles}
479
+ onAddInlineImages={(files) => {
480
+ addInlineFiles(files);
481
+ }}
482
+ />
483
+ </form>
484
+ </section>
485
+ );
486
+ }