@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.
- package/README.md +22 -22
- package/alchemy/index.ts +273 -0
- package/drizzle/mailbox/0000_puzzling_dracula.sql +149 -0
- package/drizzle/mailbox/0001_mute_jack_flag.sql +12 -0
- package/drizzle/mailbox/meta/0000_snapshot.json +1002 -0
- package/drizzle/mailbox/meta/0001_snapshot.json +1002 -0
- package/drizzle/mailbox/meta/_journal.json +20 -0
- package/drizzle/mailbox/migrations.js +12 -0
- package/index.ts +3 -1
- package/migrations/{0000_email.sql → 0000_dry_moon_knight.sql} +26 -20
- package/migrations/meta/0000_snapshot.json +125 -109
- package/migrations/meta/_journal.json +2 -2
- package/package.json +74 -68
- package/shared/attachment-preview.ts +37 -27
- package/shared/folder-order.ts +106 -0
- package/shared/mail-authentication.ts +8 -3
- package/shared/mail-feedback.ts +18 -25
- package/shared/mail-headers.ts +114 -39
- package/shared/mail-import.ts +38 -0
- package/shared/mail-path.ts +42 -0
- package/shared/mail-received.ts +174 -31
- package/shared/mail-remote.ts +59 -2
- package/shared/mail-sender-intel.ts +22 -4
- package/shared/mail.ts +77 -28
- package/shared/module.ts +33 -0
- package/shared/office-preview.ts +14 -2
- package/shared/pdfjs-assets.ts +35 -0
- package/shared/permissions.ts +73 -0
- package/shared/webhooks.ts +41 -0
- package/src/admin/blocked-senders.tsx +189 -0
- package/src/client.tsx +97 -0
- package/src/lib/mail-api.ts +9 -0
- package/src/mail/attachment-preview-dialog.tsx +161 -0
- package/src/mail/blocked-senders.tsx +138 -0
- package/src/mail/compose-window-chrome.tsx +194 -0
- package/src/mail/compose-window.tsx +486 -0
- package/src/mail/composer-asset-bookmarks.ts +409 -0
- package/src/mail/composer-asset.tsx +229 -0
- package/src/mail/composer-attachment-list.tsx +138 -0
- package/src/mail/composer-editor-document.ts +65 -0
- package/src/mail/composer-editor.tsx +284 -0
- package/src/mail/composer-linked-attachment-text.ts +12 -0
- package/src/mail/composer-recipient-fields.tsx +165 -0
- package/src/mail/composer-recipients.ts +208 -0
- package/src/mail/composer-session.ts +562 -0
- package/src/mail/composer-toolbar.tsx +303 -0
- package/src/mail/composer-upload-client.ts +105 -0
- package/src/mail/composer-upload-progress.ts +53 -0
- package/src/mail/conversation-list.tsx +550 -0
- package/src/mail/conversation-loading-skeleton.tsx +119 -0
- package/src/mail/conversation-message.tsx +346 -0
- package/src/mail/conversation-reply.tsx +470 -0
- package/src/mail/conversation-selection-toolbar.tsx +322 -0
- package/src/mail/conversation-view.tsx +437 -0
- package/src/mail/delivery-indicator.tsx +272 -0
- package/src/mail/email-html-body.tsx +203 -0
- package/src/mail/folder-glyph.tsx +46 -0
- package/src/mail/folder-manager.tsx +532 -0
- package/src/mail/format-bytes.ts +7 -0
- package/src/mail/mail-app-bar.tsx +253 -0
- package/src/mail/mail-background.tsx +12 -0
- package/src/mail/mail-dock.tsx +137 -0
- package/src/mail/mail-folders.ts +44 -0
- package/src/mail/mail-layout.css +236 -0
- package/src/mail/mail-location.ts +73 -0
- package/src/mail/mail-mailbox-sheet.tsx +275 -0
- package/src/mail/mail-nav-rail.tsx +129 -0
- package/src/mail/mail-nav.tsx +324 -0
- package/src/mail/mail-prefetch.ts +59 -0
- package/src/mail/mail-primitives.tsx +66 -0
- package/src/mail/mail-queries.ts +209 -0
- package/src/mail/mail-query-cache.ts +486 -0
- package/src/mail/mail-seed.ts +47 -0
- package/src/mail/mail-shell.tsx +126 -0
- package/src/mail/mailbox-ai-settings.tsx +185 -0
- package/src/mail/mailbox-realtime.tsx +251 -0
- package/src/mail/mailbox-route.tsx +15 -0
- package/src/mail/message-actions.tsx +353 -0
- package/src/mail/message-attachments.tsx +200 -0
- package/src/mail/message-format.ts +75 -0
- package/src/mail/message-header.tsx +260 -0
- package/src/mail/message-translation.tsx +225 -0
- package/src/mail/original-message-sheet.tsx +619 -0
- package/src/mail/original-message.ts +191 -0
- package/src/mail/outbound-notification.ts +53 -0
- package/src/mail/pane-splitter.tsx +117 -0
- package/src/mail/pdf-preview-toolbar.tsx +152 -0
- package/src/mail/pdf-preview.css +44 -0
- package/src/mail/pdf-preview.tsx +156 -0
- package/src/mail/pdf-translation-layout.ts +95 -0
- package/src/mail/print-message.ts +92 -0
- package/src/mail/reader-pane.tsx +535 -0
- package/src/mail/recipient-combobox.tsx +335 -0
- package/src/mail/recipient-field-row.tsx +34 -0
- package/src/mail/reply-to-warning.tsx +50 -0
- package/src/mail/sanitize-email-html.ts +430 -0
- package/src/mail/sender-actions.tsx +149 -0
- package/src/mail/sender-authentication.tsx +485 -0
- package/src/mail/sender-dialogs.tsx +384 -0
- package/src/mail/sender-security.tsx +545 -0
- package/src/mail/toolbar-tooltip.tsx +29 -0
- package/src/mail/translation-html.ts +49 -0
- package/src/mail/translation-language.ts +21 -0
- package/src/mail/types.ts +53 -0
- package/src/mail/unread-document-indicator.tsx +60 -0
- package/src/mail/use-compose-submission.ts +208 -0
- package/src/mail/use-composer-attachment-preflight.ts +184 -0
- package/src/mail/use-composer-submission.ts +117 -0
- package/src/mail/use-conversation-selection.ts +186 -0
- package/src/mail/use-mail-controller.ts +427 -0
- package/src/mail/use-mail-data.ts +279 -0
- package/src/mail/use-mail-safety.ts +155 -0
- package/src/mail/use-mail-send.ts +142 -0
- package/src/mail/use-pdf-translation-overlay.ts +156 -0
- package/src/mail/use-pdf-translation.ts +100 -0
- package/src/mail/use-pdf-viewer.ts +215 -0
- package/src/mail/use-reply-submission.ts +114 -0
- package/src/mail/use-sanitized-email-html.ts +20 -0
- package/src/mail/use-visible-message-read.ts +138 -0
- package/src/pwa/push-subscription.ts +151 -0
- package/src/settings/mail-apps-page.tsx +235 -0
- package/src/settings/notifications-page.tsx +291 -0
- package/worker/admin/blocked-sender-routes.ts +166 -0
- package/worker/admin/schemas.ts +11 -0
- package/worker/bindings.d.ts +14 -10
- package/worker/db/schema.ts +63 -40
- package/worker/jmap/eventsource.ts +84 -16
- package/worker/jmap/methods.ts +1630 -524
- package/worker/jmap/push-types.ts +27 -0
- package/worker/jmap/push.ts +140 -56
- package/worker/jmap/routes.ts +366 -105
- package/worker/mail/access.ts +258 -21
- package/worker/mail/ai-classification.ts +36 -20
- package/worker/mail/ai-translation.ts +13 -6
- package/worker/mail/auth-headers.ts +395 -137
- package/worker/mail/bimi.ts +200 -73
- package/worker/mail/bootstrap.ts +79 -0
- package/worker/mail/capped-body.ts +36 -0
- package/worker/mail/classification-signals.ts +35 -25
- package/worker/mail/conversation-cursor.ts +30 -19
- package/worker/mail/delivery-events.ts +88 -62
- package/worker/mail/delivery-facts.ts +46 -10
- package/worker/mail/demo-routes.ts +34 -10
- package/worker/mail/doh.ts +62 -23
- package/worker/mail/downloads.ts +75 -40
- package/worker/mail/edge-cache.ts +155 -0
- package/worker/mail/header-metadata.ts +88 -22
- package/worker/mail/impersonation.ts +160 -38
- package/worker/mail/import-routes.ts +313 -0
- package/worker/mail/inbound-delivery.ts +19 -9
- package/worker/mail/inbound-threading.ts +18 -8
- package/worker/mail/inbound.ts +82 -40
- package/worker/mail/ip-intel.ts +110 -27
- package/worker/mail/isolate-cache.ts +35 -0
- package/worker/mail/link-domains.ts +29 -7
- package/worker/mail/mailbox-deletion.ts +10 -4
- package/worker/mail/mailbox-directory.ts +16 -6
- package/worker/mail/mailing-list.ts +94 -39
- package/worker/mail/message-security.ts +90 -15
- package/worker/mail/message-view.ts +18 -6
- package/worker/mail/office-conversion.ts +31 -9
- package/worker/mail/office-preview-container.ts +40 -13
- package/worker/mail/office-preview-queue.ts +76 -32
- package/worker/mail/org-domain.ts +8 -1
- package/worker/mail/outbound-content.ts +71 -26
- package/worker/mail/outbound-delivery.ts +31 -17
- package/worker/mail/outbound-service.ts +48 -30
- package/worker/mail/outbound-threading.ts +30 -17
- package/worker/mail/outbound.ts +97 -70
- package/worker/mail/outgoing-context.ts +25 -15
- package/worker/mail/participants.ts +44 -29
- package/worker/mail/push-notifications.ts +271 -173
- package/worker/mail/quote-parser.ts +21 -10
- package/worker/mail/r2-presigned-urls.ts +31 -25
- package/worker/mail/receive.ts +23 -11
- package/worker/mail/recipients.ts +6 -1
- package/worker/mail/remote-proxy.ts +187 -31
- package/worker/mail/remote-routes.ts +87 -0
- package/worker/mail/remote-token.ts +104 -0
- package/worker/mail/repeat.ts +19 -0
- package/worker/mail/reply-plan.ts +62 -33
- package/worker/mail/rfc.ts +32 -7
- package/worker/mail/routes.ts +870 -541
- package/worker/mail/safety-routes.ts +328 -200
- package/worker/mail/schemas.ts +84 -105
- package/worker/mail/search.ts +1 -0
- package/worker/mail/security-settings.ts +41 -9
- package/worker/mail/sender-intel.ts +206 -51
- package/worker/mail/spamhaus.ts +92 -45
- package/worker/mail/unsubscribe.ts +76 -18
- package/worker/mail/uploads.ts +126 -93
- package/worker/mail/webhook-source.ts +35 -12
- package/worker/mail/workspace-safety.ts +97 -38
- package/worker/mailbox/demo-html.ts +7 -7
- package/worker/mailbox/demo-seed.ts +10 -44
- package/worker/mailbox/demo.ts +595 -225
- package/worker/mailbox/email-index.ts +53 -0
- package/worker/mailbox/folder-model.ts +53 -17
- package/worker/mailbox/import-placement.ts +88 -0
- package/worker/mailbox/inbox-counts.ts +106 -0
- package/worker/mailbox/index.ts +1734 -721
- package/worker/mailbox/jmap-store.ts +139 -35
- package/worker/mailbox/model.ts +8 -6
- package/worker/mailbox/office-preview-jobs.ts +56 -25
- package/worker/mailbox/schema.ts +87 -66
- package/worker/mailbox/senders.ts +176 -70
- package/worker/mcp/mail-action-tools.ts +128 -87
- package/worker/mcp/mail-compose-tools.ts +133 -82
- package/worker/mcp/mail-query-tools.ts +120 -71
- package/worker/mcp/mail-safety-tools.ts +321 -248
- package/worker/mcp/schemas.ts +43 -0
- package/worker/mcp/tools.ts +13 -10
- package/worker/module.ts +43 -18
- package/worker/sql.d.ts +8 -0
- package/alchemy.ts +0 -142
- package/containers/office-preview/Dockerfile +0 -10
- package/shared/mail-rules.ts +0 -156
- package/worker/admin/report-routes.ts +0 -164
- package/worker/mail/reported-messages.ts +0 -68
- package/worker/mailbox/migrations.ts +0 -10
- package/worker/mailbox/rules.ts +0 -110
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { useMemo, useState } from "react";
|
|
2
|
+
import { toast } from "sonner";
|
|
3
|
+
|
|
4
|
+
import type { ConversationSummary } from "./types";
|
|
5
|
+
import { useBulkConversationAction } from "./use-mail-data";
|
|
6
|
+
import type { BulkConversationAction } from "./use-mail-data";
|
|
7
|
+
import { useBulkReport } from "./use-mail-safety";
|
|
8
|
+
|
|
9
|
+
const EMPTY_SELECTION = new Set<string>();
|
|
10
|
+
|
|
11
|
+
type SelectionState = {
|
|
12
|
+
scope: string;
|
|
13
|
+
active: boolean;
|
|
14
|
+
ids: Set<string>;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export function useConversationSelection({
|
|
18
|
+
scope,
|
|
19
|
+
mailboxId,
|
|
20
|
+
folderId,
|
|
21
|
+
conversations,
|
|
22
|
+
}: {
|
|
23
|
+
scope: string;
|
|
24
|
+
mailboxId?: string;
|
|
25
|
+
folderId: string;
|
|
26
|
+
conversations: ConversationSummary[];
|
|
27
|
+
}) {
|
|
28
|
+
const [state, setState] = useState<SelectionState>({
|
|
29
|
+
scope,
|
|
30
|
+
active: false,
|
|
31
|
+
ids: new Set(),
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
if (state.scope !== scope) {
|
|
35
|
+
// Scope owns selection. Reset it before rendering the new list so browser
|
|
36
|
+
// Back/Forward cannot revive IDs from an earlier visit to the same scope.
|
|
37
|
+
setState({ scope, active: false, ids: new Set() });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const mutation = useBulkConversationAction();
|
|
41
|
+
const reportMutation = useBulkReport();
|
|
42
|
+
const active = state.scope === scope && state.active;
|
|
43
|
+
const selectedIds = active ? state.ids : EMPTY_SELECTION;
|
|
44
|
+
|
|
45
|
+
const selectedConversations = useMemo(
|
|
46
|
+
() =>
|
|
47
|
+
conversations.filter((conversation) =>
|
|
48
|
+
selectedIds.has(conversation.conversationId)
|
|
49
|
+
),
|
|
50
|
+
[conversations, selectedIds]
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
function start() {
|
|
54
|
+
if (mutation.isPending) return;
|
|
55
|
+
setState({ scope, active: true, ids: new Set() });
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function exit() {
|
|
59
|
+
setState({ scope, active: false, ids: new Set() });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function toggle(conversationId: string) {
|
|
63
|
+
if (mutation.isPending) return;
|
|
64
|
+
setState((current) => {
|
|
65
|
+
const ids = new Set(current.scope === scope ? current.ids : []);
|
|
66
|
+
|
|
67
|
+
if (ids.has(conversationId)) ids.delete(conversationId);
|
|
68
|
+
else ids.add(conversationId);
|
|
69
|
+
|
|
70
|
+
return { scope, active: true, ids };
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function toggleAllLoaded() {
|
|
75
|
+
if (mutation.isPending) return;
|
|
76
|
+
setState((current) => {
|
|
77
|
+
const ids = current.scope === scope ? current.ids : EMPTY_SELECTION;
|
|
78
|
+
|
|
79
|
+
const allLoadedSelected =
|
|
80
|
+
conversations.length > 0 &&
|
|
81
|
+
conversations.every((conversation) =>
|
|
82
|
+
ids.has(conversation.conversationId)
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
scope,
|
|
87
|
+
active: true,
|
|
88
|
+
ids: allLoadedSelected
|
|
89
|
+
? new Set()
|
|
90
|
+
: new Set(
|
|
91
|
+
conversations.map((conversation) => conversation.conversationId)
|
|
92
|
+
),
|
|
93
|
+
};
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function run(action: BulkConversationAction, verb: string) {
|
|
98
|
+
if (!mailboxId || !selectedConversations.length) return;
|
|
99
|
+
|
|
100
|
+
const applicableConversations =
|
|
101
|
+
action.type === "read"
|
|
102
|
+
? selectedConversations.filter(
|
|
103
|
+
(conversation) => conversation.hasIncoming
|
|
104
|
+
)
|
|
105
|
+
: selectedConversations;
|
|
106
|
+
|
|
107
|
+
const conversationIds = applicableConversations.map(
|
|
108
|
+
(conversation) => conversation.conversationId
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
if (!conversationIds.length) return;
|
|
112
|
+
const requestedCount = conversationIds.length;
|
|
113
|
+
mutation.mutate(
|
|
114
|
+
{
|
|
115
|
+
mailboxId,
|
|
116
|
+
sourceFolderId: folderId,
|
|
117
|
+
conversationIds,
|
|
118
|
+
action,
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
onSuccess: ({ updatedCount }) => {
|
|
122
|
+
const conversationLabel =
|
|
123
|
+
requestedCount === 1 ? "conversation" : "conversations";
|
|
124
|
+
|
|
125
|
+
if (updatedCount === requestedCount) {
|
|
126
|
+
toast.success(`${updatedCount} ${conversationLabel} ${verb}`);
|
|
127
|
+
} else {
|
|
128
|
+
toast.warning(
|
|
129
|
+
`${updatedCount} of ${requestedCount} ${conversationLabel} ${verb}. The rest no longer matched this action.`
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
exit();
|
|
134
|
+
},
|
|
135
|
+
onError: (error) => toast.error(error.message),
|
|
136
|
+
}
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Reports the newest incoming message of each selected conversation. */
|
|
141
|
+
function report() {
|
|
142
|
+
if (!mailboxId) return;
|
|
143
|
+
|
|
144
|
+
const conversationIds = selectedConversations
|
|
145
|
+
.filter((conversation) => conversation.hasIncoming)
|
|
146
|
+
.map((conversation) => conversation.conversationId);
|
|
147
|
+
|
|
148
|
+
if (!conversationIds.length) return;
|
|
149
|
+
reportMutation.mutate(
|
|
150
|
+
{ mailboxId, conversationIds },
|
|
151
|
+
{
|
|
152
|
+
onSuccess: ({ reportedCount }) => {
|
|
153
|
+
const label = reportedCount === 1 ? "conversation" : "conversations";
|
|
154
|
+
toast.success(`${reportedCount} ${label} reported as spam`);
|
|
155
|
+
exit();
|
|
156
|
+
},
|
|
157
|
+
onError: (error) => toast.error(error.message),
|
|
158
|
+
}
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
active,
|
|
164
|
+
selectedIds,
|
|
165
|
+
selectedConversations,
|
|
166
|
+
allLoadedSelected:
|
|
167
|
+
conversations.length > 0 &&
|
|
168
|
+
selectedConversations.length === conversations.length,
|
|
169
|
+
anySelectedUnread: selectedConversations.some(
|
|
170
|
+
(conversation) => conversation.isUnread
|
|
171
|
+
),
|
|
172
|
+
hasSelectedIncoming: selectedConversations.some(
|
|
173
|
+
(conversation) => conversation.hasIncoming
|
|
174
|
+
),
|
|
175
|
+
allSelectedHaveIncoming:
|
|
176
|
+
selectedConversations.length > 0 &&
|
|
177
|
+
selectedConversations.every((conversation) => conversation.hasIncoming),
|
|
178
|
+
pending: mutation.isPending || reportMutation.isPending,
|
|
179
|
+
report,
|
|
180
|
+
start,
|
|
181
|
+
exit,
|
|
182
|
+
toggle,
|
|
183
|
+
toggleAllLoaded,
|
|
184
|
+
run,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
import { useDebouncedValue } from "@open-workspace/core/ui";
|
|
2
|
+
import { useEffect, useMemo, useState } from "react";
|
|
3
|
+
import { useNavigate } from "react-router";
|
|
4
|
+
import { toast } from "sonner";
|
|
5
|
+
|
|
6
|
+
import { mailPath, parseMailPath } from "../../shared/mail-path";
|
|
7
|
+
import { folderDisplayName } from "./mail-folders";
|
|
8
|
+
import {
|
|
9
|
+
mailboxForRoute,
|
|
10
|
+
resolveMailLocation,
|
|
11
|
+
routePath,
|
|
12
|
+
} from "./mail-location";
|
|
13
|
+
import type { MailRoute } from "./mail-location";
|
|
14
|
+
import type { Folder, MessageDetail } from "./types";
|
|
15
|
+
import { useConversationSelection } from "./use-conversation-selection";
|
|
16
|
+
import {
|
|
17
|
+
useBulkConversationAction,
|
|
18
|
+
useConversation,
|
|
19
|
+
useConversations,
|
|
20
|
+
useFolders,
|
|
21
|
+
useMailboxes,
|
|
22
|
+
usePrefetchConversation,
|
|
23
|
+
} from "./use-mail-data";
|
|
24
|
+
|
|
25
|
+
export type MailController = ReturnType<typeof useMailController>;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Mail state and actions shared by every mail frontend: the route-derived
|
|
29
|
+
* location, list and conversation queries, selection, composer, and
|
|
30
|
+
* conversation mutations. Frontends only decide how to present them.
|
|
31
|
+
*/
|
|
32
|
+
export function useMailController(
|
|
33
|
+
route: MailRoute,
|
|
34
|
+
options: { listWithConversation?: boolean } = {}
|
|
35
|
+
) {
|
|
36
|
+
const { mailboxId } = route;
|
|
37
|
+
const navigateRoute = useNavigate();
|
|
38
|
+
|
|
39
|
+
const [composerMailboxId, setComposerMailboxId] = useState<string | null>(
|
|
40
|
+
null
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const [forwardedMessage, setForwardedMessage] = useState<
|
|
44
|
+
MessageDetail | undefined
|
|
45
|
+
>();
|
|
46
|
+
|
|
47
|
+
const [search, setSearch] = useState("");
|
|
48
|
+
const [unreadOnly, setUnreadOnly] = useState(false);
|
|
49
|
+
const mailboxQuery = useMailboxes();
|
|
50
|
+
const mailboxes = mailboxQuery.data?.mailboxes ?? [];
|
|
51
|
+
|
|
52
|
+
// A /mail/:id URL loads its folders, list and conversation at once; "/"
|
|
53
|
+
// needs the mailbox list to know which mailbox it means.
|
|
54
|
+
const dataMailboxId = mailboxQuery.data
|
|
55
|
+
? mailboxForRoute(mailboxId, mailboxes)?.id
|
|
56
|
+
: mailboxId;
|
|
57
|
+
|
|
58
|
+
const foldersQuery = useFolders(dataMailboxId);
|
|
59
|
+
const folders = foldersQuery.data?.folders;
|
|
60
|
+
|
|
61
|
+
const location = resolveMailLocation(
|
|
62
|
+
route,
|
|
63
|
+
mailboxQuery.data?.mailboxes,
|
|
64
|
+
folders
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
const {
|
|
68
|
+
mailbox,
|
|
69
|
+
folder,
|
|
70
|
+
conversationId,
|
|
71
|
+
ready: locationReady,
|
|
72
|
+
canonicalPath,
|
|
73
|
+
} = location;
|
|
74
|
+
|
|
75
|
+
const activeMailboxId = mailbox?.id;
|
|
76
|
+
const composeOpen = composerMailboxId === activeMailboxId;
|
|
77
|
+
const currentPath = routePath(route);
|
|
78
|
+
|
|
79
|
+
// Moving to another mailbox (e.g. back in history) discards its draft
|
|
80
|
+
// window: adjusted while rendering, so the old composer never shows.
|
|
81
|
+
if (
|
|
82
|
+
composerMailboxId &&
|
|
83
|
+
activeMailboxId &&
|
|
84
|
+
composerMailboxId !== activeMailboxId
|
|
85
|
+
) {
|
|
86
|
+
setComposerMailboxId(null);
|
|
87
|
+
setForwardedMessage(undefined);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
if (locationReady && canonicalPath && currentPath !== canonicalPath) {
|
|
92
|
+
navigateRoute(canonicalPath, { replace: true });
|
|
93
|
+
}
|
|
94
|
+
}, [canonicalPath, currentPath, locationReady, navigateRoute]);
|
|
95
|
+
const debouncedSearch = useDebouncedValue(search.trim(), 250);
|
|
96
|
+
|
|
97
|
+
const conversationsQuery = useConversations(
|
|
98
|
+
location.mailboxId,
|
|
99
|
+
folder,
|
|
100
|
+
debouncedSearch,
|
|
101
|
+
unreadOnly,
|
|
102
|
+
// Layouts that keep the list beside an open conversation load it too.
|
|
103
|
+
!conversationId || Boolean(options.listWithConversation)
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
const conversationQuery = useConversation(location.mailboxId, conversationId);
|
|
107
|
+
const sharedConversationAction = useBulkConversationAction();
|
|
108
|
+
|
|
109
|
+
const conversations = useMemo(
|
|
110
|
+
() =>
|
|
111
|
+
conversationsQuery.data?.pages.flatMap((page) => page.conversations) ??
|
|
112
|
+
[],
|
|
113
|
+
[conversationsQuery.data?.pages]
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
// Until the mailbox and folder are confirmed, a failed request may only
|
|
117
|
+
// mean the URL is stale, which the canonical redirect fixes.
|
|
118
|
+
const listError =
|
|
119
|
+
mailboxQuery.error?.message ??
|
|
120
|
+
(mailbox ? foldersQuery.error?.message : undefined) ??
|
|
121
|
+
(locationReady ? conversationsQuery.error?.message : undefined);
|
|
122
|
+
|
|
123
|
+
const conversationPages = conversationQuery.data?.pages;
|
|
124
|
+
|
|
125
|
+
const conversationMessages = useMemo(
|
|
126
|
+
() =>
|
|
127
|
+
conversationPages
|
|
128
|
+
? [...conversationPages].reverse().flatMap((page) => page.messages)
|
|
129
|
+
: [],
|
|
130
|
+
[conversationPages]
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
const conversation = conversationPages?.[0];
|
|
134
|
+
const prefetchConversation = usePrefetchConversation();
|
|
135
|
+
|
|
136
|
+
// With a conversation open, the previous and next one load in the
|
|
137
|
+
// background, so stepping through the list does not wait.
|
|
138
|
+
const openIndex =
|
|
139
|
+
conversationId && conversation
|
|
140
|
+
? conversations.findIndex(
|
|
141
|
+
(item) => item.conversationId === conversationId
|
|
142
|
+
)
|
|
143
|
+
: -1;
|
|
144
|
+
|
|
145
|
+
const neighbourIds =
|
|
146
|
+
openIndex >= 0
|
|
147
|
+
? [conversations[openIndex + 1], conversations[openIndex - 1]].flatMap(
|
|
148
|
+
(item) => (item ? [item.conversationId] : [])
|
|
149
|
+
)
|
|
150
|
+
: [];
|
|
151
|
+
|
|
152
|
+
const neighbourKey = neighbourIds.join("\0");
|
|
153
|
+
useEffect(() => {
|
|
154
|
+
if (!neighbourKey) return;
|
|
155
|
+
|
|
156
|
+
const timer = setTimeout(() => {
|
|
157
|
+
for (const id of neighbourKey.split("\0")) {
|
|
158
|
+
prefetchConversation(location.mailboxId, id);
|
|
159
|
+
}
|
|
160
|
+
}, 150);
|
|
161
|
+
|
|
162
|
+
return () => clearTimeout(timer);
|
|
163
|
+
}, [location.mailboxId, neighbourKey, prefetchConversation]);
|
|
164
|
+
const folderName = folderDisplayName(folder, folders);
|
|
165
|
+
const activeFolder = folders?.find((item) => item.id === folder);
|
|
166
|
+
|
|
167
|
+
const selection = useConversationSelection({
|
|
168
|
+
scope: `${activeMailboxId ?? ""}\0${folder}\0${debouncedSearch}\0${unreadOnly}`,
|
|
169
|
+
mailboxId: activeMailboxId,
|
|
170
|
+
folderId: folder,
|
|
171
|
+
conversations,
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
const moreConversationsExist = Boolean(
|
|
175
|
+
conversationsQuery.data?.pages.at(-1)?.nextCursor
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
const displayedConversationCount =
|
|
179
|
+
debouncedSearch || unreadOnly
|
|
180
|
+
? `${conversations.length}${moreConversationsExist ? "+" : ""}`
|
|
181
|
+
: String(activeFolder?.totalCount ?? conversations.length);
|
|
182
|
+
|
|
183
|
+
/** `replace` swaps the current history entry, e.g. the one an open mailbox sheet added. */
|
|
184
|
+
function navigate(
|
|
185
|
+
next: {
|
|
186
|
+
folder?: Folder;
|
|
187
|
+
mailbox?: string;
|
|
188
|
+
conversation?: string | null;
|
|
189
|
+
},
|
|
190
|
+
replace = false
|
|
191
|
+
) {
|
|
192
|
+
const targetMailboxId = next.mailbox ?? mailbox?.id;
|
|
193
|
+
|
|
194
|
+
if (!targetMailboxId) return;
|
|
195
|
+
|
|
196
|
+
const targetConversation =
|
|
197
|
+
next.conversation === null ? null : (next.conversation ?? conversationId);
|
|
198
|
+
|
|
199
|
+
navigateRoute(
|
|
200
|
+
mailPath(targetMailboxId, next.folder ?? folder, targetConversation),
|
|
201
|
+
{ replace }
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function openCompose() {
|
|
206
|
+
if (!mailbox?.canSend) return;
|
|
207
|
+
setForwardedMessage(undefined);
|
|
208
|
+
setComposerMailboxId(mailbox.id);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function openForward(message: MessageDetail) {
|
|
212
|
+
if (!mailbox?.canSend) return;
|
|
213
|
+
setForwardedMessage(message);
|
|
214
|
+
setComposerMailboxId(mailbox.id);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function closeComposer() {
|
|
218
|
+
setComposerMailboxId(null);
|
|
219
|
+
setForwardedMessage(undefined);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Reads the live URL: a mutation can settle after the user already moved to
|
|
223
|
+
// another folder, and the captured `folder` would drag them back.
|
|
224
|
+
function closeConversation() {
|
|
225
|
+
const current = parseMailPath(window.location.pathname);
|
|
226
|
+
|
|
227
|
+
if (!current?.conversationId) return;
|
|
228
|
+
navigateRoute(mailPath(current.mailboxId, current.folder));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function mutateConversation(
|
|
232
|
+
input: {
|
|
233
|
+
mailboxState?: "active" | "archive" | "trash";
|
|
234
|
+
folderId?: string | null;
|
|
235
|
+
},
|
|
236
|
+
returnToList = false,
|
|
237
|
+
successMessage?: string
|
|
238
|
+
) {
|
|
239
|
+
if (!conversationId || !mailbox) return;
|
|
240
|
+
sharedConversationAction.mutate(
|
|
241
|
+
{
|
|
242
|
+
mailboxId: mailbox.id,
|
|
243
|
+
sourceFolderId: folder,
|
|
244
|
+
conversationIds: [conversationId],
|
|
245
|
+
action: { type: "update", update: input },
|
|
246
|
+
},
|
|
247
|
+
{
|
|
248
|
+
onSuccess: ({ updatedCount }) => {
|
|
249
|
+
if (!updatedCount) {
|
|
250
|
+
toast.warning(
|
|
251
|
+
"Conversation was not changed because its folder changed."
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (successMessage) toast.success(successMessage);
|
|
258
|
+
|
|
259
|
+
if (returnToList) closeConversation();
|
|
260
|
+
},
|
|
261
|
+
onError: (error) => toast.error(error.message),
|
|
262
|
+
}
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function deleteConversationPermanently() {
|
|
267
|
+
if (!conversationId || !mailbox) return;
|
|
268
|
+
sharedConversationAction.mutate(
|
|
269
|
+
{
|
|
270
|
+
mailboxId: mailbox.id,
|
|
271
|
+
sourceFolderId: folder,
|
|
272
|
+
conversationIds: [conversationId],
|
|
273
|
+
action: { type: "delete_permanently" },
|
|
274
|
+
},
|
|
275
|
+
{
|
|
276
|
+
onSuccess: ({ updatedCount }) => {
|
|
277
|
+
if (!updatedCount) {
|
|
278
|
+
toast.warning(
|
|
279
|
+
"Conversation was not deleted because its folder changed."
|
|
280
|
+
);
|
|
281
|
+
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
toast.success("Conversation deleted permanently");
|
|
286
|
+
closeConversation();
|
|
287
|
+
},
|
|
288
|
+
onError: (error) => toast.error(error.message),
|
|
289
|
+
}
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
async function changeConversationRead(isRead: boolean, returnToList = false) {
|
|
294
|
+
if (!conversationId || !mailbox) return;
|
|
295
|
+
|
|
296
|
+
try {
|
|
297
|
+
const { updatedCount } = await sharedConversationAction.mutateAsync({
|
|
298
|
+
mailboxId: mailbox.id,
|
|
299
|
+
sourceFolderId: folder,
|
|
300
|
+
conversationIds: [conversationId],
|
|
301
|
+
action: { type: "read", isRead },
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
if (!updatedCount) {
|
|
305
|
+
toast.warning("Conversation no longer contains incoming messages.");
|
|
306
|
+
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (returnToList) closeConversation();
|
|
311
|
+
} catch (error) {
|
|
312
|
+
toast.error(
|
|
313
|
+
error instanceof Error ? error.message : "Could not update read state"
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
return {
|
|
319
|
+
mailboxQuery,
|
|
320
|
+
foldersQuery,
|
|
321
|
+
conversationsQuery,
|
|
322
|
+
conversationQuery,
|
|
323
|
+
mailboxes,
|
|
324
|
+
mailbox,
|
|
325
|
+
folders,
|
|
326
|
+
folder,
|
|
327
|
+
folderName,
|
|
328
|
+
activeFolder,
|
|
329
|
+
conversationId,
|
|
330
|
+
conversations,
|
|
331
|
+
conversation,
|
|
332
|
+
conversationMessages,
|
|
333
|
+
displayedConversationCount,
|
|
334
|
+
selection,
|
|
335
|
+
search,
|
|
336
|
+
debouncedSearch,
|
|
337
|
+
unreadOnly,
|
|
338
|
+
composeOpen,
|
|
339
|
+
forwardedMessage,
|
|
340
|
+
sharedActionPending: sharedConversationAction.isPending,
|
|
341
|
+
listLoading: !listError && conversationsQuery.isPending,
|
|
342
|
+
listError,
|
|
343
|
+
retryList() {
|
|
344
|
+
void (mailboxQuery.isError
|
|
345
|
+
? mailboxQuery.refetch()
|
|
346
|
+
: foldersQuery.isError
|
|
347
|
+
? foldersQuery.refetch()
|
|
348
|
+
: conversationsQuery.refetch());
|
|
349
|
+
},
|
|
350
|
+
setSearch(value: string) {
|
|
351
|
+
setSearch(value);
|
|
352
|
+
selection.exit();
|
|
353
|
+
},
|
|
354
|
+
toggleUnreadOnly() {
|
|
355
|
+
selection.exit();
|
|
356
|
+
setUnreadOnly((current) => !current);
|
|
357
|
+
},
|
|
358
|
+
selectFolder(next: Folder) {
|
|
359
|
+
selection.exit();
|
|
360
|
+
navigate({ folder: next, conversation: null });
|
|
361
|
+
},
|
|
362
|
+
selectMailbox(id: string) {
|
|
363
|
+
closeComposer();
|
|
364
|
+
selection.exit();
|
|
365
|
+
navigate({ mailbox: id, folder: "inbox", conversation: null });
|
|
366
|
+
},
|
|
367
|
+
/**
|
|
368
|
+
* A folder of any mailbox, e.g. from an expanded mailbox in the sidebar;
|
|
369
|
+
* `replace` swaps the current history entry instead of adding one.
|
|
370
|
+
*/
|
|
371
|
+
openFolder(nextMailboxId: string, next: Folder, replace = false) {
|
|
372
|
+
if (nextMailboxId !== mailbox?.id) closeComposer();
|
|
373
|
+
selection.exit();
|
|
374
|
+
navigate(
|
|
375
|
+
{ mailbox: nextMailboxId, folder: next, conversation: null },
|
|
376
|
+
replace
|
|
377
|
+
);
|
|
378
|
+
},
|
|
379
|
+
/** Starts loading a conversation the person is likely to open next. */
|
|
380
|
+
prefetchConversation(id: string) {
|
|
381
|
+
prefetchConversation(location.mailboxId, id);
|
|
382
|
+
},
|
|
383
|
+
openConversation(id: string, folderOverride?: Folder) {
|
|
384
|
+
// An absent folder keeps the current one.
|
|
385
|
+
navigate({ conversation: id, folder: folderOverride });
|
|
386
|
+
},
|
|
387
|
+
closeConversation() {
|
|
388
|
+
navigate({ conversation: null });
|
|
389
|
+
},
|
|
390
|
+
openAdministration() {
|
|
391
|
+
navigateRoute("/admin");
|
|
392
|
+
},
|
|
393
|
+
openSettings() {
|
|
394
|
+
navigateRoute("/settings");
|
|
395
|
+
},
|
|
396
|
+
openCompose,
|
|
397
|
+
openForward,
|
|
398
|
+
closeComposer,
|
|
399
|
+
archive: () => mutateConversation({ mailboxState: "archive" }, true),
|
|
400
|
+
restore: () => mutateConversation({ mailboxState: "active" }, true),
|
|
401
|
+
trash: () => mutateConversation({ mailboxState: "trash" }, true),
|
|
402
|
+
/** Moves the open conversation to a custom folder, or back to Inbox with null. */
|
|
403
|
+
moveToFolder: (target: { id: string | null; name: string }) =>
|
|
404
|
+
mutateConversation(
|
|
405
|
+
{ mailboxState: "active", folderId: target.id },
|
|
406
|
+
true,
|
|
407
|
+
`Moved to ${target.name}`
|
|
408
|
+
),
|
|
409
|
+
/** Reporting spam fits active and archived mail people received. */
|
|
410
|
+
canReport:
|
|
411
|
+
(conversation?.mailboxState === "active" ||
|
|
412
|
+
conversation?.mailboxState === "archive") &&
|
|
413
|
+
activeFolder?.systemType !== "sent",
|
|
414
|
+
/** Only from Trash, and only for people who can send from the mailbox. */
|
|
415
|
+
deletePermanently:
|
|
416
|
+
conversation?.mailboxState === "trash" && mailbox?.canSend
|
|
417
|
+
? deleteConversationPermanently
|
|
418
|
+
: undefined,
|
|
419
|
+
markRead:
|
|
420
|
+
(conversation?.unreadCount ?? 0) > 0
|
|
421
|
+
? () => void changeConversationRead(true)
|
|
422
|
+
: undefined,
|
|
423
|
+
markUnread: conversation?.hasIncoming
|
|
424
|
+
? () => void changeConversationRead(false, true)
|
|
425
|
+
: undefined,
|
|
426
|
+
};
|
|
427
|
+
}
|