@open-workspace/email 0.3.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (221) hide show
  1. package/README.md +22 -22
  2. package/alchemy/index.ts +273 -0
  3. package/drizzle/mailbox/0000_puzzling_dracula.sql +149 -0
  4. package/drizzle/mailbox/0001_mute_jack_flag.sql +12 -0
  5. package/drizzle/mailbox/meta/0000_snapshot.json +1002 -0
  6. package/drizzle/mailbox/meta/0001_snapshot.json +1002 -0
  7. package/drizzle/mailbox/meta/_journal.json +20 -0
  8. package/drizzle/mailbox/migrations.js +12 -0
  9. package/index.ts +3 -1
  10. package/migrations/{0000_email.sql → 0000_dry_moon_knight.sql} +26 -20
  11. package/migrations/meta/0000_snapshot.json +125 -109
  12. package/migrations/meta/_journal.json +2 -2
  13. package/package.json +74 -68
  14. package/shared/attachment-preview.ts +37 -27
  15. package/shared/folder-order.ts +106 -0
  16. package/shared/mail-authentication.ts +8 -3
  17. package/shared/mail-feedback.ts +18 -25
  18. package/shared/mail-headers.ts +114 -39
  19. package/shared/mail-import.ts +38 -0
  20. package/shared/mail-path.ts +42 -0
  21. package/shared/mail-received.ts +174 -31
  22. package/shared/mail-remote.ts +59 -2
  23. package/shared/mail-sender-intel.ts +22 -4
  24. package/shared/mail.ts +77 -28
  25. package/shared/module.ts +33 -0
  26. package/shared/office-preview.ts +14 -2
  27. package/shared/pdfjs-assets.ts +35 -0
  28. package/shared/permissions.ts +73 -0
  29. package/shared/webhooks.ts +41 -0
  30. package/src/admin/blocked-senders.tsx +189 -0
  31. package/src/client.tsx +97 -0
  32. package/src/lib/mail-api.ts +9 -0
  33. package/src/mail/attachment-preview-dialog.tsx +161 -0
  34. package/src/mail/blocked-senders.tsx +138 -0
  35. package/src/mail/compose-window-chrome.tsx +194 -0
  36. package/src/mail/compose-window.tsx +486 -0
  37. package/src/mail/composer-asset-bookmarks.ts +409 -0
  38. package/src/mail/composer-asset.tsx +229 -0
  39. package/src/mail/composer-attachment-list.tsx +138 -0
  40. package/src/mail/composer-editor-document.ts +65 -0
  41. package/src/mail/composer-editor.tsx +284 -0
  42. package/src/mail/composer-linked-attachment-text.ts +12 -0
  43. package/src/mail/composer-recipient-fields.tsx +165 -0
  44. package/src/mail/composer-recipients.ts +208 -0
  45. package/src/mail/composer-session.ts +562 -0
  46. package/src/mail/composer-toolbar.tsx +303 -0
  47. package/src/mail/composer-upload-client.ts +105 -0
  48. package/src/mail/composer-upload-progress.ts +53 -0
  49. package/src/mail/conversation-list.tsx +550 -0
  50. package/src/mail/conversation-loading-skeleton.tsx +119 -0
  51. package/src/mail/conversation-message.tsx +346 -0
  52. package/src/mail/conversation-reply.tsx +470 -0
  53. package/src/mail/conversation-selection-toolbar.tsx +322 -0
  54. package/src/mail/conversation-view.tsx +437 -0
  55. package/src/mail/delivery-indicator.tsx +272 -0
  56. package/src/mail/email-html-body.tsx +203 -0
  57. package/src/mail/folder-glyph.tsx +46 -0
  58. package/src/mail/folder-manager.tsx +532 -0
  59. package/src/mail/format-bytes.ts +7 -0
  60. package/src/mail/mail-app-bar.tsx +253 -0
  61. package/src/mail/mail-background.tsx +12 -0
  62. package/src/mail/mail-dock.tsx +137 -0
  63. package/src/mail/mail-folders.ts +44 -0
  64. package/src/mail/mail-layout.css +236 -0
  65. package/src/mail/mail-location.ts +73 -0
  66. package/src/mail/mail-mailbox-sheet.tsx +275 -0
  67. package/src/mail/mail-nav-rail.tsx +129 -0
  68. package/src/mail/mail-nav.tsx +324 -0
  69. package/src/mail/mail-prefetch.ts +59 -0
  70. package/src/mail/mail-primitives.tsx +66 -0
  71. package/src/mail/mail-queries.ts +209 -0
  72. package/src/mail/mail-query-cache.ts +486 -0
  73. package/src/mail/mail-seed.ts +47 -0
  74. package/src/mail/mail-shell.tsx +126 -0
  75. package/src/mail/mailbox-ai-settings.tsx +185 -0
  76. package/src/mail/mailbox-realtime.tsx +251 -0
  77. package/src/mail/mailbox-route.tsx +15 -0
  78. package/src/mail/message-actions.tsx +353 -0
  79. package/src/mail/message-attachments.tsx +200 -0
  80. package/src/mail/message-format.ts +75 -0
  81. package/src/mail/message-header.tsx +260 -0
  82. package/src/mail/message-translation.tsx +225 -0
  83. package/src/mail/original-message-sheet.tsx +619 -0
  84. package/src/mail/original-message.ts +191 -0
  85. package/src/mail/outbound-notification.ts +53 -0
  86. package/src/mail/pane-splitter.tsx +117 -0
  87. package/src/mail/pdf-preview-toolbar.tsx +152 -0
  88. package/src/mail/pdf-preview.css +44 -0
  89. package/src/mail/pdf-preview.tsx +156 -0
  90. package/src/mail/pdf-translation-layout.ts +95 -0
  91. package/src/mail/print-message.ts +92 -0
  92. package/src/mail/reader-pane.tsx +535 -0
  93. package/src/mail/recipient-combobox.tsx +335 -0
  94. package/src/mail/recipient-field-row.tsx +34 -0
  95. package/src/mail/reply-to-warning.tsx +50 -0
  96. package/src/mail/sanitize-email-html.ts +430 -0
  97. package/src/mail/sender-actions.tsx +149 -0
  98. package/src/mail/sender-authentication.tsx +485 -0
  99. package/src/mail/sender-dialogs.tsx +384 -0
  100. package/src/mail/sender-security.tsx +545 -0
  101. package/src/mail/toolbar-tooltip.tsx +29 -0
  102. package/src/mail/translation-html.ts +49 -0
  103. package/src/mail/translation-language.ts +21 -0
  104. package/src/mail/types.ts +53 -0
  105. package/src/mail/unread-document-indicator.tsx +60 -0
  106. package/src/mail/use-compose-submission.ts +208 -0
  107. package/src/mail/use-composer-attachment-preflight.ts +184 -0
  108. package/src/mail/use-composer-submission.ts +117 -0
  109. package/src/mail/use-conversation-selection.ts +186 -0
  110. package/src/mail/use-mail-controller.ts +427 -0
  111. package/src/mail/use-mail-data.ts +279 -0
  112. package/src/mail/use-mail-safety.ts +155 -0
  113. package/src/mail/use-mail-send.ts +142 -0
  114. package/src/mail/use-pdf-translation-overlay.ts +156 -0
  115. package/src/mail/use-pdf-translation.ts +100 -0
  116. package/src/mail/use-pdf-viewer.ts +215 -0
  117. package/src/mail/use-reply-submission.ts +114 -0
  118. package/src/mail/use-sanitized-email-html.ts +20 -0
  119. package/src/mail/use-visible-message-read.ts +138 -0
  120. package/src/pwa/push-subscription.ts +151 -0
  121. package/src/settings/mail-apps-page.tsx +235 -0
  122. package/src/settings/notifications-page.tsx +291 -0
  123. package/worker/admin/blocked-sender-routes.ts +166 -0
  124. package/worker/admin/schemas.ts +11 -0
  125. package/worker/bindings.d.ts +14 -10
  126. package/worker/db/schema.ts +63 -40
  127. package/worker/jmap/eventsource.ts +84 -16
  128. package/worker/jmap/methods.ts +1630 -524
  129. package/worker/jmap/push-types.ts +27 -0
  130. package/worker/jmap/push.ts +140 -56
  131. package/worker/jmap/routes.ts +366 -105
  132. package/worker/mail/access.ts +258 -21
  133. package/worker/mail/ai-classification.ts +36 -20
  134. package/worker/mail/ai-translation.ts +13 -6
  135. package/worker/mail/auth-headers.ts +395 -137
  136. package/worker/mail/bimi.ts +200 -73
  137. package/worker/mail/bootstrap.ts +79 -0
  138. package/worker/mail/capped-body.ts +36 -0
  139. package/worker/mail/classification-signals.ts +35 -25
  140. package/worker/mail/conversation-cursor.ts +30 -19
  141. package/worker/mail/delivery-events.ts +88 -62
  142. package/worker/mail/delivery-facts.ts +46 -10
  143. package/worker/mail/demo-routes.ts +34 -10
  144. package/worker/mail/doh.ts +62 -23
  145. package/worker/mail/downloads.ts +75 -40
  146. package/worker/mail/edge-cache.ts +155 -0
  147. package/worker/mail/header-metadata.ts +88 -22
  148. package/worker/mail/impersonation.ts +160 -38
  149. package/worker/mail/import-routes.ts +313 -0
  150. package/worker/mail/inbound-delivery.ts +19 -9
  151. package/worker/mail/inbound-threading.ts +18 -8
  152. package/worker/mail/inbound.ts +82 -40
  153. package/worker/mail/ip-intel.ts +110 -27
  154. package/worker/mail/isolate-cache.ts +35 -0
  155. package/worker/mail/link-domains.ts +29 -7
  156. package/worker/mail/mailbox-deletion.ts +10 -4
  157. package/worker/mail/mailbox-directory.ts +16 -6
  158. package/worker/mail/mailing-list.ts +94 -39
  159. package/worker/mail/message-security.ts +90 -15
  160. package/worker/mail/message-view.ts +18 -6
  161. package/worker/mail/office-conversion.ts +31 -9
  162. package/worker/mail/office-preview-container.ts +40 -13
  163. package/worker/mail/office-preview-queue.ts +76 -32
  164. package/worker/mail/org-domain.ts +8 -1
  165. package/worker/mail/outbound-content.ts +71 -26
  166. package/worker/mail/outbound-delivery.ts +31 -17
  167. package/worker/mail/outbound-service.ts +48 -30
  168. package/worker/mail/outbound-threading.ts +30 -17
  169. package/worker/mail/outbound.ts +97 -70
  170. package/worker/mail/outgoing-context.ts +25 -15
  171. package/worker/mail/participants.ts +44 -29
  172. package/worker/mail/push-notifications.ts +271 -173
  173. package/worker/mail/quote-parser.ts +21 -10
  174. package/worker/mail/r2-presigned-urls.ts +31 -25
  175. package/worker/mail/receive.ts +23 -11
  176. package/worker/mail/recipients.ts +6 -1
  177. package/worker/mail/remote-proxy.ts +187 -31
  178. package/worker/mail/remote-routes.ts +87 -0
  179. package/worker/mail/remote-token.ts +104 -0
  180. package/worker/mail/repeat.ts +19 -0
  181. package/worker/mail/reply-plan.ts +62 -33
  182. package/worker/mail/rfc.ts +32 -7
  183. package/worker/mail/routes.ts +870 -541
  184. package/worker/mail/safety-routes.ts +328 -200
  185. package/worker/mail/schemas.ts +84 -105
  186. package/worker/mail/search.ts +1 -0
  187. package/worker/mail/security-settings.ts +41 -9
  188. package/worker/mail/sender-intel.ts +206 -51
  189. package/worker/mail/spamhaus.ts +92 -45
  190. package/worker/mail/unsubscribe.ts +76 -18
  191. package/worker/mail/uploads.ts +126 -93
  192. package/worker/mail/webhook-source.ts +35 -12
  193. package/worker/mail/workspace-safety.ts +97 -38
  194. package/worker/mailbox/demo-html.ts +7 -7
  195. package/worker/mailbox/demo-seed.ts +10 -44
  196. package/worker/mailbox/demo.ts +595 -225
  197. package/worker/mailbox/email-index.ts +53 -0
  198. package/worker/mailbox/folder-model.ts +53 -17
  199. package/worker/mailbox/import-placement.ts +88 -0
  200. package/worker/mailbox/inbox-counts.ts +106 -0
  201. package/worker/mailbox/index.ts +1734 -721
  202. package/worker/mailbox/jmap-store.ts +139 -35
  203. package/worker/mailbox/model.ts +8 -6
  204. package/worker/mailbox/office-preview-jobs.ts +56 -25
  205. package/worker/mailbox/schema.ts +87 -66
  206. package/worker/mailbox/senders.ts +176 -70
  207. package/worker/mcp/mail-action-tools.ts +128 -87
  208. package/worker/mcp/mail-compose-tools.ts +133 -82
  209. package/worker/mcp/mail-query-tools.ts +120 -71
  210. package/worker/mcp/mail-safety-tools.ts +321 -248
  211. package/worker/mcp/schemas.ts +43 -0
  212. package/worker/mcp/tools.ts +13 -10
  213. package/worker/module.ts +43 -18
  214. package/worker/sql.d.ts +8 -0
  215. package/alchemy.ts +0 -142
  216. package/containers/office-preview/Dockerfile +0 -10
  217. package/shared/mail-rules.ts +0 -156
  218. package/worker/admin/report-routes.ts +0 -164
  219. package/worker/mail/reported-messages.ts +0 -68
  220. package/worker/mailbox/migrations.ts +0 -10
  221. package/worker/mailbox/rules.ts +0 -110
@@ -0,0 +1,191 @@
1
+ import type { InboundAuthentication } from "../../shared/mail-authentication";
2
+ import { headerValues, parseMailDate } from "../../shared/mail-headers";
3
+ import { receivedHops } from "../../shared/mail-received";
4
+ import type { ReceivedHop } from "../../shared/mail-received";
5
+
6
+ export {
7
+ ipKind,
8
+ receivedHops,
9
+ type IpKind,
10
+ type ReceivedHop,
11
+ } from "../../shared/mail-received";
12
+
13
+ export function originalHeaderValues(raw: string, name: string) {
14
+ return headerValues(raw, name);
15
+ }
16
+
17
+ export function dkimSigningDomains(raw: string) {
18
+ return [
19
+ ...new Set(
20
+ originalHeaderValues(raw, "dkim-signature").flatMap((signature) => {
21
+ const domain = /(?:^|;)\s*d=([^;\s]+)/iu.exec(signature)?.[1];
22
+
23
+ return domain ? [domain.toLocaleLowerCase()] : [];
24
+ })
25
+ ),
26
+ ];
27
+ }
28
+
29
+ function headerValue(raw: string, name: string) {
30
+ return originalHeaderValues(raw, name)[0] ?? null;
31
+ }
32
+
33
+ function tagValue(value: string, tag: string) {
34
+ return (
35
+ new RegExp(`(?:^|;)\\s*${tag}=([^;]+)`, "iu").exec(value)?.[1]?.trim() ??
36
+ null
37
+ );
38
+ }
39
+
40
+ type DkimSignature = {
41
+ domain: string;
42
+ selector: string | null;
43
+ algorithm: string | null;
44
+ signedHeaders: string[];
45
+ };
46
+
47
+ export function dkimSignatures(raw: string): DkimSignature[] {
48
+ return originalHeaderValues(raw, "dkim-signature").flatMap((signature) => {
49
+ const domain = tagValue(signature, "d");
50
+
51
+ if (!domain) return [];
52
+
53
+ return [
54
+ {
55
+ domain: domain.toLocaleLowerCase(),
56
+ selector: tagValue(signature, "s"),
57
+ algorithm: tagValue(signature, "a"),
58
+ signedHeaders: (tagValue(signature, "h") ?? "")
59
+ .split(":")
60
+ .map((header) => header.trim().toLocaleLowerCase())
61
+ .filter(Boolean),
62
+ },
63
+ ];
64
+ });
65
+ }
66
+
67
+ export type MimePart = {
68
+ depth: number;
69
+ contentType: string;
70
+ charset: string | null;
71
+ encoding: string | null;
72
+ filename: string | null;
73
+ bytes: number;
74
+ };
75
+
76
+ const MAX_MIME_PARTS = 64;
77
+
78
+ function splitEntity(entity: string) {
79
+ const separator = /\r?\n\r?\n/u.exec(entity);
80
+
81
+ if (!separator) return { headers: entity, body: "" };
82
+
83
+ return {
84
+ headers: entity.slice(0, separator.index),
85
+ body: entity.slice(separator.index + separator[0].length),
86
+ };
87
+ }
88
+
89
+ function walkMime(entity: string, depth: number, parts: MimePart[]) {
90
+ if (parts.length >= MAX_MIME_PARTS) return;
91
+ const { headers, body } = splitEntity(entity);
92
+ const contentType = headerValue(headers, "content-type") ?? "text/plain";
93
+ const [mediaType = ""] = contentType.split(";");
94
+ const type = mediaType.trim().toLocaleLowerCase();
95
+ const disposition = headerValue(headers, "content-disposition") ?? "";
96
+
97
+ const quoted = (value: string | null) =>
98
+ value?.replaceAll(/^"|"$/gu, "") ?? null;
99
+
100
+ parts.push({
101
+ depth,
102
+ contentType: type,
103
+ charset: quoted(tagValue(contentType, "charset")),
104
+ encoding: headerValue(headers, "content-transfer-encoding"),
105
+ filename: quoted(
106
+ tagValue(disposition, "filename") ?? tagValue(contentType, "name")
107
+ ),
108
+ bytes: new TextEncoder().encode(body).byteLength,
109
+ });
110
+
111
+ if (!type.startsWith("multipart/")) return;
112
+ const boundary = quoted(tagValue(contentType, "boundary"));
113
+
114
+ if (!boundary) return;
115
+ const delimiter = `--${boundary}`;
116
+ const sections = body.split(delimiter).slice(1);
117
+
118
+ for (const section of sections) {
119
+ if (section.startsWith("--")) break;
120
+ walkMime(section.replace(/^\r?\n/u, ""), depth + 1, parts);
121
+ }
122
+ }
123
+
124
+ /** Flattened MIME tree, parents before children, capped for huge messages. */
125
+ export function mimeStructure(raw: string): MimePart[] {
126
+ const parts: MimePart[] = [];
127
+ walkMime(raw, 0, parts);
128
+
129
+ return parts;
130
+ }
131
+
132
+ type OriginalSenderDetails = {
133
+ /** Return-Path when present; Cloudflare adds none, so the SMTP MAIL FROM otherwise. */
134
+ returnPath: string | null;
135
+ /** Connecting IP as seen by Cloudflare; never taken from sender-reported hops. */
136
+ senderIp: string | null;
137
+ mailer: string | null;
138
+ listId: string | null;
139
+ precedence: string | null;
140
+ };
141
+
142
+ export function originalSenderDetails(
143
+ raw: string,
144
+ authentication: Pick<
145
+ InboundAuthentication,
146
+ "remoteIp" | "envelopeFrom"
147
+ > | null = null,
148
+ hops: ReceivedHop[] = receivedHops(raw)
149
+ ): OriginalSenderDetails {
150
+ const trustedHop = hops.find((hop) => hop.trusted);
151
+
152
+ return {
153
+ returnPath:
154
+ headerValue(raw, "return-path")?.replaceAll(/^<|>$/gu, "") ||
155
+ authentication?.envelopeFrom ||
156
+ null,
157
+ senderIp: authentication?.remoteIp ?? trustedHop?.ip ?? null,
158
+ mailer: headerValue(raw, "x-mailer") ?? headerValue(raw, "user-agent"),
159
+ listId: headerValue(raw, "list-id"),
160
+ precedence: headerValue(raw, "precedence"),
161
+ };
162
+ }
163
+
164
+ /**
165
+ * End-to-end transit: the trusted Cloudflare hop minus the earliest dated
166
+ * hop, or minus the sender's Date header when no other hop carries a date.
167
+ */
168
+ export function transitTime(raw: string, hops: ReceivedHop[]) {
169
+ const arrivalHop = hops.find((hop) => hop.trusted) ?? hops.at(-1);
170
+ const arrival = arrivalHop?.at;
171
+
172
+ if (!arrival) return null;
173
+
174
+ const earliest = hops.find(
175
+ (hop) =>
176
+ hop !== arrivalHop && hop.at && hop.at.getTime() <= arrival.getTime()
177
+ )?.at;
178
+
179
+ if (earliest) {
180
+ return {
181
+ ms: arrival.getTime() - earliest.getTime(),
182
+ basis: "hops" as const,
183
+ };
184
+ }
185
+
186
+ const date = parseMailDate(headerValue(raw, "date"));
187
+
188
+ return date && date.getTime() <= arrival.getTime()
189
+ ? { ms: arrival.getTime() - date.getTime(), basis: "date-header" as const }
190
+ : null;
191
+ }
@@ -0,0 +1,53 @@
1
+ import { toast } from "sonner";
2
+
3
+ import type { TransportState } from "./types";
4
+
5
+ type OutboundResult = {
6
+ transportState: Exclude<TransportState, "received">;
7
+ transportError?: string | null;
8
+ externalizedAttachments?: number;
9
+ detached?: boolean;
10
+ };
11
+
12
+ export function notifyOutboundResult(
13
+ result: OutboundResult,
14
+ kind: "message" | "reply"
15
+ ) {
16
+ const label = kind === "reply" ? "Reply" : "Message";
17
+
18
+ if (result.transportState === "failed") {
19
+ toast.error(
20
+ result.transportError || `${label} could not be prepared for sending`
21
+ );
22
+
23
+ return;
24
+ }
25
+
26
+ if (result.transportState === "unconfirmed") {
27
+ toast.warning(
28
+ result.transportError
29
+ ? `${result.transportError} Submission was not confirmed.`
30
+ : "Submission was not confirmed; automatic resend is disabled"
31
+ );
32
+
33
+ return;
34
+ }
35
+
36
+ if (result.externalizedAttachments) {
37
+ toast.success(
38
+ `${result.externalizedAttachments} large attachment${
39
+ result.externalizedAttachments === 1 ? "" : "s"
40
+ } sent as 30-day download links`
41
+ );
42
+
43
+ return;
44
+ }
45
+
46
+ toast.success(
47
+ kind === "reply"
48
+ ? result.detached
49
+ ? "Private reply sent"
50
+ : "Reply sent"
51
+ : "Message sent"
52
+ );
53
+ }
@@ -0,0 +1,117 @@
1
+ import { useCallback, useState } from "react";
2
+ import type { RefObject } from "react";
3
+
4
+ const STORAGE_KEY = "openworkspace.mail.list-width";
5
+ const DEFAULT_WIDTH = 608;
6
+ const MIN_WIDTH = 320;
7
+
8
+ // The reading pane keeps at least this much room beside the list.
9
+ const READER_MIN_WIDTH = 416;
10
+ const KEY_STEP = 32;
11
+
12
+ function readWidth() {
13
+ try {
14
+ const stored = Number(localStorage.getItem(STORAGE_KEY));
15
+
16
+ return Number.isFinite(stored) && stored >= MIN_WIDTH
17
+ ? stored
18
+ : DEFAULT_WIDTH;
19
+ } catch {
20
+ return DEFAULT_WIDTH;
21
+ }
22
+ }
23
+
24
+ /** List pane width in pixels, remembered per browser. */
25
+ export function useListWidth() {
26
+ const [storedWidth, setStoredWidth] = useState(readWidth);
27
+
28
+ const rememberWidth = useCallback((next: number) => {
29
+ setStoredWidth(next);
30
+
31
+ try {
32
+ localStorage.setItem(STORAGE_KEY, String(Math.round(next)));
33
+ } catch {
34
+ // Preference only.
35
+ }
36
+ }, []);
37
+
38
+ return [storedWidth, rememberWidth] as const;
39
+ }
40
+
41
+ /** Draggable, keyboard-adjustable gap between the list and reading panes. */
42
+ export function PaneSplitter({
43
+ containerRef,
44
+ width,
45
+ onWidthChange,
46
+ }: {
47
+ containerRef: RefObject<HTMLElement | null>;
48
+ width: number;
49
+ onWidthChange: (width: number) => void;
50
+ }) {
51
+ const [dragging, setDragging] = useState(false);
52
+
53
+ function clamp(next: number) {
54
+ const containerWidth =
55
+ containerRef.current?.clientWidth ?? next + READER_MIN_WIDTH;
56
+
57
+ return Math.max(
58
+ MIN_WIDTH,
59
+ Math.min(next, containerWidth - READER_MIN_WIDTH)
60
+ );
61
+ }
62
+
63
+ // The rendered width can be narrower than the stored one on small screens.
64
+ function renderedWidth() {
65
+ const list = containerRef.current?.firstElementChild;
66
+
67
+ return list instanceof HTMLElement ? list.offsetWidth : width;
68
+ }
69
+
70
+ return (
71
+ <div
72
+ role="separator"
73
+ aria-orientation="vertical"
74
+ aria-label="Resize conversation list"
75
+ aria-valuemin={MIN_WIDTH}
76
+ aria-valuenow={Math.round(width)}
77
+ tabIndex={0}
78
+ data-dragging={dragging || undefined}
79
+ onPointerDown={(event) => {
80
+ event.preventDefault();
81
+ const startX = event.clientX;
82
+ const startWidth = renderedWidth();
83
+ const target = event.currentTarget;
84
+ target.setPointerCapture(event.pointerId);
85
+ setDragging(true);
86
+
87
+ const onMove = (moveEvent: PointerEvent) => {
88
+ onWidthChange(clamp(startWidth + moveEvent.clientX - startX));
89
+ };
90
+
91
+ const onUp = () => {
92
+ setDragging(false);
93
+ target.removeEventListener("pointermove", onMove);
94
+ target.removeEventListener("pointerup", onUp);
95
+ target.removeEventListener("pointercancel", onUp);
96
+ };
97
+
98
+ target.addEventListener("pointermove", onMove);
99
+ target.addEventListener("pointerup", onUp);
100
+ target.addEventListener("pointercancel", onUp);
101
+ }}
102
+ onDoubleClick={() => onWidthChange(clamp(DEFAULT_WIDTH))}
103
+ onKeyDown={(event) => {
104
+ if (event.key !== "ArrowLeft" && event.key !== "ArrowRight") return;
105
+ event.preventDefault();
106
+ const delta = event.key === "ArrowLeft" ? -KEY_STEP : KEY_STEP;
107
+ onWidthChange(clamp(renderedWidth() + delta));
108
+ }}
109
+ className="group relative z-10 hidden w-2.5 shrink-0 cursor-col-resize touch-none rounded-full outline-none lg:block"
110
+ >
111
+ <span
112
+ aria-hidden="true"
113
+ className="bg-foreground/0 group-hover:bg-foreground/20 group-focus-visible:bg-primary group-data-dragging:bg-primary absolute top-1/2 left-1/2 h-10 w-1 -translate-1/2 rounded-full transition-[height,background-color] group-focus-visible:h-16 group-data-dragging:h-16"
114
+ />
115
+ </div>
116
+ );
117
+ }
@@ -0,0 +1,152 @@
1
+ import { Button } from "@open-workspace/core/ui";
2
+ import { ChevronLeft, ChevronRight, Minus, Plus, Search } from "lucide-react";
3
+ import { useState } from "react";
4
+
5
+ import type { usePdfViewer } from "./use-pdf-viewer";
6
+
7
+ export function PdfPreviewToolbar({
8
+ pdf,
9
+ }: {
10
+ pdf: Omit<ReturnType<typeof usePdfViewer>, "containerRef" | "pagesRef">;
11
+ }) {
12
+ const [pageInput, setPageInput] = useState(() => String(pdf.page));
13
+ const [shownPage, setShownPage] = useState(pdf.page);
14
+ const [query, setQuery] = useState("");
15
+
16
+ // Turning the page (scrolling, the arrows) replaces whatever was typed.
17
+ if (shownPage !== pdf.page) {
18
+ setShownPage(pdf.page);
19
+ setPageInput(String(pdf.page));
20
+ }
21
+
22
+ const disabled = !pdf.ready || Boolean(pdf.error);
23
+ const pages = pdf.document?.numPages ?? 0;
24
+
25
+ function goToPage(page: number) {
26
+ setPageInput(String(pdf.goToPage(page)));
27
+ }
28
+
29
+ return (
30
+ <div className="flex flex-wrap items-center gap-x-3 gap-y-2 border-b px-3 py-2">
31
+ <div className="flex items-center gap-1">
32
+ <Button
33
+ size="icon-xs"
34
+ variant="ghost"
35
+ aria-label="Previous page"
36
+ disabled={disabled || pdf.page <= 1}
37
+ onClick={() => goToPage(pdf.page - 1)}
38
+ >
39
+ <ChevronLeft />
40
+ </Button>
41
+ <input
42
+ aria-label="Page number"
43
+ inputMode="numeric"
44
+ className="h-7 w-12 rounded border bg-transparent text-center text-xs tabular-nums"
45
+ value={pageInput}
46
+ disabled={disabled}
47
+ onChange={(event) => setPageInput(event.target.value)}
48
+ onBlur={() => goToPage(Number(pageInput))}
49
+ onKeyDown={(event) => {
50
+ if (event.key === "Enter") goToPage(Number(pageInput));
51
+ }}
52
+ />
53
+ <span className="text-muted-foreground text-xs tabular-nums">
54
+ / {pages || "—"}
55
+ </span>
56
+ <Button
57
+ size="icon-xs"
58
+ variant="ghost"
59
+ aria-label="Next page"
60
+ disabled={disabled || pdf.page >= pages}
61
+ onClick={() => goToPage(pdf.page + 1)}
62
+ >
63
+ <ChevronRight />
64
+ </Button>
65
+ </div>
66
+ <div className="flex items-center gap-1">
67
+ <Button
68
+ size="icon-xs"
69
+ variant="ghost"
70
+ aria-label="Zoom out"
71
+ disabled={disabled}
72
+ onClick={pdf.zoomOut}
73
+ >
74
+ <Minus />
75
+ </Button>
76
+ <span
77
+ aria-label="Zoom level"
78
+ className="w-10 text-center text-xs tabular-nums"
79
+ >
80
+ {Math.round(pdf.scale * 100)}%
81
+ </span>
82
+ <Button
83
+ size="icon-xs"
84
+ variant="ghost"
85
+ aria-label="Zoom in"
86
+ disabled={disabled}
87
+ onClick={pdf.zoomIn}
88
+ >
89
+ <Plus />
90
+ </Button>
91
+ <Button
92
+ size="xs"
93
+ variant="ghost"
94
+ disabled={disabled}
95
+ onClick={pdf.fitWidth}
96
+ >
97
+ Fit width
98
+ </Button>
99
+ </div>
100
+ <div
101
+ role="search"
102
+ className="flex w-full min-w-0 items-center gap-1 sm:ml-auto sm:w-auto sm:max-w-80 sm:flex-1"
103
+ >
104
+ <Search className="text-muted-foreground size-3.5 shrink-0" />
105
+ <input
106
+ type="search"
107
+ aria-label="Find in PDF"
108
+ placeholder="Find in document"
109
+ className="focus-visible:ring-ring h-7 min-w-0 flex-1 bg-transparent text-xs outline-none focus-visible:ring-2"
110
+ value={query}
111
+ disabled={disabled}
112
+ onChange={(event) => {
113
+ setQuery(event.target.value);
114
+ pdf.find(event.target.value);
115
+ }}
116
+ onKeyDown={(event) => {
117
+ if (event.key === "Enter") {
118
+ event.preventDefault();
119
+ pdf.find(query, true, event.shiftKey);
120
+ }
121
+ }}
122
+ />
123
+ {query && (
124
+ <span
125
+ role="status"
126
+ className="text-muted-foreground text-xs whitespace-nowrap tabular-nums"
127
+ >
128
+ {pdf.matches.current} / {pdf.matches.total}
129
+ </span>
130
+ )}
131
+ <Button
132
+ size="icon-xs"
133
+ variant="ghost"
134
+ aria-label="Previous match"
135
+ disabled={disabled || !query || !pdf.matches.total}
136
+ onClick={() => pdf.find(query, true, true)}
137
+ >
138
+ <ChevronLeft />
139
+ </Button>
140
+ <Button
141
+ size="icon-xs"
142
+ variant="ghost"
143
+ aria-label="Next match"
144
+ disabled={disabled || !query || !pdf.matches.total}
145
+ onClick={() => pdf.find(query, true)}
146
+ >
147
+ <ChevronRight />
148
+ </Button>
149
+ </div>
150
+ </div>
151
+ );
152
+ }
@@ -0,0 +1,44 @@
1
+ .attachment-pdf-container {
2
+ overscroll-behavior: contain;
3
+ color-scheme: light;
4
+ }
5
+
6
+ .attachment-pdf-container .pdfViewer {
7
+ padding: 12px 0;
8
+ }
9
+
10
+ /* PDF.js page dimensions exclude its border; Tailwind's border-box reset
11
+ otherwise shrinks the canvas while text coordinates keep their full scale. */
12
+ .attachment-pdf-container .pdfViewer .page {
13
+ box-sizing: content-box;
14
+ }
15
+
16
+ .attachment-pdf-container .textLayer {
17
+ caret-color: transparent;
18
+ }
19
+
20
+ .pdf-translation-overlay {
21
+ position: absolute;
22
+ inset: 0;
23
+ overflow: hidden;
24
+ z-index: 2;
25
+ color: black;
26
+ color-scheme: light;
27
+ }
28
+ .pdf-translation-overlay > canvas {
29
+ width: 100%;
30
+ height: 100%;
31
+ }
32
+ .pdf-translated-text {
33
+ position: absolute;
34
+ inset: 0;
35
+ transform-origin: top left;
36
+ }
37
+ .pdf-translated-text > span {
38
+ position: absolute;
39
+ white-space: pre;
40
+ }
41
+ .pdf-page-translated > .canvasWrapper,
42
+ .pdf-page-translated > .textLayer {
43
+ visibility: hidden;
44
+ }