@open-workspace/email 0.4.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (266) hide show
  1. package/README.md +9 -4
  2. package/alchemy/index.ts +130 -2
  3. package/drizzle/mailbox/0003_after_0_4_2.sql +180 -0
  4. package/drizzle/mailbox/meta/0003_snapshot.json +1507 -0
  5. package/drizzle/mailbox/meta/_journal.json +7 -0
  6. package/drizzle/mailbox/migrations.js +3 -1
  7. package/migrations/0002_after_0_4_2.sql +35 -0
  8. package/migrations/meta/0002_snapshot.json +807 -0
  9. package/migrations/meta/_journal.json +7 -0
  10. package/package.json +23 -7
  11. package/shared/attachment-preview.ts +18 -8
  12. package/shared/folder-order.ts +31 -6
  13. package/shared/mail-headers.ts +3 -3
  14. package/shared/mail-html.ts +25 -0
  15. package/shared/mail-import.ts +0 -7
  16. package/shared/mail-received.ts +4 -4
  17. package/shared/mail-rule-actions.ts +47 -0
  18. package/shared/mail.ts +70 -4
  19. package/shared/office-preview.ts +0 -4
  20. package/shared/pdfjs-assets.ts +1 -3
  21. package/shared/rich-content.ts +35 -0
  22. package/shared/signature.ts +15 -0
  23. package/src/admin/address-auto-reply.tsx +49 -0
  24. package/src/admin/blocked-senders.tsx +86 -66
  25. package/src/admin/forward-destinations.tsx +140 -0
  26. package/src/admin/mail-archive.tsx +134 -0
  27. package/src/admin/team-signature.tsx +90 -0
  28. package/src/client.tsx +130 -16
  29. package/src/mail/attachment-preview-dialog.tsx +13 -5
  30. package/src/mail/auto-reply-banner.tsx +61 -0
  31. package/src/mail/blocked-senders.tsx +124 -91
  32. package/src/mail/compose-window-chrome.tsx +20 -8
  33. package/src/mail/compose-window.tsx +387 -365
  34. package/src/mail/composer-asset-bookmarks.ts +47 -12
  35. package/src/mail/composer-attachment-list.tsx +42 -17
  36. package/src/mail/composer-editor-document.ts +54 -10
  37. package/src/mail/composer-editor.tsx +44 -44
  38. package/src/mail/composer-link-text.ts +42 -0
  39. package/src/mail/composer-recipient-fields.tsx +25 -16
  40. package/src/mail/composer-recipients.ts +62 -16
  41. package/src/mail/composer-session.ts +107 -23
  42. package/src/mail/composer-toolbar.tsx +15 -1
  43. package/src/mail/composer-upload-client.ts +29 -1
  44. package/src/mail/composer-upload-progress.ts +2 -2
  45. package/src/mail/conversation-actions-model.ts +56 -0
  46. package/src/mail/conversation-list.tsx +263 -128
  47. package/src/mail/conversation-loading-skeleton.tsx +165 -90
  48. package/src/mail/conversation-message.tsx +257 -102
  49. package/src/mail/conversation-order.ts +12 -0
  50. package/src/mail/conversation-reply.tsx +313 -189
  51. package/src/mail/conversation-selection-toolbar.tsx +188 -143
  52. package/src/mail/conversation-view.tsx +315 -85
  53. package/src/mail/delivery-indicator.tsx +33 -12
  54. package/src/mail/draft-attachments.ts +194 -0
  55. package/src/mail/email-html-body.tsx +97 -53
  56. package/src/mail/folder-cleanup-bar.tsx +193 -0
  57. package/src/mail/folder-glyph.tsx +5 -0
  58. package/src/mail/folder-manager.tsx +71 -38
  59. package/src/mail/mail-app-bar.tsx +15 -3
  60. package/src/mail/mail-dock.tsx +19 -3
  61. package/src/mail/mail-focus.ts +48 -0
  62. package/src/mail/mail-folders.ts +14 -2
  63. package/src/mail/mail-layout.css +20 -0
  64. package/src/mail/mail-mailbox-sheet.tsx +22 -69
  65. package/src/mail/mail-nav-rail.tsx +7 -4
  66. package/src/mail/mail-nav.tsx +66 -46
  67. package/src/mail/mail-prefetch.ts +30 -9
  68. package/src/mail/mail-primitives.tsx +6 -4
  69. package/src/mail/mail-queries.ts +122 -20
  70. package/src/mail/mail-query-cache.ts +1085 -173
  71. package/src/mail/mail-search-params.ts +35 -0
  72. package/src/mail/mail-shell.tsx +32 -9
  73. package/src/mail/mailbox-ai-settings.tsx +56 -42
  74. package/src/mail/mailbox-realtime.tsx +4 -12
  75. package/src/mail/mailbox-rules.tsx +898 -0
  76. package/src/mail/message-actions.tsx +38 -26
  77. package/src/mail/message-attachments.tsx +20 -12
  78. package/src/mail/message-body.ts +53 -0
  79. package/src/mail/message-format.ts +8 -2
  80. package/src/mail/message-header.tsx +3 -1
  81. package/src/mail/message-translation.tsx +34 -67
  82. package/src/mail/original-message-sheet.tsx +63 -50
  83. package/src/mail/original-message.ts +6 -6
  84. package/src/mail/pane-splitter.tsx +3 -3
  85. package/src/mail/pdf-preview.tsx +70 -62
  86. package/src/mail/plain-message-text.tsx +28 -0
  87. package/src/mail/print-message.ts +31 -36
  88. package/src/mail/reader-pane.tsx +134 -103
  89. package/src/mail/recipient-combobox.tsx +52 -29
  90. package/src/mail/recipient-field-row.tsx +3 -0
  91. package/src/mail/rich-content.ts +128 -0
  92. package/src/mail/sanitize-email-html.ts +53 -32
  93. package/src/mail/sender-actions.tsx +48 -46
  94. package/src/mail/sender-authentication.tsx +1 -1
  95. package/src/mail/sender-dialogs.tsx +67 -43
  96. package/src/mail/sender-security.tsx +9 -10
  97. package/src/mail/signature.ts +43 -0
  98. package/src/mail/translation-html.ts +3 -3
  99. package/src/mail/types.ts +12 -0
  100. package/src/mail/use-composer-attachment-preflight.ts +23 -60
  101. package/src/mail/use-composer-core.ts +286 -0
  102. package/src/mail/use-composer-navigation.ts +120 -0
  103. package/src/mail/use-composer-submission.ts +154 -19
  104. package/src/mail/use-conversation-actions.ts +292 -0
  105. package/src/mail/use-conversation-position.ts +51 -0
  106. package/src/mail/use-conversation-selection.ts +192 -89
  107. package/src/mail/use-draft-autosave.ts +394 -0
  108. package/src/mail/use-drafts.ts +194 -0
  109. package/src/mail/use-mail-controller.ts +754 -213
  110. package/src/mail/use-mail-data.ts +210 -100
  111. package/src/mail/use-mail-safety.ts +19 -38
  112. package/src/mail/use-mail-send.ts +18 -6
  113. package/src/mail/use-pdf-translation.ts +1 -1
  114. package/src/mail/use-reply-cc.ts +61 -0
  115. package/src/mail/use-visible-message-read.ts +128 -64
  116. package/src/pwa/push-key.ts +18 -0
  117. package/src/pwa/push-subscription.ts +88 -26
  118. package/src/settings/auto-reply-editor.tsx +454 -0
  119. package/src/settings/auto-reply-page.tsx +59 -0
  120. package/src/settings/mail-apps-page.tsx +52 -43
  121. package/src/settings/notifications-page.tsx +82 -67
  122. package/src/settings/rich-content-editor.tsx +182 -0
  123. package/src/settings/settings-queries.ts +57 -0
  124. package/src/settings/signature-editor.tsx +142 -0
  125. package/src/settings/signature-page.tsx +70 -0
  126. package/worker/admin/blocked-sender-routes.ts +10 -16
  127. package/worker/backups/eml.ts +185 -0
  128. package/worker/backups/file-parts.ts +140 -0
  129. package/worker/backups/mail-backup.ts +644 -0
  130. package/worker/backups/settings-backup.ts +866 -0
  131. package/worker/backups/stream-eml.ts +73 -0
  132. package/worker/db/schema.ts +89 -5
  133. package/worker/jmap/attachment-blobs.ts +113 -0
  134. package/worker/jmap/blobs.ts +162 -0
  135. package/worker/jmap/draft-content.ts +42 -0
  136. package/worker/jmap/drafts.ts +70 -0
  137. package/worker/jmap/email-body.ts +152 -0
  138. package/worker/jmap/email-methods.ts +1223 -0
  139. package/worker/jmap/eventsource.ts +9 -8
  140. package/worker/jmap/import-methods.ts +205 -0
  141. package/worker/jmap/mailbox-methods.ts +187 -0
  142. package/worker/jmap/method-core.ts +379 -0
  143. package/worker/jmap/methods.ts +108 -1815
  144. package/worker/jmap/push-subscription-methods.ts +373 -0
  145. package/worker/jmap/push-types.ts +46 -2
  146. package/worker/jmap/push.ts +24 -31
  147. package/worker/jmap/routes.ts +144 -153
  148. package/worker/jmap/submission-methods.ts +395 -0
  149. package/worker/mail/access.ts +35 -12
  150. package/worker/mail/ai-classification.ts +2 -2
  151. package/worker/mail/archive-object.ts +96 -0
  152. package/worker/mail/archive-routes.ts +35 -0
  153. package/worker/mail/archive.ts +682 -0
  154. package/worker/mail/auth-headers.ts +48 -40
  155. package/worker/mail/auto-reply-routes.ts +255 -0
  156. package/worker/mail/auto-reply.ts +243 -0
  157. package/worker/mail/bimi.ts +53 -36
  158. package/worker/mail/bootstrap.ts +11 -3
  159. package/worker/mail/capabilities.ts +69 -0
  160. package/worker/mail/classification-signals.ts +1 -1
  161. package/worker/mail/confusables-data.ts +9 -0
  162. package/worker/mail/conversation-cursor.ts +6 -15
  163. package/worker/mail/delivery-events.ts +9 -7
  164. package/worker/mail/delivery-facts.ts +1 -1
  165. package/worker/mail/demo-routes.ts +3 -3
  166. package/worker/mail/doh.ts +3 -3
  167. package/worker/mail/download-links.ts +153 -0
  168. package/worker/mail/downloads.ts +2 -6
  169. package/worker/mail/draft-routes.ts +473 -0
  170. package/worker/mail/draft-sender.ts +54 -0
  171. package/worker/mail/edge-cache.ts +41 -55
  172. package/worker/mail/email-routing.ts +123 -0
  173. package/worker/mail/forward-destinations.ts +114 -0
  174. package/worker/mail/forward-downloads.ts +115 -0
  175. package/worker/mail/forward-html.ts +85 -0
  176. package/worker/mail/header-metadata.ts +2 -5
  177. package/worker/mail/impersonation.ts +102 -101
  178. package/worker/mail/import-routes.ts +29 -82
  179. package/worker/mail/import.ts +130 -0
  180. package/worker/mail/inbound-delivery.ts +6 -5
  181. package/worker/mail/inbound.ts +58 -28
  182. package/worker/mail/ip-intel.ts +8 -17
  183. package/worker/mail/isolate-cache.ts +1 -1
  184. package/worker/mail/mailbox-deletion.ts +33 -12
  185. package/worker/mail/mailbox-directory.ts +39 -65
  186. package/worker/mail/mailing-list.ts +29 -58
  187. package/worker/mail/message-security.ts +3 -3
  188. package/worker/mail/message-view.ts +12 -10
  189. package/worker/mail/office-conversion.ts +3 -5
  190. package/worker/mail/office-preview-container.ts +3 -8
  191. package/worker/mail/outbound-content.ts +139 -65
  192. package/worker/mail/outbound-delivery.ts +20 -17
  193. package/worker/mail/outbound-service.ts +34 -9
  194. package/worker/mail/outbound.ts +146 -75
  195. package/worker/mail/outgoing-context.ts +60 -30
  196. package/worker/mail/participants.ts +10 -15
  197. package/worker/mail/plain-body.ts +39 -0
  198. package/worker/mail/push-endpoint.ts +17 -0
  199. package/worker/mail/push-notifications.ts +53 -62
  200. package/worker/mail/quote-parser.ts +36 -24
  201. package/worker/mail/receive.ts +146 -39
  202. package/worker/mail/recipients.ts +1 -1
  203. package/worker/mail/remote-proxy.ts +3 -3
  204. package/worker/mail/repeat.ts +1 -1
  205. package/worker/mail/reply-plan.ts +124 -26
  206. package/worker/mail/rfc.ts +15 -7
  207. package/worker/mail/rich-content.ts +207 -0
  208. package/worker/mail/routes.ts +696 -304
  209. package/worker/mail/routing.ts +274 -0
  210. package/worker/mail/rules.ts +230 -0
  211. package/worker/mail/safety-routes.ts +88 -86
  212. package/worker/mail/schemas.ts +222 -31
  213. package/worker/mail/search.ts +25 -2
  214. package/worker/mail/security-settings.ts +4 -10
  215. package/worker/mail/send-message.ts +238 -0
  216. package/worker/mail/sender-intel.ts +12 -20
  217. package/worker/mail/signature-routes.ts +201 -0
  218. package/worker/mail/signatures.ts +195 -0
  219. package/worker/mail/spamhaus.ts +28 -16
  220. package/worker/mail/storage-keys.ts +35 -0
  221. package/worker/mail/unsubscribe.ts +19 -41
  222. package/worker/mail/uploads.ts +192 -80
  223. package/worker/mail/webhook-source.ts +3 -2
  224. package/worker/mail/workspace-safety.ts +194 -60
  225. package/worker/mailbox/demo.ts +13 -15
  226. package/worker/mailbox/email-index.ts +65 -35
  227. package/worker/mailbox/folder-model.ts +0 -20
  228. package/worker/mailbox/import-placement.ts +21 -36
  229. package/worker/mailbox/inbound-decision.ts +114 -0
  230. package/worker/mailbox/inbox-counts.ts +45 -25
  231. package/worker/mailbox/index.ts +756 -3201
  232. package/worker/mailbox/jmap-change-log.ts +195 -0
  233. package/worker/mailbox/jmap-email-rows.ts +186 -0
  234. package/worker/mailbox/jmap-mailboxes.ts +116 -0
  235. package/worker/mailbox/jmap-placement.ts +139 -0
  236. package/worker/mailbox/jmap-query-filter.ts +231 -0
  237. package/worker/mailbox/jmap-store.ts +167 -299
  238. package/worker/mailbox/mailbox-auto-reply.ts +301 -0
  239. package/worker/mailbox/mailbox-backup.ts +538 -0
  240. package/worker/mailbox/mailbox-drafts.ts +417 -0
  241. package/worker/mailbox/mailbox-folders.ts +242 -0
  242. package/worker/mailbox/mailbox-import.ts +450 -0
  243. package/worker/mailbox/mailbox-inbound.ts +934 -0
  244. package/worker/mailbox/mailbox-jmap.ts +294 -0
  245. package/worker/mailbox/mailbox-outbound.ts +413 -0
  246. package/worker/mailbox/mailbox-rules.ts +336 -0
  247. package/worker/mailbox/mailbox-senders.ts +294 -0
  248. package/worker/mailbox/mailbox-storage.ts +438 -0
  249. package/worker/mailbox/mailbox-views.ts +891 -0
  250. package/worker/mailbox/model.ts +1 -0
  251. package/worker/mailbox/office-preview-jobs.ts +59 -47
  252. package/worker/mailbox/placement.ts +933 -0
  253. package/worker/mailbox/restore-thread.ts +52 -0
  254. package/worker/mailbox/retention.ts +36 -0
  255. package/worker/mailbox/schema.ts +200 -1
  256. package/worker/mailbox/senders.ts +216 -178
  257. package/worker/mailbox/spam-undo-filing.ts +138 -0
  258. package/worker/mailbox/spam-undo.ts +240 -0
  259. package/worker/mailbox/support.ts +416 -0
  260. package/worker/mcp/mail-action-tools.ts +156 -0
  261. package/worker/mcp/mail-compose-tools.ts +7 -1
  262. package/worker/mcp/mail-safety-tools.ts +4 -4
  263. package/worker/module.ts +23 -4
  264. package/src/mail/use-compose-submission.ts +0 -208
  265. package/src/mail/use-reply-submission.ts +0 -114
  266. package/worker/mail/capped-body.ts +0 -36
@@ -1,23 +1,26 @@
1
1
  import {
2
2
  Button,
3
3
  Checkbox,
4
+ DelayedFallback,
4
5
  Input,
5
6
  Skeleton,
7
+ queryErrorBlocksContent,
6
8
  responseJson,
7
9
  adminPanelClass,
8
10
  AdminPanelBody,
9
11
  AdminPanelHeader,
12
+ AdminQueryError,
10
13
  } from "@open-workspace/core/ui";
11
14
  import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
12
15
  import { Building2, LoaderCircle, ShieldBan } from "lucide-react";
13
- import { useState } from "react";
14
- import type { FormEvent } from "react";
16
+ import { useController, useForm, useWatch } from "react-hook-form";
15
17
  import { toast } from "sonner";
16
18
 
17
19
  import { normalizeBlockPattern } from "../../shared/mail-feedback";
18
20
  import { mailApi } from "../lib/mail-api";
21
+ import { adminBlockedSendersQuery } from "../settings/settings-queries";
19
22
 
20
- const BLOCKED_SENDERS_KEY = ["admin-blocked-senders"] as const;
23
+ const BLOCKED_SENDERS_KEY = adminBlockedSendersQuery.queryKey;
21
24
 
22
25
  function formatDate(value: string | Date) {
23
26
  return new Intl.DateTimeFormat("en", { dateStyle: "medium" }).format(
@@ -35,11 +38,7 @@ function useWorkspaceBlocks() {
35
38
  const invalidate = () =>
36
39
  client.invalidateQueries({ queryKey: BLOCKED_SENDERS_KEY });
37
40
 
38
- const list = useQuery({
39
- queryKey: BLOCKED_SENDERS_KEY,
40
- queryFn: async () =>
41
- responseJson(await mailApi.api.admin["blocked-senders"].$get()),
42
- });
41
+ const list = useQuery(adminBlockedSendersQuery);
43
42
 
44
43
  const block = useMutation({
45
44
  mutationFn: async (input: { pattern: string; moveExisting: boolean }) =>
@@ -85,17 +84,27 @@ function useWorkspaceBlocks() {
85
84
  /** Administration → Blocked senders: addresses and domains blocked for every mailbox. */
86
85
  export function BlockedSenders() {
87
86
  const { list, block, unblock } = useWorkspaceBlocks();
88
- const [value, setValue] = useState("");
89
- const [moveExisting, setMoveExisting] = useState(true);
90
- const pattern = normalizeBlockPattern(value);
91
- const items = list.data?.blockedSenders ?? [];
92
87
 
93
- function submit(event: FormEvent) {
94
- event.preventDefault();
88
+ const { control, handleSubmit, register, resetField } = useForm({
89
+ defaultValues: { value: "", moveExisting: true },
90
+ });
91
+
92
+ const pattern = normalizeBlockPattern(useWatch({ control, name: "value" }));
93
+
94
+ const moveExistingField = useController({
95
+ control,
96
+ name: "moveExisting",
97
+ }).field;
98
+
99
+ const items = list.data?.blockedSenders ?? [];
95
100
 
101
+ const submit = handleSubmit(({ moveExisting }) => {
96
102
  if (!pattern) return;
97
- block.mutate({ pattern, moveExisting }, { onSuccess: () => setValue("") });
98
- }
103
+ block.mutate(
104
+ { pattern, moveExisting },
105
+ { onSuccess: () => resetField("value") }
106
+ );
107
+ });
99
108
 
100
109
  return (
101
110
  <div className="max-w-4xl">
@@ -111,8 +120,7 @@ export function BlockedSenders() {
111
120
  <Input
112
121
  aria-label="Address or domain to block for everyone"
113
122
  placeholder="name@example.com or example.com"
114
- value={value}
115
- onChange={(event) => setValue(event.target.value)}
123
+ {...register("value")}
116
124
  />
117
125
  <Button
118
126
  type="submit"
@@ -129,59 +137,71 @@ export function BlockedSenders() {
129
137
  </div>
130
138
  <label className="text-muted-foreground flex items-center gap-2 text-xs">
131
139
  <Checkbox
132
- checked={moveExisting}
133
- onCheckedChange={(checked) => setMoveExisting(checked === true)}
140
+ checked={moveExistingField.value}
141
+ onCheckedChange={(checked) =>
142
+ moveExistingField.onChange(checked === true)
143
+ }
134
144
  />
135
145
  Also move mail already delivered from it to Spam
136
146
  </label>
137
147
  </form>
138
- {list.isLoading ? (
139
- <div className="space-y-2">
140
- <Skeleton className="h-12" />
141
- <Skeleton className="h-12" />
142
- </div>
143
- ) : items.length ? (
144
- <ul
145
- aria-label="Senders blocked for everyone"
146
- className="divide-border/70 border-border divide-y rounded-lg border"
147
- >
148
- {items.map((item) => (
149
- <li
150
- key={item.pattern}
151
- className="flex items-center gap-3 px-3.5 py-2.5"
152
- >
153
- {item.scope === "domain" ? (
154
- <Building2 className="text-muted-foreground size-4 shrink-0" />
155
- ) : (
156
- <ShieldBan className="text-muted-foreground size-4 shrink-0" />
157
- )}
158
- <span className="min-w-0 flex-1">
159
- <span className="block truncate text-sm font-medium">
160
- {item.pattern}
161
- </span>
162
- <span className="text-muted-foreground block truncate text-xs">
163
- {item.scope === "domain" ? "Whole domain" : "Address"}
164
- {" · "}blocked {formatDate(item.createdAt)}
165
- {item.createdByName ? ` by ${item.createdByName}` : ""}
166
- </span>
167
- </span>
168
- <Button
169
- size="sm"
170
- variant="ghost"
171
- aria-label={`Unblock ${item.pattern}`}
172
- disabled={unblock.isPending}
173
- onClick={() => unblock.mutate(item.pattern)}
148
+ <DelayedFallback
149
+ pending={list.isPending}
150
+ fallback={
151
+ <div className="space-y-2">
152
+ <Skeleton className="h-12" />
153
+ <Skeleton className="h-12" />
154
+ </div>
155
+ }
156
+ >
157
+ {queryErrorBlocksContent(list) ? (
158
+ <AdminQueryError
159
+ query={list}
160
+ message="The blocked senders couldn't be loaded."
161
+ />
162
+ ) : items.length ? (
163
+ <ul
164
+ aria-label="Senders blocked for everyone"
165
+ className="divide-border/70 border-border divide-y rounded-lg border"
166
+ >
167
+ {items.map((item) => (
168
+ <li
169
+ key={item.pattern}
170
+ className="flex items-center gap-3 px-3.5 py-2.5"
174
171
  >
175
- Unblock
176
- </Button>
177
- </li>
178
- ))}
179
- </ul>
180
- ) : (
181
- <p className="text-muted-foreground py-6 text-center text-sm">
182
- No senders are blocked for everyone.
183
- </p>
184
- )}
172
+ {item.scope === "domain" ? (
173
+ <Building2 className="text-muted-foreground size-4 shrink-0" />
174
+ ) : (
175
+ <ShieldBan className="text-muted-foreground size-4 shrink-0" />
176
+ )}
177
+ <span className="min-w-0 flex-1">
178
+ <span className="block truncate text-sm font-medium">
179
+ {item.pattern}
180
+ </span>
181
+ <span className="text-muted-foreground block truncate text-xs">
182
+ {item.scope === "domain" ? "Whole domain" : "Address"}
183
+ {" · "}blocked {formatDate(item.createdAt)}
184
+ {item.createdByName ? ` by ${item.createdByName}` : ""}
185
+ </span>
186
+ </span>
187
+ <Button
188
+ size="sm"
189
+ variant="ghost"
190
+ aria-label={`Unblock ${item.pattern}`}
191
+ disabled={unblock.isPending}
192
+ onClick={() => unblock.mutate(item.pattern)}
193
+ >
194
+ Unblock
195
+ </Button>
196
+ </li>
197
+ ))}
198
+ </ul>
199
+ ) : (
200
+ <p className="text-muted-foreground py-6 text-center text-sm">
201
+ No senders are blocked for everyone.
202
+ </p>
203
+ )}
204
+ </DelayedFallback>
185
205
  </AdminPanelBody>
186
206
  </div>
187
207
  </div>
@@ -0,0 +1,140 @@
1
+ import {
2
+ Badge,
3
+ Skeleton,
4
+ responseJson,
5
+ useAddressSection,
6
+ } from "@open-workspace/core/ui";
7
+ import type { TeamAddress, UserAddress } from "@open-workspace/core/ui";
8
+ import { useQuery, useQueryClient } from "@tanstack/react-query";
9
+
10
+ import { mailApi } from "../lib/mail-api";
11
+
12
+ /**
13
+ * A forward's destinations in the address editor (People or Teams), each
14
+ * Confirmed or Waiting for confirmation: Cloudflare forwards to one only
15
+ * after it followed the link in Cloudflare's email. Saving the address adds
16
+ * new destinations; one that Email Routing still lacks (adding it failed)
17
+ * makes this section ask "Save address" to add it again, and shows why it
18
+ * failed (worker/mail/forward-destinations.ts).
19
+ */
20
+ export function ForwardDestinations({
21
+ address,
22
+ }: {
23
+ address: TeamAddress | UserAddress;
24
+ }) {
25
+ if (address.delivery !== "forward" || !address.forwardTo.length) return null;
26
+
27
+ return (
28
+ <DestinationList
29
+ mailboxId={address.mailboxId}
30
+ forwardTo={address.forwardTo}
31
+ />
32
+ );
33
+ }
34
+
35
+ function DestinationList({
36
+ mailboxId,
37
+ forwardTo,
38
+ }: {
39
+ mailboxId: string;
40
+ /** As the form has them, saved or not. */
41
+ forwardTo: string[];
42
+ }) {
43
+ const queryClient = useQueryClient();
44
+ const queryKey = ["forward-destinations", mailboxId];
45
+ const param = { mailboxId };
46
+
47
+ const saved = useQuery({
48
+ queryKey,
49
+ queryFn: async () =>
50
+ responseJson(
51
+ await mailApi.api.mail["forward-destinations"][":mailboxId"].$get({
52
+ param,
53
+ })
54
+ ),
55
+ });
56
+
57
+ const statuses = new Map(
58
+ (saved.data?.destinations ?? []).map((destination) => [
59
+ destination.email.toLowerCase(),
60
+ destination.status,
61
+ ])
62
+ );
63
+
64
+ // A new destination, or a saved one Email Routing lacks: saving adds it.
65
+ const missing =
66
+ saved.data?.connected === true &&
67
+ forwardTo.some((email) => {
68
+ const status = statuses.get(email);
69
+
70
+ return status === undefined || status === "missing";
71
+ });
72
+
73
+ useAddressSection({
74
+ dirty: missing,
75
+ // Runs after the address is saved, so the new destinations are stored.
76
+ save: async () => {
77
+ const response = await mailApi.api.mail["forward-destinations"][
78
+ ":mailboxId"
79
+ ].$post({ param });
80
+
81
+ // Either way, show what Email Routing has now.
82
+ await queryClient.invalidateQueries({ queryKey });
83
+ await responseJson(response);
84
+ },
85
+ });
86
+
87
+ return (
88
+ <div className="space-y-2">
89
+ <p className="text-foreground/85 text-[0.8125rem] font-semibold">
90
+ Forward destinations
91
+ </p>
92
+ <ul className="divide-border/60 ring-border divide-y overflow-hidden rounded-xl ring-1">
93
+ {forwardTo.map((email) => {
94
+ const status = statuses.get(email);
95
+
96
+ return (
97
+ <li key={email} className="flex items-center gap-3 px-3.5 py-2.5">
98
+ <span className="min-w-0 flex-1 truncate text-[0.8125rem]">
99
+ {email}
100
+ </span>
101
+ {saved.isPending ? (
102
+ <Skeleton className="h-5 w-24" />
103
+ ) : status === undefined ? (
104
+ <span className="text-muted-foreground text-xs">
105
+ Gets a confirmation email when you save
106
+ </span>
107
+ ) : status === "missing" ? (
108
+ <Badge variant="destructive">Not added to Cloudflare</Badge>
109
+ ) : status === "confirmed" ? (
110
+ <Badge variant="success">Confirmed</Badge>
111
+ ) : status === "waiting" ? (
112
+ <Badge variant="warning">Waiting for confirmation</Badge>
113
+ ) : null}
114
+ </li>
115
+ );
116
+ })}
117
+ </ul>
118
+ {saved.isError ? (
119
+ <p className="text-destructive text-xs">
120
+ Couldn't check these with Cloudflare: {saved.error.message}
121
+ </p>
122
+ ) : saved.data && !saved.data.connected ? (
123
+ <p className="text-muted-foreground text-xs">
124
+ Email Routing isn't connected here (local development), so
125
+ confirmations can't be checked and nothing is forwarded.
126
+ </p>
127
+ ) : forwardTo.some((email) => statuses.get(email) === "missing") ? (
128
+ <p className="text-destructive text-xs">
129
+ Cloudflare didn't add a destination, so it got no confirmation email.
130
+ Save the address to try again.
131
+ </p>
132
+ ) : (
133
+ <p className="text-muted-foreground text-xs">
134
+ Mail goes only to confirmed destinations. Each new one gets an email
135
+ from Cloudflare with a link to confirm it.
136
+ </p>
137
+ )}
138
+ </div>
139
+ );
140
+ }
@@ -0,0 +1,134 @@
1
+ import {
2
+ Button,
3
+ DelayedFallback,
4
+ Skeleton,
5
+ queryErrorBlocksContent,
6
+ responseJson,
7
+ adminPanelClass,
8
+ AdminPanelBody,
9
+ AdminPanelHeader,
10
+ AdminQueryError,
11
+ } from "@open-workspace/core/ui";
12
+ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
13
+ import { Archive, LoaderCircle, Play } from "lucide-react";
14
+ import { toast } from "sonner";
15
+
16
+ import { mailApi } from "../lib/mail-api";
17
+ import { mailArchiveQuery } from "../settings/settings-queries";
18
+
19
+ const ARCHIVE_KEY = mailArchiveQuery.queryKey;
20
+
21
+ function formatTime(value: number) {
22
+ return new Intl.DateTimeFormat("en", {
23
+ dateStyle: "medium",
24
+ timeStyle: "short",
25
+ }).format(new Date(value));
26
+ }
27
+
28
+ /** Administration → Mail archive: counts, messages not archived yet, and Archive now. */
29
+ export function MailArchive() {
30
+ const client = useQueryClient();
31
+
32
+ const status = useQuery(mailArchiveQuery);
33
+
34
+ const run = useMutation({
35
+ mutationFn: async () =>
36
+ responseJson(await mailApi.api.mail.archive.run.$post()),
37
+ onSuccess: async () => {
38
+ toast.success("Archiving the open messages");
39
+ await client.invalidateQueries({ queryKey: ARCHIVE_KEY });
40
+ },
41
+ onError: (error) => toast.error(error.message),
42
+ });
43
+
44
+ const { data } = status;
45
+
46
+ return (
47
+ <div className="max-w-4xl">
48
+ <div className={adminPanelClass}>
49
+ <AdminPanelHeader
50
+ Icon={Archive}
51
+ title="Mail archive"
52
+ description="A write-once copy of every received and sent message, in a bucket of its own."
53
+ />
54
+ <AdminPanelBody className="space-y-4">
55
+ <DelayedFallback
56
+ pending={status.isPending}
57
+ fallback={<Skeleton className="h-20" />}
58
+ >
59
+ {queryErrorBlocksContent(status) ? (
60
+ <AdminQueryError
61
+ query={status}
62
+ message="The mail archive status couldn't be loaded."
63
+ />
64
+ ) : data?.enabled ? (
65
+ <>
66
+ <dl className="grid grid-cols-[auto_1fr] gap-x-6 gap-y-1.5 text-sm">
67
+ {data.years ? (
68
+ <>
69
+ <dt className="text-muted-foreground">Locked for</dt>
70
+ <dd>
71
+ {data.years === 1 ? "1 year" : `${data.years} years`}
72
+ </dd>
73
+ </>
74
+ ) : null}
75
+ <dt className="text-muted-foreground">Archived</dt>
76
+ <dd>{data.archived}</dd>
77
+ <dt className="text-muted-foreground">Waiting</dt>
78
+ <dd>{data.pending}</dd>
79
+ <dt className="text-muted-foreground">Failed</dt>
80
+ <dd>{data.failed}</dd>
81
+ <dt className="text-muted-foreground">Not archived</dt>
82
+ <dd>{data.abandoned}</dd>
83
+ </dl>
84
+ {data.open.length ? (
85
+ <ul
86
+ aria-label="Messages not archived"
87
+ className="divide-border/70 border-border divide-y rounded-lg border"
88
+ >
89
+ {data.open.map((entry) => (
90
+ <li key={entry.id} className="px-3.5 py-2.5 text-sm">
91
+ <span className="block truncate font-medium">
92
+ {entry.mailbox ?? "Deleted address"} ·{" "}
93
+ {entry.direction === "incoming" ? "received" : "sent"}{" "}
94
+ {formatTime(entry.createdAt)}
95
+ {entry.status === "abandoned"
96
+ ? " · not archived, no longer retried"
97
+ : ""}
98
+ </span>
99
+ <span className="text-muted-foreground block text-xs break-all">
100
+ {entry.lastError ?? "Waiting for its first copy"}
101
+ {entry.attempts ? ` · ${entry.attempts} tries` : ""}
102
+ </span>
103
+ </li>
104
+ ))}
105
+ </ul>
106
+ ) : null}
107
+ <Button
108
+ variant="outline"
109
+ disabled={run.isPending}
110
+ onClick={() => run.mutate()}
111
+ >
112
+ {run.isPending ? (
113
+ <LoaderCircle className="animate-spin" />
114
+ ) : (
115
+ <Play />
116
+ )}
117
+ Archive now
118
+ </Button>
119
+ </>
120
+ ) : (
121
+ <p className="text-muted-foreground text-sm">
122
+ The mail archive is off. Deploy with{" "}
123
+ <code className="text-foreground">
124
+ {"Email({ archive: { years: 10 } })"}
125
+ </code>{" "}
126
+ to keep a write-once copy of every message.
127
+ </p>
128
+ )}
129
+ </DelayedFallback>
130
+ </AdminPanelBody>
131
+ </div>
132
+ </div>
133
+ );
134
+ }
@@ -0,0 +1,90 @@
1
+ import { DelayedFallback, responseJson } from "@open-workspace/core/ui";
2
+ import type { TeamAddress } from "@open-workspace/core/ui";
3
+ import { useQuery, useQueryClient } from "@tanstack/react-query";
4
+ import { LoaderCircle } from "lucide-react";
5
+
6
+ import { mailApi } from "../lib/mail-api";
7
+ import { mailboxesQuery } from "../mail/mail-queries";
8
+ import { SignatureEditor } from "../settings/signature-editor";
9
+ import type { SignatureBody } from "../settings/signature-editor";
10
+
11
+ /**
12
+ * A shared inbox's signature, in its address editor under Administration →
13
+ * Teams. It follows the delivery chosen in the form: switched to a mailing
14
+ * list, it hides and is no longer saved with "Save address".
15
+ */
16
+ export function TeamSignature({ address }: { address: TeamAddress }) {
17
+ // Mailing lists copy mail to members; only shared inboxes send replies.
18
+ return address.delivery === "inbox" ? (
19
+ <TeamSignatureSection address={address} />
20
+ ) : null;
21
+ }
22
+
23
+ function TeamSignatureSection({ address }: { address: TeamAddress }) {
24
+ const client = useQueryClient();
25
+ const mailboxes = useQuery(mailboxesQuery());
26
+ const param = { mailboxId: address.mailboxId };
27
+
28
+ // Read once per opening: the editor keeps its own state after loading it.
29
+ const saved = useQuery({
30
+ queryKey: ["team-signature", address.mailboxId],
31
+ queryFn: async () =>
32
+ responseJson(
33
+ await mailApi.api.mail["team-signatures"][":mailboxId"].$get({ param })
34
+ ),
35
+ staleTime: Number.POSITIVE_INFINITY,
36
+ gcTime: 0,
37
+ });
38
+
39
+ // Images upload through a mailbox the administrator can send from.
40
+ const sendable = mailboxes.data?.mailboxes.filter(
41
+ (mailbox) => mailbox.canSend
42
+ );
43
+
44
+ const uploadMailbox =
45
+ sendable?.find((mailbox) => mailbox.id === address.mailboxId) ??
46
+ sendable?.[0];
47
+
48
+ async function save(body: SignatureBody) {
49
+ await responseJson(
50
+ await mailApi.api.mail["team-signatures"][":mailboxId"].$put({
51
+ param,
52
+ json: body,
53
+ })
54
+ );
55
+ await client.invalidateQueries({ queryKey: mailboxesQuery().queryKey });
56
+ }
57
+
58
+ return (
59
+ <div className="space-y-2">
60
+ <h3 className="text-sm leading-none font-medium">Signature</h3>
61
+ <p className="text-muted-foreground text-xs">
62
+ Added to messages sent from {address.address}. {"{name}"} becomes the
63
+ sender's name. Without one, messages from here get no signature.
64
+ </p>
65
+ <DelayedFallback
66
+ pending={saved.isPending || mailboxes.isPending}
67
+ fallback={
68
+ <div className="text-muted-foreground flex items-center gap-2 py-3 text-sm">
69
+ <LoaderCircle className="size-4 animate-spin" /> Loading signature…
70
+ </div>
71
+ }
72
+ >
73
+ {saved.data ? (
74
+ <SignatureEditor
75
+ saved={saved.data.signature}
76
+ uploadMailboxId={uploadMailbox?.id}
77
+ label={`Signature for ${address.address}`}
78
+ placeholder={`{name}, ${address.displayName}…`}
79
+ section
80
+ onSave={save}
81
+ />
82
+ ) : (
83
+ <p className="text-destructive text-sm">
84
+ The signature couldn't be loaded.
85
+ </p>
86
+ )}
87
+ </DelayedFallback>
88
+ </div>
89
+ );
90
+ }