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): Improve scheduled send behaviour (#2018)

Evan Hutnik82b73aafe1a3
diff --git a/js/app/packages/block-email/component/BaseInput.tsx b/js/app/packages/block-email/component/BaseInput.tsx
index 49adcce7fe..ba11e7d610 100644
--- a/js/app/packages/block-email/component/BaseInput.tsx
+++ b/js/app/packages/block-email/component/BaseInput.tsx
@@ -46,7 +46,6 @@ import { useEmailLinksQuery } from '@queries/email/link';
import { invalidateSoupEntity } from '@queries/soup/cache';
import { emailClient } from '@service-email/client';
import {
- useScheduleMessageMutation,
useSendMessageMutation,
useUnscheduleMessageMutation,
} from '@queries/email/thread';
@@ -688,12 +687,6 @@ export function BaseInput(props: {
return;
}
- const existingDraft = savedDraftId() !== undefined;
-
- // If there's an existing draft, we should send the sendTime so that the send time
- // stays up to date and is not removed
- const sendTime = existingDraft ? form().sendTime() : undefined;
-
const draftResponse = await saveDraftMutation.mutateAsync({
draft: {
...draftToSave,
@@ -701,7 +694,6 @@ export function BaseInput(props: {
provider_thread_id: currentThread?.provider_id,
thread_db_id: currentThread?.db_id,
},
- sendTime,
});
const draftId = draftResponse.draft.db_id;
@@ -843,15 +835,6 @@ export function BaseInput(props: {
useHotkeyDOMScope('compose-message');
let composeContainerRef: HTMLDivElement | undefined;
- const scheduleMessageMutation = useScheduleMessageMutation({
- onSuccess: () => {
- toast.success('Email scheduled');
- },
- onError: () => {
- toast.failure('Failed to schedule email');
- },
- });
-
const sendEmail = async (markDone = false) => {
if (sendMutation.isPending || uploadAttachmentMutation.isPending) return;
@@ -956,22 +939,8 @@ export function BaseInput(props: {
const currentDraftID = savedDraftId();
- const sendTime = form().sendTime();
-
- if (sendTime) {
- if (!currentDraftID) {
- console.error('No draft');
- toast.failure('Failed to schedule message', 'Draft required');
- cleanupWatermark();
- return;
- }
-
- scheduleMessageMutation.mutate({
- draftID: currentDraftID,
- sendTime,
- threadID: currentThread?.db_id,
- });
-
+ // Failsafe: don't send if a scheduled send time is set
+ if (form().sendTime()) {
cleanupWatermark();
return;
}
@@ -1072,6 +1041,7 @@ export function BaseInput(props: {
scopeId: composeHotkeyScope,
description: 'Send email',
keyDownHandler: () => {
+ if (form().sendTime()) return false;
sendEmail();
return true;
},
@@ -1085,6 +1055,7 @@ export function BaseInput(props: {
scopeId: composeHotkeyScope,
description: 'Send and mark done',
keyDownHandler: () => {
+ if (form().sendTime()) return false;
sendEmail(true);
return true;
},
@@ -1212,7 +1183,7 @@ export function BaseInput(props: {
},
});
- const handleSendTimeChange = (date: Date | null) => {
+ const handleSendTimeChange = async (date: Date | null) => {
const currentSendTime = form().sendTime();
const currentDraft = savedDraftId();
@@ -1221,10 +1192,31 @@ export function BaseInput(props: {
unscheduleMessageMutation.mutate({
draftID: currentDraft,
});
+ form().setSendTime(date);
+ return;
}
form().setSendTime(date);
- scheduleDraftSave();
+
+ if (date) {
+ // Ensure draft is saved before scheduling
+ const draftID = currentDraft ?? (await executeSaveDraft());
+ if (!draftID) {
+ toast.failure('Failed to schedule message', 'Draft required');
+ return;
+ }
+
+ await emailClient.scheduleMessage({
+ draftID,
+ send_time: date.toISOString(),
+ });
+
+ // Mark the thread as done
+ const threadID = ctx.thread()?.db_id;
+ if (threadID) {
+ await emailClient.flagArchived({ id: threadID, value: true });
+ }
+ }
};
const isDraftSaving = () => saveDraftMutation.isPending;
@@ -1650,22 +1642,30 @@ export function BaseInput(props: {
</Show>
</div>
- <button
- disabled={
- uploadAttachmentMutation.isPending || sendMutation.isPending
- }
- onClick={() => sendEmail()}
- class="text-ink-muted hover:scale-115 transition ease-in-out flex-col items-center rounded-full p-[0.25lh] hover:bg-transparent disabled:opacity-30"
+ <Tooltip
+ tooltip={form().sendTime() ? 'Send time is scheduled' : undefined}
>
- <Show
- when={!sendMutation.isPending}
- fallback={<Spinner class="size-6 animate-spin cursor-disabled" />}
+ <button
+ disabled={
+ uploadAttachmentMutation.isPending ||
+ sendMutation.isPending ||
+ !!form().sendTime()
+ }
+ onClick={() => sendEmail()}
+ class="text-ink-muted hover:scale-115 transition ease-in-out flex-col items-center rounded-full p-[0.25lh] hover:bg-transparent disabled:opacity-30"
>
- <div class="group hover:bg-accent transition ease-in-out size-6 border border-accent rounded-full flex items-center justify-center p-0">
- <ArrowUp class="group-hover:!text-input group-hover:!fill-input !text-accent-ink !fill-accent size-4 transition ease-in-out" />
- </div>
- </Show>
- </button>
+ <Show
+ when={!sendMutation.isPending}
+ fallback={
+ <Spinner class="size-6 animate-spin cursor-disabled" />
+ }
+ >
+ <div class="group hover:bg-accent transition ease-in-out size-6 border border-accent rounded-full flex items-center justify-center p-0">
+ <ArrowUp class="group-hover:!text-input group-hover:!fill-input !text-accent-ink !fill-accent size-4 transition ease-in-out" />
+ </div>
+ </Show>
+ </button>
+ </Tooltip>
</div>
</div>
</div>
diff --git a/js/app/packages/block-email/component/Compose.tsx b/js/app/packages/block-email/component/Compose.tsx
index 6dbd6ef8f9..b11071dd60 100644
--- a/js/app/packages/block-email/component/Compose.tsx
+++ b/js/app/packages/block-email/component/Compose.tsx
@@ -23,7 +23,6 @@ import {
import Caution from '@icon/regular/warning.svg';
import { useEmailLinksQuery } from '@queries/email/link';
import {
- useScheduleMessageMutation,
useSendMessageMutation,
useUnscheduleMessageMutation,
} from '@queries/email/thread';
@@ -81,7 +80,6 @@ import { unwrap } from 'solid-js/store';
import { emailClient } from '@service-email/client';
import { queryClient } from '@queries/client';
import { emailKeys } from '@queries/email/keys';
-import { LIST_VIEW_ID } from '@app/constants/list-views';
const DRAFT_DEBOUNCE_MS = 1000;
@@ -304,18 +302,11 @@ export function EmailCompose(props: EmailComposeProps) {
return;
}
- const existingDraft = currentDraftID() !== undefined;
-
- // If there's an existing draft, we should send the sendTime so that the send time
- // stays up to date and is not removed
- const sendTime = existingDraft ? form.sendTime() : undefined;
-
const draftResponse = await saveDraftMutation.mutateAsync({
draft: {
...draftToSave,
db_id: currentDraftID(),
},
- sendTime,
});
const draftId = draftResponse.draft.db_id;
@@ -592,20 +583,6 @@ export function EmailCompose(props: EmailComposeProps) {
},
});
- const scheduleMessageMutation = useScheduleMessageMutation({
- onSuccess: () => {
- toast.success('Email scheduled');
-
- replaceSplit({
- content: { type: 'component', id: LIST_VIEW_ID.mail },
- mergeHistory: true,
- });
- },
- onError: () => {
- toast.failure('Failed to schedule email');
- },
- });
-
const onSubmit = async () => {
setValidationError(null);
@@ -682,26 +659,8 @@ export function EmailCompose(props: EmailComposeProps) {
return;
}
- 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) {
- console.error('No draft');
- toast.failure('Failed to schedule message', 'Draft required');
- cleanupWatermark();
- return;
- }
-
- scheduleMessageMutation.mutate({
- draftID,
- sendTime,
- threadID: saveDraftMutation.data?.draft.thread_db_id ?? undefined,
- });
-
- cleanupWatermark();
+ // Failsafe: don't send if a scheduled send time is set
+ if (form.sendTime()) {
return;
}
@@ -737,7 +696,7 @@ export function EmailCompose(props: EmailComposeProps) {
},
});
- const handleSendTimeChange = (date: Date | null) => {
+ const handleSendTimeChange = async (date: Date | null) => {
const currentSendTime = form.sendTime();
const currentDraft = currentDraftID();
@@ -746,10 +705,31 @@ export function EmailCompose(props: EmailComposeProps) {
unscheduleMessageMutation.mutate({
draftID: currentDraft,
});
+ form.setSendTime(date);
+ return;
}
form.setSendTime(date);
- scheduleDraftSave();
+
+ if (date) {
+ // Ensure draft is saved before scheduling
+ const draftID = currentDraft ?? (await executeSaveDraft());
+ if (!draftID) {
+ toast.failure('Failed to schedule message', 'Draft required');
+ return;
+ }
+
+ await emailClient.scheduleMessage({
+ draftID,
+ send_time: date.toISOString(),
+ });
+
+ // Mark the thread as done
+ const threadID = saveDraftMutation.data?.draft.thread_db_id;
+ if (threadID) {
+ await emailClient.flagArchived({ id: threadID, value: true });
+ }
+ }
};
const resetState = () => {
diff --git a/js/app/packages/block-email/component/ComposeEmailInput.tsx b/js/app/packages/block-email/component/ComposeEmailInput.tsx
index fbe9210dd3..8478ffc508 100644
--- a/js/app/packages/block-email/component/ComposeEmailInput.tsx
+++ b/js/app/packages/block-email/component/ComposeEmailInput.tsx
@@ -42,6 +42,7 @@ import { makeAttachmentPublic } from '../util/makeAttachmentPublic';
import { Button } from '@ui/components/Button';
import { fileSelector } from '@core/directive/fileSelector';
import { toast } from '@core/component/Toast/Toast';
+import { Tooltip } from '@core/component/Tooltip';
import { plural } from '@core/util/string';
import type { DraftFormAttachment } from '@block-email/component/createEmailFormState';
import { EmailAttachmentPill } from '@block-email/component/AttachmentPill';
@@ -155,6 +156,7 @@ export function ComposeEmailInput(props: ComposeEmailInputProps) {
scopeId: composeHotkeyScope,
description: 'Send email',
keyDownHandler: () => {
+ if (props.sendTime) return false;
handleSend();
return true;
},
@@ -340,14 +342,22 @@ export function ComposeEmailInput(props: ComposeEmailInputProps) {
compact
/>
</Show>
- <Button
- disabled={props.isSubmitting || props.disabled}
- onClick={() => {
- handleSend();
- }}
+ <Tooltip
+ tooltip={
+ props.sendTime ? 'Send time is scheduled' : undefined
+ }
>
- <PaperPlane class="size-4.5 text-accent" />
- </Button>
+ <Button
+ disabled={
+ props.isSubmitting || props.disabled || !!props.sendTime
+ }
+ onClick={() => {
+ handleSend();
+ }}
+ >
+ <PaperPlane class="size-4.5 text-accent" />
+ </Button>
+ </Tooltip>
<DropdownMenu placement="bottom-end">
<DropdownMenu.Trigger as={Button} class="aspect-square p-1">
<DotsThreeIcon class="h-4.5" />
@@ -414,22 +424,30 @@ export function ComposeEmailInput(props: ComposeEmailInputProps) {
</Show>
</div>
- <button
- disabled={props.isSubmitting || props.disabled}
- onClick={() => {
- handleSend();
- }}
- class="text-ink-muted hover:scale-115 transition ease-in-out flex-col items-center rounded-full p-[0.25lh] hover:bg-transparent disabled:opacity-30"
+ <Tooltip
+ tooltip={props.sendTime ? 'Send time is scheduled' : undefined}
>
- <Show
- when={!props.isSubmitting}
- fallback={<Spinner class="size-6 animate-spin cursor-disabled" />}
+ <button
+ disabled={
+ props.isSubmitting || props.disabled || !!props.sendTime
+ }
+ onClick={() => {
+ handleSend();
+ }}
+ class="text-ink-muted hover:scale-115 transition ease-in-out flex-col items-center rounded-full p-[0.25lh] hover:bg-transparent disabled:opacity-30"
>
- <div class="group hover:bg-accent transition ease-in-out size-6 border border-accent rounded-full flex items-center justify-center p-0">
- <ArrowUp class="group-hover:!text-input group-hover:!fill-input !text-accent-ink !fill-accent size-4 transition ease-in-out" />
- </div>
- </Show>
- </button>
+ <Show
+ when={!props.isSubmitting}
+ fallback={
+ <Spinner class="size-6 animate-spin cursor-disabled" />
+ }
+ >
+ <div class="group hover:bg-accent transition ease-in-out size-6 border border-accent rounded-full flex items-center justify-center p-0">
+ <ArrowUp class="group-hover:!text-input group-hover:!fill-input !text-accent-ink !fill-accent size-4 transition ease-in-out" />
+ </div>
+ </Show>
+ </button>
+ </Tooltip>
</Show>
</div>
</div>
diff --git a/js/app/packages/block-email/component/createEmailFormState.ts b/js/app/packages/block-email/component/createEmailFormState.ts
index b11582624a..f8ae29f788 100644
--- a/js/app/packages/block-email/component/createEmailFormState.ts
+++ b/js/app/packages/block-email/component/createEmailFormState.ts
@@ -264,7 +264,6 @@ export function createEmailFormState(
const setSendTime = (date: Date | null) => {
setState('sendTime', date ?? undefined);
- callDirty();
};
const callDirty = () => {
diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/draft.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/draft.rs
index edda8a789e..efda8789bb 100644
--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/draft.rs
+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/draft.rs
@@ -37,7 +37,12 @@ pub(crate) async fn insert_message(
)
.await?;
- message::process_scheduled_message(&mut tx, link_id, message_db_id, input.send_time).await?;
+ // Only touch scheduling if send_time is explicitly provided.
+ // Scheduling is managed via the dedicated /drafts/scheduled endpoints.
+ if input.send_time.is_some() {
+ message::process_scheduled_message(&mut tx, link_id, message_db_id, input.send_time)
+ .await?;
+ }
message::upsert_recipients(&mut tx, message_db_id, contacts).await?;