@open-workspace/email 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (271) hide show
  1. package/README.md +9 -4
  2. package/alchemy/index.ts +130 -2
  3. package/drizzle/mailbox/0002_good_komodo.sql +2 -0
  4. package/drizzle/mailbox/0003_after_0_4_2.sql +180 -0
  5. package/drizzle/mailbox/meta/0002_snapshot.json +1017 -0
  6. package/drizzle/mailbox/meta/0003_snapshot.json +1507 -0
  7. package/drizzle/mailbox/meta/_journal.json +14 -0
  8. package/drizzle/mailbox/migrations.js +5 -1
  9. package/migrations/0001_overrated_ricochet.sql +2 -0
  10. package/migrations/0002_after_0_4_2.sql +35 -0
  11. package/migrations/meta/0001_snapshot.json +564 -0
  12. package/migrations/meta/0002_snapshot.json +807 -0
  13. package/migrations/meta/_journal.json +14 -0
  14. package/package.json +23 -7
  15. package/shared/attachment-preview.ts +18 -8
  16. package/shared/folder-order.ts +31 -6
  17. package/shared/mail-headers.ts +3 -3
  18. package/shared/mail-html.ts +25 -0
  19. package/shared/mail-import.ts +0 -7
  20. package/shared/mail-received.ts +4 -4
  21. package/shared/mail-rule-actions.ts +47 -0
  22. package/shared/mail.ts +70 -4
  23. package/shared/office-preview.ts +0 -4
  24. package/shared/pdfjs-assets.ts +1 -3
  25. package/shared/rich-content.ts +35 -0
  26. package/shared/signature.ts +15 -0
  27. package/src/admin/address-auto-reply.tsx +49 -0
  28. package/src/admin/blocked-senders.tsx +86 -66
  29. package/src/admin/forward-destinations.tsx +140 -0
  30. package/src/admin/mail-archive.tsx +134 -0
  31. package/src/admin/team-signature.tsx +90 -0
  32. package/src/client.tsx +130 -16
  33. package/src/mail/attachment-preview-dialog.tsx +13 -5
  34. package/src/mail/auto-reply-banner.tsx +61 -0
  35. package/src/mail/blocked-senders.tsx +124 -91
  36. package/src/mail/compose-window-chrome.tsx +20 -8
  37. package/src/mail/compose-window.tsx +387 -365
  38. package/src/mail/composer-asset-bookmarks.ts +47 -12
  39. package/src/mail/composer-attachment-list.tsx +42 -17
  40. package/src/mail/composer-editor-document.ts +54 -10
  41. package/src/mail/composer-editor.tsx +44 -44
  42. package/src/mail/composer-link-text.ts +42 -0
  43. package/src/mail/composer-recipient-fields.tsx +25 -16
  44. package/src/mail/composer-recipients.ts +62 -16
  45. package/src/mail/composer-session.ts +107 -23
  46. package/src/mail/composer-toolbar.tsx +15 -1
  47. package/src/mail/composer-upload-client.ts +29 -1
  48. package/src/mail/composer-upload-progress.ts +2 -2
  49. package/src/mail/conversation-actions-model.ts +56 -0
  50. package/src/mail/conversation-list.tsx +263 -128
  51. package/src/mail/conversation-loading-skeleton.tsx +165 -90
  52. package/src/mail/conversation-message.tsx +257 -102
  53. package/src/mail/conversation-order.ts +12 -0
  54. package/src/mail/conversation-reply.tsx +313 -189
  55. package/src/mail/conversation-selection-toolbar.tsx +188 -143
  56. package/src/mail/conversation-view.tsx +320 -85
  57. package/src/mail/delivery-indicator.tsx +33 -12
  58. package/src/mail/draft-attachments.ts +194 -0
  59. package/src/mail/email-html-body.tsx +97 -53
  60. package/src/mail/folder-cleanup-bar.tsx +193 -0
  61. package/src/mail/folder-glyph.tsx +5 -0
  62. package/src/mail/folder-manager.tsx +71 -38
  63. package/src/mail/mail-app-bar.tsx +15 -3
  64. package/src/mail/mail-dock.tsx +19 -3
  65. package/src/mail/mail-focus.ts +48 -0
  66. package/src/mail/mail-folders.ts +14 -2
  67. package/src/mail/mail-layout.css +20 -0
  68. package/src/mail/mail-mailbox-sheet.tsx +22 -69
  69. package/src/mail/mail-nav-rail.tsx +7 -4
  70. package/src/mail/mail-nav.tsx +66 -46
  71. package/src/mail/mail-prefetch.ts +30 -9
  72. package/src/mail/mail-primitives.tsx +6 -4
  73. package/src/mail/mail-queries.ts +122 -20
  74. package/src/mail/mail-query-cache.ts +1085 -173
  75. package/src/mail/mail-search-params.ts +35 -0
  76. package/src/mail/mail-shell.tsx +32 -9
  77. package/src/mail/mailbox-ai-settings.tsx +56 -42
  78. package/src/mail/mailbox-realtime.tsx +4 -12
  79. package/src/mail/mailbox-rules.tsx +898 -0
  80. package/src/mail/message-actions.tsx +38 -26
  81. package/src/mail/message-attachments.tsx +20 -12
  82. package/src/mail/message-body.ts +53 -0
  83. package/src/mail/message-format.ts +8 -2
  84. package/src/mail/message-header.tsx +3 -1
  85. package/src/mail/message-translation.tsx +34 -67
  86. package/src/mail/original-message-sheet.tsx +63 -50
  87. package/src/mail/original-message.ts +6 -6
  88. package/src/mail/pane-splitter.tsx +3 -3
  89. package/src/mail/pdf-preview.tsx +70 -62
  90. package/src/mail/plain-message-text.tsx +28 -0
  91. package/src/mail/print-message.ts +31 -36
  92. package/src/mail/reader-pane.tsx +134 -102
  93. package/src/mail/recipient-combobox.tsx +52 -29
  94. package/src/mail/recipient-field-row.tsx +3 -0
  95. package/src/mail/rich-content.ts +128 -0
  96. package/src/mail/sanitize-email-html.ts +57 -33
  97. package/src/mail/sender-actions.tsx +49 -39
  98. package/src/mail/sender-authentication.tsx +1 -1
  99. package/src/mail/sender-dialogs.tsx +67 -43
  100. package/src/mail/sender-security.tsx +9 -10
  101. package/src/mail/signature.ts +43 -0
  102. package/src/mail/translation-html.ts +3 -3
  103. package/src/mail/types.ts +12 -0
  104. package/src/mail/use-composer-attachment-preflight.ts +23 -60
  105. package/src/mail/use-composer-core.ts +286 -0
  106. package/src/mail/use-composer-navigation.ts +120 -0
  107. package/src/mail/use-composer-submission.ts +154 -19
  108. package/src/mail/use-conversation-actions.ts +292 -0
  109. package/src/mail/use-conversation-position.ts +51 -0
  110. package/src/mail/use-conversation-selection.ts +192 -89
  111. package/src/mail/use-draft-autosave.ts +394 -0
  112. package/src/mail/use-drafts.ts +194 -0
  113. package/src/mail/use-mail-controller.ts +754 -213
  114. package/src/mail/use-mail-data.ts +210 -100
  115. package/src/mail/use-mail-safety.ts +19 -38
  116. package/src/mail/use-mail-send.ts +18 -6
  117. package/src/mail/use-pdf-translation.ts +1 -1
  118. package/src/mail/use-reply-cc.ts +61 -0
  119. package/src/mail/use-visible-message-read.ts +128 -64
  120. package/src/pwa/push-key.ts +18 -0
  121. package/src/pwa/push-subscription.ts +88 -26
  122. package/src/settings/auto-reply-editor.tsx +454 -0
  123. package/src/settings/auto-reply-page.tsx +59 -0
  124. package/src/settings/mail-apps-page.tsx +52 -43
  125. package/src/settings/notifications-page.tsx +82 -67
  126. package/src/settings/rich-content-editor.tsx +182 -0
  127. package/src/settings/settings-queries.ts +57 -0
  128. package/src/settings/signature-editor.tsx +142 -0
  129. package/src/settings/signature-page.tsx +70 -0
  130. package/worker/admin/blocked-sender-routes.ts +10 -16
  131. package/worker/backups/eml.ts +185 -0
  132. package/worker/backups/file-parts.ts +140 -0
  133. package/worker/backups/mail-backup.ts +644 -0
  134. package/worker/backups/settings-backup.ts +866 -0
  135. package/worker/backups/stream-eml.ts +73 -0
  136. package/worker/db/schema.ts +98 -2
  137. package/worker/jmap/attachment-blobs.ts +113 -0
  138. package/worker/jmap/blobs.ts +162 -0
  139. package/worker/jmap/draft-content.ts +42 -0
  140. package/worker/jmap/drafts.ts +70 -0
  141. package/worker/jmap/email-body.ts +152 -0
  142. package/worker/jmap/email-methods.ts +1223 -0
  143. package/worker/jmap/eventsource.ts +9 -8
  144. package/worker/jmap/import-methods.ts +205 -0
  145. package/worker/jmap/mailbox-methods.ts +187 -0
  146. package/worker/jmap/method-core.ts +379 -0
  147. package/worker/jmap/methods.ts +108 -1815
  148. package/worker/jmap/push-subscription-methods.ts +373 -0
  149. package/worker/jmap/push-types.ts +46 -2
  150. package/worker/jmap/push.ts +24 -31
  151. package/worker/jmap/routes.ts +144 -153
  152. package/worker/jmap/submission-methods.ts +395 -0
  153. package/worker/mail/access.ts +35 -12
  154. package/worker/mail/ai-classification.ts +2 -2
  155. package/worker/mail/archive-object.ts +96 -0
  156. package/worker/mail/archive-routes.ts +35 -0
  157. package/worker/mail/archive.ts +682 -0
  158. package/worker/mail/auth-headers.ts +48 -40
  159. package/worker/mail/auto-reply-routes.ts +255 -0
  160. package/worker/mail/auto-reply.ts +243 -0
  161. package/worker/mail/bimi.ts +53 -36
  162. package/worker/mail/bootstrap.ts +11 -3
  163. package/worker/mail/capabilities.ts +69 -0
  164. package/worker/mail/classification-signals.ts +1 -1
  165. package/worker/mail/confusables-data.ts +9 -0
  166. package/worker/mail/conversation-cursor.ts +6 -15
  167. package/worker/mail/delivery-events.ts +9 -7
  168. package/worker/mail/delivery-facts.ts +1 -1
  169. package/worker/mail/demo-routes.ts +3 -3
  170. package/worker/mail/doh.ts +3 -3
  171. package/worker/mail/download-links.ts +153 -0
  172. package/worker/mail/downloads.ts +2 -6
  173. package/worker/mail/draft-routes.ts +473 -0
  174. package/worker/mail/draft-sender.ts +54 -0
  175. package/worker/mail/edge-cache.ts +41 -55
  176. package/worker/mail/email-routing.ts +123 -0
  177. package/worker/mail/forward-destinations.ts +114 -0
  178. package/worker/mail/forward-downloads.ts +115 -0
  179. package/worker/mail/forward-html.ts +85 -0
  180. package/worker/mail/header-metadata.ts +2 -5
  181. package/worker/mail/impersonation.ts +102 -101
  182. package/worker/mail/import-routes.ts +29 -82
  183. package/worker/mail/import.ts +130 -0
  184. package/worker/mail/inbound-delivery.ts +6 -5
  185. package/worker/mail/inbound.ts +58 -28
  186. package/worker/mail/ip-intel.ts +8 -17
  187. package/worker/mail/isolate-cache.ts +1 -1
  188. package/worker/mail/mailbox-deletion.ts +33 -12
  189. package/worker/mail/mailbox-directory.ts +39 -65
  190. package/worker/mail/mailing-list.ts +29 -58
  191. package/worker/mail/message-security.ts +3 -3
  192. package/worker/mail/message-view.ts +12 -10
  193. package/worker/mail/office-conversion.ts +3 -5
  194. package/worker/mail/office-preview-container.ts +3 -8
  195. package/worker/mail/outbound-content.ts +139 -65
  196. package/worker/mail/outbound-delivery.ts +20 -17
  197. package/worker/mail/outbound-service.ts +34 -9
  198. package/worker/mail/outbound.ts +146 -75
  199. package/worker/mail/outgoing-context.ts +60 -30
  200. package/worker/mail/participants.ts +10 -15
  201. package/worker/mail/plain-body.ts +39 -0
  202. package/worker/mail/push-endpoint.ts +17 -0
  203. package/worker/mail/push-notifications.ts +87 -43
  204. package/worker/mail/quote-parser.ts +107 -25
  205. package/worker/mail/r2-presigned-urls.ts +7 -0
  206. package/worker/mail/receive.ts +146 -39
  207. package/worker/mail/recipients.ts +1 -1
  208. package/worker/mail/remote-proxy.ts +3 -3
  209. package/worker/mail/repeat.ts +1 -1
  210. package/worker/mail/reply-plan.ts +124 -26
  211. package/worker/mail/rfc.ts +15 -7
  212. package/worker/mail/rich-content.ts +207 -0
  213. package/worker/mail/routes.ts +696 -304
  214. package/worker/mail/routing.ts +274 -0
  215. package/worker/mail/rules.ts +230 -0
  216. package/worker/mail/safety-routes.ts +88 -80
  217. package/worker/mail/schemas.ts +222 -31
  218. package/worker/mail/search.ts +25 -2
  219. package/worker/mail/security-settings.ts +4 -10
  220. package/worker/mail/send-message.ts +238 -0
  221. package/worker/mail/sender-intel.ts +12 -20
  222. package/worker/mail/signature-routes.ts +201 -0
  223. package/worker/mail/signatures.ts +195 -0
  224. package/worker/mail/spamhaus.ts +28 -16
  225. package/worker/mail/storage-keys.ts +35 -0
  226. package/worker/mail/unsubscribe.ts +19 -41
  227. package/worker/mail/uploads.ts +193 -80
  228. package/worker/mail/webhook-source.ts +3 -2
  229. package/worker/mail/workspace-safety.ts +194 -60
  230. package/worker/mailbox/demo.ts +13 -15
  231. package/worker/mailbox/email-index.ts +92 -13
  232. package/worker/mailbox/folder-model.ts +0 -20
  233. package/worker/mailbox/import-placement.ts +21 -36
  234. package/worker/mailbox/inbound-decision.ts +114 -0
  235. package/worker/mailbox/inbox-counts.ts +45 -25
  236. package/worker/mailbox/index.ts +843 -2888
  237. package/worker/mailbox/jmap-change-log.ts +195 -0
  238. package/worker/mailbox/jmap-email-rows.ts +186 -0
  239. package/worker/mailbox/jmap-mailboxes.ts +116 -0
  240. package/worker/mailbox/jmap-placement.ts +139 -0
  241. package/worker/mailbox/jmap-query-filter.ts +231 -0
  242. package/worker/mailbox/jmap-store.ts +167 -299
  243. package/worker/mailbox/mailbox-auto-reply.ts +301 -0
  244. package/worker/mailbox/mailbox-backup.ts +538 -0
  245. package/worker/mailbox/mailbox-drafts.ts +417 -0
  246. package/worker/mailbox/mailbox-folders.ts +242 -0
  247. package/worker/mailbox/mailbox-import.ts +450 -0
  248. package/worker/mailbox/mailbox-inbound.ts +934 -0
  249. package/worker/mailbox/mailbox-jmap.ts +294 -0
  250. package/worker/mailbox/mailbox-outbound.ts +413 -0
  251. package/worker/mailbox/mailbox-rules.ts +336 -0
  252. package/worker/mailbox/mailbox-senders.ts +294 -0
  253. package/worker/mailbox/mailbox-storage.ts +438 -0
  254. package/worker/mailbox/mailbox-views.ts +891 -0
  255. package/worker/mailbox/model.ts +1 -0
  256. package/worker/mailbox/office-preview-jobs.ts +59 -47
  257. package/worker/mailbox/placement.ts +933 -0
  258. package/worker/mailbox/restore-thread.ts +52 -0
  259. package/worker/mailbox/retention.ts +36 -0
  260. package/worker/mailbox/schema.ts +211 -1
  261. package/worker/mailbox/senders.ts +216 -178
  262. package/worker/mailbox/spam-undo-filing.ts +138 -0
  263. package/worker/mailbox/spam-undo.ts +240 -0
  264. package/worker/mailbox/support.ts +416 -0
  265. package/worker/mcp/mail-action-tools.ts +156 -0
  266. package/worker/mcp/mail-compose-tools.ts +8 -2
  267. package/worker/mcp/mail-safety-tools.ts +4 -4
  268. package/worker/module.ts +23 -2
  269. package/src/mail/use-compose-submission.ts +0 -208
  270. package/src/mail/use-reply-submission.ts +0 -114
  271. package/worker/mail/capped-body.ts +0 -36
@@ -8,8 +8,6 @@ import {
8
8
  useQuery,
9
9
  useQueryClient,
10
10
  } from "@tanstack/react-query";
11
- import type { InfiniteData } from "@tanstack/react-query";
12
- import { useEffect } from "react";
13
11
 
14
12
  import { mailApi } from "../lib/mail-api";
15
13
  import {
@@ -17,8 +15,17 @@ import {
17
15
  conversationsQuery,
18
16
  foldersQuery,
19
17
  mailboxesQuery,
18
+ prefetchConversation,
20
19
  } from "./mail-queries";
21
- import { applyOwnReadChange, scheduleMailboxRefresh } from "./mail-query-cache";
20
+ import {
21
+ applyMailboxChange,
22
+ beginOwnChange,
23
+ ownChangeDone,
24
+ ownChangeFailed,
25
+ ownChangeKey,
26
+ ownChangeSettled,
27
+ } from "./mail-query-cache";
28
+ import type { OwnChange, UndoSnapshot } from "./mail-query-cache";
22
29
  import type { Folder, MessageDetail } from "./types";
23
30
 
24
31
  export function useMailboxes() {
@@ -52,47 +59,71 @@ export function useRecipientSuggestions(
52
59
  });
53
60
  }
54
61
 
62
+ /**
63
+ * A list of conversations. While another one loads (a folder, mailbox,
64
+ * search or filter not seen yet), the one before stays as placeholder data
65
+ * (`isPlaceholderData`), so the list is never replaced by a skeleton; Unread
66
+ * starts from the cached full list's unread rows.
67
+ */
55
68
  export function useConversations(
56
69
  mailboxId: string | undefined,
57
70
  folder: Folder,
58
71
  search: string,
59
- unreadOnly: boolean,
60
- active = true
72
+ unreadOnly: boolean
61
73
  ) {
74
+ const client = useQueryClient();
75
+
62
76
  return useInfiniteQuery({
63
77
  ...conversationsQuery(mailboxId, folder, search, unreadOnly),
64
- enabled: Boolean(mailboxId) && active,
78
+ enabled: Boolean(mailboxId),
79
+ placeholderData: (previous) => {
80
+ const all = unreadOnly
81
+ ? client.getQueryData<NonNullable<typeof previous>>(
82
+ conversationsQuery(mailboxId, folder, search, false).queryKey
83
+ )
84
+ : undefined;
85
+
86
+ if (!all) return previous;
87
+
88
+ return {
89
+ ...all,
90
+ pages: all.pages.map((page) => ({
91
+ ...page,
92
+ conversations: page.conversations.filter((item) => item.isUnread),
93
+ })),
94
+ };
95
+ },
65
96
  });
66
97
  }
67
98
 
68
99
  export function useConversation(
69
100
  mailboxId: string | undefined,
70
- conversationId: string | undefined
101
+ conversationId: string | undefined,
102
+ matchId?: string
71
103
  ) {
72
- return useInfiniteQuery(conversationQuery(mailboxId, conversationId));
104
+ return useInfiniteQuery(
105
+ conversationQuery(mailboxId, conversationId, matchId)
106
+ );
73
107
  }
74
108
 
75
109
  /**
76
110
  * Starts loading a conversation the person is about to open (pointer
77
- * resting on its row, keyboard focus, or the next and previous one), so the
78
- * reader can show it at once.
111
+ * resting on its row, keyboard focus, or the next and previous one), with
112
+ * the bodies it opens with, so the reader can show it at once.
79
113
  */
80
114
  export function usePrefetchConversation() {
81
115
  const client = useQueryClient();
82
116
 
83
117
  return (
84
118
  mailboxId: string | undefined,
85
- conversationId: string | undefined
119
+ conversationId: string | undefined,
120
+ matchId?: string
86
121
  ) => {
87
122
  if (!mailboxId || !conversationId) return;
88
- void client.prefetchInfiniteQuery(
89
- conversationQuery(mailboxId, conversationId)
90
- );
123
+ void prefetchConversation(client, mailboxId, conversationId, matchId);
91
124
  };
92
125
  }
93
126
 
94
- type ConversationPage = { messages: MessageDetail[] };
95
-
96
127
  function senderIntelQuery(mailboxId: string, messageId: string) {
97
128
  return queryOptions({
98
129
  queryKey: ["sender-intel", mailboxId, messageId],
@@ -112,61 +143,40 @@ function senderIntelQuery(mailboxId: string, messageId: string) {
112
143
 
113
144
  /**
114
145
  * The reader gets a conversation before the sender lookups (DNS, blocklists)
115
- * of messages that had none finish; this fetches each one's result and puts
116
- * it into the cached conversation, which shows a pending state meanwhile.
146
+ * of messages that had none finish; this fetches each one's result and
147
+ * shows it in place of the pending state. A later read of the conversation
148
+ * has the result stored, so it no longer reports the message pending.
117
149
  */
118
- export function useSenderIntelFill(
150
+ export function useSenderIntel(
119
151
  mailboxId: string | undefined,
120
- conversationId: string | undefined,
121
152
  messages: MessageDetail[]
122
153
  ) {
123
- const client = useQueryClient();
154
+ const pending = mailboxId
155
+ ? messages.filter((message) => message.senderIntelPending)
156
+ : [];
124
157
 
125
- const pending =
126
- mailboxId && conversationId
127
- ? messages.filter((message) => message.senderIntelPending)
128
- : [];
129
-
130
- const results = useQueries({
158
+ const intel = useQueries({
131
159
  queries: mailboxId
132
160
  ? pending.map((message) => senderIntelQuery(mailboxId, message.id))
133
161
  : [],
134
- });
162
+ combine: (results) =>
163
+ new Map(
164
+ pending.flatMap((message, index) => {
165
+ const data = results[index]?.data;
135
166
 
136
- const readyKey = pending
137
- .flatMap((message, index) => (results[index]?.data ? [message.id] : []))
138
- .join(",");
167
+ return data ? [[message.id, data] as const] : [];
168
+ })
169
+ ),
170
+ });
139
171
 
140
- useEffect(() => {
141
- if (!mailboxId || !readyKey) return;
172
+ return messages.map((message) => {
173
+ const filled = message.senderIntelPending && intel.get(message.id);
142
174
 
143
- const ready = readyKey.split(",").flatMap((id) => {
144
- const data = client.getQueryData(
145
- senderIntelQuery(mailboxId, id).queryKey
146
- );
175
+ if (!filled) return message;
176
+ const { ok: _ok, ...found } = filled;
147
177
 
148
- return data ? [{ id, data }] : [];
149
- });
150
-
151
- client.setQueryData<InfiniteData<ConversationPage>>(
152
- ["conversation", mailboxId, conversationId],
153
- (data) =>
154
- data && {
155
- ...data,
156
- pages: data.pages.map((page) => ({
157
- ...page,
158
- messages: page.messages.map((message) => {
159
- const filled = ready.find((item) => item.id === message.id);
160
-
161
- if (!filled || !message.senderIntelPending) return message;
162
- const { ok: _ok, ...intel } = filled.data;
163
-
164
- return { ...message, ...intel };
165
- }),
166
- })),
167
- }
168
- );
169
- }, [client, conversationId, mailboxId, readyKey]);
178
+ return { ...message, ...found };
179
+ });
170
180
  }
171
181
 
172
182
  export function useResendMessage() {
@@ -181,15 +191,17 @@ export function useResendMessage() {
181
191
  })
182
192
  ),
183
193
  onSuccess: (_result, { mailboxId }) =>
184
- scheduleMailboxRefresh(client, mailboxId),
194
+ applyMailboxChange(client, mailboxId),
185
195
  });
186
196
  }
187
197
 
188
- export function useSetMessageRead() {
198
+ /** `scope.mailboxId` keys it as an own change of the mailbox (`ownChangeKey`). */
199
+ export function useSetMessageRead(scope: { mailboxId: string | undefined }) {
189
200
  const client = useQueryClient();
190
201
  const { user } = useAuth();
191
202
 
192
203
  return useMutation({
204
+ mutationKey: ownChangeKey(scope.mailboxId),
193
205
  mutationFn: async ({
194
206
  id,
195
207
  mailboxId,
@@ -207,22 +219,25 @@ export function useSetMessageRead() {
207
219
  json: { isRead },
208
220
  })
209
221
  ),
210
- // The change is applied to the cached conversation, lists and counts;
211
- // a refetch is needed only when the cache was not exactly one change behind.
212
- onSuccess: ({ version }, { id, mailboxId, conversationId, isRead }) => {
213
- const applied =
214
- user &&
215
- applyOwnReadChange(client, {
216
- mailboxId,
217
- conversationId,
218
- messageId: id,
219
- isRead,
220
- version,
221
- reader: { userId: user.id, name: user.name },
222
- });
223
-
224
- if (!applied) scheduleMailboxRefresh(client, mailboxId);
225
- },
222
+ // The message, its row and the counts change at once; the answer's
223
+ // versions say the cache has the change, so its echo reads nothing.
224
+ onMutate: ({ id, mailboxId, conversationId, isRead }) =>
225
+ beginOwnChange(client, mailboxId, {
226
+ type: "message-read",
227
+ conversationId,
228
+ messageId: id,
229
+ isRead,
230
+ reader: user ? { userId: user.id, name: user.name } : undefined,
231
+ }),
232
+ onError: (_error, input, change) =>
233
+ ownChangeFailed(client, input.mailboxId, change),
234
+ onSuccess: ({ previousVersion, version }, input, change) =>
235
+ ownChangeDone(client, input.mailboxId, change, {
236
+ previousVersion,
237
+ version,
238
+ }),
239
+ onSettled: (_result, _error, input) =>
240
+ ownChangeSettled(client, input.mailboxId),
226
241
  });
227
242
  }
228
243
 
@@ -234,46 +249,141 @@ export type BulkConversationAction =
234
249
  mailboxState?: "active" | "archive" | "spam" | "trash";
235
250
  folderId?: string | null;
236
251
  };
252
+ /** Puts them back as an Undo: no spam verdict, nobody trusted. */
253
+ undo?: boolean;
237
254
  }
255
+ /** Report spam of each conversation's received mail. */
256
+ | { type: "report" }
257
+ /** Not spam for each conversation's received mail, out of Spam. */
258
+ | { type: "not_spam" }
238
259
  | { type: "delete_permanently" };
239
260
 
240
- export function useBulkConversationAction() {
261
+ export type BulkConversationInput = {
262
+ mailboxId: string;
263
+ /** Where the conversations were acted on: only those still there change. */
264
+ sourceFolderId: string;
265
+ conversationIds: string[];
266
+ action: BulkConversationAction;
267
+ undoSnapshot?: UndoSnapshot;
268
+ };
269
+
270
+ function filingUpdate(
271
+ action: Extract<
272
+ BulkConversationAction,
273
+ { type: "report" | "not_spam" | "update" }
274
+ >
275
+ ) {
276
+ if (action.type === "update") return action.update;
277
+
278
+ return {
279
+ mailboxState:
280
+ action.type === "report" ? ("spam" as const) : ("active" as const),
281
+ };
282
+ }
283
+
284
+ /** What a bulk action does to the cache at once (`beginOwnChange`). */
285
+ export function bulkOwnChange(
286
+ {
287
+ sourceFolderId,
288
+ conversationIds,
289
+ action,
290
+ undoSnapshot,
291
+ }: BulkConversationInput,
292
+ reader?: { userId: string; name: string }
293
+ ) {
294
+ if (action.type === "read") {
295
+ return {
296
+ type: "read",
297
+ sourceFolderId,
298
+ conversationIds,
299
+ isRead: action.isRead,
300
+ reader,
301
+ } satisfies OwnChange;
302
+ }
303
+
304
+ if (action.type === "delete_permanently") {
305
+ return { type: "delete", conversationIds } satisfies OwnChange;
306
+ }
307
+
308
+ // Report spam goes into Spam, Not spam out of it, as a move does.
309
+
310
+ return {
311
+ type: "move",
312
+ sourceFolderId,
313
+ conversationIds,
314
+ update: filingUpdate(action),
315
+ undoSnapshot,
316
+ } satisfies OwnChange;
317
+ }
318
+
319
+ /**
320
+ * Moving, marking, reporting and deleting conversations: shown at once
321
+ * (`beginOwnChange`) and put back if the server refuses. Every answer says
322
+ * how many changed and where they went.
323
+ */
324
+ export function useBulkConversationAction(scope: {
325
+ mailboxId: string | undefined;
326
+ }) {
241
327
  const client = useQueryClient();
328
+ const { user } = useAuth();
242
329
 
243
330
  return useMutation({
331
+ mutationKey: ownChangeKey(scope.mailboxId),
244
332
  mutationFn: async ({
245
333
  mailboxId,
246
334
  sourceFolderId,
247
335
  conversationIds,
248
336
  action,
249
- }: {
250
- mailboxId: string;
251
- sourceFolderId: string;
252
- conversationIds: string[];
253
- action: BulkConversationAction;
254
- }) => {
255
- if (action.type === "update") {
256
- return responseJson(
257
- await mailApi.api.mail.conversations.bulk.$patch({
258
- query: { mailboxId },
259
- json: {
260
- type: action.type,
261
- conversationIds,
262
- sourceFolderId,
263
- update: action.update,
264
- },
265
- })
266
- );
267
- }
337
+ }: BulkConversationInput) => {
338
+ const filing =
339
+ action.type === "report" ||
340
+ action.type === "not_spam" ||
341
+ action.type === "update";
342
+
343
+ const undo = action.type === "update" && action.undo;
268
344
 
269
345
  return responseJson(
270
346
  await mailApi.api.mail.conversations.bulk.$patch({
271
347
  query: { mailboxId },
272
- json: { conversationIds, ...action },
348
+ json: filing
349
+ ? {
350
+ type: "update",
351
+ conversationIds,
352
+ sourceFolderId,
353
+ update: filingUpdate(action),
354
+ undo,
355
+ offerUndo: !undo,
356
+ }
357
+ : { conversationIds, ...action },
273
358
  })
274
359
  );
275
360
  },
276
- onSuccess: (_result, { mailboxId }) =>
277
- scheduleMailboxRefresh(client, mailboxId),
361
+ onMutate: (input) =>
362
+ beginOwnChange(
363
+ client,
364
+ input.mailboxId,
365
+ bulkOwnChange(
366
+ input,
367
+ user ? { userId: user.id, name: user.name } : undefined
368
+ )
369
+ ),
370
+ onError: (_error, input, change) =>
371
+ ownChangeFailed(client, input.mailboxId, change),
372
+ onSuccess: (_result, { mailboxId, action }, change) => {
373
+ ownChangeDone(client, mailboxId, change);
374
+
375
+ // A verdict trusts senders (Not spam) and changes who the mailbox
376
+ // hears from.
377
+ if (action.type === "report" || action.type === "not_spam") {
378
+ void client.invalidateQueries({
379
+ queryKey: ["blocked-senders", mailboxId],
380
+ });
381
+ void client.invalidateQueries({
382
+ queryKey: ["recipient-suggestions", mailboxId],
383
+ });
384
+ }
385
+ },
386
+ onSettled: (_result, _error, input) =>
387
+ ownChangeSettled(client, input.mailboxId),
278
388
  });
279
389
  }
@@ -1,17 +1,19 @@
1
1
  import { responseJson } from "@open-workspace/core/ui";
2
2
  import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
3
+ import { toast } from "sonner";
3
4
 
4
5
  import { mailApi } from "../lib/mail-api";
5
- import { scheduleMailboxRefresh } from "./mail-query-cache";
6
+ import { blockedSendersQuery } from "./mail-queries";
7
+ import { applyMailboxChange } from "./mail-query-cache";
6
8
 
7
9
  const blockedKey = (mailboxId: string) =>
8
- ["blocked-senders", mailboxId] as const;
10
+ blockedSendersQuery(mailboxId).queryKey;
9
11
 
10
12
  function useMailboxRefresh() {
11
13
  const client = useQueryClient();
12
14
 
13
15
  return (mailboxId: string) => {
14
- scheduleMailboxRefresh(client, mailboxId);
16
+ applyMailboxChange(client, mailboxId);
15
17
  void client.invalidateQueries({ queryKey: blockedKey(mailboxId) });
16
18
  void client.invalidateQueries({
17
19
  queryKey: ["recipient-suggestions", mailboxId],
@@ -54,51 +56,30 @@ export function useReportMessage() {
54
56
  });
55
57
  }
56
58
 
57
- export function useBulkReport() {
58
- const refresh = useMailboxRefresh();
59
-
60
- return useMutation({
61
- mutationFn: async (input: {
62
- mailboxId: string;
63
- conversationIds: string[];
64
- }) =>
65
- responseJson(
66
- await mailApi.api.mail.conversations.report.$post({
67
- query: { mailboxId: input.mailboxId },
68
- json: { conversationIds: input.conversationIds },
69
- })
70
- ),
71
- onSuccess: (_result, { mailboxId }) => refresh(mailboxId),
72
- });
59
+ export function undoSpamDecision(mailboxId: string, undoId: string) {
60
+ return mailApi.api.mail.conversations["undo-spam"]
61
+ .$post({ query: { mailboxId }, json: { undoId } })
62
+ .then(responseJson);
73
63
  }
74
64
 
75
- export function useNotSpam() {
65
+ export function useUndoSpamDecision() {
76
66
  const refresh = useMailboxRefresh();
77
67
 
78
68
  return useMutation({
79
- mutationFn: async (input: { mailboxId: string; messageId: string }) =>
80
- responseJson(
81
- await mailApi.api.mail.messages[":id"]["not-spam"].$post({
82
- param: { id: input.messageId },
83
- query: { mailboxId: input.mailboxId },
84
- })
85
- ),
69
+ mutationFn: ({
70
+ mailboxId,
71
+ undoId,
72
+ }: {
73
+ mailboxId: string;
74
+ undoId: string;
75
+ }) => undoSpamDecision(mailboxId, undoId),
76
+ onError: (error) => toast.error(error.message),
86
77
  onSuccess: (_result, { mailboxId }) => refresh(mailboxId),
87
78
  });
88
79
  }
89
80
 
90
81
  export function useBlockedSenders(mailboxId: string, enabled: boolean) {
91
- return useQuery({
92
- queryKey: blockedKey(mailboxId),
93
- queryFn: async ({ signal }) =>
94
- responseJson(
95
- await mailApi.api.mail.mailboxes[":id"]["blocked-senders"].$get(
96
- { param: { id: mailboxId } },
97
- { init: { signal } }
98
- )
99
- ),
100
- enabled,
101
- });
82
+ return useQuery({ ...blockedSendersQuery(mailboxId), enabled });
102
83
  }
103
84
 
104
85
  export function useBlockSender() {
@@ -3,9 +3,11 @@ import type { SuccessfulResponse } from "@open-workspace/core/ui";
3
3
  import { useMutation, useQueryClient } from "@tanstack/react-query";
4
4
 
5
5
  import type { ReplyActionMode } from "../../shared/mail";
6
+ import type { StoredImageRef } from "../../shared/rich-content";
6
7
  import { mailApi } from "../lib/mail-api";
7
8
  import type { SubmittedComposerUpload } from "./composer-session";
8
- import { scheduleMailboxRefresh } from "./mail-query-cache";
9
+ import { applyMailboxChange } from "./mail-query-cache";
10
+ import { forgetDrafts } from "./use-drafts";
9
11
 
10
12
  export type SubmittedMessage = SuccessfulResponse<
11
13
  Awaited<ReturnType<typeof mailApi.api.mail.messages.$post>>
@@ -17,6 +19,11 @@ type MessageContent = {
17
19
  bodyText: string;
18
20
  bodyHtml?: string;
19
21
  uploadedAttachments?: SubmittedComposerUpload[];
22
+ /** Saved images the body shows by `cid:`, e.g. the signature's. */
23
+ storedImages?: StoredImageRef[];
24
+ /** The composer's draft, which the server deletes once the message is accepted. */
25
+ draftId?: string;
26
+ draftVersion?: number;
20
27
  };
21
28
 
22
29
  type MailSendInput =
@@ -37,6 +44,7 @@ type MailSendInput =
37
44
  })
38
45
  | (MessageContent & {
39
46
  kind: "forward";
47
+ forwardedAttachmentIds?: string[];
40
48
  sourceEmailId: string;
41
49
  to: string[];
42
50
  cc: string[];
@@ -50,10 +58,7 @@ type WithoutRequestId<Input> = Input extends MailSendInput
50
58
 
51
59
  export type MailSendDraft = WithoutRequestId<MailSendInput>;
52
60
 
53
- export function withMailSendRequestId(
54
- draft: MailSendDraft,
55
- requestId: string
56
- ): MailSendInput {
61
+ export function withMailSendRequestId(draft: MailSendDraft, requestId: string) {
57
62
  switch (draft.kind) {
58
63
  case "compose": {
59
64
  return { ...draft, requestId };
@@ -85,6 +90,9 @@ export function useMailSend() {
85
90
  bodyText: input.bodyText,
86
91
  bodyHtml: input.bodyHtml,
87
92
  attachments: input.uploadedAttachments ?? [],
93
+ storedImages: input.storedImages ?? [],
94
+ draftId: input.draftId,
95
+ draftVersion: input.draftVersion,
88
96
  };
89
97
 
90
98
  if (input.kind === "reply") {
@@ -106,6 +114,7 @@ export function useMailSend() {
106
114
  await mailApi.api.mail.messages[":id"].forward.$post({
107
115
  param: { id: input.sourceEmailId },
108
116
  json: {
117
+ forwardedAttachmentIds: input.forwardedAttachmentIds,
109
118
  ...request,
110
119
  to: input.to,
111
120
  cc: input.cc,
@@ -133,8 +142,11 @@ export function useMailSend() {
133
142
  // message id from it, so a lost response cannot create a second message.
134
143
  retry: (failureCount, error) =>
135
144
  failureCount < 1 && (!(error instanceof ApiError) || error.status >= 500),
145
+ // Here, not in the composer: it may be gone (the person moved on) by
146
+ // the time the message is sent, and its draft must not come back.
136
147
  onSuccess: (_result, input) => {
137
- scheduleMailboxRefresh(client, input.mailboxId);
148
+ if (input.draftId) forgetDrafts(client, input.mailboxId, [input.draftId]);
149
+ else applyMailboxChange(client, input.mailboxId);
138
150
  },
139
151
  });
140
152
 
@@ -16,7 +16,7 @@ import { browserLanguage } from "./translation-language";
16
16
  function withTranslations(
17
17
  layout: PdfTranslationLayout,
18
18
  translations: readonly string[]
19
- ): PdfTranslationLayout {
19
+ ) {
20
20
  let index = 0;
21
21
 
22
22
  return {
@@ -0,0 +1,61 @@
1
+ import { useState } from "react";
2
+
3
+ import { recipientField } from "./composer-recipients";
4
+ import type {
5
+ ComposerRecipient,
6
+ RecipientFieldValue,
7
+ } from "./composer-recipients";
8
+
9
+ // A manually added address stays in Cc. A removed one stays out, even after switching back to Reply all.
10
+ function recordCcEdits(
11
+ before: ComposerRecipient[],
12
+ after: ComposerRecipient[],
13
+ edits = new Map<string, ComposerRecipient | null>()
14
+ ) {
15
+ const previous = new Set(before.map(({ address }) => address));
16
+ const current = new Set(after.map(({ address }) => address));
17
+ const updated = new Map(edits);
18
+
19
+ for (const address of previous) {
20
+ if (!current.has(address)) updated.set(address, null);
21
+ }
22
+
23
+ for (const recipient of after) {
24
+ if (!previous.has(recipient.address)) {
25
+ updated.set(recipient.address, recipient);
26
+ }
27
+ }
28
+
29
+ return updated;
30
+ }
31
+
32
+ export function useReplyCc(defaultAddresses: string[], saved?: string[]) {
33
+ const defaults = recipientField(defaultAddresses);
34
+
35
+ const [edits, setEdits] = useState(() => {
36
+ const restored = saved ? recipientField(saved) : defaults;
37
+
38
+ return recordCcEdits(defaults.recipients, restored.recipients);
39
+ });
40
+
41
+ const [typed, setTyped] = useState(() =>
42
+ saved ? recipientField(saved).input : null
43
+ );
44
+
45
+ const input = typed ?? defaults.input;
46
+
47
+ const recipients = [
48
+ ...defaults.recipients.filter(({ address }) => !edits.has(address)),
49
+ ...Array.from(edits.values()).filter((recipient) => recipient !== null),
50
+ ];
51
+
52
+ function onChange(next: RecipientFieldValue) {
53
+ if (next.recipients !== recipients) {
54
+ setEdits(recordCcEdits(recipients, next.recipients, edits));
55
+ }
56
+
57
+ if (next.input !== input) setTyped(next.input);
58
+ }
59
+
60
+ return { value: { recipients, input }, onChange };
61
+ }