import React, { useState } from 'react'; interface JsonViewerProps { data: unknown; title?: string; defaultExpanded?: boolean; maxHeight?: string; searchable?: boolean; } export function JsonViewer({ data, title, defaultExpanded = false, maxHeight = '400px', searchable = false }: JsonViewerProps) { const [expanded, setExpanded] = useState(defaultExpanded); const [searchTerm, setSearchTerm] = useState(''); const [viewMode, setViewMode] = useState<'pretty' | 'raw'>('pretty'); const filteredData = React.useMemo(() => { if (!searchTerm) return data; const filterObject = (obj: unknown): unknown => { if (typeof obj === 'string') { return obj.toLowerCase().includes(searchTerm.toLowerCase()) ? obj : null; } if (Array.isArray(obj)) { const filtered = obj.map(filterObject).filter(item => item !== null); return filtered.length > 0 ? filtered : null; } if (obj && typeof obj === 'object') { const filtered: unknown = {}; let hasMatch = false; for (const [key, value] of Object.entries(obj)) { const filteredValue = filterObject(value); if (filteredValue !== null || key.toLowerCase().includes(searchTerm.toLowerCase())) { filtered[key] = filteredValue !== null ? filteredValue : value; hasMatch = true; } } return hasMatch ? filtered : null; } return obj; }; return filterObject(data); }, [data, searchTerm]); const formatJson = (obj: unknown, indent = 0): React.ReactElement[] => { const elements: React.ReactElement[] = []; const indentStr = ' '.repeat(indent); if (Array.isArray(obj)) { elements.push(
[
); obj.forEach((item, index) => { elements.push(
{formatJson(item, indent + 1)} {index < obj.length - 1 && ,}
); }); elements.push(
{indentStr}]
); } else if (obj && typeof obj === 'object') { elements.push(
{'{'}
); const entries = Object.entries(obj); entries.forEach(([key, value], index) => { const isHighlighted = searchTerm && key.toLowerCase().includes(searchTerm.toLowerCase()); elements.push(
"{key}" : {formatJson(value, indent + 1)} {index < entries.length - 1 && ,}
); }); elements.push(
{indentStr}{'}'}
); } else if (typeof obj === 'string') { const isHighlighted = searchTerm && obj.toLowerCase().includes(searchTerm.toLowerCase()); elements.push( "{obj}" ); } else if (typeof obj === 'number') { elements.push( {obj} ); } else if (typeof obj === 'boolean') { elements.push( {obj.toString()} ); } else if (obj === null) { elements.push( null ); } else { elements.push( {String(obj)} ); } return elements; }; const copyToClipboard = () => { const jsonString = JSON.stringify(filteredData || data, null, 2); void navigator.clipboard.writeText(jsonString).then(() => { // Could add a toast notification here console.log('JSON copied to clipboard'); }); }; return (
{/* Header */}
{data && ( {Array.isArray(data) ? `${data.length} items` : `${Object.keys(data).length} properties`} )}
{expanded && (
{searchable && ( setSearchTerm(e.target.value)} placeholder="Search..." className="px-2 py-1 text-xs border border-gray-300 rounded focus:outline-none focus:ring-1 focus:ring-blue-500" /> )}
)}
{expanded && searchTerm && (
{filteredData ? 'Filtered results shown' : 'No matches found'}
)}
{/* Content */} {expanded && (
{data ? ( viewMode === 'pretty' ? (
{formatJson(filteredData || data)}
) : (
                {JSON.stringify(filteredData || data, null, 2)}
              
) ) : (
No data available
)}
)}
); } // Specialized viewers for different data types export function PersonaViewer({ persona }: { persona: unknown }) { return ( ); } export function ConversationViewer({ conversation }: { conversation: unknown }) { return ( ); } export function ObjectiveViewer({ objective }: { objective: unknown }) { return ( ); } export function GuardrailViewer({ guardrail }: { guardrail: unknown }) { return ( ); }