File size: 25,278 Bytes
4efde5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
import React, { useState, useMemo, useCallback } from 'react';
import { 
  FolderOpen, 
  Folder,
  File, 
  ChevronRight, 
  ChevronDown, 
  FileJson, 
  FileText,
  FolderTree,
  Code2,
  Package,
  Loader2,
  ExternalLink,
  FileCode,
  Maximize2,
  Copy,
  Check,
  Globe,
  Play
} from 'lucide-react';
import { ToolViewProps } from '../types';
import { getToolTitle, normalizeContentToString, extractToolData } from '../utils';
import { cn } from '@/lib/utils';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Badge } from '@/components/ui/badge';
import { ScrollArea } from "@/components/ui/scroll-area";
import { LoadingState } from '../shared/LoadingState';
import { Button } from '@/components/ui/button';
import { CheckCircle, AlertTriangle } from 'lucide-react';
import { CodeBlockCode } from '@/components/ui/code-block';
import { getLanguageFromFileName } from '../file-operation/_utils';
import { constructHtmlPreviewUrl } from '@/lib/utils/url';
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog";
import { toast } from 'sonner';
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from "@/components/ui/tooltip";

interface FileNode {
  name: string;
  path: string;
  type: 'file' | 'directory';
  children?: FileNode[];
  expanded?: boolean;
}

interface ProjectData {
  projectName: string;
  structure: FileNode;
  packageInfo?: string;
  rawStructure?: string;
  isNextJs?: boolean;
  isReact?: boolean;
  isVite?: boolean;
}

function parseFileStructure(flatList: string[]): FileNode {
  const root: FileNode = {
    name: '.',
    path: '.',
    type: 'directory',
    children: [],
    expanded: true
  };

  const paths = flatList
    .map(line => line.trim())
    .filter(line => line && line !== '.')
    .map(line => line.startsWith('./') ? line.substring(2) : line);

  const pathSet = new Set(paths);
  const directories = new Set<string>();
  paths.forEach(path => {
    const parts = path.split('/');
    for (let i = 1; i < parts.length; i++) {
      const parentPath = parts.slice(0, i).join('/');
      directories.add(parentPath);
    }
  });

  const nodeMap = new Map<string, FileNode>();
  nodeMap.set('.', root);

  paths.sort();

  paths.forEach(path => {
    const parts = path.split('/');
    let currentPath = '';
    let parent = root;

    parts.forEach((part, index) => {
      currentPath = currentPath ? `${currentPath}/${part}` : part;
      
      if (!nodeMap.has(currentPath)) {
        const isDirectory = directories.has(currentPath) || index < parts.length - 1;
        const node: FileNode = {
          name: part,
          path: currentPath,
          type: isDirectory ? 'directory' : 'file',
          children: isDirectory ? [] : undefined,
          expanded: false
        };
        
        if (!parent.children) parent.children = [];
        parent.children.push(node);
        nodeMap.set(currentPath, node);
      }
      
      if (nodeMap.get(currentPath)?.type === 'directory') {
        parent = nodeMap.get(currentPath)!;
      }
    });
  });

  const sortChildren = (node: FileNode) => {
    if (node.children) {
      node.children.sort((a, b) => {
        if (a.type !== b.type) {
          return a.type === 'directory' ? -1 : 1;
        }
        return a.name.localeCompare(b.name);
      });
      node.children.forEach(sortChildren);
    }
  };
  sortChildren(root);

  return root;
}

function getFileIcon(fileName: string): React.ReactNode {
  const ext = fileName.split('.').pop()?.toLowerCase();
  
  if (fileName === 'package.json') {
    return <FileJson className="w-4 h-4 text-green-600 dark:text-green-400" />;
  }
  
  switch (ext) {
    case 'ts':
    case 'tsx':
      return <FileCode className="w-4 h-4 text-blue-600 dark:text-blue-400" />;
    case 'js':
    case 'jsx':
      return <FileCode className="w-4 h-4 text-yellow-600 dark:text-yellow-400" />;
    case 'json':
      return <FileCode className="w-4 h-4 text-gray-600 dark:text-gray-400" />;
    case 'css':
    case 'scss':
      return <FileCode className="w-4 h-4 text-pink-600 dark:text-pink-400" />;
    case 'md':
      return <FileText className="w-4 h-4 text-gray-600 dark:text-gray-400" />;
    case 'html':
      return <FileCode className="w-4 h-4 text-orange-600 dark:text-orange-400" />;
    default:
      return <File className="w-4 h-4 text-gray-500 dark:text-gray-400" />;
  }
}

const FileTreeNode: React.FC<{
  node: FileNode;
  level: number;
  onFileClick: (path: string) => void;
  selectedFile?: string;
  onToggle: (path: string) => void;
}> = ({ node, level, onFileClick, selectedFile, onToggle }) => {
  const isSelected = selectedFile === node.path;

  const handleClick = () => {
    if (node.type === 'file') {
      onFileClick(node.path);
    } else {
      onToggle(node.path);
    }
  };

  if (node.name === '.') {
    return (
      <>
        {node.children?.map((child) => (
          <FileTreeNode
            key={child.path}
            node={child}
            level={0}
            onFileClick={onFileClick}
            selectedFile={selectedFile}
            onToggle={onToggle}
          />
        ))}
      </>
    );
  }

  return (
    <>
      <div
        className={cn(
          "flex items-center gap-1 px-2 py-1 hover:bg-zinc-100 dark:hover:bg-zinc-800 cursor-pointer transition-colors",
          isSelected && "bg-muted",
        )}
        style={{ paddingLeft: `${level * 12 + 8}px` }}
        onClick={handleClick}
      >
        {node.type === 'directory' && (
          node.expanded ? 
            <ChevronDown className="w-3 h-3 text-zinc-500" /> : 
            <ChevronRight className="w-3 h-3 text-zinc-500" />
        )}
        
        {node.type === 'directory' ? (
          node.expanded ? 
            <FolderOpen className="w-4 h-4 text-zinc-600 dark:text-zinc-400" /> :
            <Folder className="w-4 h-4 text-zinc-600 dark:text-zinc-400" />
        ) : (
          getFileIcon(node.name)
        )}
        
        <span className="text-sm text-zinc-700 dark:text-zinc-300 truncate">
          {node.name}
        </span>
      </div>
      
      {node.type === 'directory' && node.expanded && node.children && (
        <>
          {node.children.map((child) => (
            <FileTreeNode
              key={child.path}
              node={child}
              level={level + 1}
              onFileClick={onFileClick}
              selectedFile={selectedFile}
              onToggle={onToggle}
            />
          ))}
        </>
      )}
    </>
  );
};

const FileExplorer: React.FC<{
  fileTree: FileNode | null;
  projectData: ProjectData | null;
  selectedFile?: string;
  expandedNodes: Set<string>;
  onToggle: (path: string) => void;
  onFileClick: (path: string) => void;
  fileContent: string;
  loadingFile: boolean;
  previewUrl?: string;
  language: string;
}> = ({ 
  fileTree, 
  projectData, 
  selectedFile, 
  expandedNodes, 
  onToggle, 
  onFileClick, 
  fileContent, 
  loadingFile,
  previewUrl,
  language 
}) => {
  const [isCopying, setIsCopying] = useState(false);

  const handleCopyCode = async () => {
    if (!fileContent) return;
    
    try {
      await navigator.clipboard.writeText(fileContent);
      setIsCopying(true);
      toast.success('Code copied to clipboard');
      setTimeout(() => setIsCopying(false), 2000);
    } catch (err) {
      toast.error('Failed to copy code');
    }
  };

  return (
    <div className="flex h-full">
      <div className="w-56 border-r border-zinc-200 dark:border-zinc-800 flex flex-col">
        <div className="p-2 border-b border-zinc-200 dark:border-zinc-800">
          <div className="flex items-center gap-2 px-2">
            <Package className="w-4 h-4 text-zinc-500" />
            <span className="text-sm font-medium text-zinc-700 dark:text-zinc-300">
              Explorer
            </span>
            {projectData && (
              <span className="text-xs text-zinc-500 dark:text-zinc-400 ml-auto">
                {projectData.projectName}
              </span>
            )}
          </div>
        </div>
        <ScrollArea className="flex-1">
          <div className="py-1">
            {fileTree && (
              <FileTreeNode
                node={fileTree}
                level={0}
                onFileClick={onFileClick}
                selectedFile={selectedFile}
                onToggle={onToggle}
              />
            )}
          </div>
        </ScrollArea>
      </div>

      <div className="flex-1 flex flex-col overflow-hidden">
        {selectedFile ? (
          <>
            <div className="p-1 px-2 border-b border-zinc-200 dark:border-zinc-800 flex items-center justify-between">
              <div className="flex items-center gap-2">
                <Code2 className="w-4 h-4 text-zinc-500" />
                <span className="text-xs text-zinc-700 dark:text-zinc-300">
                  {selectedFile}
                </span>
              </div>
              <div className="flex items-center gap-1">
                {fileContent && (
                  <Button
                    size="sm"
                    variant="ghost"
                    className="h-7 text-xs"
                    onClick={handleCopyCode}
                    disabled={isCopying}
                  >
                    {isCopying ? (
                      <Check className="w-3 h-3 text-green-500" />
                    ) : (
                      <Copy className="w-3 h-3" />
                    )}
                    {isCopying ? 'Copied' : 'Copy'}
                  </Button>
                )}
                {previewUrl && (
                  <Button
                    size="sm"
                    variant="ghost"
                    className="h-7 text-xs"
                    asChild
                  >
                    <a href={previewUrl} target="_blank" rel="noopener noreferrer">
                      <ExternalLink className="w-3 h-3 mr-1" />
                      Preview
                    </a>
                  </Button>
                )}
              </div>
            </div>
            
            <div className="flex-1 overflow-hidden">
              {loadingFile ? (
                <div className="flex items-center justify-center h-full p-8">
                  <Loader2 className="w-6 h-6 animate-spin text-zinc-400" />
                </div>
              ) : fileContent ? (
                <ScrollArea className="h-full w-full">
                  <div>
                    <CodeBlockCode 
                      code={fileContent} 
                      language={language}
                      className="text-xs h-full"
                    />
                  </div>
                </ScrollArea>
              ) : (
                <div className="flex items-center justify-center h-full p-8 text-zinc-400">
                  <p className="text-sm">Unable to load file content</p>
                </div>
              )}
            </div>
          </>
        ) : (
          <div className="flex-1 flex items-center justify-center text-zinc-400">
            <div className="text-center">
              <File className="w-12 h-12 mx-auto mb-2 opacity-50" />
              <p className="text-sm">Select a file to view its contents</p>
            </div>
          </div>
        )}
      </div>
    </div>
  );
};

function extractProjectData(assistantContent: any, toolContent: any): ProjectData | null {
  const toolData = extractToolData(toolContent);
  
  let outputStr: string | null = null;
  let projectName: string | null = null;
  
  if (toolData.toolResult) {
    outputStr = toolData.toolResult.toolOutput;
    projectName = toolData.arguments?.project_name || null;
  }
  
  if (!outputStr) {
    outputStr = normalizeContentToString(toolContent) || normalizeContentToString(assistantContent);
  }
  
  if (!outputStr) return null;

  if (!projectName) {
    const projectNameMatch = outputStr.match(/Project structure for '([^']+)':/);
    projectName = projectNameMatch ? projectNameMatch[1] : 'Unknown Project';
  } else {
    projectName = projectName as string;
  }

  const lines = outputStr.split('\n');
  const structureLines: string[] = [];
  let packageInfo = '';
  let inPackageInfo = false;

  for (const line of lines) {
    if (line.includes('Project structure for')) {
      continue;
    }
    if (line.includes('Package.json info:') || line.includes('📋 Package.json info:')) {
      inPackageInfo = true;
      continue;
    }
    if (line.includes('To run this project:')) {
      break;
    }
    
    if (!inPackageInfo && line.trim() && !line.includes('📁')) {
      structureLines.push(line);
    }
    if (inPackageInfo) {
      packageInfo += line + '\n';
    }
  }

  const structure = parseFileStructure(structureLines);

  // Detect project type from package.json info
  const isNextJs = packageInfo.includes('"next"');
  const isReact = packageInfo.includes('"react"') || packageInfo.includes('react-scripts');
  const isVite = packageInfo.includes('"vite"');

  return {
    projectName,
    structure,
    packageInfo: packageInfo.trim(),
    rawStructure: structureLines.join('\n'),
    isNextJs,
    isReact,
    isVite
  };
}

// Helper function to construct preview URL
function getProjectPreviewUrl(project: any, projectName: string): string | null {
  // Check if there's an exposed port for this project
  // This would typically come from the project's sandbox configuration
  // For now, we'll construct a URL based on common patterns
  
  if (project?.sandbox?.exposed_ports) {
    // Look for common dev server ports (3000, 5173, 8080, etc.)
    const commonPorts = [3000, 3001, 5173, 5174, 8080, 8000, 4200];
    const exposedPort = project.sandbox.exposed_ports.find((p: any) => 
      commonPorts.includes(p.port)
    );
    
    if (exposedPort) {
      return exposedPort.url;
    }
  }
  
  // If sandbox has a base URL, construct preview URL
  if (project?.sandbox?.sandbox_url) {
    // Try common dev server ports
    return `${project.sandbox.sandbox_url}:3000`;
  }
  
  return null;
}

export function GetProjectStructureView({
  name = 'get_project_structure',
  assistantContent,
  toolContent,
  assistantTimestamp,
  toolTimestamp,
  isSuccess = true,
  isStreaming = false,
  project,
}: ToolViewProps) {
  const [selectedFile, setSelectedFile] = useState<string | undefined>();
  const [fileContent, setFileContent] = useState<string>('');
  const [loadingFile, setLoadingFile] = useState(false);
  const [expandedNodes, setExpandedNodes] = useState<Set<string>>(new Set(['.', 'src']));
  const [isDialogOpen, setIsDialogOpen] = useState(false);
  
  const projectData = useMemo(() => 
    extractProjectData(assistantContent, toolContent), 
    [assistantContent, toolContent]
  );

  const toolTitle = getToolTitle(name);

  const updateNodeExpanded = useCallback((node: FileNode): FileNode => {
    return {
      ...node,
      expanded: expandedNodes.has(node.path),
      children: node.children?.map(updateNodeExpanded)
    };
  }, [expandedNodes]);

  const fileTree = useMemo(() => {
    if (!projectData) return null;
    return updateNodeExpanded(projectData.structure);
  }, [projectData, updateNodeExpanded]);

  const handleToggle = useCallback((path: string) => {
    setExpandedNodes(prev => {
      const next = new Set(prev);
      if (next.has(path)) {
        next.delete(path);
      } else {
        next.add(path);
      }
      return next;
    });
  }, []);

  const handleFileClick = useCallback(async (filePath: string) => {
    if (!project?.sandbox?.sandbox_url || !projectData) return;
    
    setSelectedFile(filePath);
    setLoadingFile(true);
    setFileContent('');

    try {
      const fileUrl = constructHtmlPreviewUrl(
        project.sandbox.sandbox_url,
        `${projectData.projectName}/${filePath}`
      );

      if (fileUrl) {
        const response = await fetch(fileUrl);
        if (response.ok) {
          const content = await response.text();
          setFileContent(content);
        } else {
          setFileContent('// Failed to load file content');
        }
      } else {
        setFileContent('// Unable to construct file URL');
      }
    } catch (error) {
      console.error('Error loading file:', error);
      setFileContent('// Error loading file content');
    } finally {
      setLoadingFile(false);
    }
  }, [project, projectData]);

  const isHtmlFile = selectedFile?.endsWith('.html');
  const previewUrl = isHtmlFile && project?.sandbox?.sandbox_url && projectData
    ? constructHtmlPreviewUrl(
        project.sandbox.sandbox_url,
        `${projectData.projectName}/${selectedFile}`
      )
    : undefined;

  const language = selectedFile ? getLanguageFromFileName(selectedFile) : 'plaintext';

  // Get the project preview URL if available
  const projectPreviewUrl = projectData ? getProjectPreviewUrl(project, projectData.projectName) : null;

  const handlePreview = () => {
    if (projectPreviewUrl) {
      window.open(projectPreviewUrl, '_blank');
    } else {
      toast.info(
        <div>
          <p className="font-medium">Application not running</p>
          <p className="text-sm text-muted-foreground mt-1">
            For best performance, build and run in production mode:
          </p>
          <div className="text-sm text-muted-foreground mt-1 space-y-1">
            {projectData?.isNextJs && (
              <>
                <code className="bg-muted px-1 rounded block">npm run build</code>
                <code className="bg-muted px-1 rounded block">npm run start</code>
              </>
            )}
            {projectData?.isVite && (
              <>
                <code className="bg-muted px-1 rounded block">npm run build</code>
                <code className="bg-muted px-1 rounded block">npm run preview</code>
              </>
            )}
            {projectData?.isReact && !projectData?.isVite && (
              <>
                <code className="bg-muted px-1 rounded block">npm run build</code>
                <code className="bg-muted px-1 rounded block">npx serve -s build -l 3000</code>
              </>
            )}
            {!projectData?.isNextJs && !projectData?.isReact && !projectData?.isVite && (
              <code className="bg-muted px-1 rounded block">npm run dev</code>
            )}
          </div>
          <p className="text-sm text-muted-foreground mt-1">
            Then use the <code className="bg-muted px-1 rounded">expose_port</code> tool
          </p>
        </div>,
        { duration: 8000 }
      );
    }
  };

  return (
    <>
      <Card className="gap-0 flex border shadow-none border-t border-b-0 border-x-0 p-0 rounded-none flex-col h-full overflow-hidden bg-card">
        <CardHeader className="h-14 bg-zinc-50/80 dark:bg-zinc-900/80 backdrop-blur-sm border-b p-2 px-4 space-y-2">
          <div className="flex flex-row items-center justify-between">
            <div className="flex items-center gap-2">
              <div className="relative p-2 rounded-lg bg-gradient-to-br from-blue-500/20 to-blue-600/10 border border-blue-500/20">
                <FolderTree className="w-5 h-5 text-blue-500 dark:text-blue-400" />
              </div>
              <div>
                <CardTitle className="text-base font-medium text-zinc-900 dark:text-zinc-100">
                  {toolTitle}
                </CardTitle>
              </div>
            </div>

            <div className="flex items-center gap-2">
              <TooltipProvider>
                <Tooltip>
                  <TooltipTrigger asChild>
                    <Button
                      size="sm"
                      variant="ghost"
                      className="h-8 px-2"
                      onClick={handlePreview}
                      disabled={!fileTree || isStreaming}
                    >
                      {projectData?.isNextJs ? (
                        <Play className="h-4 w-4 mr-1" />
                      ) : projectData?.isReact || projectData?.isVite ? (
                        <Globe className="h-4 w-4 mr-1" />
                      ) : (
                        <ExternalLink className="h-4 w-4 mr-1" />
                      )}
                      Preview
                    </Button>
                  </TooltipTrigger>
                  <TooltipContent>
                    <p className="text-xs">
                      {projectPreviewUrl 
                        ? "Open running application" 
                        : "Build and start production server first"
                      }
                    </p>
                  </TooltipContent>
                </Tooltip>
              </TooltipProvider>

              <TooltipProvider>
                <Tooltip>
                  <TooltipTrigger asChild>
                    <Button
                      size="sm"
                      variant="ghost"
                      className="h-8 px-2"
                      onClick={() => setIsDialogOpen(true)}
                      disabled={!fileTree || isStreaming}
                    >
                      <Maximize2 className="h-4 w-4" />
                    </Button>
                  </TooltipTrigger>
                  <TooltipContent>
                    <p className="text-xs">Expand full view</p>
                  </TooltipContent>
                </Tooltip>
              </TooltipProvider>
              
              {!isStreaming && (
                <Badge
                  variant="secondary"
                  className={
                    isSuccess
                      ? "bg-gradient-to-b from-emerald-200 to-emerald-100 text-emerald-700 dark:from-emerald-800/50 dark:to-emerald-900/60 dark:text-emerald-300"
                      : "bg-gradient-to-b from-rose-200 to-rose-100 text-rose-700 dark:from-rose-800/50 dark:to-rose-900/60 dark:text-rose-300"
                  }
                >
                  {isSuccess ? (
                    <CheckCircle className="h-3.5 w-3.5" />
                  ) : (
                    <AlertTriangle className="h-3.5 w-3.5" />
                  )}
                  {isSuccess ? 'Loaded' : 'Failed'}
                </Badge>
              )}
            </div>
          </div>
        </CardHeader>

        <CardContent className="p-0 h-full flex-1 overflow-hidden relative">
          {isStreaming ? (
            <LoadingState
              icon={FolderTree}
              iconColor="text-blue-500 dark:text-blue-400"
              bgColor="bg-gradient-to-b from-blue-100 to-blue-50 shadow-inner dark:from-blue-800/40 dark:to-blue-900/60 dark:shadow-blue-950/20"
              title="Loading project structure"
              filePath={projectData?.projectName || 'Processing...'}
              showProgress={true}
            />
          ) : fileTree ? (
            <FileExplorer
              fileTree={fileTree}
              projectData={projectData}
              selectedFile={selectedFile}
              expandedNodes={expandedNodes}
              onToggle={handleToggle}
              onFileClick={handleFileClick}
              fileContent={fileContent}
              loadingFile={loadingFile}
              previewUrl={previewUrl}
              language={language}
            />
          ) : (
            <div className="flex items-center justify-center h-full">
              <p className="text-zinc-500">No project structure available</p>
            </div>
          )}
        </CardContent>
      </Card>

      <Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}>
        <DialogContent className="max-w-[90vw] w-[90vw] h-[85vh] p-0 gap-0">
          <DialogHeader className="px-6 py-4 border-b flex flex-row items-center justify-between">
            <DialogTitle className="flex items-center gap-2">
              <FolderTree className="w-5 h-5 text-blue-500 dark:text-blue-400" />
              {projectData ? `Project Explorer - ${projectData.projectName}` : 'Project Explorer'}
            </DialogTitle>
            {projectData && (
              <Button
                size="sm"
                variant="outline"
                onClick={handlePreview}
                className="mr-12"
              >
                {projectData.isNextJs ? (
                  <Play className="h-4 w-4 mr-1" />
                ) : (
                  <Globe className="h-4 w-4 mr-1" />
                )}
                Preview Application
              </Button>
            )}
          </DialogHeader>
          <div className="flex-1 h-[calc(85vh-73px)] overflow-hidden">
            {fileTree && (
              <FileExplorer
                fileTree={fileTree}
                projectData={projectData}
                selectedFile={selectedFile}
                expandedNodes={expandedNodes}
                onToggle={handleToggle}
                onFileClick={handleFileClick}
                fileContent={fileContent}
                loadingFile={loadingFile}
                previewUrl={previewUrl}
                language={language}
              />
            )}
          </div>
        </DialogContent>
      </Dialog>
    </>
  );
}