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-compose): let the trash discard a draft locked by a mailbox conflict (#9285)

teo3719647ba0a8
diff --git a/apps/web/src/features/email-compose/primitives/compose-persistence.test.ts b/apps/web/src/features/email-compose/primitives/compose-persistence.test.ts
index ba56fb40bd..cc20ee3b62 100644
--- a/apps/web/src/features/email-compose/primitives/compose-persistence.test.ts
+++ b/apps/web/src/features/email-compose/primitives/compose-persistence.test.ts
@@ -169,6 +169,40 @@ it('discards a standalone draft at its server generation', async () => {
);
});
+it.each([
+ { state: 'CONFLICT', issue: 'DRAFT_CONFLICT', discards: true },
+ { state: 'UNCERTAIN', issue: 'ATTACHMENT_UNKNOWN', discards: true },
+ { state: 'UNCERTAIN', issue: 'SEND_UNKNOWN', discards: false },
+ { state: 'SUBMITTING', issue: null, discards: false },
+] as const)(
+ 'discards a draft whose $state $issue operation locks editing: $discards',
+ async ({ state, issue, discards }) => {
+ const composeContext = providerContext('OUTLOOK');
+ const goBack = vi.fn();
+ const root = mountEmailComposer(
+ composeContext,
+ { goBack },
+ {
+ draft: message('locked', {
+ is_draft: true,
+ thread_db_id: 'thread',
+ operation: { state, revision: '1', issue },
+ }),
+ }
+ );
+ expect(root.state.context.disabled()).toBe(true);
+ expect(root.state.context.deleteDisabled?.()).toBe(!discards);
+ await root.state.deleteDraftAndReset();
+ // A host that cannot leave keeps an empty composer, now unlocked.
+ expect(root.state.context.disabled()).toBe(!discards);
+ root.dispose();
+ expect(goBack).toHaveBeenCalledTimes(discards ? 1 : 0);
+ expect(composeContext.drafts.deleteDraft).toHaveBeenCalledTimes(
+ discards ? 1 : 0
+ );
+ }
+);
+
it('discards a reply draft at its server generation', async () => {
const composeContext = createComposeContext();
const root = mountReplyComposer(composeContext, () => message('parent'), {
diff --git a/apps/web/src/features/email-compose/primitives/compose-view-state.ts b/apps/web/src/features/email-compose/primitives/compose-view-state.ts
index ef7b9a668c..56efa98035 100644
--- a/apps/web/src/features/email-compose/primitives/compose-view-state.ts
+++ b/apps/web/src/features/email-compose/primitives/compose-view-state.ts
@@ -62,6 +62,8 @@ export interface ComposeState {
// Status
disabled: Accessor<boolean>;
+ /** Discard can outlast editing; defaults to `disabled`. */
+ deleteDisabled?: Accessor<boolean>;
primaryActionDisabled: Accessor<boolean>;
isSending: Accessor<boolean>;
isSavingDraft?: Accessor<boolean>;
diff --git a/apps/web/src/features/email-compose/primitives/draft-action-hotkeys.ts b/apps/web/src/features/email-compose/primitives/draft-action-hotkeys.ts
index 4350f67749..e5a19e0aae 100644
--- a/apps/web/src/features/email-compose/primitives/draft-action-hotkeys.ts
+++ b/apps/web/src/features/email-compose/primitives/draft-action-hotkeys.ts
@@ -12,6 +12,8 @@ import type { Accessor } from 'solid-js';
export function registerDraftActionHotkeys(options: {
scopeId: string;
disabled: Accessor<boolean>;
+ /** Discard can outlast editing; defaults to `disabled`. */
+ discardDisabled?: Accessor<boolean>;
discard: () => void;
/** Omit when the composer takes no attachments. */
attach?: (files: File[]) => void;
@@ -21,7 +23,7 @@ export function registerDraftActionHotkeys(options: {
scopeId: options.scopeId,
description: 'Discard draft',
keyDownHandler: () => {
- if (options.disabled()) return false;
+ if ((options.discardDisabled ?? options.disabled)()) return false;
options.discard();
return true;
},
diff --git a/apps/web/src/features/email-compose/primitives/email-composer.ts b/apps/web/src/features/email-compose/primitives/email-composer.ts
index b3f9bcab15..5cec293946 100644
--- a/apps/web/src/features/email-compose/primitives/email-composer.ts
+++ b/apps/web/src/features/email-compose/primitives/email-composer.ts
@@ -25,6 +25,7 @@ import type {
EmailMessage,
} from '../../email-message/core/email-message';
import {
+ operationBlocksDiscard,
operationBlocksEditing,
operationBlocksInboxSwitch,
operationBlocksSending,
@@ -494,7 +495,10 @@ export function createEmailComposer(props: EmailComposerOptions) {
);
const currentOperation = () => {
const live = operationSource?.operation();
- return live === undefined ? props.draft?.operation : live;
+ // The opened draft's operation stops applying once a discard drops it.
+ return live === undefined && currentDraftId() === props.draft?.db_id
+ ? props.draft?.operation
+ : live;
};
const deliveryBlocked = () =>
operationSource?.needsReload?.() ||
@@ -505,10 +509,9 @@ export function createEmailComposer(props: EmailComposerOptions) {
const operationBlocked = () =>
operationSource?.needsReload?.() ||
operationBlocksEditing(currentOperation());
- const persistencePaused = () =>
+ const composerBusy = () =>
restoring() ||
sendLocked() ||
- operationBlocked() ||
submitting() ||
discarding() ||
movingInbox() ||
@@ -516,6 +519,12 @@ export function createEmailComposer(props: EmailComposerOptions) {
completed() ||
schedule?.pending() ||
schedule?.state().type === 'scheduled';
+ const persistencePaused = () => operationBlocked() || composerBusy();
+ // A mailbox conflict or unconfirmed write locks editing, not discarding.
+ const discardBlocked = () =>
+ composerBusy() ||
+ !!operationSource?.needsReload?.() ||
+ operationBlocksDiscard(currentOperation());
const autosave = createDraftAutosave({
...createDraftSaveNotice(props.notices.feedback),
@@ -1111,7 +1120,7 @@ export function createEmailComposer(props: EmailComposerOptions) {
);
return false;
}
- if (persistencePaused() || scheduling()) return false;
+ if (discardBlocked() || scheduling()) return false;
setDiscarding(true);
autosave.cancel();
// A standalone draft owns its thread, so nothing is left to show. Leave
@@ -1485,6 +1494,8 @@ export function createEmailComposer(props: EmailComposerOptions) {
terminalState() !== undefined ||
scheduling() ||
schedule.state().type === 'scheduled',
+ deleteDisabled: () =>
+ discardBlocked() || hasInboxError() || terminalState() !== undefined,
primaryActionDisabled: () =>
sendLocked() ||
deliveryBlocked() ||
diff --git a/apps/web/src/features/email-compose/views/compose-layout.tsx b/apps/web/src/features/email-compose/views/compose-layout.tsx
index 7b02e97d61..ba2a298bd1 100644
--- a/apps/web/src/features/email-compose/views/compose-layout.tsx
+++ b/apps/web/src/features/email-compose/views/compose-layout.tsx
@@ -176,6 +176,7 @@ export function ComposeLayout(props: {
registerDraftActionHotkeys({
scopeId: composeHotkeyScope,
disabled: ctx.disabled,
+ discardDisabled: ctx.deleteDisabled,
discard: () => ctx.onDelete?.(),
attach: ctx.hideAttachments ? undefined : ctx.onAddAttachments,
});
diff --git a/apps/web/src/features/email-compose/views/compose-toolbar.tsx b/apps/web/src/features/email-compose/views/compose-toolbar.tsx
index d5f9cabf32..24d7e0d226 100644
--- a/apps/web/src/features/email-compose/views/compose-toolbar.tsx
+++ b/apps/web/src/features/email-compose/views/compose-toolbar.tsx
@@ -54,7 +54,7 @@ export function EmailComposeToolbar(props: {
onClick={ctx.onDelete}
tooltip="Delete draft"
size="icon-md"
- disabled={ctx.disabled()}
+ disabled={ctx.deleteDisabled?.() ?? ctx.disabled()}
>
<Trash class="size-4 touch:size-6 shrink-0 pointer-events-none" />
</Button>
diff --git a/apps/web/src/features/email-message/core/message-operation.ts b/apps/web/src/features/email-message/core/message-operation.ts
index 32db0eb268..e330adcfdf 100644
--- a/apps/web/src/features/email-message/core/message-operation.ts
+++ b/apps/web/src/features/email-message/core/message-operation.ts
@@ -51,6 +51,21 @@ export function operationBlocksEditing(
);
}
+/**
+ * A draft write awaiting a decision can still be discarded: the server deletes
+ * a conflicted or unconfirmed draft. Delivery that started, or lost access,
+ * cannot.
+ */
+export function operationBlocksDiscard(
+ operation?: MessageOperation | null
+): boolean {
+ if (!operation || !operationBlocksEditing(operation)) return false;
+ if (operation.state === 'CONFLICT') return false;
+ return !(
+ operation.state === 'UNCERTAIN' && operation.issue !== 'SEND_UNKNOWN'
+ );
+}
+
/** Moving a draft is how rejected content recovers, but not mid-move. */
export function operationBlocksInboxSwitch(
operation?: MessageOperation | null
diff --git a/docs/agent-guide/email.md b/docs/agent-guide/email.md
index f85e867cd9..e6622a58be 100644
--- a/docs/agent-guide/email.md
+++ b/docs/agent-guide/email.md
@@ -105,7 +105,10 @@ delivery progress is silent, as on Gmail: a composer or sent message shows a
notice only for a conflict, uncertain or rejected delivery, revoked access,
unsavable content, or an inbox move. Use its recovery actions and check the
provider copy before choosing an explicit resend; a resend in flight shows as
-the send button's pending state. Switching the sender
+the send button's pending state. A new-email draft locked by a conflict or an
+unconfirmed draft or attachment write still discards from its trash and
+Cmd+Shift+D; delivery in progress, unknown or rejected delivery, and revoked
+access keep discard locked. Switching the sender
inbox copies the draft and attachments into the destination; wait for completion
before sending. If the copy is interrupted, use retry or keep the original.
A draft the provider could not save can still switch From, which moves it to an