@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
@@ -1,4 +1,10 @@
1
- import { Button, useConfirm } from "@open-workspace/core/ui";
1
+ import {
2
+ Button,
3
+ cn,
4
+ restoreFocus,
5
+ useConfirm,
6
+ useDelayedPending,
7
+ } from "@open-workspace/core/ui";
2
8
  import {
3
9
  ChevronUp,
4
10
  CircleAlert,
@@ -6,16 +12,21 @@ import {
6
12
  LoaderCircle,
7
13
  RotateCw,
8
14
  } from "lucide-react";
9
- import { useEffect, useRef, useState } from "react";
15
+ import { useQueryState } from "nuqs";
16
+ import { useEffect, useEffectEvent, useRef, useState } from "react";
10
17
  import { toast } from "sonner";
11
18
 
12
19
  import { baseSubject } from "../../shared/mail";
13
20
  import { ConversationLoadingSkeleton } from "./conversation-loading-skeleton";
14
- import { ConversationMessage } from "./conversation-message";
21
+ import {
22
+ ConversationHeading,
23
+ ConversationMessage,
24
+ } from "./conversation-message";
15
25
  import { ConversationReply, ReplyActionIcon } from "./conversation-reply";
16
- import type { SentReply } from "./conversation-reply";
26
+ import type { ReplyHandle, SentReply } from "./conversation-reply";
27
+ import { mailSearchParams } from "./mail-search-params";
17
28
  import { actionButtonLabel } from "./message-actions";
18
- import type { SenderHandlers } from "./message-actions";
29
+ import type { MessageHandlers } from "./message-actions";
19
30
  import {
20
31
  OriginalMessageSheet,
21
32
  originalSourceUrl,
@@ -27,17 +38,33 @@ import type {
27
38
  ConversationSummary,
28
39
  Mailbox,
29
40
  MessageDetail,
41
+ ReplySource,
42
+ ReplyAction,
30
43
  ReplyActionMode,
31
44
  } from "./types";
45
+ import { useDiscardDraft } from "./use-drafts";
46
+ import type { SavedDraft } from "./use-drafts";
32
47
  import { useResendMessage, useSetMessageRead } from "./use-mail-data";
33
48
 
49
+ /** How long a sent reply's box waits for the reply to show in the thread. */
50
+ const SENT_REPLY_WAIT_MS = 5000;
51
+
34
52
  type ReplySelection = {
35
53
  parentId: string;
36
54
  mode: ReplyActionMode;
55
+ /** The saved draft it reopened, if it did. */
56
+ draftId?: string;
37
57
  };
38
58
 
39
59
  export function ConversationView({
40
60
  messages,
61
+ matchId,
62
+ matchQuoted,
63
+ pinnedId,
64
+ replyDraft,
65
+ replyDraftSource,
66
+ refreshing,
67
+ openFromDrafts,
41
68
  messageCount,
42
69
  hasOlderMessages,
43
70
  loadingOlder,
@@ -54,6 +81,13 @@ export function ConversationView({
54
81
  summary,
55
82
  }: {
56
83
  messages: MessageDetail[];
84
+ matchId?: string;
85
+ matchQuoted: boolean;
86
+ pinnedId?: string;
87
+ replyDraft: SavedDraft | null;
88
+ replyDraftSource: ReplySource | null;
89
+ refreshing: boolean;
90
+ openFromDrafts: boolean;
57
91
  messageCount: number;
58
92
  hasOlderMessages: boolean;
59
93
  loadingOlder: boolean;
@@ -61,7 +95,6 @@ export function ConversationView({
61
95
  loading: boolean;
62
96
  error?: string;
63
97
  mailbox?: Mailbox;
64
- folderName: string;
65
98
  onRetry: () => void;
66
99
  onLoadOlder: () => void;
67
100
  onForward: (message: MessageDetail) => void;
@@ -77,11 +110,97 @@ export function ConversationView({
77
110
  null
78
111
  );
79
112
 
80
- const [originalMessageId, setOriginalMessageId] = useState<string | null>(
81
- null
113
+ // The original-message sheet is ?original=<message id>, opened as an
114
+ // entry of its own, so the system Back button closes it.
115
+ const [originalMessageId, setOriginalMessageId] = useQueryState(
116
+ "original",
117
+ mailSearchParams.original
118
+ );
119
+
120
+ // The shortcut opens it where the conversation is now.
121
+ const openOriginal = useEffectEvent(
122
+ (id: string) => void setOriginalMessageId(id)
82
123
  );
83
124
 
84
125
  const replyRegion = useRef<HTMLDivElement>(null);
126
+ const reply = useRef<ReplyHandle>(null);
127
+ const discardDraft = useDiscardDraft();
128
+
129
+ function replyParent(id?: string | null) {
130
+ const parent =
131
+ messages.find((message) => message.id === id) ??
132
+ (replyDraftSource?.id === id ? replyDraftSource : null);
133
+
134
+ return parent?.replyPlan.actions.length ? parent : null;
135
+ }
136
+
137
+ const replyDraftParent = replyParent(replyDraft?.sourceEmailId);
138
+
139
+ const [openedDraftId, setOpenedDraftId] = useState<string | null>(null);
140
+
141
+ // While reopening a cached reply, keep its box in place. If a different
142
+ // revision comes back, start the editable session from that revision.
143
+ const [checkingVersion, setCheckingVersion] = useState(
144
+ refreshing && replyDraft ? replyDraft.version : null
145
+ );
146
+
147
+ const [editorRevision, setEditorRevision] = useState(0);
148
+
149
+ if (checkingVersion !== null && !refreshing) {
150
+ setCheckingVersion(null);
151
+
152
+ if (replyDraft && replyDraft.version !== checkingVersion) {
153
+ setEditorRevision(replyDraft.version);
154
+ setOpenedDraftId(null);
155
+ setReplySelection(null);
156
+ }
157
+ }
158
+
159
+ // A sent reply's box, kept (inert) until the message is in the thread,
160
+ // then both change at once: the conversation keeps its height and scroll.
161
+ const [sentReplyId, setSentReplyId] = useState<string | null>(null);
162
+ // The sent box's height, which the Reply buttons in its place keep: the
163
+ // reply shows above them and nothing scrolls back up as the box goes.
164
+ const [sentBoxHeight, setSentBoxHeight] = useState<number | null>(null);
165
+
166
+ if (sentReplyId && messages.some((message) => message.id === sentReplyId)) {
167
+ setSentReplyId(null);
168
+ setReplySelection(null);
169
+ }
170
+
171
+ // Not shown soon (the conversation couldn't be read again): the box goes.
172
+ useEffect(() => {
173
+ if (!sentReplyId) return;
174
+
175
+ const timer = setTimeout(() => {
176
+ setSentReplyId(null);
177
+ setReplySelection(null);
178
+ }, SENT_REPLY_WAIT_MS);
179
+
180
+ return () => clearTimeout(timer);
181
+ }, [sentReplyId]);
182
+
183
+ if (
184
+ replyDraft?.replyMode &&
185
+ replyDraftParent &&
186
+ !replySelection &&
187
+ openedDraftId !== replyDraft.id
188
+ ) {
189
+ setOpenedDraftId(replyDraft.id);
190
+ setReplySelection({
191
+ parentId: replyDraftParent.id,
192
+ mode: replyDraft.replyMode,
193
+ draftId: replyDraft.id,
194
+ });
195
+ }
196
+
197
+ // The reopened draft was discarded elsewhere (the Drafts list, another
198
+ // tab): its reply box closes rather than save into a draft that's gone.
199
+ const reopenedDraftId = replySelection?.draftId;
200
+
201
+ if (reopenedDraftId && !sentReplyId && replyDraft?.id !== reopenedDraftId) {
202
+ setReplySelection(null);
203
+ }
85
204
 
86
205
  const canShowOriginal = (message: MessageDetail) =>
87
206
  Boolean(mailbox && message.hasOriginal);
@@ -90,15 +209,20 @@ export function ConversationView({
90
209
  const shortcutTarget = [...messages].reverse().find(canShowOriginal);
91
210
  const shortcutTargetId = shortcutTarget?.id;
92
211
  const resendMessage = useResendMessage();
93
- const setMessageRead = useSetMessageRead();
212
+ const setMessageRead = useSetMessageRead({ mailboxId: mailbox?.id });
94
213
 
95
- // Bring the composer into view and focus it whenever a reply target is picked.
214
+ // Picking Reply or opening a Drafts row takes the person to the editor.
215
+ // Reopening an ordinary conversation keeps the reader where it opened.
96
216
  useEffect(() => {
97
- if (!replySelection) return;
217
+ if (!replySelection || checkingVersion !== null) return;
218
+
219
+ if (replySelection.draftId && !openFromDrafts) return;
98
220
  const region = replyRegion.current;
99
221
  region?.scrollIntoView({ block: "nearest", behavior: "smooth" });
100
- region?.querySelector("textarea")?.focus({ preventScroll: true });
101
- }, [replySelection]);
222
+ region
223
+ ?.querySelector<HTMLElement>("[data-composer-editor-content]")
224
+ ?.focus({ preventScroll: true });
225
+ }, [replySelection, checkingVersion, openFromDrafts]);
102
226
 
103
227
  useEffect(() => {
104
228
  if (!shortcutTargetId) return;
@@ -115,7 +239,7 @@ export function ConversationView({
115
239
  }
116
240
 
117
241
  event.preventDefault();
118
- setOriginalMessageId(targetId);
242
+ openOriginal(targetId);
119
243
  };
120
244
 
121
245
  window.addEventListener("keydown", onKeyDown);
@@ -123,9 +247,16 @@ export function ConversationView({
123
247
  return () => window.removeEventListener("keydown", onKeyDown);
124
248
  }, [shortcutTargetId]);
125
249
  const { confirm, dialog: confirmDialog } = useConfirm();
250
+ const placeholders = useDelayedPending(loading);
126
251
 
127
- if (loading) {
128
- return <ConversationLoadingSkeleton summary={summary} />;
252
+ if (loading || placeholders) {
253
+ return (
254
+ <ConversationLoadingSkeleton
255
+ summary={summary}
256
+ mailbox={mailbox}
257
+ placeholders={placeholders}
258
+ />
259
+ );
129
260
  }
130
261
 
131
262
  if (error) {
@@ -160,12 +291,7 @@ export function ConversationView({
160
291
  const { conversationId } = latest;
161
292
  const conversationSubject = baseSubject(latest.subject) || "(no subject)";
162
293
 
163
- const selectedParent =
164
- messages.find(
165
- (message) =>
166
- message.id === replySelection?.parentId &&
167
- message.replyPlan.actions.length > 0
168
- ) ?? null;
294
+ const selectedParent = replyParent(replySelection?.parentId);
169
295
 
170
296
  const selectedMode =
171
297
  selectedParent && replySelection ? replySelection.mode : null;
@@ -179,10 +305,71 @@ export function ConversationView({
179
305
  replyCandidates.find((message) => message.direction === "incoming") ??
180
306
  replyCandidates[0];
181
307
 
308
+ /**
309
+ * Opens the reply box on `next`. Replying to another message than the
310
+ * draft here would replace the draft, so that asks first.
311
+ */
312
+ async function startReply(next: ReplySelection) {
313
+ const open = replySelection ? reply.current : null;
314
+ const replacing = replySelection?.parentId !== next.parentId;
315
+
316
+ // A draft here whose message is not shown (an older page) has no box.
317
+ const hidden =
318
+ !open && replyDraft && replyDraft.sourceEmailId !== next.parentId
319
+ ? replyDraft
320
+ : null;
321
+
322
+ if (replacing && (open?.hasContent() || hidden)) {
323
+ if (
324
+ !(await confirm({
325
+ title: "Discard your draft reply?",
326
+ description:
327
+ "This conversation has one reply draft. Replying to another message deletes the one you started.",
328
+ action: "Discard draft",
329
+ destructive: true,
330
+ }))
331
+ ) {
332
+ return;
333
+ }
334
+
335
+ try {
336
+ if (open) await open.discard();
337
+ else if (hidden && mailbox) {
338
+ await discardDraft(mailbox.id, hidden.id, hidden.version);
339
+ }
340
+ } catch (discardError) {
341
+ toast.error(
342
+ discardError instanceof Error
343
+ ? discardError.message
344
+ : "Could not discard the draft"
345
+ );
346
+
347
+ return;
348
+ }
349
+ }
350
+
351
+ setSentBoxHeight(null);
352
+ setReplySelection(
353
+ replacing ? next : { ...next, draftId: replySelection?.draftId }
354
+ );
355
+ }
356
+
182
357
  function handleSent(result: SentReply) {
183
- setReplySelection(null);
358
+ // A detached reply starts its own conversation, and a reply from mail kept
359
+ // out of its conversation as spam takes it back there: either way the
360
+ // open one is not where the reply is (it may be gone).
361
+ if (result.detached || result.conversationId !== conversationId) {
362
+ setReplySelection(null);
363
+ onOpenConversation(result.conversationId);
184
364
 
185
- if (result.detached) onOpenConversation(result.conversationId);
365
+ return;
366
+ }
367
+
368
+ // Here, the box stays until the reply shows in its place
369
+ // (`sentReplyId`), so nothing above it moves; focus goes to Reply.
370
+ setSentReplyId(result.messageId);
371
+ setSentBoxHeight(replyRegion.current?.offsetHeight ?? null);
372
+ restoreFocus(() => replyRegion.current?.querySelector("button"));
186
373
  }
187
374
 
188
375
  async function resend(message: MessageDetail) {
@@ -236,54 +423,77 @@ export function ConversationView({
236
423
  });
237
424
  }
238
425
 
426
+ function replyToMessage(message: MessageDetail, action: ReplyAction) {
427
+ void startReply({ parentId: message.id, mode: action.mode });
428
+ }
429
+
430
+ const canSend = Boolean(mailbox?.canSend);
431
+
432
+ const send: MessageHandlers["send"] = canSend
433
+ ? { reply: replyToMessage, forward: onForward, resend }
434
+ : undefined;
435
+
436
+ const earlierButton = hasOlderMessages ? (
437
+ <div className="border-border flex justify-center border-b py-2">
438
+ <Button
439
+ type="button"
440
+ variant="ghost"
441
+ size="xs"
442
+ className="text-muted-foreground"
443
+ disabled={loadingOlder}
444
+ onClick={onLoadOlder}
445
+ >
446
+ {loadingOlder ? (
447
+ <LoaderCircle className="animate-spin" />
448
+ ) : (
449
+ <ChevronUp />
450
+ )}
451
+ {olderMessagesError
452
+ ? "Retry earlier messages"
453
+ : "Load earlier messages"}
454
+ </Button>
455
+ </div>
456
+ ) : null;
457
+
239
458
  return (
240
- <article className="mx-auto w-full max-w-[60rem] pb-10">
459
+ <article className="group/conversation mx-auto w-full max-w-[60rem] pb-10">
241
460
  {confirmDialog}
242
- <header className="px-4 pt-5 pb-4 sm:px-6 sm:pt-6">
243
- <h1 className="text-[1.3125rem] leading-[1.3] font-semibold tracking-[-0.018em] text-balance [overflow-wrap:anywhere]">
244
- {conversationSubject}
245
- </h1>
246
- {messageCount > 1 && (
247
- <p className="text-muted-foreground mt-1 text-xs">
248
- {messageCount} messages
249
- </p>
250
- )}
251
- </header>
461
+ <ConversationHeading
462
+ subject={conversationSubject}
463
+ messageCount={messageCount}
464
+ />
252
465
 
253
466
  <div className="border-border border-y">
254
- {hasOlderMessages ? (
255
- <div className="border-border flex justify-center border-b py-2">
256
- <Button
257
- type="button"
258
- variant="ghost"
259
- size="xs"
260
- className="text-muted-foreground"
261
- disabled={loadingOlder}
262
- onClick={onLoadOlder}
263
- >
264
- {loadingOlder ? (
265
- <LoaderCircle className="animate-spin" />
266
- ) : (
267
- <ChevronUp />
268
- )}
269
- {olderMessagesError
270
- ? "Retry earlier messages"
271
- : "Load earlier messages"}
272
- </Button>
273
- </div>
274
- ) : null}
467
+ {!pinnedId && earlierButton}
275
468
 
276
469
  {messages.map((message, index) => {
277
- const previous = messages[index - 1];
470
+ const previous =
471
+ messages[index - 1]?.id === pinnedId
472
+ ? undefined
473
+ : messages[index - 1];
474
+
475
+ const target =
476
+ message.id === matchId
477
+ ? matchQuoted
478
+ ? "quote"
479
+ : "message"
480
+ : undefined;
481
+
278
482
  const audience = previous ? audienceChange(previous, message) : null;
279
483
 
280
484
  return (
281
485
  <div key={message.id} className="contents">
282
486
  {audience && <AudienceChange text={audience} />}
283
487
  <ConversationMessage
488
+ key={target ?? "normal"}
284
489
  message={message}
285
490
  mailbox={mailbox}
286
- defaultExpanded={message === latest || !message.isRead}
491
+ searchTarget={target}
492
+ defaultExpanded={
493
+ message.id === matchId ||
494
+ message === latest ||
495
+ !message.isRead
496
+ }
287
497
  collapsible={messages.length > 1}
288
498
  showViewedBy={mailbox?.kind === "shared"}
289
499
  resendPending={
@@ -292,15 +502,9 @@ export function ConversationView({
292
502
  }
293
503
  handlers={{
294
504
  onViewed: () => markViewed(message.id),
295
- onReply: (action) =>
296
- setReplySelection({
297
- parentId: message.id,
298
- mode: action.mode,
299
- }),
300
- onForward: () => onForward(message),
301
- onResend: () => resend(message),
505
+ send,
302
506
  onShowOriginal: canShowOriginal(message)
303
- ? () => setOriginalMessageId(message.id)
507
+ ? () => void setOriginalMessageId(message.id)
304
508
  : undefined,
305
509
  originalUrl:
306
510
  mailbox && message.hasOriginal
@@ -314,19 +518,46 @@ export function ConversationView({
314
518
  : undefined,
315
519
  sender:
316
520
  message.direction === "incoming" && senderActions && mailbox
317
- ? senderHandlers(message, senderActions, reportable)
521
+ ? senderHandlers(
522
+ message,
523
+ senderActions,
524
+ reportable,
525
+ mailbox.canOrganize
526
+ )
318
527
  : undefined,
319
528
  }}
320
529
  />
530
+ {message.id === pinnedId && earlierButton}
321
531
  </div>
322
532
  );
323
533
  })}
324
534
  </div>
325
535
 
326
- <div ref={replyRegion} className="scroll-mb-4 px-4 pt-4 sm:px-6">
327
- {selectedParent && selectedMode ? (
536
+ <div
537
+ // The reply box and the buttons in its place are blocks of their
538
+ // own: one replaces the other, nothing on screen moves.
539
+ key={selectedParent && selectedMode ? "reply" : "actions"}
540
+ ref={replyRegion}
541
+ inert={Boolean(sentReplyId) || checkingVersion !== null}
542
+ style={
543
+ sentBoxHeight === null ? undefined : { minHeight: sentBoxHeight }
544
+ }
545
+ className={cn(
546
+ "scroll-mb-4 px-4 pt-4 sm:px-6",
547
+ // The reply buttons show once the bodies above are laid out,
548
+ // rather than under their placeholders and then pushed down.
549
+ !(selectedParent && selectedMode) &&
550
+ "group-has-[[data-body-placeholder]]/conversation:invisible"
551
+ )}
552
+ >
553
+ {canSend && selectedParent && selectedMode ? (
328
554
  <ConversationReply
329
- key={selectedParent.id}
555
+ key={
556
+ reopenedDraftId
557
+ ? `${reopenedDraftId}:${editorRevision}`
558
+ : selectedParent.id
559
+ }
560
+ ref={reply}
330
561
  mailbox={mailbox}
331
562
  parent={selectedParent}
332
563
  mode={selectedMode}
@@ -334,12 +565,18 @@ export function ConversationView({
334
565
  setReplySelection({
335
566
  parentId: selectedParent.id,
336
567
  mode,
568
+ draftId: replySelection?.draftId,
337
569
  })
338
570
  }
339
571
  onCancel={() => setReplySelection(null)}
340
572
  onSent={handleSent}
573
+ draft={
574
+ replyDraft && replyDraft.id === reopenedDraftId
575
+ ? replyDraft
576
+ : undefined
577
+ }
341
578
  />
342
- ) : mailbox?.canSend ? (
579
+ ) : canSend ? (
343
580
  <div className="flex flex-wrap items-center gap-2">
344
581
  {replyTarget?.replyPlan.actions.map((action) => (
345
582
  <Button
@@ -347,12 +584,7 @@ export function ConversationView({
347
584
  type="button"
348
585
  variant="outline"
349
586
  size="sm"
350
- onClick={() =>
351
- setReplySelection({
352
- parentId: replyTarget.id,
353
- mode: action.mode,
354
- })
355
- }
587
+ onClick={() => replyToMessage(replyTarget, action)}
356
588
  >
357
589
  <ReplyActionIcon mode={action.mode} />
358
590
  {actionButtonLabel(action)}
@@ -375,7 +607,7 @@ export function ConversationView({
375
607
  message={
376
608
  messages.find((message) => message.id === originalMessageId) ?? null
377
609
  }
378
- onClose={() => setOriginalMessageId(null)}
610
+ onClose={() => void setOriginalMessageId(null)}
379
611
  />
380
612
  )}
381
613
  </article>
@@ -385,18 +617,21 @@ export function ConversationView({
385
617
  function senderHandlers(
386
618
  message: MessageDetail,
387
619
  actions: SenderActions,
388
- reportable: boolean
389
- ): SenderHandlers {
620
+ reportable: boolean,
621
+ canOrganize: boolean
622
+ ) {
390
623
  return {
391
624
  onUnsubscribe: canUnsubscribe(message)
392
625
  ? () => actions.open("unsubscribe", message)
393
626
  : undefined,
394
- onBlock: () => actions.open("block", message),
395
- onUnblock: () => actions.unblockSender(message),
627
+ onBlock: canOrganize ? () => actions.open("block", message) : undefined,
628
+ onFilter: canOrganize ? () => actions.open("filter", message) : undefined,
629
+ onUnblock: canOrganize ? () => actions.unblockSender(message) : undefined,
396
630
  onReportSpam: reportable ? () => actions.reportSpam(message) : undefined,
397
- onTrust: message.sender?.trustedAt
398
- ? undefined
399
- : () => actions.trustSender(message),
631
+ onTrust:
632
+ !canOrganize || message.sender?.trustedAt
633
+ ? undefined
634
+ : () => actions.trustSender(message),
400
635
  };
401
636
  }
402
637
 
@@ -13,13 +13,25 @@ import type { LucideIcon } from "lucide-react";
13
13
 
14
14
  import type { MessageDetail, RecipientDeliveryStatus } from "./types";
15
15
 
16
+ /** What the indicator reads: a message's, or the list row's for its latest message. */
17
+ type DeliveryFacts = Pick<
18
+ MessageDetail,
19
+ | "direction"
20
+ | "toAddresses"
21
+ | "ccAddresses"
22
+ | "bccAddresses"
23
+ | "transportState"
24
+ | "transportError"
25
+ | "deliveryStatuses"
26
+ >;
27
+
16
28
  type Indicator = {
17
29
  Icon: LucideIcon;
18
30
  label: string;
19
31
  tone: "muted" | "warning" | "danger";
20
32
  };
21
33
 
22
- function recipients(message: MessageDetail) {
34
+ function recipients(message: DeliveryFacts) {
23
35
  return [
24
36
  ...new Set(
25
37
  [
@@ -31,20 +43,25 @@ function recipients(message: MessageDetail) {
31
43
  ];
32
44
  }
33
45
 
34
- function deliveryIndicator(
35
- message: MessageDetail,
36
- totalRecipients: number
37
- ): Indicator {
46
+ function deliveryIndicator(message: DeliveryFacts, totalRecipients: number) {
38
47
  if (message.transportState === "unconfirmed") {
39
- return { Icon: CircleAlert, label: "Unconfirmed", tone: "warning" };
48
+ return {
49
+ Icon: CircleAlert,
50
+ label: "Unconfirmed",
51
+ tone: "warning",
52
+ } satisfies Indicator;
40
53
  }
41
54
 
42
55
  if (message.transportState === "failed") {
43
- return { Icon: CircleAlert, label: "Not sent", tone: "danger" };
56
+ return {
57
+ Icon: CircleAlert,
58
+ label: "Not sent",
59
+ tone: "danger",
60
+ } satisfies Indicator;
44
61
  }
45
62
 
46
63
  if (!message.deliveryStatuses.length) {
47
- return { Icon: Check, label: "Sent", tone: "muted" };
64
+ return { Icon: Check, label: "Sent", tone: "muted" } satisfies Indicator;
48
65
  }
49
66
 
50
67
  const delivered = message.deliveryStatuses.filter(
@@ -69,11 +86,15 @@ function deliveryIndicator(
69
86
  ? "Not delivered"
70
87
  : "Marked as spam",
71
88
  tone: delivered > 0 ? "warning" : "danger",
72
- };
89
+ } satisfies Indicator;
73
90
  }
74
91
 
75
92
  if (totalRecipients > 0 && delivered >= totalRecipients) {
76
- return { Icon: CheckCheck, label: "Delivered", tone: "muted" };
93
+ return {
94
+ Icon: CheckCheck,
95
+ label: "Delivered",
96
+ tone: "muted",
97
+ } satisfies Indicator;
77
98
  }
78
99
 
79
100
  return {
@@ -83,7 +104,7 @@ function deliveryIndicator(
83
104
  ? `${delivered}/${totalRecipients} delivered`
84
105
  : "Delivering",
85
106
  tone: "muted",
86
- };
107
+ } satisfies Indicator;
87
108
  }
88
109
 
89
110
  const recipientStatusIcons: Record<
@@ -156,7 +177,7 @@ export function DeliveryIndicator({
156
177
  message,
157
178
  className,
158
179
  }: {
159
- message: MessageDetail;
180
+ message: DeliveryFacts;
160
181
  className?: string;
161
182
  }) {
162
183
  if (message.direction !== "outgoing") return null;