chore: improve tag delete

This commit is contained in:
killerprojecte
2026-06-22 14:01:38 +08:00
parent 7720b6b404
commit 74f456ef7d
2 changed files with 25 additions and 2 deletions
+17 -2
View File
@@ -227,7 +227,18 @@ func (a *App) handleDeleteMailLabel(w http.ResponseWriter, r *http.Request) {
badRequest(w, fmt.Errorf("label id is required")) badRequest(w, fmt.Errorf("label id is required"))
return 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 { if err != nil {
respondError(w, http.StatusInternalServerError, "failed to delete label") respondError(w, http.StatusInternalServerError, "failed to delete label")
return return
@@ -236,7 +247,11 @@ func (a *App) handleDeleteMailLabel(w http.ResponseWriter, r *http.Request) {
respondError(w, http.StatusNotFound, "label not found") respondError(w, http.StatusNotFound, "label not found")
return 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 { if err != nil {
respondError(w, http.StatusInternalServerError, "failed to load labels") respondError(w, http.StatusInternalServerError, "failed to load labels")
return return
+8
View File
@@ -243,6 +243,14 @@ export function MailPage() {
qc.setQueryData<ListResponse<MailLabel>>(["labels", activeMailboxId], (current) => current ? { ...current, items: current.items.filter((l) => l.id !== id) } : current) qc.setQueryData<ListResponse<MailLabel>>(["labels", activeMailboxId], (current) => current ? { ...current, items: current.items.filter((l) => l.id !== id) } : current)
return { prevLabels } return { prevLabels }
}, },
onSuccess: (_data, id) => {
if (mailView === "label" && selectedLabelId === id) {
setMailView("folder")
setFolder("Inbox")
setSelectedLabelId("")
setSelectedId(null)
}
},
onError: (_error, _id, context) => { onError: (_error, _id, context) => {
if (context?.prevLabels) qc.setQueryData(["labels", activeMailboxId], context.prevLabels) if (context?.prevLabels) qc.setQueryData(["labels", activeMailboxId], context.prevLabels)
toast({ title: "删除标签失败" }) toast({ title: "删除标签失败" })