File size: 7,927 Bytes
c2ea5ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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 (
    <div className="flex flex-col h-[70vh]">
      {/* Header */}
      <div className="px-6 py-4 border-b bg-muted/50">
        <div className="flex items-center justify-between mb-2">
          <div className="flex items-center gap-2 min-w-0 flex-1">
            <FileText className="h-5 w-5 flex-shrink-0" />
            <h2
              className="text-lg font-semibold truncate"
              title={trace.filename}
            >
              {trace.filename}
            </h2>
            <Badge variant="outline" className="flex-shrink-0">
              Window {segment.windowIndex}
            </Badge>
          </div>
          <div className="flex items-center gap-2">
            <Button
              variant={viewMode === "editor" ? "default" : "outline"}
              size="sm"
              onClick={() => setViewMode("editor")}
            >
              <Code className="h-4 w-4 mr-2" />
              Editor
            </Button>
            <Button
              variant={viewMode === "text" ? "default" : "outline"}
              size="sm"
              onClick={() => setViewMode("text")}
            >
              <Type className="h-4 w-4 mr-2" />
              Text
            </Button>
            <Button
              variant={viewMode === "textarea" ? "default" : "outline"}
              size="sm"
              onClick={() => setViewMode("textarea")}
            >
              <Type className="h-4 w-4 mr-2" />
              Textarea
            </Button>
            <Button variant="outline" size="sm" onClick={handleDownload}>
              <Download className="h-4 w-4 mr-2" />
              Download
            </Button>
          </div>
        </div>

        {/* Segment Info */}
        <div className="flex items-center gap-4 text-sm text-muted-foreground">
          <span>{segment.content.length.toLocaleString()} characters</span>
          <span>{lineCount.toLocaleString()} lines</span>
          <span>{wordCount.toLocaleString()} words</span>
          <Separator orientation="vertical" className="h-4" />
          <span>
            Characters {segment.startChar.toLocaleString()} -{" "}
            {segment.endChar.toLocaleString()}
          </span>
        </div>

        {/* Debug Info */}
        <div className="mt-2 p-2 bg-gray-100 rounded text-xs text-gray-600">
          <strong>Debug:</strong> Content length: {segment.content.length},
          Preview: "{segment.content.substring(0, 50)}..."
        </div>
      </div>

      {/* Content Area */}
      <div className="flex-1 overflow-hidden bg-white border border-gray-200 rounded m-2">
        {segment.content ? (
          viewMode === "editor" ? (
            <div className="h-full">
              <Editor
                height="100%"
                defaultLanguage={detectedLanguage}
                value={segment.content}
                theme="vs"
                loading={
                  <div className="p-4 text-center">Loading editor...</div>
                }
                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,
                }}
              />
            </div>
          ) : viewMode === "text" ? (
            <div className="p-4 overflow-auto h-full">
              <pre className="text-sm font-mono whitespace-pre-wrap break-words">
                {segment.content}
              </pre>
            </div>
          ) : (
            <div className="p-4 h-full">
              <textarea
                value={segment.content}
                readOnly
                className="w-full h-full p-2 border border-gray-300 rounded font-mono text-sm resize-none focus:outline-none focus:ring-2 focus:ring-blue-500"
                placeholder="Content will appear here..."
              />
            </div>
          )
        ) : (
          <div className="p-8 text-center text-gray-500">
            <p>No content available for this segment</p>
            <p className="text-sm mt-2">
              Characters: {segment.startChar} - {segment.endChar}
            </p>
          </div>
        )}
      </div>
    </div>
  );
}