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

3.3 KB
Raw Blame
import { t } from 'i18next';
import { Copy, Check } from 'lucide-react';
import React, { useState } from 'react';
import ReactJson from 'react-json-view';
import { toast } from 'sonner';

import { useTheme } from '@/components/providers/theme-provider';
import { Button } from '@/components/ui/button';
import { syntaxTheme } from '@/lib/syntax-theme';

interface SimpleJsonViewerProps {
  data: any;
  readOnly?: boolean;
  hideCopyButton?: boolean;
  maxHeight?: number;
  fontSize?: string;
}

export const SimpleJsonViewer: React.FC<SimpleJsonViewerProps> = ({
  data,
  readOnly = true,
  hideCopyButton = false,
  maxHeight = 400,
  fontSize = '14px',
}) => {
  const [copied, setCopied] = useState(false);
  const { resolvedTheme } = useTheme();

  const formattedJson =
    typeof data === 'string' ? data : JSON.stringify(data, null, 2);

  const handleCopy = () => {
    navigator.clipboard.writeText(formattedJson);
    setCopied(true);
    toast.success(t('Copied to clipboard'), {
      duration: 1000,
    });

    setTimeout(() => {
      setCopied(false);
    }, 3000);
  };

  const viewerTheme = syntaxTheme.jsonView(resolvedTheme);

  return (
    <div
      className="w-full relative text-gray-12 overflow-hidden"
      style={{
        maxWidth: '100%',
      }}
    >
      {!hideCopyButton && (
        <div className="absolute top-2 right-5 z-10">
          <Button
            variant="ghost"
            size="sm"
            onClick={handleCopy}
            className="p-0 text-accent-11 hover:bg-transparent"
          >
            {copied ? (
              <Check className="w-4 h-4 text-success-11" />
            ) : (
              <Copy className="w-4 h-4 text-gray-12" />
            )}
          </Button>
        </div>
      )}
      <div
        className="p-2"
        style={{
          maxHeight: typeof maxHeight === 'number' ? `${maxHeight}px` : '400px',
          overflow: 'auto',
          overflowX: 'auto',
          width: '100%',
          boxSizing: 'border-box',
        }}
      >
        {typeof data === 'string' ? (
          <pre
            className="whitespace-pre-wrap break-all overflow-x-auto p-2"
            style={{ fontSize }}
          >
            {data}
          </pre>
        ) : (
          <div style={{ minWidth: 0, width: '100%', height: '100%' }}>
            <ReactJson
              style={{
                overflowX: 'auto',
                padding: '0.5rem',
                backgroundColor: 'transparent',
                fontSize,
                width: '100%',
                minWidth: 0,
                boxSizing: 'border-box',
                wordBreak: 'break-word',
                whiteSpace: 'pre-wrap',
              }}
              theme={viewerTheme}
              enableClipboard={false}
              groupArraysAfterLength={20}
              displayDataTypes={false}
              name={false}
              quotesOnKeys={false}
              src={data}
              collapsed={false}
              displayObjectSize={false}
              iconStyle="triangle"
              shouldCollapse={false}
              onEdit={readOnly ? false : undefined}
              onAdd={readOnly ? false : undefined}
              onDelete={readOnly ? false : undefined}
            />
          </div>
        )}
      </div>
    </div>
  );
};