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): Draft re-appears after send (#1800)

Evan Hutnikdea6e2d17f9e
diff --git a/js/app/packages/block-email/component/BaseInput.tsx b/js/app/packages/block-email/component/BaseInput.tsx
index 3eb4051f3a..facd6f764d 100644
--- a/js/app/packages/block-email/component/BaseInput.tsx
+++ b/js/app/packages/block-email/component/BaseInput.tsx
@@ -310,7 +310,15 @@ type UndoReplySnapshot = {
bodyHtml: string;
attachments: DraftFormAttachment[];
};
+// Set on send, persists across navigation, only consumed by undoSend.
+let undoSendSnapshot: UndoReplySnapshot | null = null;
+// Only set by undoSend for inline reply remount, consumed on mount.
let undoReplySnapshot: UndoReplySnapshot | null = null;
+// Registered by the current BaseInput instance so stale undoSend closures
+// from a previous mount can restore state into the live component.
+let restoreUndoCallback:
+ | ((snapshot: UndoReplySnapshot, draftId: string) => void)
+ | null = null;
export function BaseInput(props: {
replyingTo: Accessor<ApiMessage | undefined>;
@@ -403,6 +411,22 @@ export function BaseInput(props: {
});
}
+ // Register a callback so stale undoSend closures from a previous mount can
+ // restore state into this (the live) component instance.
+ restoreUndoCallback = (snapshot, draftId) => {
+ setSavedDraftId(draftId);
+ const currentEditor = editor();
+ if (currentEditor && snapshot.bodyHtml) {
+ setEditorStateFromHtml(currentEditor, snapshot.bodyHtml);
+ }
+ for (const attachment of snapshot.attachments) {
+ form().attachments.add(attachment);
+ }
+ };
+ onCleanup(() => {
+ restoreUndoCallback = null;
+ });
+
let pendingMentions: { documentId: string }[] = [];
const [shouldMarkDoneOnSuccess, setShouldMarkDoneOnSuccess] =
createSignal(false);
@@ -437,29 +461,19 @@ export function BaseInput(props: {
});
}
- if (props.setShowReply) {
- // Inline reply: component was unmounted by clearDraftState.
- // Re-show the reply box — the new BaseInput instance will
- // pick up undoReplySnapshot on mount and restore from it.
- props.setShowReply(true);
- } else {
- // Bottom reply: component is still mounted. Restore compose box
- // after reactive updates from setQueryData have settled
- // (form may have re-keyed).
- const snapshot = undoReplySnapshot;
- if (snapshot) {
- setTimeout(() => {
- setSavedDraftId(draftId);
- const currentEditor = editor();
- if (currentEditor && snapshot.bodyHtml) {
- setEditorStateFromHtml(currentEditor, snapshot.bodyHtml);
- }
- for (const attachment of snapshot.attachments) {
- form().attachments.add(attachment);
- }
- undoReplySnapshot = null;
- }, 0);
- }
+ const snapshot = undoSendSnapshot;
+ undoSendSnapshot = null;
+
+ if (snapshot && restoreUndoCallback) {
+ // A live BaseInput is mounted — restore after reactive updates from
+ // setQueryData have settled (form may have re-keyed).
+ const cb = restoreUndoCallback;
+ setTimeout(() => cb(snapshot, draftId), 0);
+ } else if (snapshot) {
+ // No live component (e.g. inline reply was unmounted).
+ // Stash for mount-time restore.
+ undoReplySnapshot = snapshot;
+ props.setShowReply?.(true);
}
toast.success('Send cancelled');
@@ -797,7 +811,13 @@ export function BaseInput(props: {
const currentEditor = editor();
- // Snapshot editor state before watermark so undo-send can restore it
+ // Ensure draft is saved before sending so undo-send always has a draft to restore
+ if (draftSaveTimer) window.clearTimeout(draftSaveTimer);
+ await executeSaveDraft();
+
+ // Snapshot editor state before watermark so undo-send can restore it.
+ // Stored in undoSendSnapshot (not undoReplySnapshot) so it persists across
+ // navigation but isn't mistakenly auto-restored on next mount.
if (currentEditor) {
const snapshotHtml = currentEditor.read(() =>
$generateHtmlFromNodes(currentEditor)
@@ -805,7 +825,7 @@ export function BaseInput(props: {
const snapshotDraftId = savedDraftId();
const snapshotThreadId = ctx.thread()?.db_id;
if (snapshotDraftId && snapshotThreadId) {
- undoReplySnapshot = {
+ undoSendSnapshot = {
threadId: snapshotThreadId,
draftId: snapshotDraftId,
bodyHtml: snapshotHtml,
@@ -843,15 +863,11 @@ export function BaseInput(props: {
const processedMacroBody = prepareMacroBody(bodyMacro());
const currentDraftID = savedDraftId();
- if (draftSaveTimer) window.clearTimeout(draftSaveTimer);
const sendTime = form().sendTime();
if (sendTime) {
- // Just in case, always get a fresh save of the draft so we don't miss any information
- const draftID = await executeSaveDraft();
-
- if (!draftID) {
+ if (!currentDraftID) {
console.error('No draft');
toast.failure('Failed to schedule message', 'Draft required');
cleanupWatermark();
@@ -859,7 +875,7 @@ export function BaseInput(props: {
}
scheduleMessageMutation.mutate({
- draftID,
+ draftID: currentDraftID,
sendTime,
});