@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,353 @@
1
+ import {
2
+ Button,
3
+ DropdownMenu,
4
+ DropdownMenuContent,
5
+ DropdownMenuGroup,
6
+ DropdownMenuItem,
7
+ DropdownMenuLabel,
8
+ DropdownMenuRadioGroup,
9
+ DropdownMenuRadioItem,
10
+ DropdownMenuSeparator,
11
+ DropdownMenuSub,
12
+ DropdownMenuSubContent,
13
+ DropdownMenuShortcut,
14
+ DropdownMenuSubTrigger,
15
+ DropdownMenuTrigger,
16
+ } from "@open-workspace/core/ui";
17
+ import {
18
+ Download,
19
+ Ellipsis,
20
+ FileText,
21
+ Forward,
22
+ Globe,
23
+ Languages,
24
+ Mail,
25
+ MailMinus,
26
+ OctagonAlert,
27
+ Printer,
28
+ RotateCw,
29
+ ShieldBan,
30
+ ShieldCheck,
31
+ } from "lucide-react";
32
+ import type { ReactNode } from "react";
33
+
34
+ import { ReplyActionIcon } from "./conversation-reply";
35
+ import { senderName } from "./message-format";
36
+ import { languageName } from "./message-translation";
37
+ import type { MessageTranslationState } from "./message-translation";
38
+ import { printMessage, renderedMessageHtml } from "./print-message";
39
+ import { ToolbarTooltip } from "./toolbar-tooltip";
40
+ import type { MessageDetail, ReplyAction } from "./types";
41
+
42
+ /** Apple Mail's "Raw Source" chord, which ConversationView listens for. */
43
+ const originalShortcutLabel =
44
+ typeof navigator !== "undefined" &&
45
+ /Mac|iPhone|iPad/u.test(navigator.platform)
46
+ ? "⌥⌘U"
47
+ : "Ctrl+Alt+U";
48
+
49
+ export type MessageHandlers = {
50
+ onViewed: () => Promise<void>;
51
+ onReply: (action: ReplyAction) => void;
52
+ onForward: () => void;
53
+ onResend: () => void;
54
+ /** Present when the message has a stored original to inspect. */
55
+ onShowOriginal?: () => void;
56
+ /** Download URL of the stored original (.eml). */
57
+ originalUrl?: string;
58
+ /** Marks this message unread again; incoming messages only. */
59
+ onMarkUnread?: () => void;
60
+ /** Sender-level actions; absent for outgoing messages. */
61
+ sender?: SenderHandlers;
62
+ };
63
+
64
+ export type SenderHandlers = {
65
+ onUnsubscribe?: () => void;
66
+ onBlock?: () => void;
67
+ onUnblock?: () => void;
68
+ /** Reporting spam moves the conversation to Spam; absent where that makes no sense. */
69
+ onReportSpam?: () => void;
70
+ /** "Looks safe": trusts the sender and hides caution hints. */
71
+ onTrust?: () => void;
72
+ };
73
+
74
+ /** An outgoing message whose submission failed or was never confirmed. */
75
+ export function canRetry(message: MessageDetail) {
76
+ return (
77
+ message.direction === "outgoing" &&
78
+ (message.transportState === "failed" ||
79
+ message.transportState === "unconfirmed")
80
+ );
81
+ }
82
+
83
+ export function actionButtonLabel(action: ReplyAction) {
84
+ if (action.mode === "reply_all") return "Reply all";
85
+
86
+ if (action.mode === "reply_list") return "Reply to list";
87
+
88
+ if (action.mode === "continue") return "Follow up";
89
+
90
+ return "Reply";
91
+ }
92
+
93
+ /** A message's reply button and its "More actions" menu. */
94
+ export function MessageActions({
95
+ message,
96
+ canForward,
97
+ translation,
98
+ resendPending,
99
+ handlers,
100
+ }: {
101
+ message: MessageDetail;
102
+ mailboxId?: string;
103
+ canForward: boolean;
104
+ translation: MessageTranslationState;
105
+ resendPending: boolean;
106
+ handlers: MessageHandlers;
107
+ }) {
108
+ const [primaryAction, ...otherActions] = message.replyPlan.actions;
109
+ const senderHandlers = handlers.sender;
110
+
111
+ return (
112
+ <div className="-mr-1.5 flex shrink-0 items-center">
113
+ {primaryAction && (
114
+ <IconAction
115
+ label={`${actionButtonLabel(primaryAction)} to this message`}
116
+ tooltip={actionButtonLabel(primaryAction)}
117
+ onClick={() => handlers.onReply(primaryAction)}
118
+ >
119
+ <ReplyActionIcon mode={primaryAction.mode} />
120
+ </IconAction>
121
+ )}
122
+ <DropdownMenu>
123
+ <ToolbarTooltip label="More actions" side="bottom">
124
+ <DropdownMenuTrigger
125
+ render={
126
+ <Button
127
+ type="button"
128
+ variant="ghost"
129
+ size="icon-xs"
130
+ aria-label="More actions"
131
+ className="text-muted-foreground hover:text-foreground"
132
+ />
133
+ }
134
+ >
135
+ <Ellipsis />
136
+ </DropdownMenuTrigger>
137
+ </ToolbarTooltip>
138
+ <DropdownMenuContent align="end" className="w-64">
139
+ {menuGroups([
140
+ [
141
+ ...otherActions.map((action) => (
142
+ <DropdownMenuItem
143
+ key={action.mode}
144
+ aria-label={`${actionButtonLabel(action)} to this message`}
145
+ onClick={() => handlers.onReply(action)}
146
+ >
147
+ <ReplyActionIcon mode={action.mode} />{" "}
148
+ {actionButtonLabel(action)}
149
+ </DropdownMenuItem>
150
+ )),
151
+ canForward && (
152
+ <DropdownMenuItem
153
+ key="forward"
154
+ aria-label="Forward this message"
155
+ onClick={handlers.onForward}
156
+ >
157
+ <Forward /> Forward
158
+ </DropdownMenuItem>
159
+ ),
160
+ ],
161
+ [
162
+ handlers.onMarkUnread && (
163
+ <DropdownMenuItem key="unread" onClick={handlers.onMarkUnread}>
164
+ <Mail /> Mark as unread from here
165
+ </DropdownMenuItem>
166
+ ),
167
+ canRetry(message) && (
168
+ <DropdownMenuItem
169
+ key="resend"
170
+ disabled={resendPending}
171
+ onClick={handlers.onResend}
172
+ >
173
+ <RotateCw />{" "}
174
+ {message.transportState === "failed"
175
+ ? "Retry sending"
176
+ : "Send again"}
177
+ </DropdownMenuItem>
178
+ ),
179
+ ],
180
+ [
181
+ senderHandlers?.onUnsubscribe && (
182
+ <DropdownMenuItem
183
+ key="unsubscribe"
184
+ onClick={senderHandlers.onUnsubscribe}
185
+ >
186
+ <MailMinus /> Unsubscribe
187
+ </DropdownMenuItem>
188
+ ),
189
+ message.sender?.block &&
190
+ !message.sender.block.workspace &&
191
+ senderHandlers?.onUnblock ? (
192
+ <DropdownMenuItem
193
+ key="unblock"
194
+ onClick={senderHandlers.onUnblock}
195
+ >
196
+ <ShieldCheck />{" "}
197
+ <span className="truncate">
198
+ Unblock “{message.sender.block.pattern}”
199
+ </span>
200
+ </DropdownMenuItem>
201
+ ) : !message.sender?.block && senderHandlers?.onBlock ? (
202
+ <DropdownMenuItem key="block" onClick={senderHandlers.onBlock}>
203
+ <ShieldBan />{" "}
204
+ <span className="truncate">
205
+ Block “{senderName(message)}”
206
+ </span>
207
+ </DropdownMenuItem>
208
+ ) : null,
209
+ ],
210
+ [
211
+ senderHandlers?.onReportSpam && (
212
+ <DropdownMenuItem
213
+ key="spam"
214
+ onClick={senderHandlers.onReportSpam}
215
+ >
216
+ <OctagonAlert /> Report spam
217
+ </DropdownMenuItem>
218
+ ),
219
+ ],
220
+ translation.available
221
+ ? [
222
+ translation.visible ? (
223
+ <DropdownMenuItem
224
+ key="original-language"
225
+ onClick={translation.showOriginal}
226
+ >
227
+ <Languages /> Show original language
228
+ </DropdownMenuItem>
229
+ ) : (
230
+ <DropdownMenuItem
231
+ key="translate"
232
+ disabled={!translation.ready || translation.pending}
233
+ onClick={() => void translation.translate()}
234
+ >
235
+ <Languages />
236
+ {translation.hasResult
237
+ ? "Show translation"
238
+ : `Translate to ${languageName(translation.language)}`}
239
+ </DropdownMenuItem>
240
+ ),
241
+ <DropdownMenuSub key="translate-to">
242
+ <DropdownMenuSubTrigger
243
+ disabled={!translation.ready || translation.pending}
244
+ >
245
+ <Globe />
246
+ Translate to…
247
+ </DropdownMenuSubTrigger>
248
+ <DropdownMenuSubContent className="max-h-80 w-48 overflow-y-auto">
249
+ <DropdownMenuGroup>
250
+ <DropdownMenuLabel>
251
+ Translation language
252
+ </DropdownMenuLabel>
253
+ <DropdownMenuRadioGroup
254
+ value={translation.language}
255
+ onValueChange={(code: string) =>
256
+ void translation.translate(code)
257
+ }
258
+ >
259
+ {translation.languages.map((code) => (
260
+ <DropdownMenuRadioItem key={code} value={code}>
261
+ {languageName(code)}
262
+ </DropdownMenuRadioItem>
263
+ ))}
264
+ </DropdownMenuRadioGroup>
265
+ </DropdownMenuGroup>
266
+ </DropdownMenuSubContent>
267
+ </DropdownMenuSub>,
268
+ ]
269
+ : [],
270
+ [
271
+ <DropdownMenuItem
272
+ key="print"
273
+ onClick={() =>
274
+ printMessage(message, renderedMessageHtml(message.id))
275
+ }
276
+ >
277
+ <Printer /> Print
278
+ </DropdownMenuItem>,
279
+ handlers.originalUrl && (
280
+ <DropdownMenuItem
281
+ key="download"
282
+ render={
283
+ // oxlint-disable-next-line jsx-a11y/control-has-associated-label -- Base UI renders the menu item's content inside this anchor
284
+ <a
285
+ href={handlers.originalUrl}
286
+ download={`${message.id}.eml`}
287
+ />
288
+ }
289
+ >
290
+ <Download /> Download message
291
+ </DropdownMenuItem>
292
+ ),
293
+ handlers.onShowOriginal && (
294
+ <DropdownMenuItem
295
+ key="original"
296
+ onClick={handlers.onShowOriginal}
297
+ >
298
+ <FileText /> Show original
299
+ <DropdownMenuShortcut>
300
+ {originalShortcutLabel}
301
+ </DropdownMenuShortcut>
302
+ </DropdownMenuItem>
303
+ ),
304
+ ],
305
+ ])}
306
+ </DropdownMenuContent>
307
+ </DropdownMenu>
308
+ </div>
309
+ );
310
+ }
311
+
312
+ /** Renders menu groups with separators only between groups that have items. */
313
+ function menuGroups(groups: (ReactNode | false | undefined)[][]) {
314
+ const filled = groups.flatMap((items) => {
315
+ const present = items.filter(Boolean);
316
+
317
+ return present.length > 0 ? [present] : [];
318
+ });
319
+
320
+ return filled.map((items, index) => (
321
+ <DropdownMenuGroup key={index}>
322
+ {index > 0 && <DropdownMenuSeparator />}
323
+ {items}
324
+ </DropdownMenuGroup>
325
+ ));
326
+ }
327
+
328
+ function IconAction({
329
+ label,
330
+ tooltip,
331
+ onClick,
332
+ children,
333
+ }: {
334
+ label: string;
335
+ tooltip: string;
336
+ onClick: () => void;
337
+ children: ReactNode;
338
+ }) {
339
+ return (
340
+ <ToolbarTooltip label={tooltip} side="bottom">
341
+ <Button
342
+ type="button"
343
+ variant="ghost"
344
+ size="icon-xs"
345
+ aria-label={label}
346
+ className="text-muted-foreground hover:text-foreground"
347
+ onClick={onClick}
348
+ >
349
+ {children}
350
+ </Button>
351
+ </ToolbarTooltip>
352
+ );
353
+ }
@@ -0,0 +1,200 @@
1
+ import {
2
+ Dialog,
3
+ DialogTrigger,
4
+ Tooltip,
5
+ TooltipContent,
6
+ TooltipTrigger,
7
+ } from "@open-workspace/core/ui";
8
+ import {
9
+ Download,
10
+ File,
11
+ FileArchive,
12
+ FileImage,
13
+ FileSpreadsheet,
14
+ FileText,
15
+ Presentation,
16
+ } from "lucide-react";
17
+ import type { LucideIcon } from "lucide-react";
18
+ import type { CSSProperties } from "react";
19
+
20
+ import { attachmentPreview } from "../../shared/attachment-preview";
21
+ import { AttachmentPreviewDialog } from "./attachment-preview-dialog";
22
+ import { formatBytes } from "./format-bytes";
23
+ import type { MessageAttachment } from "./types";
24
+
25
+ type FileKind = { label: string; Icon: LucideIcon; hue: number };
26
+
27
+ const ARCHIVE = /^(zip|rar|7z|gz|tgz|tar|bz2|xz)$/u;
28
+ const SHEET = /^(xlsx?|xlsm|ods|csv|tsv|numbers)$/u;
29
+ const SLIDES = /^(pptx?|odp|key)$/u;
30
+ const DOCUMENT = /^(docx?|odt|rtf|pages|txt|md)$/u;
31
+
32
+ function fileHueStyle(
33
+ hue: number
34
+ ): CSSProperties & Record<"--file-hue", number> {
35
+ return { "--file-hue": hue };
36
+ }
37
+
38
+ function fileKind(attachment: MessageAttachment): FileKind {
39
+ const { filename } = attachment;
40
+ const dot = filename.lastIndexOf(".");
41
+ const extension = dot === -1 ? "" : filename.slice(dot + 1).toLowerCase();
42
+
43
+ const type = attachment.contentType.toLowerCase();
44
+ const label = extension ? extension.toUpperCase().slice(0, 5) : "File";
45
+
46
+ if (type === "application/pdf" || extension === "pdf") {
47
+ return { label: "PDF", Icon: FileText, hue: 25 };
48
+ }
49
+
50
+ if (type.startsWith("image/")) return { label, Icon: FileImage, hue: 300 };
51
+
52
+ if (SHEET.test(extension)) return { label, Icon: FileSpreadsheet, hue: 150 };
53
+
54
+ if (SLIDES.test(extension)) return { label, Icon: Presentation, hue: 55 };
55
+
56
+ if (DOCUMENT.test(extension) || type.startsWith("text/")) {
57
+ return { label, Icon: FileText, hue: 255 };
58
+ }
59
+
60
+ if (ARCHIVE.test(extension) || type.includes("zip")) {
61
+ return { label, Icon: FileArchive, hue: 80 };
62
+ }
63
+
64
+ return { label, Icon: File, hue: 260 };
65
+ }
66
+
67
+ export function MessageAttachments({
68
+ messageId,
69
+ mailboxId,
70
+ attachments,
71
+ }: {
72
+ messageId: string;
73
+ mailboxId?: string;
74
+ attachments: MessageAttachment[];
75
+ }) {
76
+ if (!attachments.length) return null;
77
+
78
+ const totalBytes = attachments.reduce(
79
+ (total, attachment) => total + attachment.size,
80
+ 0
81
+ );
82
+
83
+ return (
84
+ <section aria-label="Attachments" className="mt-5">
85
+ <p className="text-muted-foreground mb-2 text-xs font-medium">
86
+ {attachments.length}{" "}
87
+ {attachments.length === 1 ? "attachment" : "attachments"}
88
+ <span aria-hidden="true"> · </span>
89
+ <span className="tabular-nums">{formatBytes(totalBytes)}</span>
90
+ </p>
91
+ <ul className="grid grid-cols-[repeat(auto-fill,minmax(min(100%,12.5rem),1fr))] gap-2">
92
+ {attachments.map((attachment) => (
93
+ <AttachmentTile
94
+ key={attachment.id}
95
+ messageId={messageId}
96
+ mailboxId={mailboxId}
97
+ attachment={attachment}
98
+ />
99
+ ))}
100
+ </ul>
101
+ </section>
102
+ );
103
+ }
104
+
105
+ function AttachmentTile({
106
+ messageId,
107
+ mailboxId,
108
+ attachment,
109
+ }: {
110
+ messageId: string;
111
+ mailboxId?: string;
112
+ attachment: MessageAttachment;
113
+ }) {
114
+ const baseUrl = `/api/mail/messages/${encodeURIComponent(messageId)}/attachments/${encodeURIComponent(attachment.id)}`;
115
+ const query = `?mailboxId=${encodeURIComponent(mailboxId ?? "")}`;
116
+ const downloadUrl = `${baseUrl}${query}`;
117
+ const preview = attachmentPreview(attachment);
118
+ const kind = fileKind(attachment);
119
+
120
+ const body = (
121
+ <>
122
+ <span
123
+ aria-hidden="true"
124
+ style={fileHueStyle(kind.hue)}
125
+ className="file-tile-icon grid size-9 shrink-0 place-items-center rounded-md"
126
+ >
127
+ <kind.Icon className="size-4.5" strokeWidth={1.75} />
128
+ </span>
129
+ <span className="min-w-0 flex-1">
130
+ <span
131
+ className="text-foreground block truncate text-[13px] font-medium"
132
+ title={attachment.filename}
133
+ >
134
+ {attachment.filename}
135
+ </span>
136
+ <span className="text-muted-foreground block text-[11.5px] tabular-nums">
137
+ {kind.label} · {formatBytes(attachment.size)}
138
+ </span>
139
+ </span>
140
+ </>
141
+ );
142
+
143
+ const mainClassName =
144
+ "flex min-w-0 flex-1 items-center gap-2.5 rounded-l-lg py-2 pr-1 pl-2 text-left outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset";
145
+
146
+ return (
147
+ <li className="group/file border-border bg-surface hover:border-input hover:bg-surface-sunken flex items-stretch rounded-lg border transition-colors">
148
+ {preview ? (
149
+ <Dialog>
150
+ <DialogTrigger
151
+ className={mainClassName}
152
+ aria-label={`Preview ${attachment.filename}`}
153
+ >
154
+ {body}
155
+ </DialogTrigger>
156
+ <AttachmentPreviewDialog
157
+ messageId={messageId}
158
+ mailboxId={mailboxId ?? ""}
159
+ attachment={attachment}
160
+ preview={preview}
161
+ previewUrl={`${baseUrl}/preview${query}`}
162
+ downloadUrl={downloadUrl}
163
+ />
164
+ </Dialog>
165
+ ) : (
166
+ <a
167
+ className={mainClassName}
168
+ href={downloadUrl}
169
+ download={attachment.filename}
170
+ >
171
+ {body}
172
+ </a>
173
+ )}
174
+ {preview ? (
175
+ <Tooltip>
176
+ <TooltipTrigger
177
+ render={
178
+ <a
179
+ className="text-muted-foreground hover:bg-accent hover:text-foreground focus-visible:ring-ring my-1.5 mr-1.5 grid w-8 shrink-0 place-items-center rounded-md transition-colors outline-none focus-visible:ring-2"
180
+ href={downloadUrl}
181
+ download={attachment.filename}
182
+ aria-label={`Download ${attachment.filename}`}
183
+ />
184
+ }
185
+ >
186
+ <Download className="size-4" />
187
+ </TooltipTrigger>
188
+ <TooltipContent>Download</TooltipContent>
189
+ </Tooltip>
190
+ ) : (
191
+ <span
192
+ aria-hidden="true"
193
+ className="text-muted-foreground/70 my-1.5 mr-1.5 grid w-8 shrink-0 place-items-center"
194
+ >
195
+ <Download className="size-4" />
196
+ </span>
197
+ )}
198
+ </li>
199
+ );
200
+ }
@@ -0,0 +1,75 @@
1
+ /** How a message's sender, dates and recipients read in the conversation. */
2
+ import { format, isThisYear, isToday } from "date-fns";
3
+
4
+ import type { Mailbox, MessageDetail } from "./types";
5
+
6
+ export function senderName(message: MessageDetail) {
7
+ return message.fromName || message.fromAddress;
8
+ }
9
+
10
+ /** The time today, the day this year, the full date before. */
11
+ export function shortDate(value: string | number | Date) {
12
+ const date = new Date(value);
13
+
14
+ if (isToday(date)) return format(date, "HH:mm");
15
+
16
+ if (isThisYear(date)) return format(date, "MMM d, HH:mm");
17
+
18
+ return format(date, "MMM d, yyyy");
19
+ }
20
+
21
+ /** Like shortDate, but without the time, for collapsed one-line rows. */
22
+ export function compactDate(value: string | number | Date) {
23
+ const date = new Date(value);
24
+
25
+ if (isToday(date)) return format(date, "HH:mm");
26
+
27
+ if (isThisYear(date)) return format(date, "MMM d");
28
+
29
+ return format(date, "dd.MM.yy");
30
+ }
31
+
32
+ /** The date in full, for tooltips and the details list. */
33
+ export function fullDate(value: string | number | Date) {
34
+ return format(new Date(value), "EEEE, MMMM d, yyyy 'at' HH:mm");
35
+ }
36
+
37
+ export function isOwnAddress(address: string, mailbox?: Mailbox) {
38
+ return (
39
+ mailbox !== undefined &&
40
+ address.toLowerCase() === mailbox.address.toLowerCase()
41
+ );
42
+ }
43
+
44
+ function recipientLabel(address: string, mailbox?: Mailbox) {
45
+ return isOwnAddress(address, mailbox) ? "me" : address;
46
+ }
47
+
48
+ export function recipientSummary(message: MessageDetail, mailbox?: Mailbox) {
49
+ const to = message.toAddresses.map((address) =>
50
+ recipientLabel(address, mailbox)
51
+ );
52
+
53
+ const cc = message.ccAddresses.map((address) =>
54
+ recipientLabel(address, mailbox)
55
+ );
56
+
57
+ const parts = [`to ${to.length ? to.join(", ") : "undisclosed recipients"}`];
58
+
59
+ if (cc.length) parts.push(`cc ${cc.join(", ")}`);
60
+
61
+ if (message.bccAddresses.length) {
62
+ parts.push(`bcc ${message.bccAddresses.length}`);
63
+ }
64
+
65
+ if (message.viaList) parts.push(`via ${message.viaList}`);
66
+
67
+ return parts.join(" · ");
68
+ }
69
+
70
+ export function listFormat(names: string[]) {
71
+ return new Intl.ListFormat("en", {
72
+ style: "long",
73
+ type: "conjunction",
74
+ }).format(names);
75
+ }