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(channel): start typing from composer text changes (#6187)

teo3993a27f78c2
diff --git a/apps/web/src/features/channel/Input/ChannelInput.tsx b/apps/web/src/features/channel/Input/ChannelInput.tsx
index a9a17fa976..bf42d23c38 100644
--- a/apps/web/src/features/channel/Input/ChannelInput.tsx
+++ b/apps/web/src/features/channel/Input/ChannelInput.tsx
@@ -210,6 +210,7 @@ export function ChannelInput(props: ChannelInputProps) {
const isCollapsed = () => !!props.collapsible && collapsedInput.isCollapsed();
let isEditorConnected = false;
+ let acceptTyping = false;
let pendingRestore:
| {
snapshot: InputSnapshot;
@@ -225,11 +226,15 @@ export function ChannelInput(props: ChannelInputProps) {
snapshot: InputSnapshot,
options?: RestoreSnapshotOptions
) => {
+ acceptTyping = false;
markdownEditor.controls.setMarkdown(snapshot.value);
pendingCursor = options?.cursor;
attachmentTracker.setAttachments(snapshot.attachments);
mentionsTracker.setMentions(snapshot.mentions);
if (options?.focus !== false) focusEditorNow();
+ queueMicrotask(() => {
+ acceptTyping = true;
+ });
};
const flushPendingRestore = () => {
@@ -313,7 +318,10 @@ export function ChannelInput(props: ChannelInputProps) {
mentionsTracker.onMentionRemove(mention);
},
onChange: (markdown) => {
+ const previous = inputState.view().value ?? '';
inputState.setValue(markdown);
+ if (!acceptTyping) return;
+ if (markdown.trim() === previous.trim()) return;
typingTracker.keystroke();
},
onEnter: () => {
@@ -484,6 +492,9 @@ export function ChannelInput(props: ChannelInputProps) {
isEditorConnected = true;
flushPendingRestore();
flushPendingFocus();
+ queueMicrotask(() => {
+ acceptTyping = true;
+ });
}}
/>
<DragInsertIndicator
diff --git a/apps/web/src/features/channel/Input/tests/input-slots.test.tsx b/apps/web/src/features/channel/Input/tests/input-slots.test.tsx
index 4a082c5227..835daa8108 100644
--- a/apps/web/src/features/channel/Input/tests/input-slots.test.tsx
+++ b/apps/web/src/features/channel/Input/tests/input-slots.test.tsx
@@ -5,13 +5,14 @@
import { render as renderBare, screen } from '@solidjs/testing-library';
import { QueryClient, QueryClientProvider } from '@tanstack/solid-query';
import userEvent from '@testing-library/user-event';
-import type { JSX } from 'solid-js';
+import { type JSX, onMount } from 'solid-js';
import { Portal } from 'solid-js/web';
import { beforeEach, describe, expect, it, vi } from 'vitest';
const editorMocks = vi.hoisted(() => ({
clear: vi.fn(),
focus: vi.fn(),
+ emitChange: undefined as ((markdown: string) => void) | undefined,
}));
vi.hoisted(() => {
@@ -119,19 +120,29 @@ vi.mock('@core/component/VideoPreview', () => ({
}));
vi.mock('@core/component/LexicalMarkdown/builder/MarkdownShell', () => ({
- MarkdownShell: (props: { placeholder?: string; initialValue?: string }) => (
- <>
- <div
- data-testid="markdown-shell"
- data-initial-value={props.initialValue ?? ''}
- >
- {props.placeholder}
- </div>
- <Portal>
- <input data-testid="markdown-portal-input" />
- </Portal>
- </>
- ),
+ MarkdownShell: (props: {
+ placeholder?: string;
+ initialValue?: string;
+ onConnect?: () => void;
+ }) => {
+ onMount(() => {
+ editorMocks.emitChange?.(props.initialValue ?? '');
+ props.onConnect?.();
+ });
+ return (
+ <>
+ <div
+ data-testid="markdown-shell"
+ data-initial-value={props.initialValue ?? ''}
+ >
+ {props.placeholder}
+ </div>
+ <Portal>
+ <input data-testid="markdown-portal-input" />
+ </Portal>
+ </>
+ );
+ },
}));
vi.mock(
@@ -141,6 +152,9 @@ vi.mock(
const controls = {
clear: editorMocks.clear,
focus: editorMocks.focus,
+ setMarkdown: (markdown: string) => {
+ editorMocks.emitChange?.(markdown);
+ },
};
const lexical = {
focus: vi.fn(),
@@ -169,7 +183,10 @@ vi.mock(
withSelectionData: () => builder,
withFloatingFormatMenu: () => builder,
use: () => builder,
- onChange: () => builder,
+ onChange: (handler: (markdown: string) => void) => {
+ editorMocks.emitChange = handler;
+ return builder;
+ },
onEnter: () => builder,
buildHandle: () => handle,
controls,
@@ -243,6 +260,65 @@ describe('Input slots', () => {
beforeEach(() => {
editorMocks.clear.mockClear();
editorMocks.focus.mockClear();
+ editorMocks.emitChange = undefined;
+ });
+
+ it('does not start typing when the editor hydrates an empty composer', async () => {
+ const onStartTyping = vi.fn();
+ render(() => (
+ <ChannelInput input={baseInput} onStartTyping={onStartTyping} />
+ ));
+
+ await Promise.resolve();
+ expect(onStartTyping).not.toHaveBeenCalled();
+
+ editorMocks.emitChange?.('hello');
+ expect(onStartTyping).toHaveBeenCalledTimes(1);
+ });
+
+ it('does not start typing when hydrate echoes an existing draft', async () => {
+ const onStartTyping = vi.fn();
+ render(() => (
+ <ChannelInput
+ input={{ ...baseInput, value: 'draft' }}
+ onStartTyping={onStartTyping}
+ />
+ ));
+
+ await Promise.resolve();
+ expect(onStartTyping).not.toHaveBeenCalled();
+
+ editorMocks.emitChange?.('draft');
+ expect(onStartTyping).not.toHaveBeenCalled();
+
+ editorMocks.emitChange?.('draft plus');
+ expect(onStartTyping).toHaveBeenCalledTimes(1);
+ });
+
+ it('does not start typing when a snapshot is restored', async () => {
+ const onStartTyping = vi.fn();
+ let handle: InputHandle | undefined;
+ render(() => (
+ <ChannelInput
+ input={baseInput}
+ onReady={(nextHandle) => {
+ handle = nextHandle;
+ }}
+ onStartTyping={onStartTyping}
+ />
+ ));
+
+ await Promise.resolve();
+ handle?.restoreSnapshot({
+ value: 'restored draft',
+ mentions: [],
+ attachments: [],
+ });
+ expect(onStartTyping).not.toHaveBeenCalled();
+
+ await Promise.resolve();
+ editorMocks.emitChange?.('user typed');
+ expect(onStartTyping).toHaveBeenCalledTimes(1);
});
it('does not refocus the editor when a portaled editor control is clicked', async () => {