Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(email): prevent post-send and post-delete draft resurrection (#3099)

Evan Hutnik5584c7bd8e14
diff --git a/js/app/packages/block-email/component/BaseInput.tsx b/js/app/packages/block-email/component/BaseInput.tsx
index 79f9da4302..6372887521 100644
--- a/js/app/packages/block-email/component/BaseInput.tsx
+++ b/js/app/packages/block-email/component/BaseInput.tsx
@@ -544,6 +544,11 @@ export function BaseInput(props: {
const sendMutation = useSendMessageMutation({
onSuccess: async ({ message }) => {
+ // Cancel the post-reset save scheduled by sendEmail's resetState() and
+ // re-enable autosave for any future edits in this BaseInput instance
+ // (covers new-message flows where replyingTo never changes).
+ if (draftSaveTimer) window.clearTimeout(draftSaveTimer);
+ pendingSend = false;
const draftId = message.db_id;
const toastId = toast.success(
'Email sent',
@@ -574,6 +579,9 @@ export function BaseInput(props: {
}
},
onError: () => {
+ // Restore autosave so the user can keep editing after a failed send.
+ if (draftSaveTimer) window.clearTimeout(draftSaveTimer);
+ pendingSend = false;
toast.failure('Failed to send email');
},
});
@@ -629,6 +637,8 @@ export function BaseInput(props: {
const [userName] = useDisplayName(tryMacroId(userId() ?? ''));
let draftSaveTimer: number | undefined;
+ let pendingDeletion = false;
+ let pendingSend = false;
const DRAFT_DEBOUNCE_MS = 500;
function collectDraft() {
@@ -665,7 +675,7 @@ export function BaseInput(props: {
}
async function executeSaveDraft() {
- if (sendMutation.isPending) {
+ if (sendMutation.isPending || pendingDeletion || pendingSend) {
return;
}
const draftToSave = collectDraft();
@@ -761,6 +771,25 @@ export function BaseInput(props: {
}, DRAFT_DEBOUNCE_MS);
}
+ onCleanup(() => {
+ if (draftSaveTimer) window.clearTimeout(draftSaveTimer);
+ });
+
+ // After a send, the bottom input stays mounted and its replyingTo flips to
+ // the just-sent message once the thread refetches. Cancel the inhibited
+ // post-send save and re-enable saves so a fresh edit under the new form
+ // context can be persisted.
+ createEffect(
+ on(
+ () => props.replyingTo()?.db_id,
+ () => {
+ if (draftSaveTimer) window.clearTimeout(draftSaveTimer);
+ pendingSend = false;
+ },
+ { defer: true }
+ )
+ );
+
const handleChipDragStart = (
field: 'to' | 'cc' | 'bcc',
recipient: EmailRecipient,
@@ -970,6 +999,12 @@ export function BaseInput(props: {
},
});
+ // Block any save scheduled by reset side effects (form().reset() callDirty,
+ // clearEmailBody editor onChange firing on a microtask). Without this, the
+ // 500ms timer fires after the thread refetches, the form memo switches to
+ // the just-sent message's reply context, and we POST an empty draft
+ // replying to the message we just sent — flipping it back to is_draft=TRUE.
+ pendingSend = true;
resetState();
clearDraftState();
@@ -992,14 +1027,32 @@ export function BaseInput(props: {
};
const deleteDraftAndReset = async () => {
+ // Block any save scheduled by resetState's side effects (sync form.reset
+ // callDirty + async editor onChange listener). When clearDraftState() has
+ // a setShowReply, the BaseInput unmounts and the flag goes away with it;
+ // when it doesn't (e.g. the bottom-of-thread input), the component stays
+ // mounted and we must restore the flag so subsequent edits can autosave.
+ pendingDeletion = true;
+ if (draftSaveTimer) window.clearTimeout(draftSaveTimer);
const draftId = savedDraftId();
- if (draftId) {
- await deleteDraftMutation.mutateAsync({ draftId });
- refetchThreadMessages();
+ try {
+ if (draftId) {
+ await deleteDraftMutation.mutateAsync({ draftId });
+ refetchThreadMessages();
+ }
+ resetState();
+ form().setReplyAppended(false);
+ clearDraftState();
+ } finally {
+ // Yield past any sync/microtask save scheduling triggered by resetState,
+ // then cancel the resulting timer and re-enable autosave. Runs on both
+ // success and error paths so a failed delete doesn't leave the user
+ // unable to save further edits.
+ setTimeout(() => {
+ if (draftSaveTimer) window.clearTimeout(draftSaveTimer);
+ pendingDeletion = false;
+ }, 0);
}
- resetState();
- form().setReplyAppended(false);
- clearDraftState();
};
const handleUserMention = (mention: UserMentionRecord) => {