@open-workspace/email 0.2.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 (223) hide show
  1. package/README.md +23 -20
  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_dry_moon_knight.sql +77 -0
  11. package/migrations/meta/0000_snapshot.json +248 -106
  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 +78 -26
  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 +90 -34
  127. package/worker/jmap/eventsource.ts +169 -0
  128. package/worker/jmap/methods.ts +1716 -361
  129. package/worker/jmap/push-types.ts +27 -0
  130. package/worker/jmap/push.ts +207 -0
  131. package/worker/jmap/routes.ts +448 -86
  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 +84 -52
  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 +283 -171
  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 +871 -542
  184. package/worker/mail/safety-routes.ts +329 -202
  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 +153 -91
  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 +597 -227
  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 +1761 -737
  202. package/worker/mailbox/jmap-store.ts +139 -35
  203. package/worker/mailbox/model.ts +7 -14
  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 -126
  216. package/containers/office-preview/Dockerfile +0 -10
  217. package/migrations/0000_email.sql +0 -53
  218. package/shared/mail-rules.ts +0 -156
  219. package/worker/admin/report-routes.ts +0 -164
  220. package/worker/mail/reported-messages.ts +0 -68
  221. package/worker/mailbox/legacy-migrations.ts +0 -41
  222. package/worker/mailbox/migrations.ts +0 -10
  223. package/worker/mailbox/rules.ts +0 -110
@@ -0,0 +1,73 @@
1
+ import { mailPath } from "../../shared/mail-path";
2
+ import type { Folder, Mailbox, MailFolder } from "./types";
3
+
4
+ export function mailboxForRoute(
5
+ mailboxId: string | undefined,
6
+ mailboxes: Mailbox[]
7
+ ) {
8
+ return mailboxes.find((mailbox) => mailbox.id === mailboxId) ?? mailboxes[0];
9
+ }
10
+
11
+ /** The mailbox, folder and conversation segments of a mail URL, decoded. */
12
+ export type MailRoute = {
13
+ mailboxId?: string;
14
+ folder?: string;
15
+ conversationId?: string;
16
+ };
17
+
18
+ /** The path a route's segments spell, exactly as given (see `mailPath` for the canonical one). */
19
+ export function routePath(route: MailRoute) {
20
+ if (!route.mailboxId) return "/";
21
+
22
+ return `/mail/${[route.mailboxId, route.folder, route.conversationId]
23
+ .flatMap((segment) => (segment ? [encodeURIComponent(segment)] : []))
24
+ .join("/")}`;
25
+ }
26
+
27
+ /**
28
+ * Where the mail screen is, from the URL and whatever has loaded so far.
29
+ * Before the mailbox list and folders arrive, the URL's mailbox, folder and
30
+ * conversation are used as they are, so their data can load in parallel
31
+ * (the server checks access). Once both are known, an unknown mailbox falls
32
+ * back to the first one and an unknown folder to Inbox, both dropping the
33
+ * conversation, and `canonicalPath` is what the URL should then say.
34
+ */
35
+ export function resolveMailLocation(
36
+ route: MailRoute,
37
+ mailboxes: Mailbox[] | undefined,
38
+ folders: MailFolder[] | undefined
39
+ ) {
40
+ const { mailboxId } = route;
41
+
42
+ const requestedMailbox = mailboxes?.find(
43
+ (mailbox) => mailbox.id === mailboxId
44
+ );
45
+
46
+ const mailbox = mailboxes ? mailboxForRoute(mailboxId, mailboxes) : undefined;
47
+ const requestedFolder: Folder = route.folder ?? "inbox";
48
+ const ready = Boolean(mailbox && folders);
49
+
50
+ const folderExists =
51
+ folders?.some((folder) => folder.id === requestedFolder) ?? false;
52
+
53
+ const folderStale = Boolean(folders) && !folderExists;
54
+ const mailboxStale = Boolean(mailboxes) && !requestedMailbox;
55
+ const folder = folderStale ? "inbox" : requestedFolder;
56
+
57
+ const conversationId =
58
+ mailboxId && !mailboxStale && !folderStale
59
+ ? route.conversationId
60
+ : undefined;
61
+
62
+ return {
63
+ mailbox,
64
+ /** The mailbox to load data for: the URL's until the list says otherwise. */
65
+ mailboxId: mailboxes ? mailbox?.id : mailboxId,
66
+ folder,
67
+ conversationId,
68
+ ready,
69
+ canonicalPath: mailbox
70
+ ? mailPath(mailbox.id, folder, conversationId)
71
+ : undefined,
72
+ };
73
+ }
@@ -0,0 +1,275 @@
1
+ import {
2
+ Sheet,
3
+ SheetClose,
4
+ SheetContent,
5
+ SheetDescription,
6
+ SheetTitle,
7
+ cn,
8
+ } from "@open-workspace/core/ui";
9
+ import { useQueryClient } from "@tanstack/react-query";
10
+ import { Check, ChevronsUpDown, X } from "lucide-react";
11
+ import { useEffect } from "react";
12
+ import { useLocation, useNavigate } from "react-router";
13
+ import { z } from "zod";
14
+
15
+ import { folderAfterMailboxSwitch } from "./mail-folders";
16
+ import { MailboxGlyph } from "./mail-nav";
17
+ import { CountPill } from "./mail-primitives";
18
+ import { conversationsQuery, foldersQuery } from "./mail-queries";
19
+ import type { Mailbox } from "./types";
20
+ import type { MailController } from "./use-mail-controller";
21
+
22
+ /** What names a mailbox in tight spots: the address for a person's own, the name for a shared one. */
23
+ export function mailboxCaption(mailbox: Mailbox) {
24
+ return mailbox.kind === "shared" ? mailbox.displayName : mailbox.address;
25
+ }
26
+
27
+ /** The quiet line over the phone's list title: which mailbox the folder belongs to. */
28
+ export function MailboxCaption({ mail }: { mail: MailController }) {
29
+ if (!mail.mailbox) return null;
30
+
31
+ return (
32
+ <p className="text-muted-foreground truncate text-xs leading-4">
33
+ {mailboxCaption(mail.mailbox)}
34
+ </p>
35
+ );
36
+ }
37
+
38
+ /**
39
+ * The dock's first button: the current mailbox's avatar, opening a sheet to
40
+ * switch. A dot marks unread mail in the others. With one mailbox there is
41
+ * nothing to switch to, so there is no button.
42
+ */
43
+ export function DockMailboxSwitcher({ mail }: { mail: MailController }) {
44
+ const sheet = useMailboxSheet();
45
+ const { mailbox, mailboxes } = mail;
46
+
47
+ if (!mailbox || mailboxes.length < 2) return null;
48
+
49
+ const unreadElsewhere = mailboxes.some(
50
+ (item) => item.id !== mailbox.id && item.unreadCount > 0
51
+ );
52
+
53
+ return (
54
+ <>
55
+ <button
56
+ type="button"
57
+ aria-haspopup="dialog"
58
+ aria-expanded={sheet.open}
59
+ aria-label={`Mailbox: ${mailbox.displayName}${unreadElsewhere ? ", unread mail in other mailboxes" : ""}`}
60
+ title={mailboxCaption(mailbox)}
61
+ onClick={sheet.show}
62
+ className={cn(
63
+ "text-muted-foreground hover:text-foreground focus-visible:ring-ring flex h-12 shrink-0 items-center gap-0.5 rounded-xl pr-1 pl-2 outline-none focus-visible:ring-2",
64
+ sheet.open && "bg-sidebar-accent text-foreground"
65
+ )}
66
+ >
67
+ <span className="relative shrink-0">
68
+ <MailboxGlyph
69
+ mailbox={mailbox}
70
+ active={false}
71
+ className="size-7 text-[10.5px]"
72
+ />
73
+ {unreadElsewhere && (
74
+ <span
75
+ aria-hidden="true"
76
+ className="border-surface bg-primary absolute -top-0.5 -right-0.5 size-2.5 rounded-full border-2"
77
+ />
78
+ )}
79
+ </span>
80
+ <ChevronsUpDown
81
+ aria-hidden="true"
82
+ className="size-3.5 shrink-0"
83
+ strokeWidth={2}
84
+ />
85
+ </button>
86
+ <MailboxSheet mail={mail} open={sheet.open} onClose={sheet.close} />
87
+ </>
88
+ );
89
+ }
90
+
91
+ /**
92
+ * Every mailbox the person can open, the current one checked. Picking one
93
+ * keeps the folder when that mailbox has it, otherwise opens its Inbox.
94
+ */
95
+ function MailboxSheet({
96
+ mail,
97
+ open,
98
+ onClose,
99
+ }: {
100
+ mail: MailController;
101
+ open: boolean;
102
+ onClose: () => void;
103
+ }) {
104
+ const queryClient = useQueryClient();
105
+ const { folder, debouncedSearch, unreadOnly } = mail;
106
+
107
+ // While the sheet is open, the other mailboxes' folders and the list a
108
+ // switch would land on load, so picking one swaps the screen in a frame.
109
+ const otherIds = mail.mailboxes
110
+ .filter((item) => item.id !== mail.mailbox?.id)
111
+ .map((item) => item.id)
112
+ .join(" ");
113
+
114
+ useEffect(() => {
115
+ if (!open) return;
116
+
117
+ for (const id of otherIds.split(" ").filter(Boolean)) {
118
+ void queryClient
119
+ .fetchQuery({ ...foldersQuery(id), staleTime: 30_000 })
120
+ .then(
121
+ (data) => {
122
+ const target = folderAfterMailboxSwitch(folder, data.folders);
123
+ void queryClient.prefetchInfiniteQuery({
124
+ ...conversationsQuery(id, target, debouncedSearch, unreadOnly),
125
+ staleTime: 30_000,
126
+ });
127
+ },
128
+ () => undefined
129
+ );
130
+ }
131
+ }, [open, otherIds, folder, debouncedSearch, unreadOnly, queryClient]);
132
+
133
+ async function pick(mailbox: Mailbox) {
134
+ if (mailbox.id === mail.mailbox?.id) {
135
+ onClose();
136
+
137
+ return;
138
+ }
139
+
140
+ const data = await queryClient
141
+ .ensureQueryData(foldersQuery(mailbox.id))
142
+ .catch(() => undefined);
143
+
144
+ // Replaces the sheet's history entry, so Back returns to the mailbox before.
145
+ mail.openFolder(
146
+ mailbox.id,
147
+ folderAfterMailboxSwitch(folder, data?.folders),
148
+ true
149
+ );
150
+ }
151
+
152
+ return (
153
+ <Sheet
154
+ open={open}
155
+ onOpenChange={(next) => {
156
+ if (!next) onClose();
157
+ }}
158
+ >
159
+ <SheetContent
160
+ side="bottom"
161
+ showCloseButton={false}
162
+ className="border-border bg-surface max-h-[min(85dvh,40rem)] gap-0 rounded-t-2xl p-0 pb-[env(safe-area-inset-bottom)] lg:hidden"
163
+ >
164
+ <div className="relative shrink-0 px-4 pt-2.5">
165
+ <span
166
+ aria-hidden="true"
167
+ className="bg-foreground/15 mx-auto block h-1 w-9 rounded-full"
168
+ />
169
+ <SheetTitle className="mt-3 pr-10 text-[15px] font-semibold tracking-[-0.01em]">
170
+ Mailboxes
171
+ </SheetTitle>
172
+ <SheetDescription className="pr-10 text-xs">
173
+ Switch to another mailbox you can open
174
+ </SheetDescription>
175
+ <SheetClose
176
+ aria-label="Close mailboxes"
177
+ className="text-muted-foreground active:bg-foreground/[0.05] focus-visible:ring-ring absolute top-5 right-2.5 grid size-10 place-items-center rounded-full outline-none focus-visible:ring-2"
178
+ >
179
+ <X className="size-[18px]" />
180
+ </SheetClose>
181
+ </div>
182
+ <ul className="min-h-0 flex-1 overflow-y-auto overscroll-contain px-2 pt-2 pb-3">
183
+ {mail.mailboxes.map((item) => {
184
+ const current = item.id === mail.mailbox?.id;
185
+
186
+ return (
187
+ <li key={item.id}>
188
+ <button
189
+ type="button"
190
+ aria-current={current ? "true" : undefined}
191
+ onClick={() => void pick(item)}
192
+ className={cn(
193
+ // No color transition: the highlight moves at once, as in the folder lists.
194
+ "flex h-15 w-full items-center gap-3 rounded-xl px-3 text-left outline-none",
195
+ "focus-visible:ring-ring focus-visible:ring-2",
196
+ current
197
+ ? "bg-sidebar-accent text-sidebar-accent-foreground"
198
+ : "text-foreground active:bg-foreground/[0.05]"
199
+ )}
200
+ >
201
+ <MailboxGlyph
202
+ mailbox={item}
203
+ active={current}
204
+ className="size-8 text-[11px]"
205
+ />
206
+ <span className="min-w-0 flex-1">
207
+ <span
208
+ className={cn(
209
+ "block truncate text-[15px] leading-5",
210
+ current ? "font-semibold" : "font-medium"
211
+ )}
212
+ >
213
+ {item.displayName}
214
+ </span>
215
+ <span className="text-muted-foreground block truncate text-xs leading-4">
216
+ {item.address}
217
+ </span>
218
+ </span>
219
+ <CountPill
220
+ count={item.unreadCount}
221
+ strong
222
+ label={`${item.unreadCount} unread`}
223
+ className="text-[11.5px] leading-4"
224
+ />
225
+ <Check
226
+ aria-hidden="true"
227
+ className={cn(
228
+ "text-sidebar-primary size-[18px] shrink-0",
229
+ !current && "invisible"
230
+ )}
231
+ strokeWidth={2.2}
232
+ />
233
+ </button>
234
+ </li>
235
+ );
236
+ })}
237
+ </ul>
238
+ </SheetContent>
239
+ </Sheet>
240
+ );
241
+ }
242
+
243
+ /** The history entry the mailbox sheet opened (see `show`). */
244
+ const sheetState = z.object({ mailboxSheet: z.literal(true) });
245
+
246
+ /** React Router's history state: the entry's position in this tab's history. */
247
+ const historyState = z.object({ idx: z.number() });
248
+
249
+ /**
250
+ * The mailbox sheet is a history entry (location state), so the system Back
251
+ * button closes it instead of leaving the folder.
252
+ */
253
+ function useMailboxSheet() {
254
+ const location = useLocation();
255
+ const navigate = useNavigate();
256
+
257
+ const open = sheetState.safeParse(location.state).success;
258
+
259
+ const here = { pathname: location.pathname, search: location.search };
260
+
261
+ return {
262
+ open,
263
+ show() {
264
+ if (!open) navigate(here, { state: { mailboxSheet: true } });
265
+ },
266
+ close() {
267
+ // After a reload the sheet's entry may be the first one; then it is replaced.
268
+ const history = historyState.safeParse(window.history.state);
269
+ const index = history.success ? history.data.idx : 0;
270
+
271
+ if (index > 0) navigate(-1);
272
+ else navigate(here, { replace: true, state: null });
273
+ },
274
+ };
275
+ }
@@ -0,0 +1,129 @@
1
+ import { TooltipProvider, cn } from "@open-workspace/core/ui";
2
+
3
+ import { FolderGlyph } from "./folder-glyph";
4
+ import { FolderManager } from "./folder-manager";
5
+ import { orderedFolders, unreadOf } from "./mail-folders";
6
+ import { ComposeButton, MailboxGlyph } from "./mail-nav";
7
+ import { ToolbarTooltip } from "./toolbar-tooltip";
8
+ import type { MailController } from "./use-mail-controller";
9
+
10
+ const railItemClass =
11
+ "relative grid size-10 place-items-center rounded-[10px] outline-none transition-colors focus-visible:ring-2 focus-visible:ring-sidebar-ring";
12
+
13
+ function UnreadDot() {
14
+ return (
15
+ <span
16
+ aria-hidden="true"
17
+ className="bg-primary ring-surface absolute top-1.5 right-1.5 size-2 rounded-full ring-2"
18
+ />
19
+ );
20
+ }
21
+
22
+ /**
23
+ * Icon rail used between lg and xl: compose, mailboxes, the active mailbox's
24
+ * folders, then settings. Labels move into tooltips.
25
+ */
26
+ export function NavRail({ mail }: { mail: MailController }) {
27
+ const { folders, folder } = mail;
28
+ const railFolders = orderedFolders(folders ?? []);
29
+
30
+ return (
31
+ <TooltipProvider delay={200}>
32
+ <nav
33
+ aria-label="Mailboxes and folders"
34
+ className="flex min-h-0 flex-1 flex-col items-center"
35
+ >
36
+ <div className="flex min-h-0 w-full flex-1 flex-col items-center gap-1 overflow-y-auto px-2 py-2.5">
37
+ <ToolbarTooltip label="New message" side="right">
38
+ <ComposeButton
39
+ disabled={!mail.mailbox?.canSend}
40
+ iconOnly
41
+ onClick={mail.openCompose}
42
+ />
43
+ </ToolbarTooltip>
44
+
45
+ <span
46
+ aria-hidden="true"
47
+ className="bg-sidebar-border my-1.5 h-px w-7 shrink-0"
48
+ />
49
+ {mail.mailboxes.map((item) => {
50
+ const active = item.id === mail.mailbox?.id;
51
+
52
+ return (
53
+ <ToolbarTooltip
54
+ key={item.id}
55
+ side="right"
56
+ label={`${item.displayName}${item.unreadCount ? ` · ${item.unreadCount} unread` : ""}`}
57
+ >
58
+ <button
59
+ type="button"
60
+ aria-current={active ? "true" : undefined}
61
+ aria-label={`${item.displayName}${item.unreadCount && !active ? `, ${item.unreadCount} unread` : ""}`}
62
+ onClick={() => mail.selectMailbox(item.id)}
63
+ className={cn(
64
+ railItemClass,
65
+ active ? "bg-sidebar-accent" : "hover:bg-foreground/[0.045]"
66
+ )}
67
+ >
68
+ <MailboxGlyph
69
+ mailbox={item}
70
+ active={active}
71
+ className="size-6 text-[10px]"
72
+ />
73
+ {!active && item.unreadCount > 0 && <UnreadDot />}
74
+ </button>
75
+ </ToolbarTooltip>
76
+ );
77
+ })}
78
+
79
+ <span
80
+ aria-hidden="true"
81
+ className="bg-sidebar-border my-1.5 h-px w-7 shrink-0"
82
+ />
83
+ {railFolders.map((entry) => {
84
+ const active = entry.id === folder;
85
+ const unread = unreadOf(entry);
86
+
87
+ return (
88
+ <ToolbarTooltip
89
+ key={entry.id}
90
+ side="right"
91
+ label={`${entry.name}${unread ? ` · ${unread} unread` : ""}`}
92
+ >
93
+ <button
94
+ type="button"
95
+ aria-current={active ? "page" : undefined}
96
+ aria-label={`${entry.name}${unread ? `, ${unread} unread` : ""}`}
97
+ onClick={() => mail.selectFolder(entry.id)}
98
+ className={cn(
99
+ railItemClass,
100
+ active
101
+ ? "bg-sidebar-accent text-sidebar-primary"
102
+ : "text-muted-foreground hover:bg-foreground/[0.045] hover:text-foreground"
103
+ )}
104
+ >
105
+ <FolderGlyph
106
+ folder={entry}
107
+ className="size-[17px]"
108
+ strokeWidth={active ? 2.1 : 1.8}
109
+ />
110
+ {unread > 0 && <UnreadDot />}
111
+ </button>
112
+ </ToolbarTooltip>
113
+ );
114
+ })}
115
+ {mail.mailbox && (
116
+ <span className="mt-0.5 [&_button]:size-10 [&_button]:rounded-[10px]">
117
+ <FolderManager
118
+ mailboxId={mail.mailbox.id}
119
+ activeFolderId={folder}
120
+ folders={folders ?? []}
121
+ onSelectFolder={mail.selectFolder}
122
+ />
123
+ </span>
124
+ )}
125
+ </div>
126
+ </nav>
127
+ </TooltipProvider>
128
+ );
129
+ }