@open-workspace/email 0.4.2 → 0.5.1

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 (268) hide show
  1. package/README.md +9 -4
  2. package/alchemy/index.ts +130 -2
  3. package/drizzle/mailbox/0003_after_0_4_2.sql +180 -0
  4. package/drizzle/mailbox/meta/0003_snapshot.json +1507 -0
  5. package/drizzle/mailbox/meta/_journal.json +7 -0
  6. package/drizzle/mailbox/migrations.js +3 -1
  7. package/migrations/0002_after_0_4_2.sql +35 -0
  8. package/migrations/meta/0002_snapshot.json +807 -0
  9. package/migrations/meta/_journal.json +7 -0
  10. package/package.json +23 -7
  11. package/shared/attachment-preview.ts +18 -8
  12. package/shared/folder-order.ts +31 -6
  13. package/shared/mail-headers.ts +3 -3
  14. package/shared/mail-html.ts +25 -0
  15. package/shared/mail-import.ts +0 -7
  16. package/shared/mail-received.ts +4 -4
  17. package/shared/mail-rule-actions.ts +47 -0
  18. package/shared/mail-sender-intel.ts +5 -1
  19. package/shared/mail.ts +70 -4
  20. package/shared/office-preview.ts +0 -4
  21. package/shared/pdfjs-assets.ts +1 -3
  22. package/shared/rich-content.ts +35 -0
  23. package/shared/signature.ts +15 -0
  24. package/src/admin/address-auto-reply.tsx +52 -0
  25. package/src/admin/blocked-senders.tsx +86 -66
  26. package/src/admin/forward-destinations.tsx +140 -0
  27. package/src/admin/mail-archive.tsx +134 -0
  28. package/src/admin/team-signature.tsx +90 -0
  29. package/src/client.tsx +130 -16
  30. package/src/mail/attachment-preview-dialog.tsx +13 -5
  31. package/src/mail/auto-reply-banner.tsx +61 -0
  32. package/src/mail/blocked-senders.tsx +124 -91
  33. package/src/mail/compose-window-chrome.tsx +20 -8
  34. package/src/mail/compose-window.tsx +391 -365
  35. package/src/mail/composer-asset-bookmarks.ts +47 -12
  36. package/src/mail/composer-attachment-list.tsx +42 -17
  37. package/src/mail/composer-editor-document.ts +54 -10
  38. package/src/mail/composer-editor.tsx +44 -44
  39. package/src/mail/composer-link-text.ts +42 -0
  40. package/src/mail/composer-recipient-fields.tsx +25 -16
  41. package/src/mail/composer-recipients.ts +62 -16
  42. package/src/mail/composer-session.ts +107 -23
  43. package/src/mail/composer-toolbar.tsx +15 -1
  44. package/src/mail/composer-upload-client.ts +29 -1
  45. package/src/mail/composer-upload-progress.ts +2 -2
  46. package/src/mail/conversation-actions-model.ts +56 -0
  47. package/src/mail/conversation-list.tsx +263 -128
  48. package/src/mail/conversation-loading-skeleton.tsx +165 -90
  49. package/src/mail/conversation-message.tsx +257 -102
  50. package/src/mail/conversation-order.ts +12 -0
  51. package/src/mail/conversation-reply.tsx +313 -189
  52. package/src/mail/conversation-selection-toolbar.tsx +188 -143
  53. package/src/mail/conversation-view.tsx +315 -85
  54. package/src/mail/delivery-indicator.tsx +33 -12
  55. package/src/mail/draft-attachments.ts +194 -0
  56. package/src/mail/email-html-body.tsx +97 -53
  57. package/src/mail/folder-cleanup-bar.tsx +193 -0
  58. package/src/mail/folder-glyph.tsx +5 -0
  59. package/src/mail/folder-manager.tsx +71 -38
  60. package/src/mail/mail-app-bar.tsx +15 -3
  61. package/src/mail/mail-dock.tsx +19 -3
  62. package/src/mail/mail-focus.ts +48 -0
  63. package/src/mail/mail-folders.ts +14 -2
  64. package/src/mail/mail-layout.css +20 -0
  65. package/src/mail/mail-mailbox-sheet.tsx +22 -69
  66. package/src/mail/mail-nav-rail.tsx +7 -4
  67. package/src/mail/mail-nav.tsx +66 -46
  68. package/src/mail/mail-prefetch.ts +30 -9
  69. package/src/mail/mail-primitives.tsx +6 -4
  70. package/src/mail/mail-queries.ts +122 -20
  71. package/src/mail/mail-query-cache.ts +1085 -173
  72. package/src/mail/mail-search-params.ts +35 -0
  73. package/src/mail/mail-shell.tsx +32 -9
  74. package/src/mail/mailbox-ai-settings.tsx +56 -42
  75. package/src/mail/mailbox-realtime.tsx +4 -12
  76. package/src/mail/mailbox-rules.tsx +898 -0
  77. package/src/mail/message-actions.tsx +38 -26
  78. package/src/mail/message-attachments.tsx +20 -12
  79. package/src/mail/message-body.ts +53 -0
  80. package/src/mail/message-format.ts +8 -2
  81. package/src/mail/message-header.tsx +3 -1
  82. package/src/mail/message-translation.tsx +34 -67
  83. package/src/mail/original-message-sheet.tsx +63 -50
  84. package/src/mail/original-message.ts +6 -6
  85. package/src/mail/pane-splitter.tsx +3 -3
  86. package/src/mail/pdf-preview.tsx +70 -62
  87. package/src/mail/plain-message-text.tsx +28 -0
  88. package/src/mail/print-message.ts +31 -36
  89. package/src/mail/reader-pane.tsx +134 -103
  90. package/src/mail/recipient-combobox.tsx +52 -29
  91. package/src/mail/recipient-field-row.tsx +3 -0
  92. package/src/mail/rich-content.ts +128 -0
  93. package/src/mail/sanitize-email-html.ts +53 -32
  94. package/src/mail/sender-actions.tsx +48 -46
  95. package/src/mail/sender-authentication.tsx +1 -1
  96. package/src/mail/sender-dialogs.tsx +67 -43
  97. package/src/mail/sender-security.tsx +12 -11
  98. package/src/mail/signature.ts +43 -0
  99. package/src/mail/translation-html.ts +3 -3
  100. package/src/mail/types.ts +12 -0
  101. package/src/mail/use-composer-attachment-preflight.ts +23 -60
  102. package/src/mail/use-composer-core.ts +286 -0
  103. package/src/mail/use-composer-navigation.ts +120 -0
  104. package/src/mail/use-composer-submission.ts +154 -19
  105. package/src/mail/use-conversation-actions.ts +292 -0
  106. package/src/mail/use-conversation-position.ts +51 -0
  107. package/src/mail/use-conversation-selection.ts +192 -89
  108. package/src/mail/use-draft-autosave.ts +394 -0
  109. package/src/mail/use-drafts.ts +194 -0
  110. package/src/mail/use-mail-controller.ts +758 -213
  111. package/src/mail/use-mail-data.ts +210 -100
  112. package/src/mail/use-mail-safety.ts +19 -38
  113. package/src/mail/use-mail-send.ts +18 -6
  114. package/src/mail/use-pdf-translation.ts +1 -1
  115. package/src/mail/use-reply-cc.ts +61 -0
  116. package/src/mail/use-visible-message-read.ts +128 -64
  117. package/src/pwa/push-key.ts +18 -0
  118. package/src/pwa/push-subscription.ts +88 -26
  119. package/src/settings/auto-reply-editor.tsx +454 -0
  120. package/src/settings/auto-reply-page.tsx +59 -0
  121. package/src/settings/mail-apps-page.tsx +52 -43
  122. package/src/settings/notifications-page.tsx +82 -67
  123. package/src/settings/rich-content-editor.tsx +182 -0
  124. package/src/settings/settings-queries.ts +57 -0
  125. package/src/settings/signature-editor.tsx +142 -0
  126. package/src/settings/signature-page.tsx +70 -0
  127. package/worker/admin/blocked-sender-routes.ts +10 -16
  128. package/worker/backups/eml.ts +185 -0
  129. package/worker/backups/file-parts.ts +140 -0
  130. package/worker/backups/mail-backup.ts +644 -0
  131. package/worker/backups/settings-backup.ts +866 -0
  132. package/worker/backups/stream-eml.ts +73 -0
  133. package/worker/db/schema.ts +89 -5
  134. package/worker/jmap/attachment-blobs.ts +113 -0
  135. package/worker/jmap/blobs.ts +162 -0
  136. package/worker/jmap/draft-content.ts +42 -0
  137. package/worker/jmap/drafts.ts +70 -0
  138. package/worker/jmap/email-body.ts +152 -0
  139. package/worker/jmap/email-methods.ts +1223 -0
  140. package/worker/jmap/eventsource.ts +9 -8
  141. package/worker/jmap/import-methods.ts +205 -0
  142. package/worker/jmap/mailbox-methods.ts +187 -0
  143. package/worker/jmap/method-core.ts +379 -0
  144. package/worker/jmap/methods.ts +119 -1816
  145. package/worker/jmap/push-subscription-methods.ts +373 -0
  146. package/worker/jmap/push-types.ts +46 -2
  147. package/worker/jmap/push.ts +24 -31
  148. package/worker/jmap/routes.ts +144 -153
  149. package/worker/jmap/submission-methods.ts +395 -0
  150. package/worker/mail/access.ts +35 -12
  151. package/worker/mail/ai-classification.ts +2 -2
  152. package/worker/mail/archive-object.ts +96 -0
  153. package/worker/mail/archive-routes.ts +35 -0
  154. package/worker/mail/archive.ts +700 -0
  155. package/worker/mail/auth-headers.ts +57 -43
  156. package/worker/mail/auto-reply-routes.ts +260 -0
  157. package/worker/mail/auto-reply.ts +243 -0
  158. package/worker/mail/bimi.ts +53 -36
  159. package/worker/mail/bootstrap.ts +11 -3
  160. package/worker/mail/capabilities.ts +69 -0
  161. package/worker/mail/classification-signals.ts +1 -1
  162. package/worker/mail/confusables-data.ts +9 -0
  163. package/worker/mail/conversation-cursor.ts +6 -15
  164. package/worker/mail/delivery-events.ts +9 -7
  165. package/worker/mail/delivery-facts.ts +1 -1
  166. package/worker/mail/demo-routes.ts +3 -3
  167. package/worker/mail/doh.ts +3 -3
  168. package/worker/mail/download-links.ts +153 -0
  169. package/worker/mail/downloads.ts +2 -6
  170. package/worker/mail/draft-routes.ts +473 -0
  171. package/worker/mail/draft-sender.ts +54 -0
  172. package/worker/mail/edge-cache.ts +41 -55
  173. package/worker/mail/email-routing.ts +123 -0
  174. package/worker/mail/forward-destinations.ts +114 -0
  175. package/worker/mail/forward-downloads.ts +115 -0
  176. package/worker/mail/forward-html.ts +85 -0
  177. package/worker/mail/header-metadata.ts +2 -5
  178. package/worker/mail/impersonation.ts +102 -101
  179. package/worker/mail/import-routes.ts +29 -82
  180. package/worker/mail/import.ts +130 -0
  181. package/worker/mail/inbound-delivery.ts +6 -5
  182. package/worker/mail/inbound-threading.ts +15 -2
  183. package/worker/mail/inbound.ts +58 -28
  184. package/worker/mail/ip-intel.ts +8 -17
  185. package/worker/mail/isolate-cache.ts +1 -1
  186. package/worker/mail/mailbox-deletion.ts +33 -12
  187. package/worker/mail/mailbox-directory.ts +39 -65
  188. package/worker/mail/mailing-list.ts +29 -58
  189. package/worker/mail/message-security.ts +19 -8
  190. package/worker/mail/message-view.ts +12 -10
  191. package/worker/mail/office-conversion.ts +3 -5
  192. package/worker/mail/office-preview-container.ts +3 -8
  193. package/worker/mail/outbound-content.ts +139 -65
  194. package/worker/mail/outbound-delivery.ts +20 -17
  195. package/worker/mail/outbound-service.ts +34 -9
  196. package/worker/mail/outbound.ts +146 -75
  197. package/worker/mail/outgoing-context.ts +60 -30
  198. package/worker/mail/participants.ts +10 -15
  199. package/worker/mail/plain-body.ts +39 -0
  200. package/worker/mail/push-endpoint.ts +17 -0
  201. package/worker/mail/push-notifications.ts +53 -62
  202. package/worker/mail/quote-parser.ts +36 -24
  203. package/worker/mail/receive.ts +146 -39
  204. package/worker/mail/recipients.ts +1 -1
  205. package/worker/mail/remote-proxy.ts +3 -3
  206. package/worker/mail/repeat.ts +1 -1
  207. package/worker/mail/reply-plan.ts +124 -26
  208. package/worker/mail/rfc.ts +15 -7
  209. package/worker/mail/rich-content.ts +207 -0
  210. package/worker/mail/routes.ts +698 -304
  211. package/worker/mail/routing.ts +274 -0
  212. package/worker/mail/rules.ts +230 -0
  213. package/worker/mail/safety-routes.ts +88 -86
  214. package/worker/mail/schemas.ts +222 -31
  215. package/worker/mail/search.ts +25 -2
  216. package/worker/mail/security-settings.ts +4 -10
  217. package/worker/mail/send-message.ts +238 -0
  218. package/worker/mail/sender-intel.ts +12 -20
  219. package/worker/mail/signature-routes.ts +206 -0
  220. package/worker/mail/signatures.ts +195 -0
  221. package/worker/mail/spamhaus.ts +28 -16
  222. package/worker/mail/storage-keys.ts +35 -0
  223. package/worker/mail/unsubscribe.ts +19 -41
  224. package/worker/mail/uploads.ts +192 -80
  225. package/worker/mail/webhook-source.ts +3 -2
  226. package/worker/mail/workspace-safety.ts +194 -60
  227. package/worker/mailbox/demo.ts +13 -15
  228. package/worker/mailbox/email-index.ts +65 -35
  229. package/worker/mailbox/folder-model.ts +0 -20
  230. package/worker/mailbox/import-placement.ts +21 -36
  231. package/worker/mailbox/inbound-decision.ts +114 -0
  232. package/worker/mailbox/inbox-counts.ts +45 -25
  233. package/worker/mailbox/index.ts +756 -3201
  234. package/worker/mailbox/jmap-change-log.ts +195 -0
  235. package/worker/mailbox/jmap-email-rows.ts +186 -0
  236. package/worker/mailbox/jmap-mailboxes.ts +116 -0
  237. package/worker/mailbox/jmap-placement.ts +139 -0
  238. package/worker/mailbox/jmap-query-filter.ts +231 -0
  239. package/worker/mailbox/jmap-store.ts +167 -299
  240. package/worker/mailbox/mailbox-auto-reply.ts +316 -0
  241. package/worker/mailbox/mailbox-backup.ts +538 -0
  242. package/worker/mailbox/mailbox-drafts.ts +417 -0
  243. package/worker/mailbox/mailbox-folders.ts +242 -0
  244. package/worker/mailbox/mailbox-import.ts +455 -0
  245. package/worker/mailbox/mailbox-inbound.ts +934 -0
  246. package/worker/mailbox/mailbox-jmap.ts +294 -0
  247. package/worker/mailbox/mailbox-outbound.ts +413 -0
  248. package/worker/mailbox/mailbox-rules.ts +336 -0
  249. package/worker/mailbox/mailbox-senders.ts +294 -0
  250. package/worker/mailbox/mailbox-storage.ts +438 -0
  251. package/worker/mailbox/mailbox-views.ts +891 -0
  252. package/worker/mailbox/model.ts +1 -0
  253. package/worker/mailbox/office-preview-jobs.ts +59 -47
  254. package/worker/mailbox/placement.ts +933 -0
  255. package/worker/mailbox/restore-thread.ts +52 -0
  256. package/worker/mailbox/retention.ts +36 -0
  257. package/worker/mailbox/schema.ts +200 -1
  258. package/worker/mailbox/senders.ts +216 -178
  259. package/worker/mailbox/spam-undo-filing.ts +138 -0
  260. package/worker/mailbox/spam-undo.ts +253 -0
  261. package/worker/mailbox/support.ts +416 -0
  262. package/worker/mcp/mail-action-tools.ts +161 -1
  263. package/worker/mcp/mail-compose-tools.ts +7 -1
  264. package/worker/mcp/mail-safety-tools.ts +4 -4
  265. package/worker/module.ts +23 -4
  266. package/src/mail/use-compose-submission.ts +0 -208
  267. package/src/mail/use-reply-submission.ts +0 -114
  268. package/worker/mail/capped-body.ts +0 -36
@@ -16,9 +16,7 @@ export type RecipientFieldValue = {
16
16
 
17
17
  export const RECIPIENT_SEPARATOR = /[,;\n]/u;
18
18
 
19
- export function normalizeComposerRecipient(
20
- recipient: ComposerRecipient
21
- ): ComposerRecipient | null {
19
+ export function normalizeComposerRecipient(recipient: ComposerRecipient) {
22
20
  const address = normalizeExternalEmailAddress(recipient.address);
23
21
 
24
22
  if (!isValidExternalEmailAddress(address)) return null;
@@ -29,7 +27,7 @@ export function normalizeComposerRecipient(
29
27
  };
30
28
  }
31
29
 
32
- function recipientFromText(value: string): ComposerRecipient | null {
30
+ function recipientFromText(value: string) {
33
31
  const trimmed = value.trim();
34
32
 
35
33
  if (!trimmed) return null;
@@ -109,8 +107,36 @@ export function parseComposerRecipients(value: string) {
109
107
  return { recipients, invalidParts };
110
108
  }
111
109
 
112
- export function recipientsWithPendingInput(value: RecipientFieldValue) {
113
- const { recipients: pending } = parseComposerRecipients(value.input);
110
+ /** Drafts also hold unfinished addresses so they can be edited after reopening. */
111
+ export function recipientField(addresses: readonly string[]) {
112
+ const recipients: ComposerRecipient[] = [];
113
+ const unfinished: string[] = [];
114
+
115
+ for (const address of addresses) {
116
+ const recipient = recipientFromText(address);
117
+
118
+ if (recipient) recipients.push(recipient);
119
+ else unfinished.push(address);
120
+ }
121
+
122
+ return { recipients, input: unfinished.join(", ") };
123
+ }
124
+
125
+ export function draftRecipientAddresses(value: RecipientFieldValue) {
126
+ const { recipients, invalidParts } = parseComposerRecipients(value.input);
127
+
128
+ return [
129
+ ...recipientsWithPendingInput(value, recipients).map(
130
+ (recipient) => recipient.address
131
+ ),
132
+ ...invalidParts,
133
+ ];
134
+ }
135
+
136
+ export function recipientsWithPendingInput(
137
+ value: RecipientFieldValue,
138
+ pending = parseComposerRecipients(value.input).recipients
139
+ ) {
114
140
  const seen = new Set<string>();
115
141
 
116
142
  return [...value.recipients, ...pending].filter((recipient) => {
@@ -136,16 +162,23 @@ export type ValidatedComposerRecipients = {
136
162
  error: string | null;
137
163
  };
138
164
 
165
+ const recipientErrors = {
166
+ address: "Check the recipient addresses",
167
+ empty: "Add at least one recipient in To, Cc, or Bcc",
168
+ limit: `Use at most ${MAX_MAIL_RECIPIENTS} recipients across To, Cc, and Bcc`,
169
+ replyTo: "Check the Reply-to address",
170
+ } as const;
171
+
139
172
  export function validateComposerRecipients(input: {
140
173
  to: RecipientFieldValue;
141
174
  cc: RecipientFieldValue;
142
175
  bcc: RecipientFieldValue;
143
176
  replyTo?: string;
144
- }): ValidatedComposerRecipients {
177
+ }) {
145
178
  const fields = [input.to, input.cc, input.bcc] as const;
146
179
 
147
180
  if (fields.some(hasInvalidRecipientInput)) {
148
- return invalidRecipients("Check the recipient addresses");
181
+ return invalidRecipients("address");
149
182
  }
150
183
 
151
184
  let invalidCommittedAddress = false;
@@ -172,15 +205,17 @@ export function validateComposerRecipients(input: {
172
205
  const bcc = take(input.bcc);
173
206
 
174
207
  if (invalidCommittedAddress) {
175
- return invalidRecipients("Check the recipient addresses");
208
+ return invalidRecipients("address");
176
209
  }
177
210
 
178
211
  const count = to.length + cc.length + bcc.length;
179
212
 
213
+ if (count === 0) {
214
+ return invalidRecipients("empty");
215
+ }
216
+
180
217
  if (count > MAX_MAIL_RECIPIENTS) {
181
- return invalidRecipients(
182
- `Use at most ${MAX_MAIL_RECIPIENTS} recipients across To, Cc, and Bcc`
183
- );
218
+ return invalidRecipients("limit");
184
219
  }
185
220
 
186
221
  const rawReplyTo = input.replyTo?.trim() ?? "";
@@ -190,19 +225,30 @@ export function validateComposerRecipients(input: {
190
225
  : undefined;
191
226
 
192
227
  if (replyTo && !isValidExternalEmailAddress(replyTo)) {
193
- return invalidRecipients("Check the Reply-to address");
228
+ return invalidRecipients("replyTo");
194
229
  }
195
230
 
196
- return { to, cc, bcc, replyTo, count, error: null };
231
+ return {
232
+ to,
233
+ cc,
234
+ bcc,
235
+ replyTo,
236
+ count,
237
+ issue: null,
238
+ error: null,
239
+ feedback: null,
240
+ };
197
241
  }
198
242
 
199
- function invalidRecipients(error: string): ValidatedComposerRecipients {
243
+ function invalidRecipients(issue: keyof typeof recipientErrors) {
200
244
  return {
201
245
  to: [],
202
246
  cc: [],
203
247
  bcc: [],
204
248
  replyTo: undefined,
205
249
  count: 0,
206
- error,
250
+ issue,
251
+ error: recipientErrors[issue],
252
+ feedback: issue === "empty" ? null : recipientErrors[issue],
207
253
  };
208
254
  }
@@ -57,16 +57,30 @@ export function isComposerSessionBusy(phase: ComposerSessionPhase) {
57
57
  return phase === "preflighting" || phase === "submitting";
58
58
  }
59
59
 
60
+ export type ForwardedAttachment = {
61
+ id: string;
62
+ filename: string;
63
+ size: number;
64
+ };
65
+
60
66
  type ComposerSessionSnapshot = {
67
+ forwardedAttachments: readonly ForwardedAttachment[];
68
+ /** Every asset still held, including those removed but kept for undo. */
61
69
  assets: readonly ComposerAsset[];
70
+ /**
71
+ * The assets the message holds: those the editor's document refers to.
72
+ * Only these count against the limits, are saved and are sent.
73
+ */
74
+ documentAssetIds: ReadonlySet<string>;
62
75
  phase: ComposerSessionPhase;
63
76
  planError: boolean;
64
77
  linkedAssetIds: ReadonlySet<string>;
78
+ linkedForwardedIds: ReadonlySet<string>;
65
79
  };
66
80
 
67
81
  type Listener = () => void;
68
82
 
69
- const EMPTY_RESERVED_ASSETS: readonly { size: number }[] = [];
83
+ const NO_FORWARDED_FILES: readonly ForwardedAttachment[] = [];
70
84
  const EMPTY_LINKED_ASSETS: ReadonlySet<string> = new Set();
71
85
 
72
86
  /**
@@ -77,10 +91,13 @@ export class ComposerSession {
77
91
  readonly progress = createComposerUploadProgressStore();
78
92
 
79
93
  private snapshot: ComposerSessionSnapshot = {
94
+ forwardedAttachments: NO_FORWARDED_FILES,
80
95
  assets: [],
96
+ documentAssetIds: EMPTY_LINKED_ASSETS,
81
97
  phase: "editing",
82
98
  planError: false,
83
99
  linkedAssetIds: EMPTY_LINKED_ASSETS,
100
+ linkedForwardedIds: EMPTY_LINKED_ASSETS,
84
101
  };
85
102
  private readonly listeners = new Set<Listener>();
86
103
  private readonly files = new Map<string, File>();
@@ -88,19 +105,38 @@ export class ComposerSession {
88
105
  private readonly operations = new Map<string, string>();
89
106
  private claimedAssetIds: ReadonlySet<string> = EMPTY_LINKED_ASSETS;
90
107
  private planKey: string | null = null;
108
+ private planReady = false;
91
109
  private submissionAttempt: { id: string; key: string } | null = null;
92
110
  private owners = 0;
93
111
  private disposed = false;
94
112
 
113
+ /**
114
+ * Whether closing keeps finished uploads, because a draft holds (or is
115
+ * about to copy) them. Unkept uploads expire with `composer-uploads/`.
116
+ */
117
+ private retainUploads = false;
118
+
95
119
  constructor(
96
120
  readonly mailboxId: string,
97
- private readonly reservedAssets: readonly {
98
- size: number;
99
- }[] = EMPTY_RESERVED_ASSETS
100
- ) {}
121
+ forwardedAttachments: readonly ForwardedAttachment[] = NO_FORWARDED_FILES,
122
+ /** Uploaded assets a reopened draft brings back. */
123
+ restoredAssets: readonly ComposerAsset[] = []
124
+ ) {
125
+ this.snapshot = {
126
+ ...this.snapshot,
127
+ assets: restoredAssets,
128
+ forwardedAttachments,
129
+ // The editor bookmarks every asset it opens with.
130
+ documentAssetIds: new Set(restoredAssets.map((asset) => asset.id)),
131
+ };
132
+ }
101
133
 
102
134
  getSnapshot = () => this.snapshot;
103
135
 
136
+ setRetainUploads(retain: boolean) {
137
+ this.retainUploads = retain;
138
+ }
139
+
104
140
  subscribe = (listener: Listener) => {
105
141
  this.listeners.add(listener);
106
142
 
@@ -126,17 +162,39 @@ export class ComposerSession {
126
162
  return this.snapshot.linkedAssetIds.has(id);
127
163
  }
128
164
 
165
+ /** The assets the message holds (documentAssetIds), in upload order. */
166
+ documentAssets() {
167
+ const { assets, documentAssetIds } = this.snapshot;
168
+
169
+ return assets.filter((asset) => documentAssetIds.has(asset.id));
170
+ }
171
+
172
+ /** The editor's document now refers to these assets. */
173
+ setDocumentAssetIds = (assetIds: readonly string[]) => {
174
+ const next = new Set(assetIds);
175
+
176
+ if (setsEqual(next, this.snapshot.documentAssetIds)) return;
177
+ this.publish({ ...this.snapshot, documentAssetIds: next });
178
+ };
179
+
180
+ removeForwardedAttachment(id: string) {
181
+ if (this.snapshot.phase !== "editing") return;
182
+ this.publish({
183
+ ...this.snapshot,
184
+ forwardedAttachments: this.snapshot.forwardedAttachments.filter(
185
+ (file) => file.id !== id
186
+ ),
187
+ });
188
+ }
189
+
129
190
  limitError() {
130
191
  return composerAttachmentLimitError([
131
- ...this.snapshot.assets,
132
- ...this.reservedAssets,
192
+ ...this.documentAssets(),
193
+ ...this.snapshot.forwardedAttachments,
133
194
  ]);
134
195
  }
135
196
 
136
- addFiles(
137
- selected: Iterable<File>,
138
- intent: ComposerAssetIntent
139
- ): ComposerAsset[] | null {
197
+ addFiles(selected: Iterable<File>, intent: ComposerAssetIntent) {
140
198
  if (this.snapshot.phase !== "editing") {
141
199
  toast.error("Wait for the message to finish sending");
142
200
 
@@ -157,8 +215,8 @@ export class ComposerSession {
157
215
  if (!accepted.length) return null;
158
216
 
159
217
  const limitError = composerAttachmentLimitError([
160
- ...this.snapshot.assets,
161
- ...this.reservedAssets,
218
+ ...this.documentAssets(),
219
+ ...this.snapshot.forwardedAttachments,
162
220
  ...accepted.map((file) => ({ size: file.size })),
163
221
  ]);
164
222
 
@@ -276,13 +334,20 @@ export class ComposerSession {
276
334
  return false;
277
335
  }
278
336
 
337
+ if (this.planKey === planKey && this.planReady) return false;
279
338
  this.planKey = planKey;
339
+ this.planReady = false;
280
340
 
281
341
  return true;
282
342
  }
283
343
 
284
- resolvePlan(planKey: string, linkedAssetIds: Iterable<string>) {
344
+ resolvePlan(
345
+ planKey: string,
346
+ linkedAssetIds: Iterable<string>,
347
+ linkedForwardedIds: ReadonlySet<string>
348
+ ) {
285
349
  if (this.planKey !== planKey) return false;
350
+ this.planReady = true;
286
351
  const nextLinkedAssetIds = new Set(linkedAssetIds);
287
352
 
288
353
  const linkedAssetsChanged = !setsEqual(
@@ -290,10 +355,18 @@ export class ComposerSession {
290
355
  nextLinkedAssetIds
291
356
  );
292
357
 
293
- if (!this.snapshot.planError && !linkedAssetsChanged) return true;
358
+ if (
359
+ !this.snapshot.planError &&
360
+ !linkedAssetsChanged &&
361
+ setsEqual(this.snapshot.linkedForwardedIds, linkedForwardedIds)
362
+ ) {
363
+ return true;
364
+ }
365
+
294
366
  this.publish({
295
367
  ...this.snapshot,
296
368
  planError: false,
369
+ linkedForwardedIds,
297
370
  linkedAssetIds: linkedAssetsChanged
298
371
  ? nextLinkedAssetIds
299
372
  : this.snapshot.linkedAssetIds,
@@ -303,7 +376,10 @@ export class ComposerSession {
303
376
  }
304
377
 
305
378
  failPlan(planKey: string) {
306
- if (this.planKey !== planKey || this.snapshot.planError) return;
379
+ if (this.planKey !== planKey) return;
380
+ this.planReady = false;
381
+
382
+ if (this.snapshot.planError) return;
307
383
  this.publish({ ...this.snapshot, planError: true });
308
384
  }
309
385
 
@@ -335,6 +411,10 @@ export class ComposerSession {
335
411
  return this.submissionAttempt.id;
336
412
  }
337
413
 
414
+ hasUnconfirmedSubmission() {
415
+ return this.submissionAttempt !== null;
416
+ }
417
+
338
418
  releaseSubmission(preserveAttempt = false) {
339
419
  const claimed = this.claimedAssetIds;
340
420
  this.claimedAssetIds = EMPTY_LINKED_ASSETS;
@@ -369,14 +449,17 @@ export class ComposerSession {
369
449
  if (this.disposed) return;
370
450
  this.disposed = true;
371
451
  const claimed = this.claimedAssetIds;
372
- this.disposeAssets((asset) => !claimed.has(asset.id));
452
+ const { documentAssetIds } = this.snapshot;
453
+ // A saved draft holds only the files still in the message.
454
+ this.disposeAssets(
455
+ (asset) =>
456
+ !claimed.has(asset.id) &&
457
+ !(this.retainUploads && documentAssetIds.has(asset.id))
458
+ );
373
459
  this.publish({ ...this.snapshot, phase: "closed" });
374
460
  };
375
461
 
376
- private startUpload = async (
377
- assetId: string,
378
- file: File
379
- ): Promise<ComposerAsset | null> => {
462
+ private startUpload = async (assetId: string, file: File) => {
380
463
  const operationId = crypto.randomUUID();
381
464
  this.operations.set(assetId, operationId);
382
465
  let uploadId: string | null = null;
@@ -542,12 +625,13 @@ function setsEqual<T>(left: ReadonlySet<T>, right: ReadonlySet<T>) {
542
625
 
543
626
  export function useComposerSession(
544
627
  mailboxId: string,
545
- reservedAssets?: readonly { size: number }[]
628
+ forwardedAttachments?: readonly ForwardedAttachment[],
629
+ restoredAssets?: readonly ComposerAsset[]
546
630
  ) {
547
631
  // One session for the composer's lifetime: created once, never replaced.
548
632
  // oxlint-disable-next-line react/hook-use-state -- the lazy initializer keeps one instance; there is nothing to set
549
633
  const [session] = useState(
550
- () => new ComposerSession(mailboxId, reservedAssets)
634
+ () => new ComposerSession(mailboxId, forwardedAttachments, restoredAssets)
551
635
  );
552
636
 
553
637
  const snapshot = useSyncExternalStore(
@@ -142,7 +142,21 @@ export function ComposerToolbar({
142
142
  ? value
143
143
  : `https://${value}`;
144
144
 
145
- editor.chain().focus().extendMarkRange("link").setLink({ href }).run();
145
+ editor
146
+ .chain()
147
+ .focus()
148
+ .extendMarkRange("link")
149
+ .command(({ commands, tr }) => {
150
+ if (!commands.setLink({ href })) return false;
151
+
152
+ if (!tr.selection.empty) return true;
153
+
154
+ // With no label selected, insert the address under the validated link mark.
155
+ tr.insertText(href);
156
+
157
+ return commands.unsetMark("link");
158
+ })
159
+ .run();
146
160
  setLinkValue("");
147
161
  closeLinkEditor(true);
148
162
  }
@@ -65,6 +65,9 @@ async function requestComposerUploadCompletion(
65
65
  );
66
66
  }
67
67
 
68
+ /** An upload that sends nothing for this long has stalled: it fails, and can be retried. */
69
+ const UPLOAD_STALL_MS = 60_000;
70
+
68
71
  export function uploadComposerFile(
69
72
  uploadUrl: string,
70
73
  headers: Record<string, string>,
@@ -74,6 +77,20 @@ export function uploadComposerFile(
74
77
  ) {
75
78
  return new Promise<void>((resolve, reject) => {
76
79
  const request = new XMLHttpRequest();
80
+ let stalled = false;
81
+ let stallTimer: ReturnType<typeof setTimeout> | undefined;
82
+
83
+ // A hung connection would otherwise keep Send disabled for good.
84
+ const watch = () => {
85
+ clearTimeout(stallTimer);
86
+ stallTimer = setTimeout(() => {
87
+ stalled = true;
88
+ request.abort();
89
+ }, UPLOAD_STALL_MS);
90
+ };
91
+
92
+ const settle = () => clearTimeout(stallTimer);
93
+
77
94
  onRequest(request);
78
95
  request.open("PUT", uploadUrl);
79
96
 
@@ -82,11 +99,15 @@ export function uploadComposerFile(
82
99
  }
83
100
 
84
101
  request.upload.addEventListener("progress", (event) => {
102
+ watch();
103
+
85
104
  if (event.lengthComputable && event.total > 0) {
86
105
  onProgress(Math.min(1, event.loaded / event.total));
87
106
  }
88
107
  });
89
108
  request.addEventListener("load", () => {
109
+ settle();
110
+
90
111
  if (request.status >= 200 && request.status < 300) {
91
112
  onProgress(1);
92
113
  resolve();
@@ -95,11 +116,18 @@ export function uploadComposerFile(
95
116
  }
96
117
  });
97
118
  request.addEventListener("error", () => {
119
+ settle();
98
120
  reject(new Error("Attachment upload failed"));
99
121
  });
100
122
  request.addEventListener("abort", () => {
101
- reject(new DOMException("Attachment upload was cancelled", "AbortError"));
123
+ settle();
124
+ reject(
125
+ stalled
126
+ ? new Error("Attachment upload stalled")
127
+ : new DOMException("Attachment upload was cancelled", "AbortError")
128
+ );
102
129
  });
130
+ watch();
103
131
  request.send(file);
104
132
  });
105
133
  }
@@ -10,7 +10,7 @@ type MutableComposerUploadProgressStore = ComposerUploadProgressStore & {
10
10
  set: (localId: string, progress: number) => void;
11
11
  };
12
12
 
13
- export function createComposerUploadProgressStore(): MutableComposerUploadProgressStore {
13
+ export function createComposerUploadProgressStore() {
14
14
  const values = new Map<string, number>();
15
15
  const listeners = new Map<string, Set<() => void>>();
16
16
 
@@ -38,7 +38,7 @@ export function createComposerUploadProgressStore(): MutableComposerUploadProgre
38
38
 
39
39
  for (const listener of listeners.get(localId) ?? []) listener();
40
40
  },
41
- };
41
+ } satisfies MutableComposerUploadProgressStore;
42
42
  }
43
43
 
44
44
  export function useComposerUploadProgress(
@@ -0,0 +1,56 @@
1
+ import { conversationFolder } from "../../shared/folder-order";
2
+ import type { ConversationSummary } from "./types";
3
+
4
+ /**
5
+ * Which actions conversations in a folder offer, the same for the open
6
+ * conversation's bar and a selection's toolbar. `folder` is where they are
7
+ * listed (`conversationFolder` for one conversation): a system folder's
8
+ * id, or "custom" for a folder of the person's.
9
+ */
10
+ export function conversationActionsIn(
11
+ folder: "inbox" | "custom" | "sent" | "archive" | "spam" | "trash"
12
+ ) {
13
+ const inInbox = folder === "inbox" || folder === "custom";
14
+
15
+ return {
16
+ /** Out of the Inbox, and out of Sent (a thread you started). */
17
+ archive: inInbox || folder === "sent",
18
+ /** Back to the Inbox from Archive or Trash. */
19
+ restore: folder === "archive" || folder === "trash",
20
+ /** Back from Spam, which also trusts the senders. */
21
+ notSpam: folder === "spam",
22
+ trash: folder !== "trash",
23
+ /** For people who can send from the mailbox. */
24
+ deleteForever: folder === "trash",
25
+ /** Received mail in the Inbox or Archive. */
26
+ report: inInbox || folder === "archive",
27
+ };
28
+ }
29
+
30
+ /** The folder kind `conversationActionsIn` takes, from a folder's id: a system folder's, else one of the person's. */
31
+ export function actionFolder(folderId: string) {
32
+ switch (folderId) {
33
+ case "inbox":
34
+ case "sent":
35
+ case "archive":
36
+ case "spam":
37
+ case "trash": {
38
+ return folderId;
39
+ }
40
+
41
+ default: {
42
+ return "custom";
43
+ }
44
+ }
45
+ }
46
+
47
+ /** Sent can show both active and archived conversations in the same selection. */
48
+ export function canArchiveOrRestore(
49
+ row: ConversationSummary,
50
+ state: "archive" | "active"
51
+ ) {
52
+ if (row.draftId !== null) return false;
53
+ const actions = conversationActionsIn(actionFolder(conversationFolder(row)));
54
+
55
+ return state === "archive" ? actions.archive : actions.restore;
56
+ }