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.

chg(compose): rework compose to use the new input primitives (#2480)

teod04cb06ccb04
diff --git a/js/app/packages/block-channel/component/Compose.tsx b/js/app/packages/block-channel/component/Compose.tsx
index f26831b2fd..b4c2cf5d98 100644
--- a/js/app/packages/block-channel/component/Compose.tsx
+++ b/js/app/packages/block-channel/component/Compose.tsx
@@ -4,33 +4,44 @@ import {
StaticSplitLabel,
} from '@app/component/split-layout/components/SplitLabel';
import { SplitToolbarLeft } from '@app/component/split-layout/components/SplitToolbar';
-import { FileDropOverlay } from '@core/component/FileDropOverlay';
+import {
+ buildPostMessageRequest,
+ createConfiguredChannelMarkdownEditor,
+ createInputAttachmentTracker,
+ createInputState,
+ createMentionsTracker,
+ FormatButtons,
+ Input,
+ applyInlineFormat,
+ applyNodeFormat,
+ uploadInputAttachments,
+ type InputSnapshot,
+} from '@channel/Input';
+import { MarkdownShell } from '@core/component/LexicalMarkdown/builder/MarkdownShell';
+import { getDestinationFromOptions } from '@core/component/NewMessage';
import { RecipientSelector } from '@core/component/RecipientSelector';
-import { fileFolderDrop } from '@core/directive/fileFolderDrop';
+import { isMobile } from '@core/mobile/isMobile';
import { useCombinedRecipients } from '@core/signal/useCombinedRecipient';
-import type { InputAttachment } from '@core/store/cacheChannelInput';
import {
tryMacroId,
useDisplayName,
type WithCustomUserInput,
} from '@core/user';
-import { handleFileFolderDrop } from '@core/util/upload';
+import {
+ chatRuleset,
+ handleFileFolderDrop,
+ uploadFile,
+} from '@core/util/upload';
+import { useSendMessageToPeople } from '@core/util/channels';
+import { isErr } from '@core/util/maybeResult';
+import { commsServiceClient } from '@service-comms/client';
import InfoIcon from '@icon/regular/info.svg';
import { createEffect, createMemo, createSignal, on, Show } from 'solid-js';
-import { createStore } from 'solid-js/store';
-import { handleFileUpload } from './DeprecatedChannelInput/inputAttachments';
-import { DraftChannelInput } from './DeprecatedChannelInput/ChannelInput';
-
-false && fileFolderDrop;
+import { ChannelInputContainer } from '@channel/Input/ChannelInputContainer';
export function ChannelCompose() {
const [channelName, setChannelName] = createSignal<string>('');
- const [isDraggedOver, setIsDraggedOver] = createSignal(false);
-
- const [channelInputAttachmentsStore, setChannelInputAttachmentsStore] =
- createStore<Record<string, InputAttachment[]>>({});
-
const { users: destinationOptions } = useCombinedRecipients();
const [selectedRecipients, setSelectedRecipients] = createSignal<
WithCustomUserInput<'user' | 'contact'>[]
@@ -89,6 +100,109 @@ export function ChannelCompose() {
}
});
+ const [error, setError] = createSignal<string>();
+
+ const { sendToUsers, sendToChannel } = useSendMessageToPeople();
+
+ async function handleSend(snapshot: InputSnapshot) {
+ setError(undefined);
+ const recipients = selectedRecipients();
+
+ if (recipients.length === 0) {
+ setError('Please select at least one recipient');
+ throw new Error('Please select at least one recipient');
+ }
+
+ const destination = getDestinationFromOptions(recipients);
+ const { content, mentions, attachments } = buildPostMessageRequest({
+ snapshot,
+ });
+
+ try {
+ if (
+ destination.type === 'users' &&
+ channelName() &&
+ destination.users.length > 1
+ ) {
+ const res = await commsServiceClient.createChannel({
+ channel_type: 'private',
+ name: channelName() ?? null,
+ participants: destination.users,
+ });
+ if (isErr(res)) {
+ throw new Error('Could not create channel');
+ }
+ const [, { id }] = res;
+ await sendToChannel({
+ channelId: id,
+ content,
+ mentions,
+ attachments,
+ navigate: { navigate: true, mergeHistory: true },
+ });
+ } else {
+ await sendToUsers({
+ users: destination.users,
+ content,
+ mentions,
+ attachments,
+ navigate: { navigate: true, mergeHistory: true },
+ });
+ }
+ } catch (e) {
+ setError(e instanceof Error ? e.message : 'Failed to send message');
+ throw e;
+ }
+ }
+
+ const mentionsTracker = createMentionsTracker();
+ const attachmentTracker = createInputAttachmentTracker();
+ let clearComposer = () => {};
+
+ const inputState = createInputState({
+ initialInput: { mode: 'channel' as const },
+ mentions: mentionsTracker.mentions,
+ attachmentTracker,
+ clearComposer: () => clearComposer(),
+ attachFiles: async (files) => {
+ await uploadInputAttachments({
+ files,
+ tracker: attachmentTracker,
+ uploadFile: async (file) =>
+ uploadFile(file, chatRuleset, { hideProgressIndicator: true }),
+ });
+ },
+ clearInput: () => markdownEditor.controls.clear(),
+ callbacks: { onSend: handleSend },
+ });
+
+ const [scrollContainer, setScrollContainer] = createSignal<HTMLElement>();
+
+ const markdownEditor = createConfiguredChannelMarkdownEditor({
+ namespace: 'compose-input-markdown',
+ enableMentions: true,
+ scrollContainer,
+ onMentionCreate: (mention) => mentionsTracker.onMentionCreate(mention),
+ onMentionRemove: (mention) => mentionsTracker.onMentionRemove(mention),
+ onChange: (markdown) => inputState.setValue(markdown),
+ onEnter: () => {
+ if (isMobile()) return false;
+ void inputState.commands.send();
+ return true;
+ },
+ onPasteFilesAndDirs: (files, directories) => {
+ void handleFileFolderDrop(files, directories, (entries) =>
+ inputState.commands.attachFiles(entries.map((e) => e.file))
+ );
+ },
+ });
+ clearComposer = () => markdownEditor.controls.clear();
+
+ const placeholder = createMemo(() => {
+ const name = channelName();
+ return name ? `Send message to ${name}` : 'Send message';
+ });
+
return (
<>
<SplitHeaderLeft>
@@ -104,33 +218,7 @@ export function ChannelCompose() {
<SplitToolbarLeft>
<div class="h-full items-center flex" p-1></div>
</SplitToolbarLeft>
- <div
- class="relative flex flex-col w-full h-full panel"
- use:fileFolderDrop={{
- onDrop: (files, folders) => {
- handleFileFolderDrop(files, folders, (uploadEntries) =>
- handleFileUpload(uploadEntries, {
- store: channelInputAttachmentsStore,
- setStore: setChannelInputAttachmentsStore,
- key: 'draft',
- })
- );
- },
- onDragStart: () => {
- setIsDraggedOver(true);
- },
- onDragEnd: () => {
- setIsDraggedOver(false);
- },
- }}
- >
- <Show when={isDraggedOver()}>
- <FileDropOverlay valid={true}>
- <div class="font-mono">
- Drop any file here to add it to the conversation
- </div>
- </FileDropOverlay>
- </Show>
+ <div class="relative flex flex-col w-full h-full panel">
<div class="pt-2 h-full grow w-full overflow-y-auto @min-[40rem]:px-4">
<div class="macro-message-width macro-message-padding mx-auto pb-1 h-full">
<input
@@ -165,19 +253,67 @@ export function ChannelCompose() {
</div>
</div>
</div>
- <div class="shrink-0 w-full pb-2 @min-[40rem]:px-4">
- <div class="mx-auto w-full macro-message-width macro-message-padding">
- <DraftChannelInput
- selectedRecipients={selectedRecipients}
- channelName={channelName}
- inputAttachments={{
- store: channelInputAttachmentsStore,
- setStore: setChannelInputAttachmentsStore,
- key: 'draft',
- }}
- />
+ <Show when={error()}>
+ <div class="shrink-0 w-full @min-[40rem]:px-4">
+ <div class="mx-auto w-full macro-message-width macro-message-padding">
+ <div class="text-sm font-mono text-failure-ink">{error()}</div>
+ </div>
</div>
- </div>
+ </Show>
+ <ChannelInputContainer>
+ <Input.Root input={inputState.view()} commands={inputState.commands}>
+ <Input.DropZone
+ onDragStart={(valid) => inputState.setIsDraggedOver(valid)}
+ onDragEnd={() => inputState.setIsDraggedOver(false)}
+ >
+ <Input.Layout>
+ <Input.DropOverlay />
+ <Input.FormatRibbon>
+ <FormatButtons
+ selectionState={() => markdownEditor.selection}
+ onInlineFormat={(format) =>
+ applyInlineFormat(markdownEditor.lexical, format)
+ }
+ onNodeFormat={(format) =>
+ applyNodeFormat(markdownEditor.lexical, format)
+ }
+ />
+ </Input.FormatRibbon>
+ <Input.EditorShell
+ ref={setScrollContainer}
+ onClick={(event) => {
+ if (!isMobile()) {
+ event.stopPropagation();
+ markdownEditor.controls.focus();
+ }
+ }}
+ >
+ <Input.Editor>
+ <MarkdownShell
+ config={markdownEditor}
+ placeholder={placeholder()}
+ autofocus={false}
+ class="text-sm"
+ />
+ </Input.Editor>
+ </Input.EditorShell>
+ <Input.Attachments kind="media" />
+ <Input.Attachments kind="document" />
+ <Input.Footer>
+ <Input.Actions>
+ <Input.Actions.Left>
+ <Input.AttachFilesAction />
+ <Input.ToggleFormatAction />
+ </Input.Actions.Left>
+ <Input.Actions.Right>
+ <Input.SendAction />
+ </Input.Actions.Right>
+ </Input.Actions>
+ </Input.Footer>
+ </Input.Layout>
+ </Input.DropZone>
+ </Input.Root>
+ </ChannelInputContainer>
</div>
</>
);
diff --git a/js/app/packages/channel/Input/ChannelInputContainer.tsx b/js/app/packages/channel/Input/ChannelInputContainer.tsx
index caf13d44b9..b3acf9f58a 100644
--- a/js/app/packages/channel/Input/ChannelInputContainer.tsx
+++ b/js/app/packages/channel/Input/ChannelInputContainer.tsx
@@ -2,7 +2,7 @@ import type { JSX } from 'solid-js';
import { cn } from '@ui/utils/classname';
export function ChannelInputContainer(props: {
- ref: (el: HTMLDivElement) => void;
+ ref?: (el: HTMLDivElement) => void;
children: JSX.Element;
isHidden?: boolean;
}) {
diff --git a/js/app/packages/channel/Input/index.ts b/js/app/packages/channel/Input/index.ts
index 200b2034ba..8b0c6d1ace 100644
--- a/js/app/packages/channel/Input/index.ts
+++ b/js/app/packages/channel/Input/index.ts
@@ -4,6 +4,9 @@ export { ThreadInput } from './ThreadInput';
export { createInputAttachmentTracker } from './attachment-tracker';
export { createInputState } from './create-input-state';
export { createConfiguredChannelMarkdownEditor } from './configured-markdown-editor';
+export { createMentionsTracker } from './mentions-tracker';
+export { buildPostMessageRequest } from './message-payload';
+export { uploadInputAttachments } from './upload-attachments';
export { Root } from './Root';
export { Layout } from './Layout';
@@ -26,6 +29,7 @@ export { SendAction } from './SendAction';
export { RibbonButton } from './RibbonButton';
export { FormatButtons } from './FormatButtons';
export { useInput, useInputCommands, InputProvider } from './context';
+export { applyInlineFormat, applyNodeFormat } from './utils/formatting';
export { isReplyInput } from './types';
diff --git a/js/app/packages/core/component/RecipientSelector.tsx b/js/app/packages/core/component/RecipientSelector.tsx
index f985b5c2c2..2a3b3e2874 100644
--- a/js/app/packages/core/component/RecipientSelector.tsx
+++ b/js/app/packages/core/component/RecipientSelector.tsx
@@ -474,7 +474,7 @@ export function RecipientSelector<K extends CombinedRecipientKind>(
? (props.placeholder ?? placeholderText())
: undefined
}
- class="ph-no-capture w-full text-sm offset-2"
+ class="ph-no-capture w-full text-sm offset-2 bg-input"
classList={{
'border border-edge': !props.hideBorder,
'py-2': !props.noPadding,