Files
ihasmail-inbuxa/web/src/lib/emptyFolder.tsx
T
jcoffey-dev 18e493bcd1 Delete all spam, and call folders what the server calls them
Junk Mail can now be emptied in one action, the way every other mail
client offers it: a banner across the top of the folder, and an item in
both the folder's right-click menu and the list's own menu.

The messages are destroyed rather than moved to Deleted Items. Routing
spam through the bin on its way out leaves you with the same problem in
a different folder, and "delete all spam" means gone everywhere else. So
the dialog says it before you commit, and there is no undo.

emptyMailbox already did the hard part -- walking a folder a page at a
time so it survives maxObjectsInSet, which a Deleted Items of 5192 once
did not. All that changed is which folders it will accept. The guard
stays in the store rather than living only in the menus, so a fourth
caller cannot empty the Inbox by asking nicely.

The three entry points share one helper, because three dialogs warning
about a permanent deletion in three slightly different ways is how one
of them ends up not warning at all. A folder with nothing in it offers
the item greyed out rather than hiding it, so it is where you expect it
to be next time.

Folder naming is fixed in the same commit because it changed the same
file, and because testing this is what surfaced it. Two problems, one
cause:

  - The mock called its folders "Trash" and "Sent". Stalwart's defaults
    follow the Exchange convention -- "Deleted Items", "Sent Items" --
    so anything built from a folder's name read differently against the
    mock than against a real server, and every screenshot in the README
    showed a folder list no user has.

  - Worse, and in shipping code: the undo toast took a hardcoded label
    in preference to the folder's actual name, so deleting a message
    announced "moved to Trash" on a server whose folder is called
    "Deleted Items", and reporting spam said "moved to Spam" where it is
    "Junk Mail". The one message whose job is saying where mail went was
    naming somewhere that does not exist. It now prefers the mailbox's
    own name and keeps the hardcoded word only as a fallback.

Verified against the mock: the banner appears only in Junk and only with
something to delete, the dialog counts and pluralises, the messages are
destroyed and Deleted Items stays empty afterwards, the banner
disappears once the folder is, the item greys out when empty, Archive is
offered neither, and Trash still says "Empty Deleted Items".
2026-08-26 12:00:42 -07:00

53 lines
2.2 KiB
TypeScript

/**
* Emptying a folder, and asking first.
*
* There are three ways in — the folder's right-click menu, the list's own
* menu, and the banner across the top of Junk Mail — and they must not drift
* apart in what they warn about. A folder can only be emptied when it is one
* whose whole purpose is holding things you did not want: Deleted Items, or
* Junk Mail.
*
* The wording differs between them for a reason. Emptying Deleted Items is
* what anyone expects it to do. Emptying Junk Mail is the surprising one: the
* messages do not travel to Deleted Items on the way out, so there is no
* second chance to change your mind, and the dialog says so rather than
* leaving it to be discovered.
*/
import { confirmDialog } from "@/ui/dialog";
import { useMail } from "@/store/mail";
import type { Id, MailboxRole } from "@/jmap/types";
export interface EmptyTarget {
id: Id;
name: string;
role: MailboxRole;
totalEmails: number;
}
/** Whether this folder is one that may be emptied at all. */
export function canEmpty(role: MailboxRole | undefined | null): boolean {
return role === "trash" || role === "junk";
}
const plural = (n: number) => `${n.toLocaleString()} message${n === 1 ? "" : "s"}`;
/** What the button or menu item is called, in the folder's own terms. */
export function emptyLabel(target: Pick<EmptyTarget, "name" | "role">): string {
return target.role === "junk" ? "Delete all spam" : `Empty ${target.name}`;
}
/** Ask, then empty. Resolves once the emptying has been attempted, or declined. */
export async function confirmAndEmpty(target: EmptyTarget): Promise<void> {
if (!canEmpty(target.role)) return;
const junk = target.role === "junk";
const ok = await confirmDialog({
title: junk ? `Delete all spam in “${target.name}”?` : `Empty “${target.name}”?`,
message: junk
? `All ${plural(target.totalEmails)} will be deleted permanently. They do not go to Deleted Items first, so this cannot be undone.`
: `All ${plural(target.totalEmails)} will be permanently deleted.`,
confirmLabel: junk ? "Delete all spam" : "Empty folder",
danger: true,
});
if (ok) await useMail.getState().emptyMailbox(target.id);
}