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(new-channels): prevent empty message send (#2103)

teo56df7b982cfa
diff --git a/js/app/packages/channel/Input/SendAction.tsx b/js/app/packages/channel/Input/SendAction.tsx
index ba4ce2344a..622ede4656 100644
--- a/js/app/packages/channel/Input/SendAction.tsx
+++ b/js/app/packages/channel/Input/SendAction.tsx
@@ -4,6 +4,7 @@ import { useInput, useInputCommands } from './context';
import ArrowUpIcon from '@icon/bold/arrow-up-bold.svg';
import SpinnerIcon from '@icon/bold/spinner-gap-bold.svg';
import { LabelAndHotKey, Tooltip } from '@core/component/Tooltip';
+import { hasSendableInputContent } from './utils/sendable-content';
export function SendAction(props: JSX.ButtonHTMLAttributes<HTMLButtonElement>) {
const input = useInput();
@@ -11,6 +12,7 @@ export function SendAction(props: JSX.ButtonHTMLAttributes<HTMLButtonElement>) {
const [local, rest] = splitProps(props, ['class', 'children']);
const resolved = children(() => local.children);
const isBlockedByPending = () => !!input().hasPendingAttachments;
+ const isBlockedByEmptyInput = () => !hasSendableInputContent(input());
return (
<Tooltip tooltip={<LabelAndHotKey label="Send message" />}>
@@ -18,7 +20,7 @@ export function SendAction(props: JSX.ButtonHTMLAttributes<HTMLButtonElement>) {
aria-label="Send message"
title="Send message"
data-input-action="send"
- disabled={isBlockedByPending()}
+ disabled={isBlockedByPending() || isBlockedByEmptyInput()}
class={cn('bg-red! group transition ease-in-out', local.class)}
onPointerDown={(event) => {
event.preventDefault();
diff --git a/js/app/packages/channel/Input/create-input-commands.ts b/js/app/packages/channel/Input/create-input-commands.ts
index 1dae68eba4..3b5045f4bd 100644
--- a/js/app/packages/channel/Input/create-input-commands.ts
+++ b/js/app/packages/channel/Input/create-input-commands.ts
@@ -6,6 +6,7 @@ import type {
InputData,
InputSnapshot,
} from './types';
+import { hasSendableInputContent } from './utils/sendable-content';
type CreateInputCommandsDeps = {
view: Accessor<InputData>;
@@ -34,6 +35,7 @@ export function createInputCommands(
if (!deps.callbacks?.onSend) return false;
const current = deps.snapshot();
+ if (!hasSendableInputContent(current)) return false;
deps.setIsSending(true);
try {
await deps.callbacks.onSend(current);
diff --git a/js/app/packages/channel/Input/tests/create-input-commands.test.ts b/js/app/packages/channel/Input/tests/create-input-commands.test.ts
index a2f55306f1..ba6b0a6370 100644
--- a/js/app/packages/channel/Input/tests/create-input-commands.test.ts
+++ b/js/app/packages/channel/Input/tests/create-input-commands.test.ts
@@ -3,6 +3,70 @@ import { createInputCommands } from '../create-input-commands';
import type { InputSnapshot } from '../types';
describe('createInputCommands', () => {
+ it('does not send an empty message', async () => {
+ const onSend = vi.fn();
+
+ const commands = createInputCommands({
+ view: () => ({
+ mode: 'channel',
+ hasPendingAttachments: false,
+ }),
+ snapshot: () => ({
+ value: ' ',
+ mentions: [],
+ attachments: [],
+ }),
+ setIsSending: vi.fn(),
+ setShowFormatRibbon: vi.fn(),
+ reset: vi.fn(),
+ removeTrackedAttachment: vi.fn(),
+ callbacks: {
+ onSend,
+ },
+ });
+
+ await expect(commands.send()).resolves.toBe(false);
+ expect(onSend).not.toHaveBeenCalled();
+ });
+
+ it('allows attachment-only messages', async () => {
+ const onSend = vi.fn();
+ const reset = vi.fn();
+ const clearComposer = vi.fn();
+ const snapshot: InputSnapshot = {
+ value: ' ',
+ mentions: [],
+ attachments: [
+ {
+ id: 'attachment-1',
+ name: 'image.png',
+ kind: 'image',
+ },
+ ],
+ };
+
+ const commands = createInputCommands({
+ view: () => ({
+ mode: 'channel',
+ hasPendingAttachments: false,
+ }),
+ snapshot: () => snapshot,
+ setIsSending: vi.fn(),
+ setShowFormatRibbon: vi.fn(),
+ reset,
+ clearComposer,
+ removeTrackedAttachment: vi.fn(),
+ callbacks: {
+ onSend,
+ },
+ });
+
+ await expect(commands.send()).resolves.toBe(true);
+ expect(onSend).toHaveBeenCalledWith(snapshot);
+ expect(reset).toHaveBeenCalledOnce();
+ expect(clearComposer).toHaveBeenCalledOnce();
+ });
+
it('resets before invoking onClose', () => {
const events: string[] = [];
const snapshot: InputSnapshot = {
diff --git a/js/app/packages/channel/Input/tests/input-slots.test.tsx b/js/app/packages/channel/Input/tests/input-slots.test.tsx
index c82784596e..d1b187f36f 100644
--- a/js/app/packages/channel/Input/tests/input-slots.test.tsx
+++ b/js/app/packages/channel/Input/tests/input-slots.test.tsx
@@ -162,7 +162,7 @@ describe('Input slots', () => {
(() => {
return (
<ChannelInput
- input={{ ...baseInput, mode: 'reply' }}
+ input={{ ...baseInput, mode: 'reply', value: 'reply' }}
onSend={onSend}
onToggleFormatRibbon={onToggleFormatRibbon}
onClose={onClose}
@@ -186,7 +186,7 @@ describe('Input slots', () => {
clickSpy.mockRestore();
expect(onToggleFormatRibbon).toHaveBeenCalledOnce();
expect(onClose).toHaveBeenCalledOnce();
- expect(onSend.mock.calls[0]?.[0]?.value).toBe('');
+ expect(onSend.mock.calls[0]?.[0]?.value).toBe('reply');
});
it('omits the reply action for channel mode', () => {
@@ -236,6 +236,15 @@ describe('Input slots', () => {
);
});
+ it('disables send when the input is empty', () => {
+ render(() => <ChannelInput input={{ ...baseInput, value: ' ' }} />);
+
+ expect(screen.getByRole('button', { name: 'Send message' })).toHaveProperty(
+ 'disabled',
+ true
+ );
+ });
+
it('shows invalid state in drop overlay', () => {
render(() => (
<Root
diff --git a/js/app/packages/channel/Input/utils/sendable-content.ts b/js/app/packages/channel/Input/utils/sendable-content.ts
new file mode 100644
index 0000000000..e6fd329c90
--- /dev/null
+++ b/js/app/packages/channel/Input/utils/sendable-content.ts
@@ -0,0 +1,12 @@
+import type { InputAttachmentData } from '../types';
+
+type InputContent = {
+ value?: string;
+ attachments?: readonly InputAttachmentData[];
+};
+
+export function hasSendableInputContent(input: InputContent): boolean {
+ return (
+ input.value?.trim().length !== 0 || (input.attachments?.length ?? 0) > 0
+ );
+}