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.

6.3 KB
Raw Blame
import {
  ChannelInput,
  type InputHandle,
  type InputSnapshot,
} from '@channel/Input';
import { buildPostMessageSendPayload } from '@channel/Input/message-payload';
import { buildReplyTargetValue } from '@channel/Thread/utils/message-actions';
import { StaticMarkdownContext } from '@core/component/LexicalMarkdown/component/core/StaticMarkdown';
import { useUserId } from '@core/context/user';
import { MessageThread, threadListItem } from '@core/messages/MessageThread';
import { MessageReferenceNavigation } from '@core/messages/message-reference-navigation';
import { fireAndForget } from '@core/util/fireAndForget';
import {
  newMessageId,
  useSendMessageMutation,
} from '@queries/messages/mutations';
import {
  createEffect,
  createMemo,
  createSignal,
  on,
  Show,
  Suspense,
} from 'solid-js';
import { buildCallMessageLink } from './build-call-message-link';
import { CallChatBody } from './components/CallChatBody';
import { useCallChat } from './queries/use-call-chat';

export default function CallChat(props: {
  callId: string;
  open: boolean;
  focusOnOpen?: boolean;
  onClose: () => void;
}) {
  // Queries and all shared Markdown descendants stay within the chat boundary.
  return (
    <Suspense
      fallback={
        <CallChatBody open={props.open} composer={null}>
          <p role="status" class="text-sm text-ink-muted">
            Loading chat…
          </p>
        </CallChatBody>
      }
    >
      <CallChatContent {...props} />
    </Suspense>
  );
}

function CallChatContent(props: {
  callId: string;
  open: boolean;
  focusOnOpen?: boolean;
  onClose: () => void;
}) {
  const chat = useCallChat(() => props.callId);
  const send = useSendMessageMutation();
  const userId = useUserId();
  const [sendFailed, setSendFailed] = createSignal(false);
  const [input, setInput] = createSignal<InputHandle>();
  const [messageTarget, setMessageTarget] = createSignal<{
    id: string;
    requestKey: number;
  }>();
  const canSend = createMemo(() => !!chat.thread() || chat.empty());

  createEffect(
    on([() => props.open, canSend, input], ([open, ready, handle]) => {
      if (open && ready && props.focusOnOpen !== false) {
        handle?.focus({
          when: () => props.open && props.focusOnOpen !== false,
        });
      }
    })
  );

  async function sendMessage(snapshot: InputSnapshot) {
    const senderId = userId();
    if (!senderId || (!chat.thread() && !chat.empty())) {
      throw new Error('Call chat is not ready');
    }
    setSendFailed(false);
    try {
      await send.mutateAsync({
        parent: chat.parent(),
        senderId,
        optimisticId: newMessageId(),
        ...buildPostMessageSendPayload({
          snapshot,
          threadId: chat.thread() ? props.callId : undefined,
        }),
      });
    } catch (error) {
      setSendFailed(true);
      throw error;
    }
    await chat.refresh();
  }

  return (
    <MessageReferenceNavigation.Provider
      value={(target) => {
        if (target.parent.type !== 'call' || target.parent.id !== props.callId)
          return false;
        setMessageTarget((previous) => ({
          id: target.targetMessageId,
          requestKey: (previous?.requestKey ?? 0) + 1,
        }));
        return true;
      }}
    >
      <StaticMarkdownContext>
        <CallChatBody
          open={props.open}
          composer={
            <div inert={!canSend()} classList={{ 'opacity-50': !canSend() }}>
              <Show when={sendFailed()}>
                <p role="alert" class="mb-2 text-xs text-failure">
                  Message could not be sent. Try again.
                </p>
              </Show>
              <ChannelInput
                parent={chat.parent()}
                input={{ mode: 'channel', placeholder: 'Message this call…' }}
                persistenceKey={`call-chat-${userId()}-${props.callId}-persist-v1`}
                autofocus={false}
                onReady={setInput}
                onSend={sendMessage}
                onSendError={() => setSendFailed(true)}
                onEscape={props.onClose}
              />
            </div>
          }
        >
          <Show when={chat.loading()}>
            <p role="status" class="text-sm text-ink-muted">
              Loading chat…
            </p>
          </Show>
          <Show when={chat.empty()}>
            <p class="px-1 text-sm text-ink-muted">
              Start the conversation. Messages stay with this call.
            </p>
          </Show>
          <Show when={chat.failed()}>
            <p role="alert" class="text-sm text-ink-muted">
              Could not load chat.
            </p>
            <button
              type="button"
              class="mt-2 text-sm text-accent"
              onClick={() => fireAndForget(chat.refresh(), 'channel-call')}
            >
              Retry
            </button>
          </Show>
          <Show when={chat.thread()}>
            {(thread) => (
              <MessageThread
                data={threadListItem(thread())}
                canWrite
                expanded
                hideReplyInput
                monorail
                hideRail
                targetId={messageTarget()?.id}
                targetRequestKey={messageTarget()?.requestKey}
                onClearTarget={() => setMessageTarget(undefined)}
                onReply={({ message, selectedText, renderedText }) => {
                  const handle = input();
                  if (!handle) return;
                  const current = handle.snapshot();
                  handle.restoreSnapshot(
                    {
                      value: buildReplyTargetValue({
                        parent: chat.parent(),
                        message,
                        selectedText,
                        renderedText,
                        existingValue: current.value,
                      }),
                      mentions: current.mentions,
                      attachments: current.attachments,
                    },
                    { cursor: 'trailing-paragraph' }
                  );
                }}
                buildLink={(message) =>
                  buildCallMessageLink(props.callId, message.id)
                }
              />
            )}
          </Show>
        </CallChatBody>
      </StaticMarkdownContext>
    </MessageReferenceNavigation.Provider>
  );
}