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

7.4 KB
Raw Blame
import { MarkdownVariant } from '@activepieces/shared';
import { useMutation } from '@tanstack/react-query';
import { t } from 'i18next';
import { Check, Copy, Info, AlertTriangle, Lightbulb } from 'lucide-react';
import React, { useState } from 'react';
import ReactMarkdown from 'react-markdown';
import breaks from 'remark-breaks';
import gfm from 'remark-gfm';
import { toast } from 'sonner';

import { cn } from '@/lib/utils';

import { Alert, AlertDescription } from '../ui/alert';
import { Button } from '../ui/button';

function applyVariables(markdown: string, variables: Record<string, string>) {
  if (typeof markdown !== 'string') {
    return '';
  }
  let result = markdown.split('<br>').join('\n');
  result = result.replace(/\{\{(.*?)\}\}/g, (_, variableName) => {
    return variables[variableName] ?? '';
  });
  return result;
}

type MarkdownProps = {
  markdown: string | undefined;
  variables?: Record<string, string>;
  variant?: MarkdownVariant;
  className?: string;
  loading?: string;
};

const Container = ({
  variant,
  children,
}: {
  variant?: MarkdownVariant;
  children: React.ReactNode;
}) => {
  return (
    <Alert
      className={cn('rounded-md border', {
        'bg-warning-3 border-none text-warning-11':
          variant === MarkdownVariant.WARNING,
        'bg-success-3 text-success-11 border-none':
          variant === MarkdownVariant.TIP,
        'p-0 bg-transparent border-none':
          variant === MarkdownVariant.BORDERLESS,
      })}
    >
      {variant !== MarkdownVariant.BORDERLESS && (
        <>
          {(variant === MarkdownVariant.INFO || variant === undefined) && (
            <Info className="w-4 h-4 mt-1" />
          )}
          {variant === MarkdownVariant.WARNING && (
            <AlertTriangle className="w-4 h-4 mt-1 stroke-warning-11" />
          )}
          {variant === MarkdownVariant.TIP && (
            <Lightbulb className="w-4 h-4 mt-1" />
          )}
        </>
      )}
      <AlertDescription className="grow w-full min-w-0">
        {children}
      </AlertDescription>
    </Alert>
  );
};

const ApMarkdown = React.memo(
  ({ markdown, variables, variant, className, loading }: MarkdownProps) => {
    const [copiedText, setCopiedText] = useState<string | null>(null);

    const { mutate: copyToClipboard } = useMutation({
      mutationFn: async (text: string) => {
        await navigator.clipboard.writeText(text);
        setCopiedText(text);
        await new Promise((resolve) => setTimeout(resolve, 1000));
        setCopiedText(null);
      },
      onError: () => {
        toast.error(t('Failed to copy to clipboard'), {
          duration: 3000,
        });
      },
    });

    if (loading && loading.length > 0) {
      return (
        <Container variant={variant}>
          <div className="flex items-center gap-2">{loading}</div>
        </Container>
      );
    }

    if (!markdown) {
      return null;
    }

    const markdownProcessed = applyVariables(markdown, variables ?? {});

    return (
      <Container variant={variant}>
        <ReactMarkdown
          className={cn('grow w-full ', className)}
          remarkPlugins={[gfm, breaks]}
          components={{
            code({ node: _node, ref: _ref, ...props }) {
              const isLanguageText = props.className?.includes('language-text');
              if (!isLanguageText) {
                return <code {...props} className="text-wrap" />;
              }
              const codeContent = String(props.children).trim();
              const isCopying = codeContent === copiedText;
              return (
                <div className="relative flex w-full max-w-full items-center gap-1 rounded-md border border-solid bg-gray-1 p-1.5 text-sm">
                  <code
                    data-testid="markdown-code-block"
                    className="grow min-w-0 whitespace-pre-wrap break-all select-all px-1 py-1.5 font-mono text-sm"
                  >
                    {codeContent}
                  </code>
                  <Button
                    variant="ghost"
                    size="icon-sm"
                    className="bg-gray-1"
                    onClick={() => copyToClipboard(codeContent)}
                  >
                    {isCopying ? (
                      <Check className="w-3 h-3" />
                    ) : (
                      <Copy className="w-3 h-3" />
                    )}
                  </Button>
                </div>
              );
            },
            h1: ({ node: _node, ref: _ref, ...props }) => (
              <h1
                className="scroll-m-20 text-xl font-semibold tracking-tight lg:text-3xl"
                {...props}
              />
            ),
            h2: ({ node: _node, ref: _ref, ...props }) => (
              <h2
                className="scroll-m-20 text-lg text-xl font-semibold tracking-tight first:mt-0"
                {...props}
              />
            ),
            h3: ({ node: _node, ref: _ref, ...props }) => (
              <h3
                className="scroll-m-20 text-lg font-semibold tracking-tight"
                {...props}
              />
            ),
            p: ({ node: _node, ref: _ref, ...props }) => (
              <p
                className="leading-5 first-of-type:mt-1 not-first-of-type:mt-2 w-full mb-2 break-words"
                {...props}
              />
            ),
            ul: ({ node: _node, ref: _ref, ...props }) => (
              <ul className="mt-4 ml-6 list-disc [&>li]:mt-2" {...props} />
            ),
            ol: ({ node: _node, ref: _ref, ...props }) => (
              <ol className="mt-4 ml-6 list-decimal [&>li]:mt-2" {...props} />
            ),
            li: ({ node: _node, ref: _ref, ...props }) => <li {...props} />,
            a: ({ node: _node, ref: _ref, ...props }) => (
              <a
                className="font-medium text-accent-11 underline underline-offset-4"
                target="_blank"
                rel="noreferrer noopener"
                {...props}
              />
            ),
            blockquote: ({ node: _node, ref: _ref, ...props }) => (
              <blockquote
                className="mt-4 first:mt-0 border-l-2 pl-6 italic"
                {...props}
              />
            ),
            hr: ({ node: _node, ref: _ref, ...props }) => (
              <hr className="my-4 border-t border-gray-6/50" {...props} />
            ),
            img: ({ node: _node, ref: _ref, ...props }) => (
              <img className="my-8" {...props} />
            ),
            b: ({ node: _node, ref: _ref, ...props }) => <b {...props} />,
            em: ({ node: _node, ref: _ref, ...props }) => <em {...props} />,
            table: ({ node: _node, ref: _ref, ...props }) => (
              <table className="w-full my-4 border-collapse" {...props} />
            ),
            thead: ({ node: _node, ref: _ref, ...props }) => (
              <thead className="bg-gray-3" {...props} />
            ),
            tr: ({ node: _node, ref: _ref, ...props }) => (
              <tr className="border-b border-gray-6" {...props} />
            ),
            th: ({ node: _node, ref: _ref, ...props }) => (
              <th className="text-left p-2 font-medium" {...props} />
            ),
            td: ({ node: _node, ref: _ref, ...props }) => (
              <td className="p-2" {...props} />
            ),
          }}
        >
          {markdownProcessed.trim()}
        </ReactMarkdown>
      </Container>
    );
  },
);

ApMarkdown.displayName = 'ApMarkdown';
export { ApMarkdown };