@open-workspace/email 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (221) hide show
  1. package/README.md +22 -22
  2. package/alchemy/index.ts +273 -0
  3. package/drizzle/mailbox/0000_puzzling_dracula.sql +149 -0
  4. package/drizzle/mailbox/0001_mute_jack_flag.sql +12 -0
  5. package/drizzle/mailbox/meta/0000_snapshot.json +1002 -0
  6. package/drizzle/mailbox/meta/0001_snapshot.json +1002 -0
  7. package/drizzle/mailbox/meta/_journal.json +20 -0
  8. package/drizzle/mailbox/migrations.js +12 -0
  9. package/index.ts +3 -1
  10. package/migrations/{0000_email.sql → 0000_dry_moon_knight.sql} +26 -20
  11. package/migrations/meta/0000_snapshot.json +125 -109
  12. package/migrations/meta/_journal.json +2 -2
  13. package/package.json +74 -68
  14. package/shared/attachment-preview.ts +37 -27
  15. package/shared/folder-order.ts +106 -0
  16. package/shared/mail-authentication.ts +8 -3
  17. package/shared/mail-feedback.ts +18 -25
  18. package/shared/mail-headers.ts +114 -39
  19. package/shared/mail-import.ts +38 -0
  20. package/shared/mail-path.ts +42 -0
  21. package/shared/mail-received.ts +174 -31
  22. package/shared/mail-remote.ts +59 -2
  23. package/shared/mail-sender-intel.ts +22 -4
  24. package/shared/mail.ts +77 -28
  25. package/shared/module.ts +33 -0
  26. package/shared/office-preview.ts +14 -2
  27. package/shared/pdfjs-assets.ts +35 -0
  28. package/shared/permissions.ts +73 -0
  29. package/shared/webhooks.ts +41 -0
  30. package/src/admin/blocked-senders.tsx +189 -0
  31. package/src/client.tsx +97 -0
  32. package/src/lib/mail-api.ts +9 -0
  33. package/src/mail/attachment-preview-dialog.tsx +161 -0
  34. package/src/mail/blocked-senders.tsx +138 -0
  35. package/src/mail/compose-window-chrome.tsx +194 -0
  36. package/src/mail/compose-window.tsx +486 -0
  37. package/src/mail/composer-asset-bookmarks.ts +409 -0
  38. package/src/mail/composer-asset.tsx +229 -0
  39. package/src/mail/composer-attachment-list.tsx +138 -0
  40. package/src/mail/composer-editor-document.ts +65 -0
  41. package/src/mail/composer-editor.tsx +284 -0
  42. package/src/mail/composer-linked-attachment-text.ts +12 -0
  43. package/src/mail/composer-recipient-fields.tsx +165 -0
  44. package/src/mail/composer-recipients.ts +208 -0
  45. package/src/mail/composer-session.ts +562 -0
  46. package/src/mail/composer-toolbar.tsx +303 -0
  47. package/src/mail/composer-upload-client.ts +105 -0
  48. package/src/mail/composer-upload-progress.ts +53 -0
  49. package/src/mail/conversation-list.tsx +550 -0
  50. package/src/mail/conversation-loading-skeleton.tsx +119 -0
  51. package/src/mail/conversation-message.tsx +346 -0
  52. package/src/mail/conversation-reply.tsx +470 -0
  53. package/src/mail/conversation-selection-toolbar.tsx +322 -0
  54. package/src/mail/conversation-view.tsx +437 -0
  55. package/src/mail/delivery-indicator.tsx +272 -0
  56. package/src/mail/email-html-body.tsx +203 -0
  57. package/src/mail/folder-glyph.tsx +46 -0
  58. package/src/mail/folder-manager.tsx +532 -0
  59. package/src/mail/format-bytes.ts +7 -0
  60. package/src/mail/mail-app-bar.tsx +253 -0
  61. package/src/mail/mail-background.tsx +12 -0
  62. package/src/mail/mail-dock.tsx +137 -0
  63. package/src/mail/mail-folders.ts +44 -0
  64. package/src/mail/mail-layout.css +236 -0
  65. package/src/mail/mail-location.ts +73 -0
  66. package/src/mail/mail-mailbox-sheet.tsx +275 -0
  67. package/src/mail/mail-nav-rail.tsx +129 -0
  68. package/src/mail/mail-nav.tsx +324 -0
  69. package/src/mail/mail-prefetch.ts +59 -0
  70. package/src/mail/mail-primitives.tsx +66 -0
  71. package/src/mail/mail-queries.ts +209 -0
  72. package/src/mail/mail-query-cache.ts +486 -0
  73. package/src/mail/mail-seed.ts +47 -0
  74. package/src/mail/mail-shell.tsx +126 -0
  75. package/src/mail/mailbox-ai-settings.tsx +185 -0
  76. package/src/mail/mailbox-realtime.tsx +251 -0
  77. package/src/mail/mailbox-route.tsx +15 -0
  78. package/src/mail/message-actions.tsx +353 -0
  79. package/src/mail/message-attachments.tsx +200 -0
  80. package/src/mail/message-format.ts +75 -0
  81. package/src/mail/message-header.tsx +260 -0
  82. package/src/mail/message-translation.tsx +225 -0
  83. package/src/mail/original-message-sheet.tsx +619 -0
  84. package/src/mail/original-message.ts +191 -0
  85. package/src/mail/outbound-notification.ts +53 -0
  86. package/src/mail/pane-splitter.tsx +117 -0
  87. package/src/mail/pdf-preview-toolbar.tsx +152 -0
  88. package/src/mail/pdf-preview.css +44 -0
  89. package/src/mail/pdf-preview.tsx +156 -0
  90. package/src/mail/pdf-translation-layout.ts +95 -0
  91. package/src/mail/print-message.ts +92 -0
  92. package/src/mail/reader-pane.tsx +535 -0
  93. package/src/mail/recipient-combobox.tsx +335 -0
  94. package/src/mail/recipient-field-row.tsx +34 -0
  95. package/src/mail/reply-to-warning.tsx +50 -0
  96. package/src/mail/sanitize-email-html.ts +430 -0
  97. package/src/mail/sender-actions.tsx +149 -0
  98. package/src/mail/sender-authentication.tsx +485 -0
  99. package/src/mail/sender-dialogs.tsx +384 -0
  100. package/src/mail/sender-security.tsx +545 -0
  101. package/src/mail/toolbar-tooltip.tsx +29 -0
  102. package/src/mail/translation-html.ts +49 -0
  103. package/src/mail/translation-language.ts +21 -0
  104. package/src/mail/types.ts +53 -0
  105. package/src/mail/unread-document-indicator.tsx +60 -0
  106. package/src/mail/use-compose-submission.ts +208 -0
  107. package/src/mail/use-composer-attachment-preflight.ts +184 -0
  108. package/src/mail/use-composer-submission.ts +117 -0
  109. package/src/mail/use-conversation-selection.ts +186 -0
  110. package/src/mail/use-mail-controller.ts +427 -0
  111. package/src/mail/use-mail-data.ts +279 -0
  112. package/src/mail/use-mail-safety.ts +155 -0
  113. package/src/mail/use-mail-send.ts +142 -0
  114. package/src/mail/use-pdf-translation-overlay.ts +156 -0
  115. package/src/mail/use-pdf-translation.ts +100 -0
  116. package/src/mail/use-pdf-viewer.ts +215 -0
  117. package/src/mail/use-reply-submission.ts +114 -0
  118. package/src/mail/use-sanitized-email-html.ts +20 -0
  119. package/src/mail/use-visible-message-read.ts +138 -0
  120. package/src/pwa/push-subscription.ts +151 -0
  121. package/src/settings/mail-apps-page.tsx +235 -0
  122. package/src/settings/notifications-page.tsx +291 -0
  123. package/worker/admin/blocked-sender-routes.ts +166 -0
  124. package/worker/admin/schemas.ts +11 -0
  125. package/worker/bindings.d.ts +14 -10
  126. package/worker/db/schema.ts +63 -40
  127. package/worker/jmap/eventsource.ts +84 -16
  128. package/worker/jmap/methods.ts +1630 -524
  129. package/worker/jmap/push-types.ts +27 -0
  130. package/worker/jmap/push.ts +140 -56
  131. package/worker/jmap/routes.ts +366 -105
  132. package/worker/mail/access.ts +258 -21
  133. package/worker/mail/ai-classification.ts +36 -20
  134. package/worker/mail/ai-translation.ts +13 -6
  135. package/worker/mail/auth-headers.ts +395 -137
  136. package/worker/mail/bimi.ts +200 -73
  137. package/worker/mail/bootstrap.ts +79 -0
  138. package/worker/mail/capped-body.ts +36 -0
  139. package/worker/mail/classification-signals.ts +35 -25
  140. package/worker/mail/conversation-cursor.ts +30 -19
  141. package/worker/mail/delivery-events.ts +88 -62
  142. package/worker/mail/delivery-facts.ts +46 -10
  143. package/worker/mail/demo-routes.ts +34 -10
  144. package/worker/mail/doh.ts +62 -23
  145. package/worker/mail/downloads.ts +75 -40
  146. package/worker/mail/edge-cache.ts +155 -0
  147. package/worker/mail/header-metadata.ts +88 -22
  148. package/worker/mail/impersonation.ts +160 -38
  149. package/worker/mail/import-routes.ts +313 -0
  150. package/worker/mail/inbound-delivery.ts +19 -9
  151. package/worker/mail/inbound-threading.ts +18 -8
  152. package/worker/mail/inbound.ts +82 -40
  153. package/worker/mail/ip-intel.ts +110 -27
  154. package/worker/mail/isolate-cache.ts +35 -0
  155. package/worker/mail/link-domains.ts +29 -7
  156. package/worker/mail/mailbox-deletion.ts +10 -4
  157. package/worker/mail/mailbox-directory.ts +16 -6
  158. package/worker/mail/mailing-list.ts +94 -39
  159. package/worker/mail/message-security.ts +90 -15
  160. package/worker/mail/message-view.ts +18 -6
  161. package/worker/mail/office-conversion.ts +31 -9
  162. package/worker/mail/office-preview-container.ts +40 -13
  163. package/worker/mail/office-preview-queue.ts +76 -32
  164. package/worker/mail/org-domain.ts +8 -1
  165. package/worker/mail/outbound-content.ts +71 -26
  166. package/worker/mail/outbound-delivery.ts +31 -17
  167. package/worker/mail/outbound-service.ts +48 -30
  168. package/worker/mail/outbound-threading.ts +30 -17
  169. package/worker/mail/outbound.ts +97 -70
  170. package/worker/mail/outgoing-context.ts +25 -15
  171. package/worker/mail/participants.ts +44 -29
  172. package/worker/mail/push-notifications.ts +271 -173
  173. package/worker/mail/quote-parser.ts +21 -10
  174. package/worker/mail/r2-presigned-urls.ts +31 -25
  175. package/worker/mail/receive.ts +23 -11
  176. package/worker/mail/recipients.ts +6 -1
  177. package/worker/mail/remote-proxy.ts +187 -31
  178. package/worker/mail/remote-routes.ts +87 -0
  179. package/worker/mail/remote-token.ts +104 -0
  180. package/worker/mail/repeat.ts +19 -0
  181. package/worker/mail/reply-plan.ts +62 -33
  182. package/worker/mail/rfc.ts +32 -7
  183. package/worker/mail/routes.ts +870 -541
  184. package/worker/mail/safety-routes.ts +328 -200
  185. package/worker/mail/schemas.ts +84 -105
  186. package/worker/mail/search.ts +1 -0
  187. package/worker/mail/security-settings.ts +41 -9
  188. package/worker/mail/sender-intel.ts +206 -51
  189. package/worker/mail/spamhaus.ts +92 -45
  190. package/worker/mail/unsubscribe.ts +76 -18
  191. package/worker/mail/uploads.ts +126 -93
  192. package/worker/mail/webhook-source.ts +35 -12
  193. package/worker/mail/workspace-safety.ts +97 -38
  194. package/worker/mailbox/demo-html.ts +7 -7
  195. package/worker/mailbox/demo-seed.ts +10 -44
  196. package/worker/mailbox/demo.ts +595 -225
  197. package/worker/mailbox/email-index.ts +53 -0
  198. package/worker/mailbox/folder-model.ts +53 -17
  199. package/worker/mailbox/import-placement.ts +88 -0
  200. package/worker/mailbox/inbox-counts.ts +106 -0
  201. package/worker/mailbox/index.ts +1734 -721
  202. package/worker/mailbox/jmap-store.ts +139 -35
  203. package/worker/mailbox/model.ts +8 -6
  204. package/worker/mailbox/office-preview-jobs.ts +56 -25
  205. package/worker/mailbox/schema.ts +87 -66
  206. package/worker/mailbox/senders.ts +176 -70
  207. package/worker/mcp/mail-action-tools.ts +128 -87
  208. package/worker/mcp/mail-compose-tools.ts +133 -82
  209. package/worker/mcp/mail-query-tools.ts +120 -71
  210. package/worker/mcp/mail-safety-tools.ts +321 -248
  211. package/worker/mcp/schemas.ts +43 -0
  212. package/worker/mcp/tools.ts +13 -10
  213. package/worker/module.ts +43 -18
  214. package/worker/sql.d.ts +8 -0
  215. package/alchemy.ts +0 -142
  216. package/containers/office-preview/Dockerfile +0 -10
  217. package/shared/mail-rules.ts +0 -156
  218. package/worker/admin/report-routes.ts +0 -164
  219. package/worker/mail/reported-messages.ts +0 -68
  220. package/worker/mailbox/migrations.ts +0 -10
  221. package/worker/mailbox/rules.ts +0 -110
@@ -0,0 +1,437 @@
1
+ import { Button, useConfirm } from "@open-workspace/core/ui";
2
+ import {
3
+ ChevronUp,
4
+ CircleAlert,
5
+ Forward,
6
+ LoaderCircle,
7
+ RotateCw,
8
+ } from "lucide-react";
9
+ import { useEffect, useRef, useState } from "react";
10
+ import { toast } from "sonner";
11
+
12
+ import { baseSubject } from "../../shared/mail";
13
+ import { ConversationLoadingSkeleton } from "./conversation-loading-skeleton";
14
+ import { ConversationMessage } from "./conversation-message";
15
+ import { ConversationReply, ReplyActionIcon } from "./conversation-reply";
16
+ import type { SentReply } from "./conversation-reply";
17
+ import { actionButtonLabel } from "./message-actions";
18
+ import type { SenderHandlers } from "./message-actions";
19
+ import {
20
+ OriginalMessageSheet,
21
+ originalSourceUrl,
22
+ } from "./original-message-sheet";
23
+ import { notifyOutboundResult } from "./outbound-notification";
24
+ import { canUnsubscribe } from "./sender-actions";
25
+ import type { SenderActions } from "./sender-actions";
26
+ import type {
27
+ ConversationSummary,
28
+ Mailbox,
29
+ MessageDetail,
30
+ ReplyActionMode,
31
+ } from "./types";
32
+ import { useResendMessage, useSetMessageRead } from "./use-mail-data";
33
+
34
+ type ReplySelection = {
35
+ parentId: string;
36
+ mode: ReplyActionMode;
37
+ };
38
+
39
+ export function ConversationView({
40
+ messages,
41
+ messageCount,
42
+ hasOlderMessages,
43
+ loadingOlder,
44
+ olderMessagesError,
45
+ loading,
46
+ error,
47
+ mailbox,
48
+ onRetry,
49
+ onLoadOlder,
50
+ onForward,
51
+ onOpenConversation,
52
+ senderActions,
53
+ reportable = false,
54
+ summary,
55
+ }: {
56
+ messages: MessageDetail[];
57
+ messageCount: number;
58
+ hasOlderMessages: boolean;
59
+ loadingOlder: boolean;
60
+ olderMessagesError: boolean;
61
+ loading: boolean;
62
+ error?: string;
63
+ mailbox?: Mailbox;
64
+ folderName: string;
65
+ onRetry: () => void;
66
+ onLoadOlder: () => void;
67
+ onForward: (message: MessageDetail) => void;
68
+ onOpenConversation: (conversationId: string) => void;
69
+ /** Unsubscribe, block, report and filter actions with their dialogs. */
70
+ senderActions?: SenderActions;
71
+ /** Reporting moves the conversation to Spam, so it fits received mail outside Spam and Trash. */
72
+ reportable?: boolean;
73
+ /** The list's row for this conversation, shown while it loads. */
74
+ summary?: ConversationSummary;
75
+ }) {
76
+ const [replySelection, setReplySelection] = useState<ReplySelection | null>(
77
+ null
78
+ );
79
+
80
+ const [originalMessageId, setOriginalMessageId] = useState<string | null>(
81
+ null
82
+ );
83
+
84
+ const replyRegion = useRef<HTMLDivElement>(null);
85
+
86
+ const canShowOriginal = (message: MessageDetail) =>
87
+ Boolean(mailbox && message.hasOriginal);
88
+
89
+ // ⌥⌘U (Ctrl+Alt+U elsewhere) opens the latest original in the thread.
90
+ const shortcutTarget = [...messages].reverse().find(canShowOriginal);
91
+ const shortcutTargetId = shortcutTarget?.id;
92
+ const resendMessage = useResendMessage();
93
+ const setMessageRead = useSetMessageRead();
94
+
95
+ // Bring the composer into view and focus it whenever a reply target is picked.
96
+ useEffect(() => {
97
+ if (!replySelection) return;
98
+ const region = replyRegion.current;
99
+ region?.scrollIntoView({ block: "nearest", behavior: "smooth" });
100
+ region?.querySelector("textarea")?.focus({ preventScroll: true });
101
+ }, [replySelection]);
102
+
103
+ useEffect(() => {
104
+ if (!shortcutTargetId) return;
105
+
106
+ const targetId = shortcutTargetId;
107
+
108
+ const onKeyDown = (event: KeyboardEvent) => {
109
+ if (
110
+ event.code !== "KeyU" ||
111
+ !event.altKey ||
112
+ !(event.metaKey || event.ctrlKey)
113
+ ) {
114
+ return;
115
+ }
116
+
117
+ event.preventDefault();
118
+ setOriginalMessageId(targetId);
119
+ };
120
+
121
+ window.addEventListener("keydown", onKeyDown);
122
+
123
+ return () => window.removeEventListener("keydown", onKeyDown);
124
+ }, [shortcutTargetId]);
125
+ const { confirm, dialog: confirmDialog } = useConfirm();
126
+
127
+ if (loading) {
128
+ return <ConversationLoadingSkeleton summary={summary} />;
129
+ }
130
+
131
+ if (error) {
132
+ return (
133
+ <section className="grid min-h-80 place-items-center px-6 py-16 text-center">
134
+ <div className="animate-fade-in">
135
+ <span className="bg-destructive/10 text-destructive mx-auto grid size-10 place-items-center rounded-lg">
136
+ <CircleAlert className="size-5" strokeWidth={1.75} />
137
+ </span>
138
+ <p className="mt-3.5 text-[14px] font-semibold">
139
+ Could not load this conversation
140
+ </p>
141
+ <p className="text-muted-foreground mx-auto mt-1 max-w-sm text-[13px] leading-5">
142
+ {error}
143
+ </p>
144
+ <Button
145
+ className="mt-4"
146
+ variant="outline"
147
+ size="sm"
148
+ onClick={onRetry}
149
+ >
150
+ <RotateCw /> Retry
151
+ </Button>
152
+ </div>
153
+ </section>
154
+ );
155
+ }
156
+
157
+ const latest = messages.at(-1);
158
+
159
+ if (!latest) return null;
160
+ const { conversationId } = latest;
161
+ const conversationSubject = baseSubject(latest.subject) || "(no subject)";
162
+
163
+ const selectedParent =
164
+ messages.find(
165
+ (message) =>
166
+ message.id === replySelection?.parentId &&
167
+ message.replyPlan.actions.length > 0
168
+ ) ?? null;
169
+
170
+ const selectedMode =
171
+ selectedParent && replySelection ? replySelection.mode : null;
172
+
173
+ // Reply from the end of the thread answers the latest incoming message when there is one.
174
+ const replyCandidates = [...messages]
175
+ .reverse()
176
+ .filter((message) => message.replyPlan.actions.length > 0);
177
+
178
+ const replyTarget =
179
+ replyCandidates.find((message) => message.direction === "incoming") ??
180
+ replyCandidates[0];
181
+
182
+ function handleSent(result: SentReply) {
183
+ setReplySelection(null);
184
+
185
+ if (result.detached) onOpenConversation(result.conversationId);
186
+ }
187
+
188
+ async function resend(message: MessageDetail) {
189
+ if (!mailbox) return;
190
+
191
+ if (
192
+ message.transportState === "unconfirmed" &&
193
+ !(await confirm({
194
+ title: "Send this message again?",
195
+ description:
196
+ "The first attempt was never confirmed, so it may already have been delivered. Sending again can deliver a duplicate.",
197
+ action: "Send again",
198
+ }))
199
+ ) {
200
+ return;
201
+ }
202
+
203
+ resendMessage.mutate(
204
+ { id: message.id, mailboxId: mailbox.id },
205
+ {
206
+ onSuccess: (result) => notifyOutboundResult(result, "message"),
207
+ onError: (failure) => toast.error(failure.message),
208
+ }
209
+ );
210
+ }
211
+
212
+ async function markUnread(messageId: string) {
213
+ if (!mailbox) return;
214
+
215
+ try {
216
+ await setMessageRead.mutateAsync({
217
+ id: messageId,
218
+ mailboxId: mailbox.id,
219
+ conversationId,
220
+ isRead: false,
221
+ });
222
+ } catch (failure) {
223
+ toast.error(
224
+ failure instanceof Error ? failure.message : "Could not mark as unread"
225
+ );
226
+ }
227
+ }
228
+
229
+ async function markViewed(messageId: string) {
230
+ if (!mailbox) return;
231
+ await setMessageRead.mutateAsync({
232
+ id: messageId,
233
+ mailboxId: mailbox.id,
234
+ conversationId,
235
+ isRead: true,
236
+ });
237
+ }
238
+
239
+ return (
240
+ <article className="mx-auto w-full max-w-[60rem] pb-10">
241
+ {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>
252
+
253
+ <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}
275
+
276
+ {messages.map((message, index) => {
277
+ const previous = messages[index - 1];
278
+ const audience = previous ? audienceChange(previous, message) : null;
279
+
280
+ return (
281
+ <div key={message.id} className="contents">
282
+ {audience && <AudienceChange text={audience} />}
283
+ <ConversationMessage
284
+ message={message}
285
+ mailbox={mailbox}
286
+ defaultExpanded={message === latest || !message.isRead}
287
+ collapsible={messages.length > 1}
288
+ showViewedBy={mailbox?.kind === "shared"}
289
+ resendPending={
290
+ resendMessage.isPending &&
291
+ resendMessage.variables?.id === message.id
292
+ }
293
+ handlers={{
294
+ 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),
302
+ onShowOriginal: canShowOriginal(message)
303
+ ? () => setOriginalMessageId(message.id)
304
+ : undefined,
305
+ originalUrl:
306
+ mailbox && message.hasOriginal
307
+ ? originalSourceUrl(mailbox.id, message.id)
308
+ : undefined,
309
+ onMarkUnread:
310
+ mailbox &&
311
+ message.direction === "incoming" &&
312
+ message.isRead
313
+ ? () => void markUnread(message.id)
314
+ : undefined,
315
+ sender:
316
+ message.direction === "incoming" && senderActions && mailbox
317
+ ? senderHandlers(message, senderActions, reportable)
318
+ : undefined,
319
+ }}
320
+ />
321
+ </div>
322
+ );
323
+ })}
324
+ </div>
325
+
326
+ <div ref={replyRegion} className="scroll-mb-4 px-4 pt-4 sm:px-6">
327
+ {selectedParent && selectedMode ? (
328
+ <ConversationReply
329
+ key={selectedParent.id}
330
+ mailbox={mailbox}
331
+ parent={selectedParent}
332
+ mode={selectedMode}
333
+ onModeChange={(mode) =>
334
+ setReplySelection({
335
+ parentId: selectedParent.id,
336
+ mode,
337
+ })
338
+ }
339
+ onCancel={() => setReplySelection(null)}
340
+ onSent={handleSent}
341
+ />
342
+ ) : mailbox?.canSend ? (
343
+ <div className="flex flex-wrap items-center gap-2">
344
+ {replyTarget?.replyPlan.actions.map((action) => (
345
+ <Button
346
+ key={action.mode}
347
+ type="button"
348
+ variant="outline"
349
+ size="sm"
350
+ onClick={() =>
351
+ setReplySelection({
352
+ parentId: replyTarget.id,
353
+ mode: action.mode,
354
+ })
355
+ }
356
+ >
357
+ <ReplyActionIcon mode={action.mode} />
358
+ {actionButtonLabel(action)}
359
+ </Button>
360
+ ))}
361
+ <Button
362
+ type="button"
363
+ variant="outline"
364
+ size="sm"
365
+ onClick={() => onForward(latest)}
366
+ >
367
+ <Forward /> Forward
368
+ </Button>
369
+ </div>
370
+ ) : null}
371
+ </div>
372
+ {mailbox && (
373
+ <OriginalMessageSheet
374
+ mailboxId={mailbox.id}
375
+ message={
376
+ messages.find((message) => message.id === originalMessageId) ?? null
377
+ }
378
+ onClose={() => setOriginalMessageId(null)}
379
+ />
380
+ )}
381
+ </article>
382
+ );
383
+ }
384
+
385
+ function senderHandlers(
386
+ message: MessageDetail,
387
+ actions: SenderActions,
388
+ reportable: boolean
389
+ ): SenderHandlers {
390
+ return {
391
+ onUnsubscribe: canUnsubscribe(message)
392
+ ? () => actions.open("unsubscribe", message)
393
+ : undefined,
394
+ onBlock: () => actions.open("block", message),
395
+ onUnblock: () => actions.unblockSender(message),
396
+ onReportSpam: reportable ? () => actions.reportSpam(message) : undefined,
397
+ onTrust: message.sender?.trustedAt
398
+ ? undefined
399
+ : () => actions.trustSender(message),
400
+ };
401
+ }
402
+
403
+ function AudienceChange({ text }: { text: string }) {
404
+ return (
405
+ <div className="border-border text-muted-foreground flex items-center gap-3 border-t px-4 py-2 text-[11px] sm:px-6">
406
+ <span className="bg-border h-px flex-1" />
407
+ <span className="font-medium">{text}</span>
408
+ <span className="bg-border h-px flex-1" />
409
+ </div>
410
+ );
411
+ }
412
+
413
+ function audienceChange(previous: MessageDetail, current: MessageDetail) {
414
+ const before = new Set(previous.replyPlan.participants);
415
+ const after = new Set(current.replyPlan.participants);
416
+
417
+ if (
418
+ before.size === after.size &&
419
+ [...before].every((participant) => after.has(participant))
420
+ ) {
421
+ return null;
422
+ }
423
+
424
+ const added = current.replyPlan.participants.filter(
425
+ (value) => !before.has(value)
426
+ );
427
+
428
+ const removed = previous.replyPlan.participants.filter(
429
+ (value) => !after.has(value)
430
+ );
431
+
432
+ if (added.length && !removed.length) return `${added.join(", ")} joined`;
433
+
434
+ if (removed.length && !added.length) return `${removed.join(", ")} left`;
435
+
436
+ return "Recipients changed";
437
+ }
@@ -0,0 +1,272 @@
1
+ import {
2
+ DropdownMenu,
3
+ DropdownMenuContent,
4
+ DropdownMenuGroup,
5
+ DropdownMenuLabel,
6
+ DropdownMenuSeparator,
7
+ DropdownMenuTrigger,
8
+ cn,
9
+ } from "@open-workspace/core/ui";
10
+ import { format } from "date-fns";
11
+ import { Check, CheckCheck, CircleAlert, Clock3 } from "lucide-react";
12
+ import type { LucideIcon } from "lucide-react";
13
+
14
+ import type { MessageDetail, RecipientDeliveryStatus } from "./types";
15
+
16
+ type Indicator = {
17
+ Icon: LucideIcon;
18
+ label: string;
19
+ tone: "muted" | "warning" | "danger";
20
+ };
21
+
22
+ function recipients(message: MessageDetail) {
23
+ return [
24
+ ...new Set(
25
+ [
26
+ ...message.toAddresses,
27
+ ...message.ccAddresses,
28
+ ...message.bccAddresses,
29
+ ].map((address) => address.trim().toLocaleLowerCase())
30
+ ),
31
+ ];
32
+ }
33
+
34
+ function deliveryIndicator(
35
+ message: MessageDetail,
36
+ totalRecipients: number
37
+ ): Indicator {
38
+ if (message.transportState === "unconfirmed") {
39
+ return { Icon: CircleAlert, label: "Unconfirmed", tone: "warning" };
40
+ }
41
+
42
+ if (message.transportState === "failed") {
43
+ return { Icon: CircleAlert, label: "Not sent", tone: "danger" };
44
+ }
45
+
46
+ if (!message.deliveryStatuses.length) {
47
+ return { Icon: Check, label: "Sent", tone: "muted" };
48
+ }
49
+
50
+ const delivered = message.deliveryStatuses.filter(
51
+ (item) => item.status === "delivered" || item.status === "complained"
52
+ ).length;
53
+
54
+ const failed = message.deliveryStatuses.filter((item) =>
55
+ ["bounced", "failed", "rejected"].includes(item.status)
56
+ ).length;
57
+
58
+ const complained = message.deliveryStatuses.some(
59
+ (item) => item.status === "complained"
60
+ );
61
+
62
+ if (failed > 0 || complained) {
63
+ return {
64
+ Icon: CircleAlert,
65
+ label:
66
+ delivered > 0 && totalRecipients > 1
67
+ ? `${delivered}/${totalRecipients} delivered`
68
+ : failed > 0
69
+ ? "Not delivered"
70
+ : "Marked as spam",
71
+ tone: delivered > 0 ? "warning" : "danger",
72
+ };
73
+ }
74
+
75
+ if (totalRecipients > 0 && delivered >= totalRecipients) {
76
+ return { Icon: CheckCheck, label: "Delivered", tone: "muted" };
77
+ }
78
+
79
+ return {
80
+ Icon: Clock3,
81
+ label:
82
+ delivered > 0 && totalRecipients > 1
83
+ ? `${delivered}/${totalRecipients} delivered`
84
+ : "Delivering",
85
+ tone: "muted",
86
+ };
87
+ }
88
+
89
+ const recipientStatusIcons: Record<
90
+ RecipientDeliveryStatus["status"],
91
+ { Icon: LucideIcon; className: string }
92
+ > = {
93
+ delivered: { Icon: CheckCheck, className: "text-success" },
94
+ deferred: { Icon: Clock3, className: "text-amber-600 dark:text-amber-400" },
95
+ bounced: { Icon: CircleAlert, className: "text-destructive" },
96
+ failed: { Icon: CircleAlert, className: "text-destructive" },
97
+ rejected: { Icon: CircleAlert, className: "text-destructive" },
98
+ complained: {
99
+ Icon: CircleAlert,
100
+ className: "text-amber-600 dark:text-amber-400",
101
+ },
102
+ };
103
+
104
+ /** Plain-language outcome; the SMTP detail stays available underneath. */
105
+ function recipientExplanation(status: RecipientDeliveryStatus | undefined) {
106
+ if (!status) {
107
+ return "Sent. Waiting for the recipient's server to report back.";
108
+ }
109
+
110
+ if (status.status === "delivered") {
111
+ return "Accepted by the recipient's mail server.";
112
+ }
113
+
114
+ if (status.status === "deferred") {
115
+ return "The recipient's server asked to try later. Delivery is retried automatically.";
116
+ }
117
+
118
+ if (status.status === "bounced") {
119
+ return "The recipient's server refused the message permanently.";
120
+ }
121
+
122
+ if (status.status === "failed") return "The message could not be delivered.";
123
+
124
+ if (status.status === "rejected") return "Rejected before delivery.";
125
+
126
+ return "Delivered, but the recipient reported it as spam.";
127
+ }
128
+
129
+ function smtpCodeMeaning(code: string) {
130
+ if (code.startsWith("2")) return "OK";
131
+
132
+ if (code.startsWith("4")) return "temporary failure";
133
+
134
+ if (code.startsWith("5")) return "permanent failure";
135
+
136
+ return null;
137
+ }
138
+
139
+ function recipientStatusLabel(status: RecipientDeliveryStatus | undefined) {
140
+ if (!status) return "Awaiting status";
141
+
142
+ if (status.status === "delivered") return "Delivered";
143
+
144
+ if (status.status === "deferred") return "Delivery delayed";
145
+
146
+ if (status.status === "bounced") return "Bounced";
147
+
148
+ if (status.status === "failed") return "Delivery failed";
149
+
150
+ if (status.status === "rejected") return "Rejected";
151
+
152
+ return "Marked as spam";
153
+ }
154
+
155
+ export function DeliveryIndicator({
156
+ message,
157
+ className,
158
+ }: {
159
+ message: MessageDetail;
160
+ className?: string;
161
+ }) {
162
+ if (message.direction !== "outgoing") return null;
163
+ const addresses = recipients(message);
164
+
165
+ const statuses = new Map(
166
+ message.deliveryStatuses.map((status) => [
167
+ status.recipient.toLocaleLowerCase(),
168
+ status,
169
+ ])
170
+ );
171
+
172
+ const indicator = deliveryIndicator(message, addresses.length);
173
+ const { Icon } = indicator;
174
+
175
+ return (
176
+ <DropdownMenu>
177
+ <DropdownMenuTrigger
178
+ render={
179
+ <button
180
+ type="button"
181
+ className={cn(
182
+ "focus-visible:ring-ring inline-flex items-center gap-1 rounded-sm text-xs font-medium transition-opacity outline-none hover:opacity-75 focus-visible:ring-2",
183
+ indicator.tone === "muted" && "text-muted-foreground",
184
+ indicator.tone === "warning" &&
185
+ "text-amber-700 dark:text-amber-400",
186
+ indicator.tone === "danger" && "text-destructive",
187
+ className
188
+ )}
189
+ aria-label={`Delivery status: ${indicator.label}`}
190
+ />
191
+ }
192
+ >
193
+ <Icon
194
+ className={cn(
195
+ "size-3.5",
196
+ indicator.Icon === CheckCheck && "text-success"
197
+ )}
198
+ />
199
+ <span>{indicator.label}</span>
200
+ </DropdownMenuTrigger>
201
+ <DropdownMenuContent
202
+ align="end"
203
+ className="w-80 max-w-[calc(100vw-2rem)]"
204
+ >
205
+ <DropdownMenuGroup>
206
+ <DropdownMenuLabel>Delivery status</DropdownMenuLabel>
207
+ </DropdownMenuGroup>
208
+ <DropdownMenuSeparator />
209
+ {message.transportState === "unconfirmed" ? (
210
+ <p className="px-2 py-2 text-xs text-amber-700 dark:text-amber-400">
211
+ {message.transportError
212
+ ? `${message.transportError} Submission could not be confirmed.`
213
+ : "Submission could not be confirmed."}{" "}
214
+ Automatic resend is disabled because it could deliver a duplicate.
215
+ </p>
216
+ ) : message.transportState === "failed" ? (
217
+ <p className="text-destructive px-2 py-2 text-xs">
218
+ {message.transportError ||
219
+ "The message could not be prepared for sending."}
220
+ </p>
221
+ ) : (
222
+ <div className="divide-y">
223
+ {addresses.map((address) => {
224
+ const status = statuses.get(address);
225
+
226
+ const { Icon: StatusIcon, className: iconClassName } = status
227
+ ? recipientStatusIcons[status.status]
228
+ : { Icon: Clock3, className: "text-muted-foreground" };
229
+
230
+ const meaning = status?.smtpCode
231
+ ? smtpCodeMeaning(status.smtpCode)
232
+ : null;
233
+
234
+ return (
235
+ <div key={address} className="flex gap-2.5 px-2 py-2.5 text-xs">
236
+ <StatusIcon
237
+ className={cn("mt-0.5 size-3.5 shrink-0", iconClassName)}
238
+ />
239
+ <div className="min-w-0 flex-1">
240
+ <p className="flex items-baseline justify-between gap-3">
241
+ <span className="min-w-0 truncate font-medium">
242
+ {address}
243
+ </span>
244
+ <span className="text-muted-foreground shrink-0">
245
+ {recipientStatusLabel(status)}
246
+ </span>
247
+ </p>
248
+ <p className="text-muted-foreground mt-0.5">
249
+ {recipientExplanation(status)}
250
+ </p>
251
+ {status && (
252
+ <p className="text-muted-foreground/80 mt-1 text-[11px]">
253
+ {[
254
+ status.smtpCode &&
255
+ `SMTP ${status.smtpCode}${meaning ? ` (${meaning})` : ""}`,
256
+ status.detail,
257
+ format(new Date(status.eventAt), "MMM d 'at' HH:mm"),
258
+ ]
259
+ .filter(Boolean)
260
+ .join(" · ")}
261
+ </p>
262
+ )}
263
+ </div>
264
+ </div>
265
+ );
266
+ })}
267
+ </div>
268
+ )}
269
+ </DropdownMenuContent>
270
+ </DropdownMenu>
271
+ );
272
+ }