Skip to content

Email for professionals and teams

Stay in the current folder after spam, trash and archive (#84)

Can K.e4c4dd5b9726
diff --git a/src/components/message-actions/message-actions.tsx b/src/components/message-actions/message-actions.tsx
index 88d06ff..dcbe813 100644
--- a/src/components/message-actions/message-actions.tsx
+++ b/src/components/message-actions/message-actions.tsx
@@ -6,6 +6,7 @@ import { Archive, Ban, BellOff, Clock, FileCode2, Forward, Mail, MailOpen, MoreV
import { useLanguage } from "@/components/language-provider";
import { useCompose } from "@/components/compose/compose-context";
import { MessageSourceDialog } from "@/components/messages/message-source-dialog";
+import { useMessageListVisibility } from "@/components/messages/message-list-visibility";
import { MessageSnoozeDialog } from "./message-snooze-dialog";
import { useIsMobile } from "@/components/sidebar-mobile-utils";
import { useHotkeys, useShortcuts } from "@/components/shortcuts";
@@ -50,6 +51,7 @@ export function MessageActions({
const router = useRouter();
const isMobile = useIsMobile();
const { openDraftComposer } = useCompose();
+ const { backHref: listHref } = useMessageListVisibility();
const { shortcutsEnabled } = useShortcuts();
const [pendingAction, setPendingAction] = useState<
BulkMessageAction | "unsubscribe" | ReplyMode | "forward" | "block" | null
@@ -65,7 +67,7 @@ export function MessageActions({
setError(null);
try {
await runSingleMessageAction(messageId, action);
- const redirect = getMessageActionRedirect(action, direction);
+ const redirect = getMessageActionRedirect(action, listHref);
if (redirect) router.push(redirect);
router.refresh();
} catch {
@@ -73,7 +75,7 @@ export function MessageActions({
} finally {
setPendingAction(null);
}
- }, [messageId, direction, router, t]);
+ }, [messageId, listHref, router, t]);
// In Trash and Spam the delete button removes the message for good instead of
// being a no-op, after the user confirms.
@@ -237,7 +239,7 @@ export function MessageActions({
try {
await blockMessageContact({ mailboxId, senderAddress });
await runSingleMessageAction(messageId, "trash");
- router.push("/trash");
+ router.push(listHref);
router.refresh();
} catch (blockError) {
setError(blockError instanceof Error ? blockError.message : t("message.error.block"));
diff --git a/src/components/message-actions/utils.ts b/src/components/message-actions/utils.ts
index 699f186..c318370 100644
--- a/src/components/message-actions/utils.ts
+++ b/src/components/message-actions/utils.ts
@@ -121,12 +121,9 @@ export function getMoveMessageActions(
export function getMessageActionRedirect(
action: BulkMessageAction,
- direction: "inbound" | "outbound",
+ listHref: string,
) {
- if (action === "trash") return "/trash";
- if (action === "spam") return "/spam";
- if (action === "archive") return "/archived";
- if (action === "inbox") return "/inbox";
+ if (action === "trash" || action === "spam" || action === "archive" || action === "inbox") return listHref;
return null;
}
diff --git a/tests/message-action-redirect.test.mjs b/tests/message-action-redirect.test.mjs
new file mode 100644
index 0000000..ef77b77
--- /dev/null
+++ b/tests/message-action-redirect.test.mjs
@@ -0,0 +1,37 @@
+import assert from "node:assert/strict";
+import { mkdtempSync, rmSync } from "node:fs";
+import { dirname, join } from "node:path";
+import test, { after } from "node:test";
+import { fileURLToPath, pathToFileURL } from "node:url";
+import { build } from "esbuild";
+
+const root = join(dirname(fileURLToPath(import.meta.url)), "..");
+const directory = mkdtempSync(join(root, "node_modules", "mailflare-message-actions-"));
+after(() => rmSync(directory, { recursive: true, force: true }));
+await build({
+ stdin: {
+ contents: `export { getMessageActionRedirect } from "./src/components/message-actions/utils.ts";`,
+ resolveDir: root,
+ sourcefile: "message-action-redirect-entry.ts",
+ },
+ outfile: join(directory, "entry.mjs"),
+ bundle: true,
+ platform: "node",
+ format: "esm",
+ target: "node24",
+ tsconfig: join(root, "tsconfig.json"),
+ packages: "external",
+ logLevel: "silent",
+});
+const { getMessageActionRedirect } = await import(pathToFileURL(join(directory, "entry.mjs")).href);
+
+test("moving a message sends the reader back to the list they opened it from", () => {
+ for (const action of ["trash", "spam", "archive", "inbox"]) {
+ assert.equal(getMessageActionRedirect(action, "/inbox"), "/inbox");
+ assert.equal(getMessageActionRedirect(action, "/spam"), "/spam");
+ }
+});
+
+test("actions that leave the message in place do not navigate", () => {
+ for (const action of ["read", "unread", "delete"]) assert.equal(getMessageActionRedirect(action, "/inbox"), null);
+});