Skip to content

The open alternative to Salesforce, designed for AI.

1.7 KB
Raw Blame
import { styled } from '@linaria/react';
import { useState } from 'react';

import { AiChatTab } from '@/ai/components/AiChatTab';
import { AI_CHAT_SURFACE } from '@/ai/constants/AiChatSurface';
import { AiChatSurfaceContext } from '@/ai/contexts/AiChatSurfaceContext';
import { ChatWidgetOpenInChatPageEffect } from '@/page-layout/widgets/chat/components/ChatWidgetOpenInChatPageEffect';
import { ChatWidgetThreadSyncEffect } from '@/page-layout/widgets/chat/components/ChatWidgetThreadSyncEffect';
import { useTargetRecord } from '@/ui/layout/contexts/useTargetRecord';
import { useWorkspaceSurface } from '@/ui/layout/hooks/useWorkspaceSurface';
import { isCurrentPathAiChatPage } from '~/utils/isCurrentPathAiChatPage';

const StyledChatContainer = styled.div`
  --ai-chat-content-max-width: 768px;

  display: flex;
  flex: 1;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  width: 100%;
`;

export const ChatWidget = () => {
  const targetRecord = useTargetRecord();
  const isInSidePanel = useWorkspaceSurface().type === 'side-panel';
  // Checked once so leaving for the chat page later does not pull this chat into it
  const [isOpenedBesideChatPage] = useState(
    () => isInSidePanel && isCurrentPathAiChatPage(),
  );

  if (isOpenedBesideChatPage) {
    return <ChatWidgetOpenInChatPageEffect threadId={targetRecord.id} />;
  }

  return (
    <StyledChatContainer>
      <ChatWidgetThreadSyncEffect threadId={targetRecord.id} />
      <AiChatSurfaceContext.Provider
        value={
          isInSidePanel ? AI_CHAT_SURFACE.SIDE_PANEL : AI_CHAT_SURFACE.PAGE
        }
      >
        <AiChatTab />
      </AiChatSurfaceContext.Provider>
    </StyledChatContainer>
  );
};