Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

4.5 KB
Raw Blame
import { FileResponseInterface } from '@activepieces/shared';
import { javascript } from '@codemirror/lang-javascript';
import ReactCodeMirror, {
  EditorState,
  EditorView,
} from '@uiw/react-codemirror';
import { CodeIcon, Copy } from 'lucide-react';
import React from 'react';
import Markdown from 'react-markdown';
import remarkGfm from 'remark-gfm';

import { CopyButton } from '@/components/custom/clipboard/copy-button';
import { useTheme } from '@/components/providers/theme-provider';
import { Button } from '@/components/ui/button';
import { syntaxTheme } from '@/lib/syntax-theme';
import { cn } from '@/lib/utils';

interface TextMessageProps {
  content: string;
  role: 'user' | 'bot';
  attachments?: FileResponseInterface[];
}

export const TextMessage: React.FC<TextMessageProps> = React.memo(
  ({ content, role }) => {
    const { resolvedTheme } = useTheme();
    const extensions = [
      syntaxTheme.codeMirror(resolvedTheme),
      EditorState.readOnly.of(true),
      EditorView.editable.of(false),
      javascript({ jsx: false, typescript: true }),
    ];

    return (
      <>
        <Markdown
          remarkPlugins={[remarkGfm]}
          className="bg-inherit"
          components={{
            code({ node: _node, inline, className, children, ...props }: any) {
              if (role === 'user') {
                return <div className="font-mono text-sm">{children}</div>;
              }
              const match = /language-(\w+)/.exec(className || '');

              return !inline && match && match[1] ? (
                <div className="relative border rounded-md p-4 pt-12 bg-panel">
                  <ReactCodeMirror
                    value={String(children).trim()}
                    className="border-none"
                    width="100%"
                    minWidth="100%"
                    maxWidth="100%"
                    minHeight="50px"
                    basicSetup={{
                      syntaxHighlighting: true,
                      foldGutter: false,
                      lineNumbers: false,
                      searchKeymap: true,
                      lintKeymap: true,
                      autocompletion: false,
                      highlightActiveLine: false,
                      highlightActiveLineGutter: false,
                      highlightSpecialChars: false,
                      indentOnInput: false,
                      bracketMatching: false,
                      closeBrackets: false,
                    }}
                    lang={match[1]}
                    theme={syntaxTheme.codeMirror(resolvedTheme)}
                    readOnly={true}
                    extensions={extensions}
                  />
                  <div className="absolute top-4 left-5 text-xs text-gray-11">
                    <div className="flex items-center gap-1">
                      <CodeIcon className="size-3" />
                      <span>{match[1]}</span>
                    </div>
                  </div>
                  <CopyCode
                    textToCopy={String(children).trim()}
                    className="absolute top-2 right-2 text-xs text-gray-11"
                  />
                </div>
              ) : (
                <code
                  className={cn(
                    className,
                    'bg-gray-3 px-[6px] py-[2px] rounded-md font-mono text-sm',
                  )}
                  {...props}
                >
                  {String(children).trim()}
                </code>
              );
            },
          }}
        >
          {content}
        </Markdown>
        {role === 'bot' && (
          <CopyButton
            textToCopy={content}
            tooltipSide="bottom"
            size="icon-xs"
            className="mt-2"
          />
        )}
      </>
    );
  },
  (prevProps, nextProps) => {
    return (
      prevProps.content === nextProps.content &&
      prevProps.role === nextProps.role
    );
  },
);
TextMessage.displayName = 'TextMessage';

const CopyCode = ({
  textToCopy,
  className,
}: {
  textToCopy: string;
  className?: string;
}) => {
  const [isCopied, setIsCopied] = React.useState(false);
  return (
    <div className={className}>
      <Button
        variant="ghost"
        className="gap-2"
        size="xs"
        onClick={() => {
          setIsCopied(true);
          navigator.clipboard.writeText(textToCopy);
          setTimeout(() => setIsCopied(false), 1500);
        }}
      >
        <Copy className="size-4" />
        <span className="text-xs">{isCopied ? 'Copied!' : 'Copy Code'}</span>
      </Button>
    </div>
  );
};