From 1e8bd40b93b35ede03863f68bcd01ffa88df4b17 Mon Sep 17 00:00:00 2001 From: Lucas Gaitzsch Date: Wed, 20 May 2026 19:58:41 +0200 Subject: [PATCH] fix draft duplicating --- stores/email-store.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/stores/email-store.ts b/stores/email-store.ts index cf4ddf3c..dacabdb3 100644 --- a/stores/email-store.ts +++ b/stores/email-store.ts @@ -1753,6 +1753,7 @@ export const useEmailStore = create((set, get) => ({ } const currentEmails = get().emails; + const previousTotal = get().totalEmails; // Only notify for genuinely new incoming mail in the Inbox. // Without these guards the toast/sound also fires when sending, @@ -1773,12 +1774,16 @@ export const useEmailStore = create((set, get) => ({ const refreshedEmails = annotateScheduledEmails(result.emails, get().scheduledSubmissionByEmailId); // Build the merged list: start with the fresh first page, then append - // existing emails beyond that page (if any), skipping duplicates and - // emails removed from the first page (e.g. deleted or moved). + // existing emails beyond that page (if any), skipping duplicates. Do not + // append the whole previous list: drafts are saved as destroy+create, so + // the old draft can disappear from the refreshed first page and must not + // be reintroduced from stale local state. const merged: Email[] = [...refreshedEmails]; const mergedIds = new Set(refreshedEmails.map((e: Email) => e.id)); + const insertedCount = Math.max((result.total || 0) - previousTotal, 0); + const appendFromIndex = Math.max(refreshedEmails.length - insertedCount, 0); - for (const email of currentEmails) { + for (const email of currentEmails.slice(appendFromIndex)) { if (!mergedIds.has(email.id)) { merged.push(email); mergedIds.add(email.id);