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.

chore(new-channels): clean up input composition (#1934)

teo718075b61480
diff --git a/js/app/packages/channel/Channel/InlineMessageEditor.tsx b/js/app/packages/channel/Channel/InlineMessageEditor.tsx
index f9d2d3d020..7025e22802 100644
--- a/js/app/packages/channel/Channel/InlineMessageEditor.tsx
+++ b/js/app/packages/channel/Channel/InlineMessageEditor.tsx
@@ -1,35 +1,7 @@
-import TrashIcon from '@icon/regular/trash.svg';
-import FormatIcon from '@icon/regular/text-aa.svg';
-import {
- ChannelInput,
- createInputAttachmentTracker,
- useInput,
- useInputCommands,
-} from '../Input';
-import { InputActionButton } from '@channel/Input/PrimaryActions';
+import { Input, ChannelInput, createInputAttachmentTracker } from '../Input';
import type { MessageEditor } from './create-message-editor';
import { useMessage } from '../Message';
-function EditPrimaryActions() {
- const commands = useInputCommands();
- const input = useInput();
-
- return (
- <>
- <InputActionButton
- label="Format"
- active={input().showFormatRibbon}
- onClick={() => commands.toggleFormatRibbon()}
- >
- <FormatIcon class="size-5" />
- </InputActionButton>
- <InputActionButton label="Discard Edit" onClick={() => commands.close()}>
- <TrashIcon class="size-5" />
- </InputActionButton>
- </>
- );
-}
-
type MessageEditorContentProps = {
channelId: string;
messageEditor: MessageEditor;
@@ -60,7 +32,16 @@ export function MessageEditorContent(props: MessageEditorContentProps) {
onSend={(nextSnapshot) =>
props.messageEditor.save(message(), nextSnapshot)
}
- primaryActions={<EditPrimaryActions />}
- />
+ >
+ <Input.Actions>
+ <Input.Actions.Left>
+ <Input.ToggleFormatAction />
+ <Input.DiscardDraftAction />
+ </Input.Actions.Left>
+ <Input.Actions.Right>
+ <Input.SendAction />
+ </Input.Actions.Right>
+ </Input.Actions>
+ </ChannelInput>
);
}
diff --git a/js/app/packages/channel/Input/ActionButton.tsx b/js/app/packages/channel/Input/ActionButton.tsx
new file mode 100644
index 0000000000..e118742ac1
--- /dev/null
+++ b/js/app/packages/channel/Input/ActionButton.tsx
@@ -0,0 +1,22 @@
+import { Button } from '@ui/components/Button';
+import { LabelAndHotKey } from '@core/component/Tooltip';
+import type { JSX } from 'solid-js';
+
+export function InputActionButton(props: {
+ label: string;
+ onClick?: (event: MouseEvent) => void;
+ active?: boolean;
+ children: JSX.Element;
+}) {
+ return (
+ <Button
+ title={props.label}
+ aria-label={props.label}
+ tooltip={<LabelAndHotKey label={props.label} />}
+ onClick={(event) => props.onClick?.(event)}
+ classList={{ 'bg-active': props.active }}
+ >
+ {props.children}
+ </Button>
+ );
+}
diff --git a/js/app/packages/channel/Input/Actions.tsx b/js/app/packages/channel/Input/Actions.tsx
new file mode 100644
index 0000000000..8460895045
--- /dev/null
+++ b/js/app/packages/channel/Input/Actions.tsx
@@ -0,0 +1,52 @@
+import { cn } from '@ui/utils/classname';
+import { splitProps, type JSX } from 'solid-js';
+
+function ActionsRoot(props: JSX.HTMLAttributes<HTMLDivElement>) {
+ const [local, rest] = splitProps(props, ['class', 'children']);
+
+ return (
+ <div
+ class={cn(
+ 'flex flex-row w-full justify-between items-center',
+ local.class
+ )}
+ data-input-actions
+ {...rest}
+ >
+ {local.children}
+ </div>
+ );
+}
+
+function ActionsLeft(props: JSX.HTMLAttributes<HTMLDivElement>) {
+ const [local, rest] = splitProps(props, ['class', 'children']);
+
+ return (
+ <div
+ class={cn('flex flex-row items-center gap-2', local.class)}
+ data-input-actions-left
+ {...rest}
+ >
+ {local.children}
+ </div>
+ );
+}
+
+function ActionsRight(props: JSX.HTMLAttributes<HTMLDivElement>) {
+ const [local, rest] = splitProps(props, ['class', 'children']);
+
+ return (
+ <div
+ class={cn('flex flex-row items-center gap-2', local.class)}
+ data-input-actions-right
+ {...rest}
+ >
+ {local.children}
+ </div>
+ );
+}
+
+export const Actions = Object.assign(ActionsRoot, {
+ Left: ActionsLeft,
+ Right: ActionsRight,
+});
diff --git a/js/app/packages/channel/Input/ChannelInput.tsx b/js/app/packages/channel/Input/ChannelInput.tsx
index 0bac6a1684..9a9031b0e0 100644
--- a/js/app/packages/channel/Input/ChannelInput.tsx
+++ b/js/app/packages/channel/Input/ChannelInput.tsx
@@ -16,7 +16,8 @@ import type {
InputPersistenceKey,
} from './types';
import { applyInlineFormat, applyNodeFormat } from './utils/formatting';
-import type { JSX } from 'solid-js';
+import { children, Match, Show, Switch, type JSX } from 'solid-js';
+import { isReplyInput } from './types';
export type ChannelInputProps = InputCallbacks & {
input: InputData;
@@ -24,12 +25,29 @@ export type ChannelInputProps = InputCallbacks & {
persistenceKey?: InputPersistenceKey;
attachmentTracker?: InputAttachmentTracker;
onReady?: (handle: InputHandle) => void;
- primaryActions?: JSX.Element;
- sendAction?: JSX.Element;
+ children?: JSX.Element;
};
+function DefaultActions(props: { input: InputData }) {
+ return (
+ <Input.Actions>
+ <Input.Actions.Left>
+ <Input.AttachFilesAction />
+ <Input.ToggleFormatAction />
+ <Show when={isReplyInput(props.input)}>
+ <Input.CloseReplyAction />
+ </Show>
+ </Input.Actions.Left>
+ <Input.Actions.Right>
+ <Input.SendAction />
+ </Input.Actions.Right>
+ </Input.Actions>
+ );
+}
+
export function ChannelInput(props: ChannelInputProps) {
const mentionsTracker = createMentionsTracker();
+ const customActions = children(() => props.children);
const attachmentTracker =
props.attachmentTracker ??
createInputAttachmentTracker({
@@ -128,8 +146,12 @@ export function ChannelInput(props: ChannelInputProps) {
<Input.Attachments kind="media" />
<Input.Attachments kind="document" />
<Input.Footer>
- <Input.PrimaryActions>{props.primaryActions}</Input.PrimaryActions>
- <Input.SendAction>{props.sendAction}</Input.SendAction>
+ <Switch>
+ <Match when={customActions()}>{(actions) => actions()}</Match>
+ <Match when>
+ <DefaultActions input={inputState.view()} />
+ </Match>
+ </Switch>
</Input.Footer>
</Input.Layout>
</Input.DropZone>
diff --git a/js/app/packages/channel/Input/Input.ts b/js/app/packages/channel/Input/Input.ts
index 495a930cbd..b3c4ab071d 100644
--- a/js/app/packages/channel/Input/Input.ts
+++ b/js/app/packages/channel/Input/Input.ts
@@ -7,7 +7,13 @@ import { EditorShell } from './EditorShell';
import { Editor } from './Editor';
import { Attachments } from './Attachments';
import { Footer } from './Footer';
-import { PrimaryActions } from './PrimaryActions';
+import { Actions } from './Actions';
+import {
+ AttachFilesAction,
+ CloseReplyAction,
+ DiscardDraftAction,
+ ToggleFormatAction,
+} from './InputActions';
import { SendAction } from './SendAction';
export const Input = {
@@ -20,6 +26,10 @@ export const Input = {
Editor,
Attachments,
Footer,
- PrimaryActions,
+ Actions,
+ AttachFilesAction,
+ ToggleFormatAction,
+ CloseReplyAction,
+ DiscardDraftAction,
SendAction,
};
diff --git a/js/app/packages/channel/Input/InputActions.tsx b/js/app/packages/channel/Input/InputActions.tsx
new file mode 100644
index 0000000000..494e5804b6
--- /dev/null
+++ b/js/app/packages/channel/Input/InputActions.tsx
@@ -0,0 +1,78 @@
+import FormatIcon from '@icon/regular/text-aa.svg';
+import TrashIcon from '@icon/regular/trash.svg';
+import PaperclipIcon from '@phosphor-icons/core/regular/paperclip.svg?component-solid';
+import type { JSX } from 'solid-js';
+import { useInput, useInputCommands } from './context';
+import { CHANNEL_FILE_PICKER_ACCEPT } from './accepted-file-types';
+import { InputActionButton } from './ActionButton';
+
+export function AttachFilesAction() {
+ const commands = useInputCommands();
+ let fileInputRef: HTMLInputElement | undefined;
+
+ const onAttachFiles: JSX.EventHandlerUnion<HTMLInputElement, Event> = (
+ event
+ ) => {
+ const files = Array.from(event.currentTarget.files ?? []);
+ event.currentTarget.value = '';
+ if (files.length === 0) return;
+ void commands.attachFiles(files);
+ };
+
+ return (
+ <>
+ <input
+ ref={(element) => {
+ fileInputRef = element;
+ }}
+ type="file"
+ class="hidden"
+ multiple
+ accept={CHANNEL_FILE_PICKER_ACCEPT}
+ onChange={onAttachFiles}
+ data-input-attach-file-picker
+ />
+ <InputActionButton
+ label="Attach files"
+ onClick={() => fileInputRef?.click()}
+ >
+ <PaperclipIcon class="size-5" />
+ </InputActionButton>
+ </>
+ );
+}
+
+export function ToggleFormatAction() {
+ const input = useInput();
+ const commands = useInputCommands();
+
+ return (
+ <InputActionButton
+ label="Format"
+ active={input().showFormatRibbon}
+ onClick={() => commands.toggleFormatRibbon()}
+ >
+ <FormatIcon class="size-5" />
+ </InputActionButton>
+ );
+}
+
+export function CloseReplyAction() {
+ const commands = useInputCommands();
+
+ return (
+ <InputActionButton label="Delete reply" onClick={() => commands.close()}>
+ <TrashIcon class="size-5" />
+ </InputActionButton>
+ );
+}
+
+export function DiscardDraftAction() {
+ const commands = useInputCommands();
+
+ return (
+ <InputActionButton label="Discard Edit" onClick={() => commands.close()}>
+ <TrashIcon class="size-5" />
+ </InputActionButton>
+ );
+}
diff --git a/js/app/packages/channel/Input/index.ts b/js/app/packages/channel/Input/index.ts
index a1974b212b..200b2034ba 100644
--- a/js/app/packages/channel/Input/index.ts
+++ b/js/app/packages/channel/Input/index.ts
@@ -14,7 +14,14 @@ export { EditorShell } from './EditorShell';
export { Editor } from './Editor';
export { Attachments } from './Attachments';
export { Footer } from './Footer';
-export { PrimaryActions } from './PrimaryActions';
+export { Actions } from './Actions';
+export { InputActionButton } from './ActionButton';
+export {
+ AttachFilesAction,
+ ToggleFormatAction,
+ CloseReplyAction,
+ DiscardDraftAction,
+} from './InputActions';
export { SendAction } from './SendAction';
export { RibbonButton } from './RibbonButton';
export { FormatButtons } from './FormatButtons';
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 7322160be1..3ee00f555f 100644
--- a/js/app/packages/channel/Input/tests/input-slots.test.tsx
+++ b/js/app/packages/channel/Input/tests/input-slots.test.tsx
@@ -43,6 +43,10 @@ vi.mock('@icon/regular/trash.svg', () => ({
default: () => <span data-testid="trash-icon" />,
}));
+vi.mock('@icon/regular/x.svg', () => ({
+ default: () => <span data-testid="close-icon" />,
+}));
+
vi.mock('@icon/bold/arrow-up-bold.svg', () => ({
default: () => <span data-testid="send-icon" />,
}));
@@ -133,6 +137,7 @@ vi.mock('../FormatButtons', () => ({
import { ChannelInput } from '../ChannelInput';
import { Root } from '../Root';
import { DropOverlay } from '../DropOverlay';
+import { createInputAttachmentTracker } from '../attachment-tracker';
import type { InputData } from '../types';
const baseInput: InputData = {
@@ -146,13 +151,13 @@ const baseInput: InputData = {
};
describe('Input slots', () => {
- it('wires send and primary action handlers through context', async () => {
+ it('renders the default action composition and wires handlers through context', async () => {
const user = userEvent.setup();
const onSend = vi.fn();
const onToggleFormatRibbon = vi.fn();
const onClose = vi.fn();
- render(() =>
+ const { container } = render(() =>
(() => {
return (
<ChannelInput
@@ -165,6 +170,10 @@ describe('Input slots', () => {
})()
);
+ expect(container.querySelector('[data-input-actions]')).toBeTruthy();
+ expect(container.querySelector('[data-input-actions-left]')).toBeTruthy();
+ expect(container.querySelector('[data-input-actions-right]')).toBeTruthy();
+
await user.click(screen.getByRole('button', { name: 'Send message' }));
const clickSpy = vi.spyOn(HTMLInputElement.prototype, 'click');
await user.click(screen.getByRole('button', { name: 'Attach files' }));
@@ -179,6 +188,53 @@ describe('Input slots', () => {
expect(onSend.mock.calls[0]?.[0]?.value).toBe('');
});
+ it('omits the reply action for channel mode', () => {
+ render(() => <ChannelInput input={baseInput} />);
+
+ expect(screen.queryByRole('button', { name: 'Delete reply' })).toBeNull();
+ });
+
+ it('renders custom action composition from children instead of defaults', () => {
+ render(() => (
+ <ChannelInput input={baseInput}>
+ <div data-testid="custom-actions">custom actions</div>
+ </ChannelInput>
+ ));
+
+ expect(screen.getByTestId('custom-actions')).toBeTruthy();
+ expect(screen.queryByRole('button', { name: 'Attach files' })).toBeNull();
+ expect(screen.queryByRole('button', { name: 'Send message' })).toBeNull();
+ });
+
+ it('disables send while attachments are pending', () => {
+ render(() =>
+ (() => {
+ const attachmentTracker = createInputAttachmentTracker({
+ initialAttachments: [
+ {
+ id: 'pending-1',
+ name: 'uploading.png',
+ kind: 'image',
+ pending: true,
+ },
+ ],
+ });
+
+ return (
+ <ChannelInput
+ input={baseInput}
+ attachmentTracker={attachmentTracker}
+ />
+ );
+ })()
+ );
+
+ expect(screen.getByRole('button', { name: 'Send message' })).toHaveProperty(
+ 'disabled',
+ true
+ );
+ });
+
it('shows invalid state in drop overlay', () => {
render(() => (
<Root