From 74f456ef7de03c9aaa3ec245ac435e3faaa30dab Mon Sep 17 00:00:00 2001 From: killerprojecte Date: Mon, 22 Jun 2026 14:01:38 +0800 Subject: [PATCH] chore: improve tag delete --- apps/api/internal/app/mail_handlers.go | 19 +++++++++++++++++-- apps/web/src/pages/mail.tsx | 8 ++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/apps/api/internal/app/mail_handlers.go b/apps/api/internal/app/mail_handlers.go index cca892f..d3ff4a1 100644 --- a/apps/api/internal/app/mail_handlers.go +++ b/apps/api/internal/app/mail_handlers.go @@ -227,7 +227,18 @@ func (a *App) handleDeleteMailLabel(w http.ResponseWriter, r *http.Request) { badRequest(w, fmt.Errorf("label id is required")) return } - result, err := a.db.ExecContext(r.Context(), `DELETE FROM mail_labels WHERE id=? AND mailbox_id=?`, labelID, mb.ID) + ctx := r.Context() + tx, err := a.db.BeginTx(ctx, nil) + if err != nil { + respondError(w, http.StatusInternalServerError, "failed to begin transaction") + return + } + defer tx.Rollback() + if _, err := tx.ExecContext(ctx, `DELETE FROM message_labels WHERE label_id=?`, labelID); err != nil { + respondError(w, http.StatusInternalServerError, "failed to remove label associations") + return + } + result, err := tx.ExecContext(ctx, `DELETE FROM mail_labels WHERE id=? AND mailbox_id=?`, labelID, mb.ID) if err != nil { respondError(w, http.StatusInternalServerError, "failed to delete label") return @@ -236,7 +247,11 @@ func (a *App) handleDeleteMailLabel(w http.ResponseWriter, r *http.Request) { respondError(w, http.StatusNotFound, "label not found") return } - labels, err := a.labelsForMailbox(r.Context(), mb.ID) + if err := tx.Commit(); err != nil { + respondError(w, http.StatusInternalServerError, "failed to commit transaction") + return + } + labels, err := a.labelsForMailbox(ctx, mb.ID) if err != nil { respondError(w, http.StatusInternalServerError, "failed to load labels") return diff --git a/apps/web/src/pages/mail.tsx b/apps/web/src/pages/mail.tsx index 111a3f1..31a58fc 100644 --- a/apps/web/src/pages/mail.tsx +++ b/apps/web/src/pages/mail.tsx @@ -243,6 +243,14 @@ export function MailPage() { qc.setQueryData>(["labels", activeMailboxId], (current) => current ? { ...current, items: current.items.filter((l) => l.id !== id) } : current) return { prevLabels } }, + onSuccess: (_data, id) => { + if (mailView === "label" && selectedLabelId === id) { + setMailView("folder") + setFolder("Inbox") + setSelectedLabelId("") + setSelectedId(null) + } + }, onError: (_error, _id, context) => { if (context?.prevLabels) qc.setQueryData(["labels", activeMailboxId], context.prevLabels) toast({ title: "删除标签失败" })