@open-workspace/email 0.5.1 → 0.6.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 (58) hide show
  1. package/README.md +1 -2
  2. package/alchemy/index.ts +0 -16
  3. package/migrations/0003_after_0_5_2.sql +1 -0
  4. package/migrations/meta/0003_snapshot.json +703 -0
  5. package/migrations/meta/_journal.json +7 -0
  6. package/package.json +36 -35
  7. package/shared/mail.ts +3 -2
  8. package/shared/module.ts +1 -1
  9. package/src/client.tsx +23 -31
  10. package/src/extensions.ts +176 -0
  11. package/src/mail/compose-window-chrome.tsx +5 -10
  12. package/src/mail/compose-window.tsx +13 -2
  13. package/src/mail/composer-asset-bookmarks.ts +3 -3
  14. package/src/mail/composer-attach-button.tsx +106 -0
  15. package/src/mail/composer-editor.tsx +17 -0
  16. package/src/mail/composer-inserts.tsx +186 -0
  17. package/src/mail/composer-toolbar.tsx +3 -0
  18. package/src/mail/conversation-message.tsx +37 -1
  19. package/src/mail/conversation-reply.tsx +5 -10
  20. package/src/mail/mail-mailbox-sheet.tsx +2 -2
  21. package/src/mail/mail-prefetch.ts +1 -1
  22. package/src/mail/message-actions.tsx +30 -0
  23. package/src/mail/message-attachments.tsx +62 -0
  24. package/src/mail/message-panels.tsx +206 -0
  25. package/src/mail/module-extensions.ts +87 -0
  26. package/src/mail/reader-pane.tsx +75 -47
  27. package/src/mail/recipient-combobox.tsx +32 -12
  28. package/src/mail/shared-compose.ts +45 -0
  29. package/src/mail/unread-document-indicator.tsx +4 -3
  30. package/src/mail/use-composer-attachment-preflight.ts +2 -1
  31. package/src/mail/use-composer-core.ts +4 -6
  32. package/src/settings/mail-notifications.tsx +119 -0
  33. package/src/settings/settings-queries.ts +0 -7
  34. package/worker/backups/file-parts.ts +9 -6
  35. package/worker/backups/mail-backup.ts +21 -4
  36. package/worker/backups/settings-backup.ts +56 -24
  37. package/worker/db/schema.ts +0 -36
  38. package/worker/jmap/eventsource.ts +4 -1
  39. package/worker/jmap/method-core.ts +2 -3
  40. package/worker/jmap/push-subscription-methods.ts +5 -2
  41. package/worker/jmap/push.ts +10 -11
  42. package/worker/jmap/routes.ts +10 -49
  43. package/worker/mail/bimi.ts +2 -3
  44. package/worker/mail/person-erasure.ts +58 -0
  45. package/worker/mail/push-notifications.ts +59 -419
  46. package/worker/mail/remote-proxy.ts +3 -206
  47. package/worker/mail/remote-routes.ts +2 -2
  48. package/worker/mail/routes.ts +8 -2
  49. package/worker/mail/uploads.ts +14 -6
  50. package/worker/mail/webhook-source.ts +13 -1
  51. package/worker/mailbox/index.ts +27 -0
  52. package/worker/mailbox/mailbox-drafts.ts +5 -0
  53. package/worker/module-api.ts +239 -0
  54. package/worker/module.ts +11 -7
  55. package/src/pwa/push-key.ts +0 -18
  56. package/src/pwa/push-subscription.ts +0 -213
  57. package/src/settings/notifications-page.tsx +0 -306
  58. package/worker/mail/push-endpoint.ts +0 -17
@@ -22,6 +22,13 @@
22
22
  "when": 1795144622538,
23
23
  "tag": "0002_after_0_4_2",
24
24
  "breakpoints": true
25
+ },
26
+ {
27
+ "idx": 3,
28
+ "version": "6",
29
+ "when": 1796067768848,
30
+ "tag": "0003_after_0_5_2",
31
+ "breakpoints": true
25
32
  }
26
33
  ]
27
34
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-workspace/email",
3
- "version": "0.5.1",
3
+ "version": "0.6.0",
4
4
  "description": "Team mail for OpenWorkspace: personal and shared inboxes, mailing lists, previews, safety, and AI.",
5
5
  "keywords": [
6
6
  "alchemy",
@@ -32,7 +32,8 @@
32
32
  "exports": {
33
33
  ".": "./index.ts",
34
34
  "./alchemy": "./alchemy/index.ts",
35
- "./client": "./src/client.tsx",
35
+ "./client": "./src/extensions.ts",
36
+ "./module": "./worker/module-api.ts",
36
37
  "./shared/*": "./shared/*",
37
38
  "./src/lib/mail-api.ts": "./src/lib/mail-api.ts",
38
39
  "./src/mail/*": "./src/mail/*",
@@ -53,63 +54,63 @@
53
54
  },
54
55
  "dependencies": {
55
56
  "@hono/zod-validator": "0.9.1",
56
- "@hookform/resolvers": "7.0.6",
57
+ "@hookform/resolvers": "7.0.10",
57
58
  "@mmmike/web-push": "1.5.0",
58
59
  "@modelcontextprotocol/server": "2.4.0",
59
- "@tanstack/react-query": "5.107.1",
60
- "@tiptap/extension-placeholder": "3.33.1",
61
- "@tiptap/extension-table": "3.33.1",
62
- "@tiptap/pm": "3.33.1",
63
- "@tiptap/react": "3.33.1",
64
- "@tiptap/starter-kit": "3.33.1",
60
+ "@tanstack/react-query": "5.108.4",
61
+ "@tiptap/extension-placeholder": "3.35.1",
62
+ "@tiptap/extension-table": "3.35.1",
63
+ "@tiptap/pm": "3.35.1",
64
+ "@tiptap/react": "3.35.1",
65
+ "@tiptap/starter-kit": "3.35.1",
65
66
  "aws4fetch": "1.0.20",
66
67
  "date-fns": "4.4.0",
67
- "dompurify": "3.4.17",
68
+ "dompurify": "3.5.0",
68
69
  "drizzle-orm": "0.45.4",
69
- "hono": "4.13.17",
70
+ "hono": "4.13.23",
70
71
  "html-to-text": "10.0.1",
71
72
  "linkifyjs": "4.4.0",
72
- "lucide-react": "1.71.0",
73
+ "lucide-react": "1.84.0",
73
74
  "nuqs": "2.10.3",
74
75
  "pdfjs-dist": "6.5.438",
75
76
  "postal-mime": "4.2.1",
76
77
  "react": "19.3.1",
77
78
  "react-hook-form": "7.92.0",
78
- "react-router": "8.5.1",
79
+ "react-router": "8.5.3",
79
80
  "sanitize-html": "2.18.2",
80
81
  "sonner": "2.0.8",
81
- "tldts": "7.4.21",
82
+ "tldts": "7.6.4",
82
83
  "zod": "4.6.7"
83
84
  },
84
85
  "devDependencies": {
85
- "@alchemy.run/cloudflare": "2.0.0-beta.85",
86
- "@alchemy.run/test": "2.0.0-beta.85",
87
- "@effect/platform-bun": "4.1.3",
88
- "@effect/platform-node": "4.1.3",
89
- "@open-workspace/core": "0.5.1",
90
- "@open-workspace/create": "0.5.1",
91
- "@types/bun": "1.4.4",
86
+ "@alchemy.run/cloudflare": "2.0.0-beta.87",
87
+ "@alchemy.run/test": "2.0.0-beta.87",
88
+ "@effect/platform-bun": "4.1.7",
89
+ "@effect/platform-node": "4.1.7",
90
+ "@open-workspace/core": "0.6.0",
91
+ "@open-workspace/create": "0.6.0",
92
+ "@types/bun": "1.4.5",
92
93
  "@types/html-to-text": "9.0.4",
93
94
  "@types/jsdom": "30.0.0",
94
- "@types/node": "26.6.6",
95
- "@types/react": "19.3.0",
95
+ "@types/node": "26.6.11",
96
+ "@types/react": "19.3.3",
96
97
  "@types/react-dom": "19.3.0",
97
98
  "@types/sanitize-html": "2.16.2",
98
- "ai": "7.0.166",
99
- "alchemy": "2.0.0-beta.85",
99
+ "ai": "7.0.185",
100
+ "alchemy": "2.0.0-beta.87",
100
101
  "drizzle-kit": "0.31.11",
101
- "effect": "4.1.3",
102
- "jsdom": "30.1.3",
103
- "openid-client": "6.8.9",
104
- "playwright": "1.65.0",
105
- "standardwebhooks": "1.1.1",
102
+ "effect": "4.1.7",
103
+ "jsdom": "31.0.1",
104
+ "openid-client": "6.9.1",
105
+ "playwright": "1.65.1",
106
+ "standardwebhooks": "1.3.0",
106
107
  "typescript": "7.0.2",
107
- "vite": "8.3.7"
108
+ "vite": "8.4.0"
108
109
  },
109
110
  "peerDependencies": {
110
- "@alchemy.run/cloudflare": "2.0.0-beta.85",
111
- "@open-workspace/core": "0.5.1",
112
- "alchemy": "2.0.0-beta.85",
113
- "effect": "4.1.3"
111
+ "@alchemy.run/cloudflare": "2.0.0-beta.87",
112
+ "@open-workspace/core": "0.6.0",
113
+ "alchemy": "2.0.0-beta.87",
114
+ "effect": "4.1.7"
114
115
  }
115
116
  }
package/shared/mail.ts CHANGED
@@ -112,9 +112,10 @@ export type MailboxPushJob =
112
112
  | (MailboxPushJobBase &
113
113
  (
114
114
  | { type: "dispatch" }
115
+ /** One person's notification, sent to their devices with `sendPush`. */
115
116
  | {
116
- type: "deliver";
117
- targetSubscriptionId: string;
117
+ type: "notify";
118
+ userId: string;
118
119
  mailboxDisplayName: string;
119
120
  }
120
121
  ))
package/shared/module.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * The mail module's id: its `Email()` infrastructure (its migrations are
3
- * recorded as `modules/email/…`, so it never changes), its server part
3
+ * recorded in `__alchemy_migrations_email`, so it never changes), its server part
4
4
  * (`WorkspaceModule.name`, the key of its bootstrap data) and its part of the
5
5
  * browser app (`ClientModule.name`).
6
6
  */
package/src/client.tsx CHANGED
@@ -6,11 +6,11 @@ import type {
6
6
  } from "@open-workspace/core/ui";
7
7
  import {
8
8
  Archive,
9
- Bell,
10
9
  Mail,
11
10
  MonitorSmartphone,
12
11
  PenLine,
13
12
  Plane,
13
+ Send,
14
14
  ShieldBan,
15
15
  } from "lucide-react";
16
16
  import { lazy, Suspense } from "react";
@@ -27,8 +27,6 @@ import {
27
27
  adminBlockedSendersQuery,
28
28
  autoReplyMailboxesQuery,
29
29
  mailArchiveQuery,
30
- notificationPreferencesQuery,
31
- pushConfigQuery,
32
30
  signatureQuery,
33
31
  } from "./settings/settings-queries";
34
32
 
@@ -79,27 +77,18 @@ function AddressAutoReplySection({
79
77
  );
80
78
  }
81
79
 
80
+ const MailNotifications = lazy(() =>
81
+ import("./settings/mail-notifications").then((module) => ({
82
+ default: module.MailNotifications,
83
+ }))
84
+ );
85
+
82
86
  const mail: ClientModule = {
83
87
  name: EMAIL_MODULE,
84
88
  home: { label: "mail", Component: MailboxRoute },
85
89
  app: { label: "Mail", Icon: Mail, path: "/" },
86
90
  routes: [{ path: MAIL_ROUTE, Component: MailboxRoute }],
87
91
  settings: [
88
- {
89
- id: "notifications",
90
- label: "Notifications",
91
- description: "New mail alerts for this device and your mailboxes.",
92
- Icon: Bell,
93
- ...preloadable(() =>
94
- import("./settings/notifications-page").then(
95
- (module) => module.NotificationsSettings
96
- )
97
- ),
98
- prefetch: (queryClient) => {
99
- void queryClient.prefetchQuery(pushConfigQuery);
100
- void queryClient.prefetchQuery(notificationPreferencesQuery);
101
- },
102
- },
103
92
  {
104
93
  id: "signature",
105
94
  label: "Signature",
@@ -184,28 +173,31 @@ const mail: ClientModule = {
184
173
  { id: "auto-reply", Component: AddressAutoReplySection },
185
174
  { id: "forward-destinations", Component: ForwardDestinations },
186
175
  ],
176
+ notifications: [{ id: "mailboxes", Component: MailNotifications }],
177
+ shareTargets: [
178
+ {
179
+ id: "email.new-message",
180
+ label: "New message",
181
+ Icon: Send,
182
+ accepts: { files: ["*/*"], text: true },
183
+ open: async (shared, app) =>
184
+ (await import("./mail/shared-compose")).composeShared(shared, app),
185
+ },
186
+ ],
187
+ personErasure:
188
+ "Their mailboxes with all their mail, their drafts and signature are deleted.",
187
189
  webhookEvents: emailWebhookEvents,
188
190
  background: mailBackground.Component,
189
191
  preload: async () => {
190
192
  // Message bodies need the sanitizer (DOMPurify); the screen need not wait for it.
191
- loadSanitizer().catch(() => undefined);
193
+ loadSanitizer().catch((error) =>
194
+ console.warn("Could not preload the sanitizer", error)
195
+ );
192
196
 
193
197
  await Promise.all([mailboxRoute.preload(), mailBackground.preload()]);
194
198
  },
195
199
  seed: seedMail,
196
200
  prefetch: prefetchMail,
197
- afterSignIn: async () => {
198
- const { syncCurrentPushSubscription } =
199
- await import("./pwa/push-subscription");
200
-
201
- await syncCurrentPushSubscription();
202
- },
203
- beforeSignOut: async () => {
204
- const { removePushSubscriptionForSignOut } =
205
- await import("./pwa/push-subscription");
206
-
207
- await removePushSubscriptionForSignOut();
208
- },
209
201
  };
210
202
 
211
203
  export default mail;
@@ -0,0 +1,176 @@
1
+ /**
2
+ * `@open-workspace/email/client`: the slots mail offers other modules' client
3
+ * parts, as `ClientModule.extensions` entries:
4
+ *
5
+ * import type { MessageAction } from "@open-workspace/email/client";
6
+ *
7
+ * extensions: { "email.messageActions": [createNote] }
8
+ *
9
+ * Types only: importing it declares the slots and adds no code to the app.
10
+ * Mail's other files are not an API.
11
+ */
12
+ import type { LucideIcon } from "lucide-react";
13
+ import type { ComponentType } from "react";
14
+
15
+ /** An attachment of a stored message. */
16
+ export type MailAttachment = {
17
+ id: string;
18
+ filename: string;
19
+ /** Without parameters, e.g. "text/calendar". */
20
+ contentType: string;
21
+ /** In bytes. */
22
+ size: number;
23
+ };
24
+
25
+ /** What an attachment action runs with. */
26
+ export type AttachmentActionContext = {
27
+ mailboxId: string;
28
+ messageId: string;
29
+ attachment: MailAttachment;
30
+ /** Downloads the attachment; rejects if it can't. */
31
+ content: () => Promise<Blob>;
32
+ };
33
+
34
+ /** An entry in an attachment's menu, under a message. */
35
+ export type AttachmentAction = {
36
+ /** Unique within the module. */
37
+ id: string;
38
+ label: string;
39
+ Icon: LucideIcon;
40
+ /** A rejection is shown to the person as an error. */
41
+ run: (context: AttachmentActionContext) => void | Promise<void>;
42
+ };
43
+
44
+ /** What a message card gets: the message and the part it is for. */
45
+ export type MessageCardProps = {
46
+ mailboxId: string;
47
+ messageId: string;
48
+ part: {
49
+ contentType: string;
50
+ filename?: string;
51
+ /** Downloads the part; rejects if it can't. */
52
+ content: () => Promise<Blob>;
53
+ };
54
+ };
55
+
56
+ /**
57
+ * A card above a message's text, shown when the message has a part of one
58
+ * of `contentTypes` (compared without parameters, e.g. "text/calendar" for
59
+ * an invitation), once for its first such part.
60
+ */
61
+ export type MessageCard = {
62
+ id: string;
63
+ contentTypes: string[];
64
+ Component: ComponentType<MessageCardProps>;
65
+ };
66
+
67
+ /** What a composer source gets while it is open. */
68
+ export type ComposerSourceProps = {
69
+ /** Attaches the files to the message, like files from this device. */
70
+ onPick: (files: File[]) => void;
71
+ onClose: () => void;
72
+ };
73
+
74
+ /**
75
+ * Another place to attach files from, next to "This device" in the
76
+ * composer's attach menu. Its component opens in a dialog titled `label`.
77
+ */
78
+ export type ComposerSource = {
79
+ id: string;
80
+ label: string;
81
+ Icon: LucideIcon;
82
+ Component: ComponentType<ComposerSourceProps>;
83
+ };
84
+
85
+ /** What a message action runs with. */
86
+ export type MessageActionContext = {
87
+ mailboxId: string;
88
+ messageId: string;
89
+ conversationId: string;
90
+ subject: string;
91
+ from: { name: string | null; address: string };
92
+ /** When it was sent or received, as an ISO 8601 string. */
93
+ date: string;
94
+ };
95
+
96
+ /** An entry in a message's "More actions" menu. */
97
+ export type MessageAction = {
98
+ id: string;
99
+ label: string;
100
+ Icon: LucideIcon;
101
+ /** A rejection is shown to the person as an error. */
102
+ run: (context: MessageActionContext) => void | Promise<void>;
103
+ };
104
+
105
+ /** An address a recipient source suggests. */
106
+ export type RecipientSuggestion = { name?: string; address: string };
107
+
108
+ /**
109
+ * More suggestions in the recipient fields, after mail's own, e.g. a CRM's
110
+ * contacts. `search` gets what is typed; `signal` aborts when the text
111
+ * changes. A source that fails or is slow never holds mail's suggestions.
112
+ */
113
+ export type RecipientSource = {
114
+ id: string;
115
+ search: (
116
+ query: string,
117
+ options: { signal: AbortSignal }
118
+ ) => Promise<RecipientSuggestion[]>;
119
+ };
120
+
121
+ /** What a message panel gets: the open message and its sender. */
122
+ export type MessagePanelProps = {
123
+ mailboxId: string;
124
+ messageId: string;
125
+ conversationId: string;
126
+ from: { name: string | null; address: string };
127
+ };
128
+
129
+ /**
130
+ * A side panel next to the open conversation, about the latest incoming
131
+ * message's sender (else the latest message's), e.g. meetings with them.
132
+ * Its toggle is in the conversation's toolbar; one panel is open at a time,
133
+ * in a sheet on phones.
134
+ */
135
+ export type MessagePanel = {
136
+ id: string;
137
+ label: string;
138
+ Icon: LucideIcon;
139
+ Component: ComponentType<MessagePanelProps>;
140
+ };
141
+
142
+ /** What a composer insert puts at the cursor. */
143
+ export type ComposerInsertContent =
144
+ | { text: string }
145
+ | { link: { url: string; text: string } };
146
+
147
+ /** What a composer insert gets while it is open. */
148
+ export type ComposerInsertProps = {
149
+ /** Puts the content at the cursor and closes the dialog. */
150
+ insert: (content: ComposerInsertContent) => void;
151
+ onClose: () => void;
152
+ };
153
+
154
+ /**
155
+ * An entry in the composer's toolbar (the new message window's and the
156
+ * reply's), opened in a dialog titled `label`, that inserts text or a link.
157
+ */
158
+ export type ComposerInsert = {
159
+ id: string;
160
+ label: string;
161
+ Icon: LucideIcon;
162
+ Component: ComponentType<ComposerInsertProps>;
163
+ };
164
+
165
+ declare module "@open-workspace/core/ui" {
166
+ // oxlint-disable-next-line typescript/consistent-type-definitions -- declaration merging
167
+ interface ClientExtensions {
168
+ "email.attachmentActions": AttachmentAction;
169
+ "email.messageCards": MessageCard;
170
+ "email.composerSources": ComposerSource;
171
+ "email.messageActions": MessageAction;
172
+ "email.recipientSources": RecipientSource;
173
+ "email.messagePanels": MessagePanel;
174
+ "email.composerInserts": ComposerInsert;
175
+ }
176
+ }
@@ -4,13 +4,13 @@ import {
4
4
  ImagePlus,
5
5
  LoaderCircle,
6
6
  Minus,
7
- Paperclip,
8
7
  Send,
9
8
  Trash2,
10
9
  X,
11
10
  } from "lucide-react";
12
11
  import type { Ref, RefObject } from "react";
13
12
 
13
+ import { ComposerAttachButton } from "./composer-attach-button";
14
14
  import { formatBytes } from "./format-bytes";
15
15
  import type { MessageDetail } from "./types";
16
16
 
@@ -168,16 +168,11 @@ export function ComposeWindowFooter({
168
168
  {sending ? <LoaderCircle className="animate-spin" /> : <Send />}
169
169
  Send
170
170
  </Button>
171
- <Button
172
- type="button"
173
- variant="ghost"
174
- size="icon-sm"
175
- aria-label="Add attachments"
171
+ <ComposerAttachButton
176
172
  disabled={sending}
177
- onClick={() => fileInput.current?.click()}
178
- >
179
- <Paperclip />
180
- </Button>
173
+ onChooseFromDevice={() => fileInput.current?.click()}
174
+ onPick={onAddAttachments}
175
+ />
181
176
  <Button
182
177
  type="button"
183
178
  variant="ghost"
@@ -33,6 +33,7 @@ import {
33
33
  import type { RecipientFieldValue } from "./composer-recipients";
34
34
  import { notifyOutboundResult } from "./outbound-notification";
35
35
  import { ReplyToWarning } from "./reply-to-warning";
36
+ import { forgetSharedMessage, sharedMessage } from "./shared-compose";
36
37
  import type { Mailbox } from "./types";
37
38
  import { useComposerAttachmentPreflight } from "./use-composer-attachment-preflight";
38
39
  import { useComposerCore } from "./use-composer-core";
@@ -98,6 +99,14 @@ export function ComposeWindow({
98
99
  /** It closed; `ended` when its draft is gone (sent or discarded), not just put away. */
99
100
  onClose: (ended?: boolean) => void;
100
101
  }) {
102
+ // What another app shared, when the composer opened for it (/share).
103
+ // oxlint-disable-next-line react/hook-use-state -- read once; nothing sets it
104
+ const [shared] = useState(() => (draft ? undefined : sharedMessage(draftId)));
105
+
106
+ useEffect(() => {
107
+ forgetSharedMessage(draftId);
108
+ }, [draftId]);
109
+
101
110
  const [to, setTo] = useState<RecipientFieldValue>(() =>
102
111
  draft ? recipientField(draft.to) : EMPTY_RECIPIENT_FIELD
103
112
  );
@@ -115,7 +124,9 @@ export function ComposeWindow({
115
124
  const [subject, setSubject] = useState(
116
125
  () =>
117
126
  draft?.subject ??
118
- (forwardedMessage ? forwardSubject(forwardedMessage.subject) : "")
127
+ (forwardedMessage
128
+ ? forwardSubject(forwardedMessage.subject)
129
+ : (shared?.subject ?? ""))
119
130
  );
120
131
 
121
132
  const [minimized, setMinimized] = useState(false);
@@ -476,7 +487,7 @@ export function ComposeWindow({
476
487
  </>
477
488
  ) : null}
478
489
 
479
- <ComposerEditor ref={editor} {...editorProps} />
490
+ <ComposerEditor ref={editor} {...editorProps} shared={shared} />
480
491
 
481
492
  <ComposerAttachmentList
482
493
  {...attachmentListProps}
@@ -80,9 +80,9 @@ class AssetBookmarkStep extends Step {
80
80
  try {
81
81
  Step.jsonID("openworkspaceAssetBookmark", AssetBookmarkStep);
82
82
  } catch (error) {
83
- if (
84
- !(error instanceof RangeError && error.message.includes("Duplicate use"))
85
- ) {
83
+ if (!(
84
+ error instanceof RangeError && error.message.includes("Duplicate use")
85
+ )) {
86
86
  throw error;
87
87
  }
88
88
  }
@@ -0,0 +1,106 @@
1
+ import {
2
+ Button,
3
+ Dialog,
4
+ DialogContent,
5
+ DialogHeader,
6
+ DialogTitle,
7
+ DropdownMenu,
8
+ DropdownMenuContent,
9
+ DropdownMenuItem,
10
+ DropdownMenuTrigger,
11
+ moduleExtensions,
12
+ } from "@open-workspace/core/ui";
13
+ import { HardDrive, Paperclip } from "lucide-react";
14
+ import { Suspense, useState } from "react";
15
+
16
+ import type { ComposerSource } from "../extensions";
17
+
18
+ /**
19
+ * The composer's "Add attachments": files from this device, or, with other
20
+ * modules' sources (`email.composerSources`), a menu of them too. A source
21
+ * opens in a dialog, and the files it picks attach like the device's.
22
+ */
23
+ export function ComposerAttachButton({
24
+ disabled,
25
+ onChooseFromDevice,
26
+ onPick,
27
+ }: {
28
+ disabled: boolean;
29
+ onChooseFromDevice: () => void;
30
+ onPick: (files: File[]) => void;
31
+ }) {
32
+ const sources = moduleExtensions("email.composerSources");
33
+ const [source, setSource] = useState<ComposerSource | null>(null);
34
+
35
+ if (!sources.length) {
36
+ return (
37
+ <Button
38
+ type="button"
39
+ variant="ghost"
40
+ size="icon-sm"
41
+ aria-label="Add attachments"
42
+ disabled={disabled}
43
+ onClick={onChooseFromDevice}
44
+ >
45
+ <Paperclip />
46
+ </Button>
47
+ );
48
+ }
49
+
50
+ return (
51
+ <>
52
+ <DropdownMenu>
53
+ <DropdownMenuTrigger
54
+ render={
55
+ <Button
56
+ type="button"
57
+ variant="ghost"
58
+ size="icon-sm"
59
+ aria-label="Add attachments"
60
+ disabled={disabled}
61
+ />
62
+ }
63
+ >
64
+ <Paperclip />
65
+ </DropdownMenuTrigger>
66
+ <DropdownMenuContent align="start" side="top" className="w-56">
67
+ <DropdownMenuItem onClick={onChooseFromDevice}>
68
+ <HardDrive /> This device
69
+ </DropdownMenuItem>
70
+ {sources.map((entry) => (
71
+ <DropdownMenuItem key={entry.id} onClick={() => setSource(entry)}>
72
+ <entry.Icon /> {entry.label}
73
+ </DropdownMenuItem>
74
+ ))}
75
+ </DropdownMenuContent>
76
+ </DropdownMenu>
77
+ <Dialog
78
+ open={source !== null}
79
+ onOpenChange={(open) => {
80
+ if (!open) setSource(null);
81
+ }}
82
+ >
83
+ {source ? (
84
+ <DialogContent anchor="top" className="sm:max-w-md">
85
+ <DialogHeader>
86
+ <DialogTitle>{source.label}</DialogTitle>
87
+ </DialogHeader>
88
+ {/* The dialog is a portal, but React still bubbles its events
89
+ to the composer's form: a source's own form mustn't send. */}
90
+ <div onSubmit={(event) => event.stopPropagation()}>
91
+ <Suspense fallback={null}>
92
+ <source.Component
93
+ onPick={(files) => {
94
+ setSource(null);
95
+ onPick(files);
96
+ }}
97
+ onClose={() => setSource(null)}
98
+ />
99
+ </Suspense>
100
+ </div>
101
+ </DialogContent>
102
+ ) : null}
103
+ </Dialog>
104
+ </>
105
+ );
106
+ }
@@ -16,6 +16,7 @@ import {
16
16
  } from "./composer-asset-bookmarks";
17
17
  import { composerContent } from "./composer-editor-document";
18
18
  import type { ComposerContent } from "./composer-editor-document";
19
+ import { textNodes } from "./composer-inserts";
19
20
  import type {
20
21
  ComposerAsset as ComposerAssetRecord,
21
22
  ComposerSession,
@@ -56,6 +57,8 @@ export const ComposerEditor = forwardRef<
56
57
  label?: string;
57
58
  /** Read once. */
58
59
  placeholder?: string;
60
+ /** Shared from another app: text put first, files attached; read once. */
61
+ shared?: { text: string; files: File[] };
59
62
  }
60
63
  >(function ComposerEditor(
61
64
  {
@@ -72,6 +75,7 @@ export const ComposerEditor = forwardRef<
72
75
  originalBody,
73
76
  label = "Message body",
74
77
  placeholder = "Write a message",
78
+ shared,
75
79
  },
76
80
  ref
77
81
  ) {
@@ -223,6 +227,19 @@ export const ComposerEditor = forwardRef<
223
227
  []
224
228
  );
225
229
 
230
+ // As edits, so the composer counts as written and saves a draft.
231
+ const sharedAdded = useRef(false);
232
+ useEffect(() => {
233
+ if (!shared || sharedAdded.current) return;
234
+ sharedAdded.current = true;
235
+
236
+ if (shared.text) {
237
+ editor.commands.insertContentAt(1, textNodes(shared.text));
238
+ }
239
+
240
+ if (shared.files.length) onAddAttachmentFiles(shared.files);
241
+ });
242
+
226
243
  useImperativeHandle(
227
244
  ref,
228
245
  () => ({