import React, { useMemo, useState } from "react"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { Separator } from "@/components/ui/separator"; import { FileText, Download, Code, Type } from "lucide-react"; import { Trace } from "@/types"; import Editor from "@monaco-editor/react"; interface TraceSegmentModalProps { data: { trace: Trace; segment: { content: string; startChar: number; endChar: number; windowIndex: number; }; }; onClose: () => void; } export function TraceSegmentModal({ data, onClose: _onClose, }: TraceSegmentModalProps) { const { trace, segment } = data; const [viewMode, setViewMode] = useState<"editor" | "text" | "textarea">( "textarea" ); // Debug logging console.log("TraceSegmentModal data:", { trace, segment }); console.log("Segment content preview:", segment.content.substring(0, 200)); // Content analysis const lineCount = segment.content.split("\n").length; const wordCount = segment.content.trim() ? segment.content.trim().split(/\s+/).length : 0; // Language detection for syntax highlighting const detectedLanguage = useMemo(() => { // Check for JSON content if ( segment.content.trim().startsWith("{") && segment.content.trim().endsWith("}") ) { try { JSON.parse(segment.content); return "json"; } catch { // Not valid JSON, continue checking } } // Check for Python code patterns if (/^(import|from|def|class|if __name__|print\()/m.test(segment.content)) { return "python"; } // Check for JavaScript/TypeScript patterns if ( /^(import|export|function|const|let|var|console\.log)/m.test( segment.content ) ) { return "javascript"; } // Check for shell/bash patterns if ( /^(#!\/bin\/|cd |ls |mkdir |rm |pip install|npm install)/m.test( segment.content ) ) { return "shell"; } // Check for SQL patterns if ( /^(SELECT|INSERT|UPDATE|DELETE|CREATE|DROP|ALTER)/im.test(segment.content) ) { return "sql"; } // Default to plain text return "text"; }, [segment.content]); const handleDownload = () => { const blob = new Blob([segment.content], { type: "text/plain" }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = `${trace.filename}_window_${segment.windowIndex}_segment.txt`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); }; return (
{/* Header */}

{trace.filename}

Window {segment.windowIndex}
{/* Segment Info */}
{segment.content.length.toLocaleString()} characters {lineCount.toLocaleString()} lines {wordCount.toLocaleString()} words Characters {segment.startChar.toLocaleString()} -{" "} {segment.endChar.toLocaleString()}
{/* Debug Info */}
Debug: Content length: {segment.content.length}, Preview: "{segment.content.substring(0, 50)}..."
{/* Content Area */}
{segment.content ? ( viewMode === "editor" ? (
Loading editor...
} onMount={(editor) => { console.log("Monaco Editor mounted"); console.log( "Editor content length:", editor.getValue().length ); console.log( "Editor content preview:", editor.getValue().substring(0, 200) ); // Force a layout update setTimeout(() => { editor.layout(); }, 100); }} options={{ readOnly: true, minimap: { enabled: false }, scrollBeyondLastLine: false, wordWrap: "on", lineNumbers: "on", folding: false, renderWhitespace: "selection", fontSize: 14, lineHeight: 20, fontFamily: "'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', monospace", padding: { top: 10, bottom: 10 }, automaticLayout: true, contextmenu: false, selectOnLineNumbers: true, }} />
) : viewMode === "text" ? (
                {segment.content}
              
) : (