@open-workspace/email 0.3.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (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,324 @@
1
+ import { Skeleton, TooltipProvider, cn } from "@open-workspace/core/ui";
2
+ import { ChevronRight, PenLine } from "lucide-react";
3
+ import { useEffect, useState } from "react";
4
+ import { z } from "zod";
5
+
6
+ import { FolderGlyph } from "./folder-glyph";
7
+ import { FolderManager } from "./folder-manager";
8
+ import { orderedFolders, unreadOf } from "./mail-folders";
9
+ import { CountPill, Monogram } from "./mail-primitives";
10
+ import type { Mailbox, MailFolder } from "./types";
11
+ import type { MailController } from "./use-mail-controller";
12
+ import { useFolders } from "./use-mail-data";
13
+
14
+ export function MailboxGlyph({
15
+ mailbox,
16
+ active,
17
+ className,
18
+ }: {
19
+ mailbox: Mailbox;
20
+ active: boolean;
21
+ className?: string;
22
+ }) {
23
+ return (
24
+ <Monogram
25
+ name={mailbox.displayName}
26
+ className={cn(
27
+ "size-5 rounded-[6px] text-[9px] tracking-wide",
28
+ mailbox.kind === "shared" && "rounded-full",
29
+ active && "bg-primary! text-primary-foreground!",
30
+ className
31
+ )}
32
+ />
33
+ );
34
+ }
35
+
36
+ export function ComposeButton({
37
+ disabled,
38
+ iconOnly = false,
39
+ onClick,
40
+ }: {
41
+ disabled: boolean;
42
+ iconOnly?: boolean;
43
+ onClick: () => void;
44
+ }) {
45
+ return (
46
+ <button
47
+ type="button"
48
+ aria-label={iconOnly ? "New message" : undefined}
49
+ disabled={disabled}
50
+ onClick={onClick}
51
+ className={cn(
52
+ "bg-primary text-primary-foreground flex shrink-0 items-center justify-center gap-2 rounded-[10px] font-semibold outline-none",
53
+ "shadow-[inset_0_1px_0_oklch(1_0_0/0.18),0_1px_2px_oklch(0.3_0.1_266/0.25)] transition-[filter,transform]",
54
+ "focus-visible:ring-ring focus-visible:ring-offset-surface hover:brightness-110 focus-visible:ring-2 focus-visible:ring-offset-2 active:scale-[0.98]",
55
+ "disabled:pointer-events-none disabled:opacity-50",
56
+ iconOnly ? "size-10" : "h-9 w-full text-[13px]"
57
+ )}
58
+ >
59
+ <PenLine className="size-4" />
60
+ {!iconOnly && "New message"}
61
+ </button>
62
+ );
63
+ }
64
+
65
+ function FolderRow({
66
+ folder,
67
+ active,
68
+ onSelect,
69
+ }: {
70
+ folder: MailFolder;
71
+ active: boolean;
72
+ onSelect: () => void;
73
+ }) {
74
+ const unread = unreadOf(folder);
75
+
76
+ return (
77
+ <li>
78
+ <button
79
+ type="button"
80
+ aria-current={active ? "page" : undefined}
81
+ onClick={onSelect}
82
+ className={cn(
83
+ "relative flex h-7.5 w-full items-center gap-2.5 rounded-md pr-1 pl-2.5 text-left text-[13px] transition-colors outline-none",
84
+ "focus-visible:ring-sidebar-ring focus-visible:ring-2",
85
+ active
86
+ ? "bg-sidebar-accent text-sidebar-accent-foreground before:bg-sidebar-primary font-semibold before:absolute before:top-1.5 before:bottom-1.5 before:-left-[9px] before:w-[3px] before:rounded-full"
87
+ : "text-sidebar-foreground hover:bg-foreground/[0.045]",
88
+ unread > 0 && !active && "text-foreground font-semibold"
89
+ )}
90
+ >
91
+ <FolderGlyph
92
+ folder={folder}
93
+ className={cn(
94
+ "size-[15px] shrink-0",
95
+ active ? "text-sidebar-primary" : "text-muted-foreground"
96
+ )}
97
+ strokeWidth={active ? 2.1 : 1.8}
98
+ />
99
+ <span className="min-w-0 flex-1 truncate">{folder.name}</span>
100
+ <CountPill
101
+ count={unread}
102
+ strong={active || folder.systemType === "inbox"}
103
+ label={`${unread} unread`}
104
+ />
105
+ </button>
106
+ </li>
107
+ );
108
+ }
109
+
110
+ const EXPANDED_KEY = "openworkspace.mail.expandedMailboxes";
111
+
112
+ /** What `writeExpanded` stores: the ids of the expanded mailboxes. */
113
+ const expandedIds = z.array(z.string());
114
+
115
+ function readExpanded(): string[] {
116
+ try {
117
+ const stored = expandedIds.safeParse(
118
+ JSON.parse(localStorage.getItem(EXPANDED_KEY) ?? "[]")
119
+ );
120
+
121
+ return stored.success ? stored.data : [];
122
+ } catch {
123
+ return [];
124
+ }
125
+ }
126
+
127
+ function writeExpanded(ids: string[]) {
128
+ try {
129
+ localStorage.setItem(EXPANDED_KEY, JSON.stringify(ids));
130
+ } catch {
131
+ // Private windows and blocked storage: the tree just doesn't remember.
132
+ }
133
+ }
134
+
135
+ /**
136
+ * Which mailboxes are open in the tree. Any number can be; the active one
137
+ * opens when it becomes active, and the choice is kept on this device.
138
+ */
139
+ function useExpandedMailboxes(activeId: string | undefined) {
140
+ const [expanded, setExpanded] = useState<string[]>(() => readExpanded());
141
+ const [seenActive, setSeenActive] = useState<string | undefined>(undefined);
142
+
143
+ if (activeId && activeId !== seenActive) {
144
+ setSeenActive(activeId);
145
+
146
+ if (!expanded.includes(activeId)) setExpanded([...expanded, activeId]);
147
+ }
148
+
149
+ useEffect(() => writeExpanded(expanded), [expanded]);
150
+
151
+ return {
152
+ isExpanded: (id: string) => expanded.includes(id),
153
+ toggle: (id: string) =>
154
+ setExpanded((current) =>
155
+ current.includes(id)
156
+ ? current.filter((item) => item !== id)
157
+ : [...current, id]
158
+ ),
159
+ open: (id: string) =>
160
+ setExpanded((current) =>
161
+ current.includes(id) ? current : [...current, id]
162
+ ),
163
+ };
164
+ }
165
+
166
+ /**
167
+ * Compose on top, then every mailbox as a tree node. Any number of mailboxes
168
+ * can be open at once, each listing its folders with counts; closed ones show
169
+ * their unread badge.
170
+ */
171
+ export function NavTree({ mail }: { mail: MailController }) {
172
+ const { mailbox: activeMailbox } = mail;
173
+ const tree = useExpandedMailboxes(activeMailbox?.id);
174
+
175
+ return (
176
+ <TooltipProvider delay={300}>
177
+ <nav
178
+ aria-label="Mailboxes and folders"
179
+ className="flex min-h-0 flex-1 flex-col"
180
+ >
181
+ <div className="shrink-0 p-2.5 pb-1">
182
+ <ComposeButton
183
+ disabled={!mail.mailbox?.canSend}
184
+ onClick={mail.openCompose}
185
+ />
186
+ </div>
187
+ <div className="min-h-0 flex-1 overflow-y-auto px-2.5 pb-4">
188
+ <p className="text-muted-foreground px-2 pt-3 pb-1.5 text-[11px] font-semibold tracking-[0.06em] uppercase">
189
+ Mailboxes
190
+ </p>
191
+ {mail.mailboxQuery.isLoading ? (
192
+ <div className="space-y-2 px-2 pt-1">
193
+ {Array.from({ length: 6 }, (_, index) => (
194
+ <Skeleton key={index} className="h-5 w-full" />
195
+ ))}
196
+ </div>
197
+ ) : (
198
+ <ul className="space-y-0.5">
199
+ {mail.mailboxes.map((item) => {
200
+ const active = item.id === activeMailbox?.id;
201
+ const expanded = tree.isExpanded(item.id);
202
+
203
+ return (
204
+ <li key={item.id}>
205
+ <div className="group hover:bg-foreground/[0.045] flex h-8 w-full items-center rounded-md pr-1 transition-colors">
206
+ <button
207
+ type="button"
208
+ aria-expanded={expanded}
209
+ aria-label={`${expanded ? "Collapse" : "Expand"} ${item.displayName}`}
210
+ onClick={() => tree.toggle(item.id)}
211
+ className="focus-visible:ring-sidebar-ring grid size-6 shrink-0 place-items-center rounded-md outline-none focus-visible:ring-2"
212
+ >
213
+ <ChevronRight
214
+ aria-hidden="true"
215
+ className={cn(
216
+ "text-muted-foreground size-3.5 transition-transform duration-150",
217
+ expanded && "rotate-90"
218
+ )}
219
+ />
220
+ </button>
221
+ <button
222
+ type="button"
223
+ title={item.address}
224
+ aria-current={active ? "true" : undefined}
225
+ onClick={() => {
226
+ tree.open(item.id);
227
+
228
+ if (!active) mail.selectMailbox(item.id);
229
+ }}
230
+ className="focus-visible:ring-sidebar-ring flex h-full min-w-0 flex-1 items-center gap-1.5 rounded-md text-left outline-none focus-visible:ring-2"
231
+ >
232
+ <MailboxGlyph mailbox={item} active={active} />
233
+ <span
234
+ className={cn(
235
+ "min-w-0 flex-1 truncate pl-0.5 text-[13px] leading-tight",
236
+ active
237
+ ? "text-foreground font-semibold"
238
+ : "text-sidebar-foreground font-medium"
239
+ )}
240
+ >
241
+ {item.displayName}
242
+ </span>
243
+ {!expanded && (
244
+ <CountPill
245
+ count={item.unreadCount}
246
+ strong
247
+ label={`${item.unreadCount} unread in ${item.displayName}`}
248
+ />
249
+ )}
250
+ </button>
251
+ </div>
252
+ {expanded && (
253
+ <MailboxFolders
254
+ mailbox={item}
255
+ activeFolder={active ? mail.folder : null}
256
+ onSelect={(folder) => {
257
+ mail.openFolder(item.id, folder);
258
+ }}
259
+ />
260
+ )}
261
+ </li>
262
+ );
263
+ })}
264
+ </ul>
265
+ )}
266
+ </div>
267
+ </nav>
268
+ </TooltipProvider>
269
+ );
270
+ }
271
+
272
+ /** One open mailbox's folders, loaded for that mailbox. */
273
+ function MailboxFolders({
274
+ mailbox,
275
+ activeFolder,
276
+ onSelect,
277
+ }: {
278
+ mailbox: Mailbox;
279
+ /** The selected folder when this is the active mailbox. */
280
+ activeFolder: string | null;
281
+ onSelect: (folder: string) => void;
282
+ }) {
283
+ const foldersQuery = useFolders(mailbox.id);
284
+ const folders = foldersQuery.data?.folders ?? [];
285
+
286
+ return (
287
+ <div className="border-sidebar-border relative mt-0.5 mb-2 ml-[13px] border-l pl-2">
288
+ {foldersQuery.isLoading ? (
289
+ <div className="space-y-2 py-1.5 pl-2">
290
+ {Array.from({ length: 5 }, (_, index) => (
291
+ <Skeleton key={index} className="h-4 w-4/5" />
292
+ ))}
293
+ </div>
294
+ ) : (
295
+ <>
296
+ {/* The mailbox's address heads its one list of folders, with their settings beside it. */}
297
+ <div className="flex h-7 items-center gap-1 pl-2.5">
298
+ <p className="text-muted-foreground min-w-0 flex-1 truncate text-[11px]">
299
+ {mailbox.address}
300
+ </p>
301
+ <span className="[&_button]:size-6 [&_button]:rounded-md [&_svg]:size-3.5">
302
+ <FolderManager
303
+ mailboxId={mailbox.id}
304
+ activeFolderId={activeFolder ?? "inbox"}
305
+ folders={folders}
306
+ onSelectFolder={onSelect}
307
+ />
308
+ </span>
309
+ </div>
310
+ <ul className="space-y-px">
311
+ {orderedFolders(folders).map((entry) => (
312
+ <FolderRow
313
+ key={entry.id}
314
+ folder={entry}
315
+ active={entry.id === activeFolder}
316
+ onSelect={() => onSelect(entry.id)}
317
+ />
318
+ ))}
319
+ </ul>
320
+ </>
321
+ )}
322
+ </div>
323
+ );
324
+ }
@@ -0,0 +1,59 @@
1
+ import type { QueryClient } from "@tanstack/react-query";
2
+
3
+ import { parseMailPath } from "../../shared/mail-path";
4
+ import {
5
+ conversationQuery,
6
+ conversationsQuery,
7
+ foldersQuery,
8
+ mailboxesQuery,
9
+ } from "./mail-queries";
10
+
11
+ /**
12
+ * Starts what the mail screen at this URL loads first, while its code is
13
+ * still on the way: the mailbox list and, for a /mail/:id/:folder?/:conversationId? path, that mailbox's
14
+ * folders, the first page of the folder's list and the open conversation;
15
+ * for "/", the first mailbox's once the list names it. The screen's own
16
+ * queries pick these up by key.
17
+ */
18
+ export function prefetchMail({
19
+ queryClient,
20
+ pathname,
21
+ }: {
22
+ queryClient: QueryClient;
23
+ pathname: string;
24
+ }) {
25
+ const route = parseMailPath(pathname);
26
+
27
+ if (pathname !== "/" && !route) return;
28
+ const folder = route?.folder ?? "inbox";
29
+
30
+ const start = (mailboxId: string, conversationId: string | null) => {
31
+ void queryClient.prefetchQuery(foldersQuery(mailboxId));
32
+ void queryClient.prefetchInfiniteQuery(
33
+ conversationsQuery(mailboxId, folder, "", false)
34
+ );
35
+
36
+ if (conversationId) {
37
+ void queryClient.prefetchInfiniteQuery(
38
+ conversationQuery(mailboxId, conversationId)
39
+ );
40
+ }
41
+ };
42
+
43
+ const mailboxes = queryClient.fetchQuery(mailboxesQuery());
44
+
45
+ if (route) {
46
+ start(route.mailboxId, route.conversationId);
47
+
48
+ return;
49
+ }
50
+
51
+ void mailboxes.then(
52
+ (data) => {
53
+ const first = data.mailboxes[0];
54
+
55
+ if (first) start(first.id, null);
56
+ },
57
+ () => undefined
58
+ );
59
+ }
@@ -0,0 +1,66 @@
1
+ import { cn, initials } from "@open-workspace/core/ui";
2
+ import type { CSSProperties } from "react";
3
+
4
+ /** Stable hue per name so the same mailbox or person keeps the same tint. */
5
+ function hueFor(value: string) {
6
+ let hash = 0;
7
+
8
+ // oxlint-disable-next-line unicorn/prefer-math-trunc -- | 0 wraps the hash to 32 bits, which Math.trunc does not
9
+ for (const char of value) hash = (hash * 31 + char.charCodeAt(0)) | 0;
10
+
11
+ return Math.abs(hash) % 360;
12
+ }
13
+
14
+ function monogramStyle(
15
+ name: string
16
+ ): CSSProperties & Record<"--monogram-hue", number> {
17
+ return { "--monogram-hue": hueFor(name) };
18
+ }
19
+
20
+ export function Monogram({
21
+ name,
22
+ className,
23
+ }: {
24
+ name: string;
25
+ className?: string;
26
+ }) {
27
+ return (
28
+ <span
29
+ aria-hidden="true"
30
+ style={monogramStyle(name)}
31
+ className={cn(
32
+ "monogram grid shrink-0 place-items-center font-semibold",
33
+ className
34
+ )}
35
+ >
36
+ {initials(name)}
37
+ </span>
38
+ );
39
+ }
40
+
41
+ export function CountPill({
42
+ count,
43
+ label,
44
+ strong = false,
45
+ className,
46
+ }: {
47
+ count: number;
48
+ label: string;
49
+ strong?: boolean;
50
+ className?: string;
51
+ }) {
52
+ if (count <= 0) return null;
53
+
54
+ return (
55
+ <span
56
+ aria-label={label}
57
+ className={cn(
58
+ "ml-auto min-w-5 shrink-0 rounded-full px-1.5 py-0.5 text-center text-[10.5px] leading-[14px] font-semibold tabular-nums",
59
+ strong ? "bg-primary text-primary-foreground" : "text-muted-foreground",
60
+ className
61
+ )}
62
+ >
63
+ {count > 999 ? "999+" : count}
64
+ </span>
65
+ );
66
+ }
@@ -0,0 +1,209 @@
1
+ import { ApiError, responseJson } from "@open-workspace/core/ui";
2
+ import {
3
+ infiniteQueryOptions,
4
+ queryOptions,
5
+ skipToken,
6
+ } from "@tanstack/react-query";
7
+ import type { QueryClient } from "@tanstack/react-query";
8
+
9
+ import {
10
+ CONVERSATION_PAGE_SIZE,
11
+ MAX_BULK_CONVERSATION_COUNT,
12
+ } from "../../shared/mail";
13
+ import { mailApi } from "../lib/mail-api";
14
+ import type { MessageDetail } from "./types";
15
+
16
+ /**
17
+ * Query definitions shared by the mail screen's hooks and by the prefetch
18
+ * that starts them before the screen's code has loaded (see client.tsx).
19
+ * Kept free of UI imports so the app's entry stays small.
20
+ */
21
+
22
+ const MESSAGE_PAGE_SIZE = 25;
23
+
24
+ /**
25
+ * Realtime updates (with versions) say when mail data changed, so cached
26
+ * conversations stay fresh for a while: reopening one shows it at once.
27
+ */
28
+ const CONVERSATION_STALE_MS = 5 * 60_000;
29
+
30
+ /** A 4xx (no access, unknown folder) will not change by asking again. */
31
+ function retryServerErrors(failureCount: number, error: Error) {
32
+ if (error instanceof ApiError && error.status < 500) return false;
33
+
34
+ return failureCount < 1;
35
+ }
36
+
37
+ export function mailboxesQuery() {
38
+ return queryOptions({
39
+ queryKey: ["mailboxes"],
40
+ queryFn: async ({ signal }) =>
41
+ responseJson(
42
+ await mailApi.api.mail.mailboxes.$get(undefined, {
43
+ init: { signal },
44
+ })
45
+ ),
46
+ });
47
+ }
48
+
49
+ /** A list's first page has no cursor; later pages pass the one before's. */
50
+ const FIRST_PAGE: string | undefined = undefined;
51
+
52
+ export function foldersQuery(mailboxId: string | undefined) {
53
+ return queryOptions({
54
+ queryKey: ["folders", mailboxId],
55
+ queryFn: mailboxId
56
+ ? async ({ signal }) =>
57
+ responseJson(
58
+ await mailApi.api.mail.mailboxes[":id"].folders.$get(
59
+ { param: { id: mailboxId } },
60
+ { init: { signal } }
61
+ )
62
+ )
63
+ : skipToken,
64
+ retry: retryServerErrors,
65
+ });
66
+ }
67
+
68
+ export function conversationsQuery(
69
+ mailboxId: string | undefined,
70
+ folder: string,
71
+ search: string,
72
+ unreadOnly: boolean
73
+ ) {
74
+ return infiniteQueryOptions({
75
+ queryKey: ["conversations", mailboxId, folder, search, unreadOnly],
76
+ initialPageParam: FIRST_PAGE,
77
+ queryFn: mailboxId
78
+ ? async ({ pageParam, signal }) =>
79
+ responseJson(
80
+ await mailApi.api.mail.conversations.$get(
81
+ {
82
+ query: {
83
+ mailboxId,
84
+ folder,
85
+ limit: String(CONVERSATION_PAGE_SIZE),
86
+ cursor: pageParam,
87
+ search: search || undefined,
88
+ unreadOnly: unreadOnly ? "true" : undefined,
89
+ },
90
+ },
91
+ { init: { signal } }
92
+ )
93
+ )
94
+ : skipToken,
95
+ getNextPageParam: (page, pages) => {
96
+ const loadedCount = pages.reduce(
97
+ (count, loadedPage) => count + loadedPage.conversations.length,
98
+ 0
99
+ );
100
+
101
+ return loadedCount < MAX_BULK_CONVERSATION_COUNT
102
+ ? (page.nextCursor ?? undefined)
103
+ : undefined;
104
+ },
105
+ retry: retryServerErrors,
106
+ });
107
+ }
108
+
109
+ let sanitizer: Promise<typeof import("./sanitize-email-html")> | undefined;
110
+
111
+ /**
112
+ * The HTML sanitizer and DOMPurify, one chunk off the app's entry. The mail
113
+ * screen's preload starts it, so the first message body need not wait.
114
+ */
115
+ export function loadSanitizer() {
116
+ sanitizer ??= import("./sanitize-email-html").catch((cause: unknown) => {
117
+ sanitizer = undefined;
118
+ throw cause;
119
+ });
120
+
121
+ return sanitizer;
122
+ }
123
+
124
+ /**
125
+ * A message's sanitized HTML. Stored bodies never change, so it stays; the
126
+ * remote token is in the key because the document's image URLs carry it,
127
+ * and a conversation fetched in a later token window sanitizes again.
128
+ */
129
+ export function sanitizedHtmlQuery(mailboxId: string, message: MessageDetail) {
130
+ return queryOptions({
131
+ queryKey: [
132
+ "sanitized-message-html",
133
+ mailboxId,
134
+ message.id,
135
+ message.remoteTokens,
136
+ ],
137
+ queryFn: async () =>
138
+ (await loadSanitizer()).sanitizeEmailHtml({
139
+ html: message.bodyHtml ?? "",
140
+ mailboxId,
141
+ messageId: message.id,
142
+ attachments: message.attachments,
143
+ remoteTokens: message.remoteTokens,
144
+ }),
145
+ enabled: Boolean(message.bodyHtml),
146
+ staleTime: Number.POSITIVE_INFINITY,
147
+ });
148
+ }
149
+
150
+ /**
151
+ * Sanitizes the bodies a newly fetched conversation page shows expanded (the
152
+ * latest message and unread ones), so opening the conversation, prefetched
153
+ * or not, finds them ready instead of a skeleton.
154
+ */
155
+ function sanitizeAhead(
156
+ client: QueryClient,
157
+ mailboxId: string,
158
+ messages: MessageDetail[],
159
+ firstPage: boolean
160
+ ) {
161
+ messages.forEach((message, index) => {
162
+ const expanded =
163
+ !message.isRead || (firstPage && index === messages.length - 1);
164
+
165
+ if (expanded && message.bodyHtml) {
166
+ void client.prefetchQuery(sanitizedHtmlQuery(mailboxId, message));
167
+ }
168
+ });
169
+ }
170
+
171
+ export function conversationQuery(
172
+ mailboxId: string | undefined,
173
+ conversationId: string | undefined
174
+ ) {
175
+ return infiniteQueryOptions({
176
+ queryKey: ["conversation", mailboxId, conversationId],
177
+ initialPageParam: FIRST_PAGE,
178
+ queryFn:
179
+ mailboxId && conversationId
180
+ ? async ({ client, pageParam, signal }) => {
181
+ const page = await responseJson(
182
+ await mailApi.api.mail.conversations[":id"].$get(
183
+ {
184
+ param: { id: conversationId },
185
+ query: {
186
+ mailboxId,
187
+ limit: String(MESSAGE_PAGE_SIZE),
188
+ cursor: pageParam,
189
+ },
190
+ },
191
+ { init: { signal } }
192
+ )
193
+ );
194
+
195
+ sanitizeAhead(
196
+ client,
197
+ mailboxId,
198
+ page.messages,
199
+ pageParam === undefined
200
+ );
201
+
202
+ return page;
203
+ }
204
+ : skipToken,
205
+ getNextPageParam: (page) => page.nextCursor ?? undefined,
206
+ staleTime: CONVERSATION_STALE_MS,
207
+ retry: retryServerErrors,
208
+ });
209
+ }