@open-workspace/email 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (221) hide show
  1. package/README.md +22 -22
  2. package/alchemy/index.ts +273 -0
  3. package/drizzle/mailbox/0000_puzzling_dracula.sql +149 -0
  4. package/drizzle/mailbox/0001_mute_jack_flag.sql +12 -0
  5. package/drizzle/mailbox/meta/0000_snapshot.json +1002 -0
  6. package/drizzle/mailbox/meta/0001_snapshot.json +1002 -0
  7. package/drizzle/mailbox/meta/_journal.json +20 -0
  8. package/drizzle/mailbox/migrations.js +12 -0
  9. package/index.ts +3 -1
  10. package/migrations/{0000_email.sql → 0000_dry_moon_knight.sql} +26 -20
  11. package/migrations/meta/0000_snapshot.json +125 -109
  12. package/migrations/meta/_journal.json +2 -2
  13. package/package.json +74 -68
  14. package/shared/attachment-preview.ts +37 -27
  15. package/shared/folder-order.ts +106 -0
  16. package/shared/mail-authentication.ts +8 -3
  17. package/shared/mail-feedback.ts +18 -25
  18. package/shared/mail-headers.ts +114 -39
  19. package/shared/mail-import.ts +38 -0
  20. package/shared/mail-path.ts +42 -0
  21. package/shared/mail-received.ts +174 -31
  22. package/shared/mail-remote.ts +59 -2
  23. package/shared/mail-sender-intel.ts +22 -4
  24. package/shared/mail.ts +77 -28
  25. package/shared/module.ts +33 -0
  26. package/shared/office-preview.ts +14 -2
  27. package/shared/pdfjs-assets.ts +35 -0
  28. package/shared/permissions.ts +73 -0
  29. package/shared/webhooks.ts +41 -0
  30. package/src/admin/blocked-senders.tsx +189 -0
  31. package/src/client.tsx +97 -0
  32. package/src/lib/mail-api.ts +9 -0
  33. package/src/mail/attachment-preview-dialog.tsx +161 -0
  34. package/src/mail/blocked-senders.tsx +138 -0
  35. package/src/mail/compose-window-chrome.tsx +194 -0
  36. package/src/mail/compose-window.tsx +486 -0
  37. package/src/mail/composer-asset-bookmarks.ts +409 -0
  38. package/src/mail/composer-asset.tsx +229 -0
  39. package/src/mail/composer-attachment-list.tsx +138 -0
  40. package/src/mail/composer-editor-document.ts +65 -0
  41. package/src/mail/composer-editor.tsx +284 -0
  42. package/src/mail/composer-linked-attachment-text.ts +12 -0
  43. package/src/mail/composer-recipient-fields.tsx +165 -0
  44. package/src/mail/composer-recipients.ts +208 -0
  45. package/src/mail/composer-session.ts +562 -0
  46. package/src/mail/composer-toolbar.tsx +303 -0
  47. package/src/mail/composer-upload-client.ts +105 -0
  48. package/src/mail/composer-upload-progress.ts +53 -0
  49. package/src/mail/conversation-list.tsx +550 -0
  50. package/src/mail/conversation-loading-skeleton.tsx +119 -0
  51. package/src/mail/conversation-message.tsx +346 -0
  52. package/src/mail/conversation-reply.tsx +470 -0
  53. package/src/mail/conversation-selection-toolbar.tsx +322 -0
  54. package/src/mail/conversation-view.tsx +437 -0
  55. package/src/mail/delivery-indicator.tsx +272 -0
  56. package/src/mail/email-html-body.tsx +203 -0
  57. package/src/mail/folder-glyph.tsx +46 -0
  58. package/src/mail/folder-manager.tsx +532 -0
  59. package/src/mail/format-bytes.ts +7 -0
  60. package/src/mail/mail-app-bar.tsx +253 -0
  61. package/src/mail/mail-background.tsx +12 -0
  62. package/src/mail/mail-dock.tsx +137 -0
  63. package/src/mail/mail-folders.ts +44 -0
  64. package/src/mail/mail-layout.css +236 -0
  65. package/src/mail/mail-location.ts +73 -0
  66. package/src/mail/mail-mailbox-sheet.tsx +275 -0
  67. package/src/mail/mail-nav-rail.tsx +129 -0
  68. package/src/mail/mail-nav.tsx +324 -0
  69. package/src/mail/mail-prefetch.ts +59 -0
  70. package/src/mail/mail-primitives.tsx +66 -0
  71. package/src/mail/mail-queries.ts +209 -0
  72. package/src/mail/mail-query-cache.ts +486 -0
  73. package/src/mail/mail-seed.ts +47 -0
  74. package/src/mail/mail-shell.tsx +126 -0
  75. package/src/mail/mailbox-ai-settings.tsx +185 -0
  76. package/src/mail/mailbox-realtime.tsx +251 -0
  77. package/src/mail/mailbox-route.tsx +15 -0
  78. package/src/mail/message-actions.tsx +353 -0
  79. package/src/mail/message-attachments.tsx +200 -0
  80. package/src/mail/message-format.ts +75 -0
  81. package/src/mail/message-header.tsx +260 -0
  82. package/src/mail/message-translation.tsx +225 -0
  83. package/src/mail/original-message-sheet.tsx +619 -0
  84. package/src/mail/original-message.ts +191 -0
  85. package/src/mail/outbound-notification.ts +53 -0
  86. package/src/mail/pane-splitter.tsx +117 -0
  87. package/src/mail/pdf-preview-toolbar.tsx +152 -0
  88. package/src/mail/pdf-preview.css +44 -0
  89. package/src/mail/pdf-preview.tsx +156 -0
  90. package/src/mail/pdf-translation-layout.ts +95 -0
  91. package/src/mail/print-message.ts +92 -0
  92. package/src/mail/reader-pane.tsx +535 -0
  93. package/src/mail/recipient-combobox.tsx +335 -0
  94. package/src/mail/recipient-field-row.tsx +34 -0
  95. package/src/mail/reply-to-warning.tsx +50 -0
  96. package/src/mail/sanitize-email-html.ts +430 -0
  97. package/src/mail/sender-actions.tsx +149 -0
  98. package/src/mail/sender-authentication.tsx +485 -0
  99. package/src/mail/sender-dialogs.tsx +384 -0
  100. package/src/mail/sender-security.tsx +545 -0
  101. package/src/mail/toolbar-tooltip.tsx +29 -0
  102. package/src/mail/translation-html.ts +49 -0
  103. package/src/mail/translation-language.ts +21 -0
  104. package/src/mail/types.ts +53 -0
  105. package/src/mail/unread-document-indicator.tsx +60 -0
  106. package/src/mail/use-compose-submission.ts +208 -0
  107. package/src/mail/use-composer-attachment-preflight.ts +184 -0
  108. package/src/mail/use-composer-submission.ts +117 -0
  109. package/src/mail/use-conversation-selection.ts +186 -0
  110. package/src/mail/use-mail-controller.ts +427 -0
  111. package/src/mail/use-mail-data.ts +279 -0
  112. package/src/mail/use-mail-safety.ts +155 -0
  113. package/src/mail/use-mail-send.ts +142 -0
  114. package/src/mail/use-pdf-translation-overlay.ts +156 -0
  115. package/src/mail/use-pdf-translation.ts +100 -0
  116. package/src/mail/use-pdf-viewer.ts +215 -0
  117. package/src/mail/use-reply-submission.ts +114 -0
  118. package/src/mail/use-sanitized-email-html.ts +20 -0
  119. package/src/mail/use-visible-message-read.ts +138 -0
  120. package/src/pwa/push-subscription.ts +151 -0
  121. package/src/settings/mail-apps-page.tsx +235 -0
  122. package/src/settings/notifications-page.tsx +291 -0
  123. package/worker/admin/blocked-sender-routes.ts +166 -0
  124. package/worker/admin/schemas.ts +11 -0
  125. package/worker/bindings.d.ts +14 -10
  126. package/worker/db/schema.ts +63 -40
  127. package/worker/jmap/eventsource.ts +84 -16
  128. package/worker/jmap/methods.ts +1630 -524
  129. package/worker/jmap/push-types.ts +27 -0
  130. package/worker/jmap/push.ts +140 -56
  131. package/worker/jmap/routes.ts +366 -105
  132. package/worker/mail/access.ts +258 -21
  133. package/worker/mail/ai-classification.ts +36 -20
  134. package/worker/mail/ai-translation.ts +13 -6
  135. package/worker/mail/auth-headers.ts +395 -137
  136. package/worker/mail/bimi.ts +200 -73
  137. package/worker/mail/bootstrap.ts +79 -0
  138. package/worker/mail/capped-body.ts +36 -0
  139. package/worker/mail/classification-signals.ts +35 -25
  140. package/worker/mail/conversation-cursor.ts +30 -19
  141. package/worker/mail/delivery-events.ts +88 -62
  142. package/worker/mail/delivery-facts.ts +46 -10
  143. package/worker/mail/demo-routes.ts +34 -10
  144. package/worker/mail/doh.ts +62 -23
  145. package/worker/mail/downloads.ts +75 -40
  146. package/worker/mail/edge-cache.ts +155 -0
  147. package/worker/mail/header-metadata.ts +88 -22
  148. package/worker/mail/impersonation.ts +160 -38
  149. package/worker/mail/import-routes.ts +313 -0
  150. package/worker/mail/inbound-delivery.ts +19 -9
  151. package/worker/mail/inbound-threading.ts +18 -8
  152. package/worker/mail/inbound.ts +82 -40
  153. package/worker/mail/ip-intel.ts +110 -27
  154. package/worker/mail/isolate-cache.ts +35 -0
  155. package/worker/mail/link-domains.ts +29 -7
  156. package/worker/mail/mailbox-deletion.ts +10 -4
  157. package/worker/mail/mailbox-directory.ts +16 -6
  158. package/worker/mail/mailing-list.ts +94 -39
  159. package/worker/mail/message-security.ts +90 -15
  160. package/worker/mail/message-view.ts +18 -6
  161. package/worker/mail/office-conversion.ts +31 -9
  162. package/worker/mail/office-preview-container.ts +40 -13
  163. package/worker/mail/office-preview-queue.ts +76 -32
  164. package/worker/mail/org-domain.ts +8 -1
  165. package/worker/mail/outbound-content.ts +71 -26
  166. package/worker/mail/outbound-delivery.ts +31 -17
  167. package/worker/mail/outbound-service.ts +48 -30
  168. package/worker/mail/outbound-threading.ts +30 -17
  169. package/worker/mail/outbound.ts +97 -70
  170. package/worker/mail/outgoing-context.ts +25 -15
  171. package/worker/mail/participants.ts +44 -29
  172. package/worker/mail/push-notifications.ts +271 -173
  173. package/worker/mail/quote-parser.ts +21 -10
  174. package/worker/mail/r2-presigned-urls.ts +31 -25
  175. package/worker/mail/receive.ts +23 -11
  176. package/worker/mail/recipients.ts +6 -1
  177. package/worker/mail/remote-proxy.ts +187 -31
  178. package/worker/mail/remote-routes.ts +87 -0
  179. package/worker/mail/remote-token.ts +104 -0
  180. package/worker/mail/repeat.ts +19 -0
  181. package/worker/mail/reply-plan.ts +62 -33
  182. package/worker/mail/rfc.ts +32 -7
  183. package/worker/mail/routes.ts +870 -541
  184. package/worker/mail/safety-routes.ts +328 -200
  185. package/worker/mail/schemas.ts +84 -105
  186. package/worker/mail/search.ts +1 -0
  187. package/worker/mail/security-settings.ts +41 -9
  188. package/worker/mail/sender-intel.ts +206 -51
  189. package/worker/mail/spamhaus.ts +92 -45
  190. package/worker/mail/unsubscribe.ts +76 -18
  191. package/worker/mail/uploads.ts +126 -93
  192. package/worker/mail/webhook-source.ts +35 -12
  193. package/worker/mail/workspace-safety.ts +97 -38
  194. package/worker/mailbox/demo-html.ts +7 -7
  195. package/worker/mailbox/demo-seed.ts +10 -44
  196. package/worker/mailbox/demo.ts +595 -225
  197. package/worker/mailbox/email-index.ts +53 -0
  198. package/worker/mailbox/folder-model.ts +53 -17
  199. package/worker/mailbox/import-placement.ts +88 -0
  200. package/worker/mailbox/inbox-counts.ts +106 -0
  201. package/worker/mailbox/index.ts +1734 -721
  202. package/worker/mailbox/jmap-store.ts +139 -35
  203. package/worker/mailbox/model.ts +8 -6
  204. package/worker/mailbox/office-preview-jobs.ts +56 -25
  205. package/worker/mailbox/schema.ts +87 -66
  206. package/worker/mailbox/senders.ts +176 -70
  207. package/worker/mcp/mail-action-tools.ts +128 -87
  208. package/worker/mcp/mail-compose-tools.ts +133 -82
  209. package/worker/mcp/mail-query-tools.ts +120 -71
  210. package/worker/mcp/mail-safety-tools.ts +321 -248
  211. package/worker/mcp/schemas.ts +43 -0
  212. package/worker/mcp/tools.ts +13 -10
  213. package/worker/module.ts +43 -18
  214. package/worker/sql.d.ts +8 -0
  215. package/alchemy.ts +0 -142
  216. package/containers/office-preview/Dockerfile +0 -10
  217. package/shared/mail-rules.ts +0 -156
  218. package/worker/admin/report-routes.ts +0 -164
  219. package/worker/mail/reported-messages.ts +0 -68
  220. package/worker/mailbox/migrations.ts +0 -10
  221. package/worker/mailbox/rules.ts +0 -110
@@ -0,0 +1,138 @@
1
+ import { ApiError } from "@open-workspace/core/ui";
2
+ import { useEffect, useRef } from "react";
3
+ import type { RefObject } from "react";
4
+
5
+ const READ_VISIBILITY_DELAY_MS = 400;
6
+ const READ_RETRY_DELAY_MS = 2000;
7
+ const MAX_READ_ATTEMPTS = 2;
8
+ const MIN_VISIBLE_HEIGHT_PX = 120;
9
+
10
+ /** Marks a message only after a meaningful part of it remains on screen. */
11
+ export function useVisibleMessageRead({
12
+ enabled,
13
+ messageId,
14
+ onVisible,
15
+ }: {
16
+ enabled: boolean;
17
+ messageId: string;
18
+ onVisible: () => Promise<void>;
19
+ }): RefObject<HTMLElement | null> {
20
+ const element = useRef<HTMLElement>(null);
21
+ const onVisibleRef = useRef(onVisible);
22
+ const reportedMessageId = useRef<string | null>(null);
23
+
24
+ useEffect(() => {
25
+ onVisibleRef.current = onVisible;
26
+ }, [onVisible]);
27
+
28
+ useEffect(() => {
29
+ if (!enabled || reportedMessageId.current === messageId) return;
30
+ const target = element.current;
31
+
32
+ if (!target) return;
33
+
34
+ let intersecting = false;
35
+ let pending = false;
36
+ let attempts = 0;
37
+ let stopped = false;
38
+ let visibleTimer: ReturnType<typeof setTimeout> | undefined;
39
+
40
+ const isVisible = () =>
41
+ intersecting && document.visibilityState === "visible";
42
+
43
+ const scheduleRead = (delay: number) => {
44
+ clearTimeout(visibleTimer);
45
+
46
+ if (!isVisible() || pending || stopped) return;
47
+ visibleTimer = setTimeout(async () => {
48
+ visibleTimer = undefined;
49
+
50
+ if (!isVisible() || stopped) return;
51
+ pending = true;
52
+ attempts += 1;
53
+
54
+ try {
55
+ await onVisibleRef.current();
56
+
57
+ if (stopped) return;
58
+ reportedMessageId.current = messageId;
59
+ observer.disconnect();
60
+ } catch (error) {
61
+ pending = false;
62
+ const retryable = !(error instanceof ApiError) || error.status >= 500;
63
+
64
+ if (
65
+ retryable &&
66
+ attempts < MAX_READ_ATTEMPTS &&
67
+ isVisible() &&
68
+ !stopped
69
+ ) {
70
+ scheduleRead(READ_RETRY_DELAY_MS);
71
+ }
72
+
73
+ return;
74
+ }
75
+
76
+ pending = false;
77
+ }, delay);
78
+ };
79
+
80
+ const targetHeight = Math.max(target.getBoundingClientRect().height, 1);
81
+
82
+ const visibilityThreshold = Math.min(
83
+ 0.5,
84
+ MIN_VISIBLE_HEIGHT_PX / targetHeight
85
+ );
86
+
87
+ const observer = new IntersectionObserver(
88
+ ([entry]) => {
89
+ if (!entry) return;
90
+
91
+ const requiredHeight = Math.min(
92
+ MIN_VISIBLE_HEIGHT_PX,
93
+ entry.boundingClientRect.height * 0.5
94
+ );
95
+
96
+ intersecting =
97
+ entry.isIntersecting &&
98
+ entry.intersectionRect.height >= requiredHeight;
99
+
100
+ if (!isVisible()) {
101
+ clearTimeout(visibleTimer);
102
+ visibleTimer = undefined;
103
+
104
+ return;
105
+ }
106
+
107
+ if (!visibleTimer && !pending) scheduleRead(READ_VISIBILITY_DELAY_MS);
108
+ },
109
+ {
110
+ rootMargin: "-8% 0px -8% 0px",
111
+ threshold: [0, visibilityThreshold],
112
+ }
113
+ );
114
+
115
+ observer.observe(target);
116
+
117
+ const handleDocumentVisibility = () => {
118
+ clearTimeout(visibleTimer);
119
+ visibleTimer = undefined;
120
+
121
+ if (isVisible() && !pending) scheduleRead(READ_VISIBILITY_DELAY_MS);
122
+ };
123
+
124
+ document.addEventListener("visibilitychange", handleDocumentVisibility);
125
+
126
+ return () => {
127
+ stopped = true;
128
+ clearTimeout(visibleTimer);
129
+ document.removeEventListener(
130
+ "visibilitychange",
131
+ handleDocumentVisibility
132
+ );
133
+ observer.disconnect();
134
+ };
135
+ }, [enabled, messageId]);
136
+
137
+ return element;
138
+ }
@@ -0,0 +1,151 @@
1
+ import type { PushSubscriptionData } from "@mmmike/web-push/client";
2
+ import { responseJson } from "@open-workspace/core/ui";
3
+
4
+ import { mailApi } from "../lib/mail-api";
5
+
6
+ const SERVICE_WORKER_READY_TIMEOUT_MS = 10_000;
7
+
8
+ function pushIsSupported() {
9
+ return (
10
+ "serviceWorker" in navigator &&
11
+ "PushManager" in window &&
12
+ "Notification" in window
13
+ );
14
+ }
15
+
16
+ async function activeServiceWorkerRegistration() {
17
+ const current = await navigator.serviceWorker.getRegistration();
18
+
19
+ if (current?.active) return current;
20
+
21
+ let timeout: ReturnType<typeof setTimeout> | undefined;
22
+
23
+ try {
24
+ return await Promise.race([
25
+ navigator.serviceWorker.ready,
26
+ new Promise<never>((_resolve, reject) => {
27
+ timeout = setTimeout(() => {
28
+ reject(new Error("The notification service is unavailable"));
29
+ }, SERVICE_WORKER_READY_TIMEOUT_MS);
30
+ }),
31
+ ]);
32
+ } finally {
33
+ if (timeout) clearTimeout(timeout);
34
+ }
35
+ }
36
+
37
+ async function createPushSubscription(
38
+ registration: ServiceWorkerRegistration,
39
+ publicKey: string
40
+ ) {
41
+ const { urlBase64ToUint8Array } = await import("@mmmike/web-push/vapid");
42
+
43
+ return registration.pushManager.subscribe({
44
+ userVisibleOnly: true,
45
+ applicationServerKey: urlBase64ToUint8Array(publicKey),
46
+ });
47
+ }
48
+
49
+ async function currentBrowserPushSubscription() {
50
+ if (!pushIsSupported()) return null;
51
+ const registration = await navigator.serviceWorker.getRegistration();
52
+
53
+ return registration?.pushManager.getSubscription() ?? null;
54
+ }
55
+
56
+ async function serializedSubscription(subscription: PushSubscription) {
57
+ const client = await import("@mmmike/web-push/client");
58
+
59
+ return client.serializeSubscription(subscription);
60
+ }
61
+
62
+ async function bindPushSubscription(subscription: PushSubscriptionData) {
63
+ await responseJson(
64
+ await mailApi.api.notifications.subscriptions.$put({ json: subscription })
65
+ );
66
+ }
67
+
68
+ async function removeServerPushSubscription(endpoint: string) {
69
+ await responseJson(
70
+ await mailApi.api.notifications.subscriptions.$delete({
71
+ json: { endpoint },
72
+ })
73
+ );
74
+ }
75
+
76
+ /**
77
+ * Before sign-out, stops this device getting the account's notifications.
78
+ * The browser keeps its subscription, so the next person to sign in on this
79
+ * device can claim it.
80
+ */
81
+ export async function removePushSubscriptionForSignOut() {
82
+ let endpoint: string | undefined;
83
+
84
+ try {
85
+ endpoint = (await currentBrowserPushSubscription())?.endpoint;
86
+ } catch {
87
+ // Sign-out must not depend on browser push APIs being healthy; the
88
+ // session is revoked even when this device cannot identify itself.
89
+ return;
90
+ }
91
+
92
+ if (endpoint) await removeServerPushSubscription(endpoint);
93
+ }
94
+
95
+ export async function syncCurrentPushSubscription() {
96
+ const subscription = await currentBrowserPushSubscription();
97
+
98
+ if (!subscription) return;
99
+ await bindPushSubscription(await serializedSubscription(subscription));
100
+ }
101
+
102
+ export async function currentPushRegistrationStatus() {
103
+ if (!pushIsSupported()) return "unsupported" as const;
104
+ const subscription = await currentBrowserPushSubscription();
105
+
106
+ if (Notification.permission === "denied") {
107
+ if (subscription) await removeServerPushSubscription(subscription.endpoint);
108
+
109
+ return "denied" as const;
110
+ }
111
+
112
+ if (!subscription) return "prompt" as const;
113
+ await bindPushSubscription(await serializedSubscription(subscription));
114
+
115
+ return "enabled" as const;
116
+ }
117
+
118
+ export async function enableCurrentPushSubscription(publicKey: string) {
119
+ if (!pushIsSupported()) {
120
+ throw new Error("Push is not supported on this device");
121
+ }
122
+
123
+ if ((await Notification.requestPermission()) !== "granted") {
124
+ throw new Error("Notification permission was denied");
125
+ }
126
+
127
+ const registration = await activeServiceWorkerRegistration();
128
+
129
+ const subscription =
130
+ (await registration.pushManager.getSubscription()) ??
131
+ (await createPushSubscription(registration, publicKey));
132
+
133
+ await bindPushSubscription(await serializedSubscription(subscription));
134
+ }
135
+
136
+ export async function disableCurrentPushSubscription() {
137
+ const subscription = await currentBrowserPushSubscription();
138
+
139
+ if (!subscription) return { browserUnsubscribed: true };
140
+
141
+ await removeServerPushSubscription(subscription.endpoint);
142
+ let browserUnsubscribed = false;
143
+
144
+ try {
145
+ browserUnsubscribed = await subscription.unsubscribe();
146
+ } catch {
147
+ // The account mapping is already gone, which is the security boundary.
148
+ }
149
+
150
+ return { browserUnsubscribed };
151
+ }
@@ -0,0 +1,235 @@
1
+ import {
2
+ adminPanelClass,
3
+ AdminPanelBody,
4
+ AdminPanelHeader,
5
+ Badge,
6
+ Button,
7
+ buttonVariants,
8
+ Input,
9
+ Label,
10
+ cn,
11
+ } from "@open-workspace/core/ui";
12
+ import { useQuery } from "@tanstack/react-query";
13
+ import {
14
+ Check,
15
+ Clipboard,
16
+ Inbox,
17
+ LoaderCircle,
18
+ MonitorSmartphone,
19
+ TriangleAlert,
20
+ Unplug,
21
+ UsersRound,
22
+ } from "lucide-react";
23
+ import { useState } from "react";
24
+ import type { ReactNode } from "react";
25
+ import { Link } from "react-router";
26
+ import { toast } from "sonner";
27
+
28
+ import { mailOauthScopes } from "../../shared/permissions";
29
+ import { mailboxesQuery } from "../mail/mail-queries";
30
+
31
+ /** What each mail scope is called where a person chooses it. */
32
+ const scopeNames = new Map([
33
+ ["mail.read", "Read"],
34
+ ["mail.send", "Send"],
35
+ ["mail.organize", "Organize"],
36
+ ]);
37
+
38
+ const sectionClass = "space-y-3 border-t border-border/70 pt-5";
39
+
40
+ function SectionTitle({
41
+ title,
42
+ children,
43
+ }: {
44
+ title: string;
45
+ children?: ReactNode;
46
+ }) {
47
+ return (
48
+ <div>
49
+ <h3 className="text-sm font-semibold">{title}</h3>
50
+ {children ? (
51
+ <p className="text-muted-foreground mt-0.5 text-xs leading-relaxed">
52
+ {children}
53
+ </p>
54
+ ) : null}
55
+ </div>
56
+ );
57
+ }
58
+
59
+ /**
60
+ * Settings → Mail apps: how to connect a mail app over JMAP (RFC 8620/8621).
61
+ * Apps sign in with OAuth in the browser; the apps a person approved are
62
+ * listed, with a way to disconnect them, under Settings → Connected apps.
63
+ */
64
+ export function MailAppsSettings() {
65
+ const sessionUrl = new URL(
66
+ "/.well-known/jmap",
67
+ window.location.origin
68
+ ).toString();
69
+
70
+ const [copied, setCopied] = useState(false);
71
+ const mailboxes = useQuery(mailboxesQuery());
72
+
73
+ async function copySessionUrl() {
74
+ try {
75
+ await navigator.clipboard.writeText(sessionUrl);
76
+ setCopied(true);
77
+ toast.success("Session URL copied");
78
+ window.setTimeout(() => setCopied(false), 1500);
79
+ } catch {
80
+ toast.error("Session URL could not be copied");
81
+ }
82
+ }
83
+
84
+ const items = mailboxes.data?.mailboxes ?? [];
85
+
86
+ return (
87
+ <div className={adminPanelClass}>
88
+ <AdminPanelHeader
89
+ Icon={MonitorSmartphone}
90
+ title="Connect a mail app"
91
+ description="Your mail in an app that speaks JMAP."
92
+ />
93
+ <AdminPanelBody className="space-y-5">
94
+ <div className="space-y-2">
95
+ <Label htmlFor="jmap-session-url">JMAP session URL</Label>
96
+ <div className="flex gap-2">
97
+ <Input
98
+ id="jmap-session-url"
99
+ value={sessionUrl}
100
+ readOnly
101
+ className="font-mono text-xs"
102
+ />
103
+ <Button
104
+ type="button"
105
+ variant="outline"
106
+ onClick={() => void copySessionUrl()}
107
+ >
108
+ {copied ? <Check /> : <Clipboard />}
109
+ {copied ? "Copied" : "Copy"}
110
+ </Button>
111
+ </div>
112
+ <p className="text-muted-foreground text-xs leading-relaxed">
113
+ Paste it where the app asks for a JMAP server or session URL. Some
114
+ apps find it from this site's address,{" "}
115
+ <span className="text-foreground font-medium">
116
+ {window.location.host}
117
+ </span>
118
+ , alone.
119
+ </p>
120
+ </div>
121
+
122
+ <section className={sectionClass}>
123
+ <SectionTitle title="Signing in">
124
+ There is no password to give the app. It opens this site in your
125
+ browser, you sign in with your passkey, and you choose what it may
126
+ do:
127
+ </SectionTitle>
128
+ <ul
129
+ className="grid gap-2 sm:grid-cols-3"
130
+ aria-label="What an app may be given"
131
+ >
132
+ {mailOauthScopes.scopes.map((scope) => (
133
+ <li
134
+ key={scope.id}
135
+ className="bg-surface-sunken/60 ring-border rounded-lg px-3 py-2.5 ring-1"
136
+ >
137
+ <p className="text-sm font-medium">
138
+ {scopeNames.get(scope.id) ?? scope.id}
139
+ </p>
140
+ <p className="text-muted-foreground mt-0.5 text-xs leading-5">
141
+ {scope.label}.
142
+ </p>
143
+ </li>
144
+ ))}
145
+ </ul>
146
+ <p className="text-muted-foreground text-xs leading-relaxed">
147
+ The app gets only what you approve, and never more than you can do
148
+ yourself in Mail.
149
+ </p>
150
+ </section>
151
+
152
+ <section className={sectionClass}>
153
+ <SectionTitle title="Your mailboxes in the app">
154
+ Every mailbox you can open shows up as its own account.
155
+ </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
+ )}
199
+ </section>
200
+
201
+ <section>
202
+ <div className="bg-warning/8 ring-warning/25 flex gap-3 rounded-lg px-3.5 py-3 ring-1">
203
+ <TriangleAlert className="text-warning mt-0.5 size-4 shrink-0" />
204
+ <div className="space-y-1.5 text-xs leading-relaxed">
205
+ <p className="text-sm font-medium">
206
+ Apple Mail, Outlook and Gmail can't connect
207
+ </p>
208
+ <p className="text-muted-foreground">
209
+ They use IMAP and SMTP with a password, which this workspace
210
+ doesn't offer. Few apps speak JMAP yet: look for one that asks
211
+ for a JMAP session URL and signs in through your browser.
212
+ Everywhere else, use Mail here; it installs as an app on phones
213
+ and computers.
214
+ </p>
215
+ </div>
216
+ </div>
217
+ </section>
218
+
219
+ <section className={sectionClass}>
220
+ <SectionTitle title="Apps you connected">
221
+ Every app you approve, mail apps and AI agents alike, is listed
222
+ under Connected apps with what it may do. Disconnect one there and
223
+ it loses access at once.
224
+ </SectionTitle>
225
+ <Link
226
+ to="/settings/connected-apps"
227
+ className={cn(buttonVariants({ variant: "outline", size: "sm" }))}
228
+ >
229
+ <Unplug /> Manage connected apps
230
+ </Link>
231
+ </section>
232
+ </AdminPanelBody>
233
+ </div>
234
+ );
235
+ }