@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
@@ -0,0 +1,454 @@
1
+ import { zodResolver } from "@hookform/resolvers/zod";
2
+ import {
3
+ ApiError,
4
+ Badge,
5
+ Button,
6
+ DateTimePicker,
7
+ FieldError,
8
+ Input,
9
+ Label,
10
+ PlaceholderFrame,
11
+ RadioGroup,
12
+ RadioCard,
13
+ Switch,
14
+ queryErrorBlocksContent,
15
+ responseJson,
16
+ useAddressSection,
17
+ useDelayedPending,
18
+ } from "@open-workspace/core/ui";
19
+ import type { SuccessfulResponse } from "@open-workspace/core/ui";
20
+ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
21
+ import { LoaderCircle } from "lucide-react";
22
+ import { useRef } from "react";
23
+ import { useController, useForm } from "react-hook-form";
24
+ import type { Control, UseFormRegister } from "react-hook-form";
25
+ import { toast } from "sonner";
26
+ import { z } from "zod";
27
+
28
+ import { autoReplyAudiences } from "../../shared/mail";
29
+ import type { AutoReplyAudience } from "../../shared/mail";
30
+ import { mailApi } from "../lib/mail-api";
31
+ import { RichContentEditor } from "./rich-content-editor";
32
+ import type { RichContentEditorHandle } from "./rich-content-editor";
33
+ import { autoReplyQuery } from "./settings-queries";
34
+
35
+ const autoReplyRoute = mailApi.api.mail.mailboxes[":id"]["auto-reply"];
36
+
37
+ export type AutoReplyState = SuccessfulResponse<
38
+ Awaited<ReturnType<typeof autoReplyRoute.$get>>
39
+ >;
40
+
41
+ type SavedMessage = NonNullable<AutoReplyState["settings"]>["inside"];
42
+
43
+ /** Where the app shows a saved auto-reply image (worker/mail/auto-reply-routes.ts). */
44
+ function autoReplyImageUrl(mailboxId: string, contentId: string) {
45
+ return `/api/mail/mailboxes/${encodeURIComponent(mailboxId)}/auto-reply/images/${encodeURIComponent(contentId)}`;
46
+ }
47
+
48
+ function at(value: string | Date | null | undefined) {
49
+ return value
50
+ ? new Date(value).toLocaleString(undefined, {
51
+ dateStyle: "medium",
52
+ timeStyle: "short",
53
+ })
54
+ : "";
55
+ }
56
+
57
+ function autoReplyStatusLabel(state: AutoReplyState) {
58
+ const { status, settings } = state;
59
+
60
+ if (status === "scheduled") return `Starts ${at(settings?.startsAt)}`;
61
+
62
+ if (status === "active") {
63
+ return settings?.endsAt ? `On until ${at(settings.endsAt)}` : "On";
64
+ }
65
+
66
+ if (status === "ended") return `Ended ${at(settings?.endsAt)}`;
67
+
68
+ return "Off";
69
+ }
70
+
71
+ function dateOf(value: string | Date | null | undefined) {
72
+ return value ? new Date(value) : null;
73
+ }
74
+
75
+ const intervalMessage = "Enter a whole number of days from 1 to 30";
76
+
77
+ /** The rich text lives in the editors; `body` is each one's HTML, for dirtiness. */
78
+ const messageSchema = z.object({
79
+ enabled: z.boolean(),
80
+ subject: z.string(),
81
+ body: z.string(),
82
+ });
83
+
84
+ const autoReplySchema = z.object({
85
+ enabled: z.boolean(),
86
+ startsAt: z.number().nullable(),
87
+ endsAt: z.number().nullable(),
88
+ replyIntervalDays: z
89
+ .number(intervalMessage)
90
+ .int(intervalMessage)
91
+ .min(1, intervalMessage)
92
+ .max(30, intervalMessage),
93
+ inside: messageSchema,
94
+ outside: messageSchema.extend({ audience: z.enum(autoReplyAudiences) }),
95
+ });
96
+
97
+ type AutoReplyValues = z.infer<typeof autoReplySchema>;
98
+
99
+ /** One of the two messages: whether it is sent, its subject and its text. */
100
+ function MessageFields({
101
+ id,
102
+ name,
103
+ title,
104
+ description,
105
+ mailboxId,
106
+ saved,
107
+ uploadMailboxId,
108
+ control,
109
+ register,
110
+ editor,
111
+ children,
112
+ }: {
113
+ id: string;
114
+ name: "inside" | "outside";
115
+ title: string;
116
+ description: string;
117
+ mailboxId: string;
118
+ saved: SavedMessage | undefined;
119
+ uploadMailboxId: string | undefined;
120
+ control: Control<AutoReplyValues>;
121
+ register: UseFormRegister<AutoReplyValues>;
122
+ editor: React.RefObject<RichContentEditorHandle | null>;
123
+ children?: React.ReactNode;
124
+ }) {
125
+ const enabled = useController({ control, name: `${name}.enabled` }).field;
126
+ const body = useController({ control, name: `${name}.body` }).field;
127
+
128
+ return (
129
+ <fieldset className="border-border/70 min-w-0 space-y-3 rounded-lg border p-3">
130
+ <div className="flex items-start gap-3">
131
+ <div className="min-w-0 flex-1">
132
+ <legend className="text-sm font-medium">{title}</legend>
133
+ <p className="text-muted-foreground text-xs">{description}</p>
134
+ </div>
135
+ <Switch
136
+ aria-label={`Reply to ${title.toLowerCase()}`}
137
+ checked={enabled.value}
138
+ onCheckedChange={enabled.onChange}
139
+ />
140
+ </div>
141
+ {children}
142
+ <div className="space-y-1.5">
143
+ <Label htmlFor={`${id}-subject`}>Subject</Label>
144
+ <Input
145
+ id={`${id}-subject`}
146
+ maxLength={998}
147
+ placeholder="Auto: <original subject>"
148
+ {...register(`${name}.subject`)}
149
+ />
150
+ </div>
151
+ <RichContentEditor
152
+ ref={editor}
153
+ saved={saved ? { ownerId: mailboxId, ...saved } : null}
154
+ imageUrl={autoReplyImageUrl}
155
+ uploadMailboxId={uploadMailboxId}
156
+ label={`Message to ${title.toLowerCase()}`}
157
+ placeholder="Thanks for your message. I'm away until Monday and will reply when I'm back."
158
+ disabled={false}
159
+ onChange={body.onChange}
160
+ />
161
+ </fieldset>
162
+ );
163
+ }
164
+
165
+ /**
166
+ * A mailbox's auto-reply: when it runs, and one message for people inside
167
+ * the workspace and one for everyone else, each in the shared rich content
168
+ * editor and each able to be off. With a Save button of its own, or, inside
169
+ * an address editor (`section`), saved by its "Save address".
170
+ */
171
+ function AutoReplyForm({
172
+ mailbox,
173
+ state,
174
+ uploadMailboxId,
175
+ section,
176
+ }: {
177
+ mailbox: { id: string; address: string };
178
+ state: AutoReplyState;
179
+ uploadMailboxId: string | undefined;
180
+ section: boolean;
181
+ }) {
182
+ const client = useQueryClient();
183
+ const saved = state.settings;
184
+ const id = `auto-reply-${mailbox.id}`;
185
+ const insideEditor = useRef<RichContentEditorHandle>(null);
186
+ const outsideEditor = useRef<RichContentEditorHandle>(null);
187
+
188
+ const {
189
+ formState: { isDirty, isValid, defaultValues, errors },
190
+ control,
191
+ getValues,
192
+ handleSubmit,
193
+ register,
194
+ reset,
195
+ setValue,
196
+ } = useForm({
197
+ resolver: zodResolver(autoReplySchema),
198
+ mode: "onChange",
199
+ defaultValues: {
200
+ enabled: saved?.enabled ?? false,
201
+ startsAt: dateOf(saved?.startsAt)?.getTime() ?? null,
202
+ endsAt: dateOf(saved?.endsAt)?.getTime() ?? null,
203
+ replyIntervalDays: saved?.replyIntervalDays ?? 4,
204
+ inside: {
205
+ enabled: saved?.inside.enabled ?? true,
206
+ subject: saved?.inside.subject ?? "",
207
+ body: "",
208
+ },
209
+ outside: {
210
+ enabled: saved?.outside.enabled ?? true,
211
+ subject: saved?.outside.subject ?? "",
212
+ body: "",
213
+ audience: saved?.outside.audience ?? "everyone",
214
+ },
215
+ },
216
+ // The compiler keeps register's refs, so a reset keeps them too.
217
+ resetOptions: { keepFieldsRef: true },
218
+ });
219
+
220
+ const enabled = useController({ control, name: "enabled" }).field;
221
+ const startsAt = useController({ control, name: "startsAt" }).field;
222
+ const endsAt = useController({ control, name: "endsAt" }).field;
223
+ const audience = useController({ control, name: "outside.audience" }).field;
224
+ const intervalError = errors.replyIntervalDays?.message;
225
+
226
+ async function submit(values: AutoReplyValues) {
227
+ const inside = insideEditor.current?.value();
228
+ const outside = outsideEditor.current?.value();
229
+
230
+ if (!inside || !outside) return;
231
+
232
+ const result = await responseJson(
233
+ await autoReplyRoute.$put({
234
+ param: { id: mailbox.id },
235
+ json: {
236
+ enabled:
237
+ values.enabled === defaultValues?.enabled
238
+ ? undefined
239
+ : values.enabled,
240
+ startsAt: values.startsAt
241
+ ? new Date(values.startsAt).toISOString()
242
+ : null,
243
+ endsAt: values.endsAt ? new Date(values.endsAt).toISOString() : null,
244
+ replyIntervalDays: values.replyIntervalDays,
245
+ inside: {
246
+ ...inside,
247
+ enabled: values.inside.enabled,
248
+ subject: values.inside.subject || null,
249
+ },
250
+ outside: {
251
+ ...outside,
252
+ enabled: values.outside.enabled,
253
+ subject: values.outside.subject || null,
254
+ audience: values.outside.audience,
255
+ },
256
+ },
257
+ })
258
+ );
259
+
260
+ client.setQueryData(autoReplyQuery(mailbox.id).queryKey, result);
261
+ const savedEnabled = result.settings?.enabled ?? false;
262
+
263
+ // Apply the mailbox's switch unless the user changed it during this save.
264
+ if (getValues("enabled") === values.enabled) {
265
+ setValue("enabled", savedEnabled);
266
+ }
267
+
268
+ reset({ ...values, enabled: savedEnabled }, { keepValues: true });
269
+ }
270
+
271
+ const save = () =>
272
+ handleSubmit(submit, (invalid) => {
273
+ throw new Error(invalid.replyIntervalDays?.message ?? "Check the form");
274
+ })();
275
+
276
+ // Inside an address editor, its "Save address" saves this; elsewhere a no-op.
277
+ useAddressSection({
278
+ dirty: isDirty,
279
+ save: () =>
280
+ save().catch((error: Error) => {
281
+ throw new Error(`Auto-reply: ${error.message}`, { cause: error });
282
+ }),
283
+ });
284
+
285
+ const saveNow = useMutation({
286
+ mutationFn: save,
287
+ onSuccess: () => toast.success("Auto-reply saved"),
288
+ onError: (error) => toast.error(error.message),
289
+ });
290
+
291
+ return (
292
+ <div className="space-y-4">
293
+ <div className="flex flex-wrap items-center gap-3">
294
+ <label className="flex min-w-0 flex-1 items-center gap-2 text-sm">
295
+ <Switch checked={enabled.value} onCheckedChange={enabled.onChange} />
296
+ Send automatic replies from {mailbox.address}
297
+ </label>
298
+ <Badge variant={state.status === "active" ? "default" : "outline"}>
299
+ {autoReplyStatusLabel(state)}
300
+ </Badge>
301
+ </div>
302
+
303
+ <div className="grid gap-3 sm:grid-cols-2">
304
+ <div className="space-y-1.5">
305
+ <Label htmlFor={`${id}-start`}>Start</Label>
306
+ <DateTimePicker
307
+ id={`${id}-start`}
308
+ value={startsAt.value === null ? null : new Date(startsAt.value)}
309
+ placeholder="Now"
310
+ onChange={(value) => startsAt.onChange(value?.getTime() ?? null)}
311
+ />
312
+ </div>
313
+ <div className="space-y-1.5">
314
+ <Label htmlFor={`${id}-end`}>End</Label>
315
+ <DateTimePicker
316
+ id={`${id}-end`}
317
+ value={endsAt.value === null ? null : new Date(endsAt.value)}
318
+ placeholder="Until turned off"
319
+ // A day picked as the last one away ends with it, not at 9 AM.
320
+ defaultTime="23:59"
321
+ onChange={(value) => endsAt.onChange(value?.getTime() ?? null)}
322
+ />
323
+ </div>
324
+ </div>
325
+
326
+ <MessageFields
327
+ id={`${id}-inside`}
328
+ name="inside"
329
+ title="People in this workspace"
330
+ description="Senders at the workspace's own mail domains."
331
+ mailboxId={mailbox.id}
332
+ saved={saved?.inside}
333
+ uploadMailboxId={uploadMailboxId}
334
+ control={control}
335
+ register={register}
336
+ editor={insideEditor}
337
+ />
338
+
339
+ <MessageFields
340
+ id={`${id}-outside`}
341
+ name="outside"
342
+ title="People outside the workspace"
343
+ description="Everyone else who writes to this address."
344
+ mailboxId={mailbox.id}
345
+ saved={saved?.outside}
346
+ uploadMailboxId={uploadMailboxId}
347
+ control={control}
348
+ register={register}
349
+ editor={outsideEditor}
350
+ >
351
+ <RadioGroup
352
+ className="sm:grid-cols-2"
353
+ aria-label="Who outside the workspace gets it"
354
+ value={audience.value}
355
+ onValueChange={(value: AutoReplyAudience) => audience.onChange(value)}
356
+ >
357
+ <RadioCard value="everyone" className="text-sm">
358
+ Everyone
359
+ </RadioCard>
360
+ <RadioCard value="knownSenders" className="text-sm">
361
+ Only people this mailbox has written to
362
+ </RadioCard>
363
+ </RadioGroup>
364
+ </MessageFields>
365
+
366
+ <div className="flex flex-wrap items-center gap-2 text-sm">
367
+ <Label htmlFor={`${id}-interval`} className="font-normal">
368
+ The same sender gets at most one reply every
369
+ </Label>
370
+ <Input
371
+ id={`${id}-interval`}
372
+ type="number"
373
+ min={1}
374
+ max={30}
375
+ className="w-16"
376
+ aria-invalid={Boolean(intervalError) || undefined}
377
+ aria-describedby={intervalError ? `${id}-interval-error` : undefined}
378
+ {...register("replyIntervalDays", { valueAsNumber: true })}
379
+ />
380
+ <span>days</span>
381
+ {section ? null : (
382
+ <Button
383
+ type="button"
384
+ size="sm"
385
+ className="ml-auto"
386
+ disabled={saveNow.isPending || !isDirty || !isValid}
387
+ onClick={() => saveNow.mutate()}
388
+ >
389
+ {saveNow.isPending && <LoaderCircle className="animate-spin" />}
390
+ Save
391
+ </Button>
392
+ )}
393
+ </div>
394
+ <FieldError id={`${id}-interval-error`}>{intervalError}</FieldError>
395
+ </div>
396
+ );
397
+ }
398
+
399
+ /** One mailbox's auto-reply, loaded; Settings, Teams and People use it. */
400
+ export function MailboxAutoReply({
401
+ mailbox,
402
+ uploadMailboxId,
403
+ section = false,
404
+ }: {
405
+ mailbox: { id: string; address: string };
406
+ /** A mailbox the person can send from, for images. */
407
+ uploadMailboxId: string | undefined;
408
+ section?: boolean;
409
+ }) {
410
+ const query = useQuery({ ...autoReplyQuery(mailbox.id), gcTime: 0 });
411
+ const spinner = useDelayedPending(query.isPending);
412
+
413
+ if (query.isPending || spinner) {
414
+ return (
415
+ <PlaceholderFrame shown={spinner}>
416
+ <div className="text-muted-foreground flex items-center gap-2 py-2 text-sm">
417
+ <LoaderCircle className="size-4 animate-spin" /> Loading auto-reply…
418
+ </div>
419
+ </PlaceholderFrame>
420
+ );
421
+ }
422
+
423
+ // In an address editor whose delivery was just switched to a mailbox:
424
+ // the address keeps no mail yet, so it has no auto-reply until saved.
425
+ if (
426
+ section &&
427
+ query.error instanceof ApiError &&
428
+ query.error.status === 404
429
+ ) {
430
+ return (
431
+ <p className="text-muted-foreground py-2 text-sm">
432
+ Save the address first, then set its auto-reply.
433
+ </p>
434
+ );
435
+ }
436
+
437
+ if (!query.data || queryErrorBlocksContent(query)) {
438
+ return (
439
+ <p className="text-destructive py-2 text-sm">
440
+ The auto-reply for {mailbox.address} couldn't be loaded.
441
+ </p>
442
+ );
443
+ }
444
+
445
+ return (
446
+ <AutoReplyForm
447
+ key={mailbox.id}
448
+ mailbox={mailbox}
449
+ state={query.data}
450
+ uploadMailboxId={uploadMailboxId}
451
+ section={section}
452
+ />
453
+ );
454
+ }
@@ -0,0 +1,59 @@
1
+ import {
2
+ adminPanelClass,
3
+ AdminPanelBody,
4
+ AdminPanelHeader,
5
+ DelayedFallback,
6
+ queryErrorBlocksContent,
7
+ } from "@open-workspace/core/ui";
8
+ import { useQuery } from "@tanstack/react-query";
9
+ import { LoaderCircle, Plane } from "lucide-react";
10
+
11
+ import { MailboxAutoReply } from "./auto-reply-editor";
12
+ import { autoReplyMailboxesQuery } from "./settings-queries";
13
+
14
+ /** Settings → Auto-reply: the person's own mailboxes, not team inboxes. */
15
+ export function AutoReplySettings() {
16
+ const mailboxes = useQuery(autoReplyMailboxesQuery);
17
+
18
+ const own = mailboxes.data?.mailboxes ?? [];
19
+
20
+ return (
21
+ <div className={adminPanelClass}>
22
+ <AdminPanelHeader
23
+ Icon={Plane}
24
+ title="Auto-reply"
25
+ description="Answer incoming mail automatically while you're away. Mailing lists, bounces, other auto-responders, Spam and Trash are never answered."
26
+ />
27
+ <AdminPanelBody className="divide-border/70 divide-y">
28
+ <DelayedFallback
29
+ pending={mailboxes.isPending}
30
+ fallback={
31
+ <div className="text-muted-foreground flex items-center gap-2 py-5 text-sm">
32
+ <LoaderCircle className="size-4 animate-spin" /> Loading
33
+ mailboxes…
34
+ </div>
35
+ }
36
+ >
37
+ {queryErrorBlocksContent(mailboxes) ? (
38
+ <p role="alert" className="text-destructive py-4 text-sm">
39
+ Mailboxes couldn’t be loaded.
40
+ </p>
41
+ ) : own.length > 0 ? (
42
+ own.map((mailbox) => (
43
+ <section key={mailbox.id} className="py-5 first:pt-0">
44
+ <MailboxAutoReply
45
+ mailbox={mailbox}
46
+ uploadMailboxId={mailbox.id}
47
+ />
48
+ </section>
49
+ ))
50
+ ) : (
51
+ <p className="text-muted-foreground py-4 text-sm">
52
+ You have no mailbox of your own.
53
+ </p>
54
+ )}
55
+ </DelayedFallback>
56
+ </AdminPanelBody>
57
+ </div>
58
+ );
59
+ }
@@ -5,6 +5,7 @@ import {
5
5
  Badge,
6
6
  Button,
7
7
  buttonVariants,
8
+ DelayedFallback,
8
9
  Input,
9
10
  Label,
10
11
  cn,
@@ -153,49 +154,57 @@ export function MailAppsSettings() {
153
154
  <SectionTitle title="Your mailboxes in the app">
154
155
  Every mailbox you can open shows up as its own account.
155
156
  </SectionTitle>
156
- {mailboxes.isLoading ? (
157
- <div className="text-muted-foreground flex items-center gap-2 py-2 text-sm">
158
- <LoaderCircle className="size-4 animate-spin" /> Loading
159
- mailboxes…
160
- </div>
161
- ) : mailboxes.isError ? (
162
- <p className="text-destructive text-sm">
163
- Could not load your mailboxes.
164
- </p>
165
- ) : (
166
- <ul
167
- className="divide-border/70 border-border/70 divide-y border-y"
168
- aria-label="Accounts a mail app sees"
169
- >
170
- {items.map((mailbox) => (
171
- <li key={mailbox.id} className="flex items-center gap-3 py-2.5">
172
- <span className="bg-surface-sunken text-muted-foreground grid size-8 shrink-0 place-items-center rounded-lg">
173
- {mailbox.kind === "shared" ? (
174
- <UsersRound className="size-4" />
175
- ) : (
176
- <Inbox className="size-4" />
177
- )}
178
- </span>
179
- <div className="min-w-0 flex-1">
180
- <p className="truncate text-sm font-medium">
181
- {mailbox.address}
182
- </p>
183
- <p className="text-muted-foreground truncate text-xs">
184
- {mailbox.displayName}
185
- </p>
186
- </div>
187
- <div className="flex shrink-0 gap-1">
188
- <Badge variant="outline">
189
- {mailbox.kind === "shared" ? "Shared" : "Yours"}
190
- </Badge>
191
- {mailbox.canSend ? null : (
192
- <Badge variant="outline">Read only</Badge>
193
- )}
194
- </div>
195
- </li>
196
- ))}
197
- </ul>
198
- )}
157
+ <DelayedFallback
158
+ pending={mailboxes.isLoading}
159
+ fallback={
160
+ <div className="text-muted-foreground flex items-center gap-2 py-2 text-sm">
161
+ <LoaderCircle className="size-4 animate-spin" /> Loading
162
+ mailboxes…
163
+ </div>
164
+ }
165
+ >
166
+ {mailboxes.isError ? (
167
+ <p className="text-destructive text-sm">
168
+ Could not load your mailboxes.
169
+ </p>
170
+ ) : (
171
+ <ul
172
+ className="divide-border/70 border-border/70 divide-y border-y"
173
+ aria-label="Accounts a mail app sees"
174
+ >
175
+ {items.map((mailbox) => (
176
+ <li
177
+ key={mailbox.id}
178
+ className="flex items-center gap-3 py-2.5"
179
+ >
180
+ <span className="bg-surface-sunken text-muted-foreground grid size-8 shrink-0 place-items-center rounded-lg">
181
+ {mailbox.kind === "shared" ? (
182
+ <UsersRound className="size-4" />
183
+ ) : (
184
+ <Inbox className="size-4" />
185
+ )}
186
+ </span>
187
+ <div className="min-w-0 flex-1">
188
+ <p className="truncate text-sm font-medium">
189
+ {mailbox.address}
190
+ </p>
191
+ <p className="text-muted-foreground truncate text-xs">
192
+ {mailbox.displayName}
193
+ </p>
194
+ </div>
195
+ <div className="flex shrink-0 gap-1">
196
+ <Badge variant="outline">
197
+ {mailbox.kind === "shared" ? "Shared" : "Yours"}
198
+ </Badge>
199
+ {mailbox.canSend ? null : (
200
+ <Badge variant="outline">Read only</Badge>
201
+ )}
202
+ </div>
203
+ </li>
204
+ ))}
205
+ </ul>
206
+ )}
207
+ </DelayedFallback>
199
208
  </section>
200
209
 
201
210
  <section>