|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { ChevronDown, ChevronRight, Download } from 'lucide-react'; |
| 4 | +import { Badge } from '@/components/ui/badge'; |
| 5 | +import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/ui/collapsible'; |
| 6 | +import { cn } from '@/lib/utils'; |
| 7 | +import { formatLogMessage, formatDataPreview, formatVerboseData, type FormattedLog } from './utils/log-formatter'; |
| 8 | + |
| 9 | +interface LogEntryProps { |
| 10 | + log: FormattedLog; |
| 11 | + isCollapsed: boolean; |
| 12 | + onToggleCollapse: () => void; |
| 13 | +} |
| 14 | + |
| 15 | +export function LogEntry({ log, isCollapsed, onToggleCollapse }: LogEntryProps) { |
| 16 | + const hasVerboseData = log.isVerbose && log.data != null; |
| 17 | + |
| 18 | + return ( |
| 19 | + <div |
| 20 | + className={cn( |
| 21 | + 'text-sm border-l-2 pl-3 py-2 transition-colors', |
| 22 | + log.color, |
| 23 | + 'bg-muted/30 dark:bg-muted/10 hover:bg-muted/50' |
| 24 | + )} |
| 25 | + > |
| 26 | + <div className="flex items-start gap-2"> |
| 27 | + <div className="flex-shrink-0 mt-0.5">{log.icon}</div> |
| 28 | + <div className="flex-1 min-w-0"> |
| 29 | + <div className="flex items-center gap-2 flex-wrap"> |
| 30 | + <span className="text-xs text-muted-foreground font-mono"> |
| 31 | + {log.timestamp} |
| 32 | + </span> |
| 33 | + <Badge variant="outline" className="text-xs px-1.5 py-0"> |
| 34 | + {log.level} |
| 35 | + </Badge> |
| 36 | + <span className="font-medium">{formatLogMessage(log.message, log.data)}</span> |
| 37 | + </div> |
| 38 | + |
| 39 | + {log.progressInfo && ( |
| 40 | + <ProgressInfo progressInfo={log.progressInfo} /> |
| 41 | + )} |
| 42 | + |
| 43 | + {log.data != null && !hasVerboseData && ( |
| 44 | + <div className="mt-2 text-xs font-mono bg-muted/50 p-2 rounded border break-all"> |
| 45 | + {formatDataPreview(log.data)} |
| 46 | + </div> |
| 47 | + )} |
| 48 | + |
| 49 | + {hasVerboseData && log.data != null && ( |
| 50 | + <VerboseDataSection |
| 51 | + data={log.data} |
| 52 | + isCollapsed={isCollapsed} |
| 53 | + onToggle={onToggleCollapse} |
| 54 | + /> |
| 55 | + )} |
| 56 | + </div> |
| 57 | + </div> |
| 58 | + </div> |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +interface ProgressInfoProps { |
| 63 | + progressInfo: FormattedLog['progressInfo']; |
| 64 | +} |
| 65 | + |
| 66 | +function ProgressInfo({ progressInfo }: ProgressInfoProps) { |
| 67 | + if (!progressInfo) return null; |
| 68 | + |
| 69 | + return ( |
| 70 | + <div className="mt-2 flex items-center gap-2 text-xs bg-blue-50 dark:bg-blue-950/20 p-2 rounded border border-blue-200 dark:border-blue-800"> |
| 71 | + <Download className="h-3.5 w-3.5 text-blue-600 dark:text-blue-400" /> |
| 72 | + <div className="flex-1"> |
| 73 | + <div className="font-medium text-blue-900 dark:text-blue-300"> |
| 74 | + {progressInfo.status} |
| 75 | + </div> |
| 76 | + {progressInfo.progress && ( |
| 77 | + <div className="text-blue-700 dark:text-blue-400 mt-0.5"> |
| 78 | + {progressInfo.progress} |
| 79 | + </div> |
| 80 | + )} |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + ); |
| 84 | +} |
| 85 | + |
| 86 | +interface VerboseDataSectionProps { |
| 87 | + data: unknown; |
| 88 | + isCollapsed: boolean; |
| 89 | + onToggle: () => void; |
| 90 | +} |
| 91 | + |
| 92 | +function VerboseDataSection({ data, isCollapsed, onToggle }: VerboseDataSectionProps) { |
| 93 | + return ( |
| 94 | + <Collapsible open={!isCollapsed} onOpenChange={onToggle}> |
| 95 | + <CollapsibleTrigger className="mt-2 flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground"> |
| 96 | + {isCollapsed ? ( |
| 97 | + <ChevronRight className="h-3 w-3" /> |
| 98 | + ) : ( |
| 99 | + <ChevronDown className="h-3 w-3" /> |
| 100 | + )} |
| 101 | + <span> |
| 102 | + {isCollapsed |
| 103 | + ? 'Show verbose output' |
| 104 | + : 'Hide verbose output'} |
| 105 | + </span> |
| 106 | + </CollapsibleTrigger> |
| 107 | + <CollapsibleContent className="mt-2"> |
| 108 | + <div className="text-xs font-mono bg-muted/50 p-3 rounded border overflow-x-auto max-h-96 overflow-y-auto"> |
| 109 | + <pre className="whitespace-pre-wrap break-all"> |
| 110 | + {formatVerboseData(data)} |
| 111 | + </pre> |
| 112 | + </div> |
| 113 | + </CollapsibleContent> |
| 114 | + </Collapsible> |
| 115 | + ); |
| 116 | +} |
| 117 | + |
0 commit comments