Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

ExecutionResult.tsx

194 lines5.9 KB

1import {
2 CheckCircle,
3 XCircle,
4 Terminal,
5 ChevronDown,
6 ChevronRight,
7} from 'lucide-react'
8import { useState, useEffect, useRef } from 'react'
9​
10interface ExecutionResultProps {
11 result?: unknown
12 error?: string
13 errorName?: string
14 errorStack?: string
15 logs?: Array<string>
16 status: 'running' | 'success' | 'error'
18​
19export default function ExecutionResult({
20 result,
21 error,
22 errorName,
23 errorStack,
24 logs,
25 status,
26}: ExecutionResultProps) {
27 // Track if user has manually toggled
28 const [userControlled, setUserControlled] = useState(false)
29 const [isCollapsed, setIsCollapsed] = useState(false)
30 const prevStatusRef = useRef(status)
31​
32 // Auto-collapse success only — keep errors expanded so the failure is visible
33 useEffect(() => {
34 let timeoutId: ReturnType<typeof setTimeout> | null = null
35​
36 if (!userControlled) {
37 const wasRunning = prevStatusRef.current === 'running'
38​
39 if (wasRunning && status === 'success') {
40 timeoutId = setTimeout(() => {
41 setIsCollapsed(true)
42 }, 3000)
43 } else if (status === 'running' || status === 'error') {
44 setIsCollapsed(false)
45 }
46 }
47 prevStatusRef.current = status
48​
49 return () => {
50 if (timeoutId) clearTimeout(timeoutId)
51 }
52 }, [status, userControlled])
53​
54 const handleToggle = () => {
55 setUserControlled(true)
56 setIsCollapsed(!isCollapsed)
57 }
58​
59 const hasContent =
60 (logs && logs.length > 0) ||
61 error ||
62 errorName ||
63 errorStack ||
64 result !== undefined
65​
66 return (
67 <div
68 className={`rounded-lg border overflow-hidden ${
69 status === 'error'
70 ? 'bg-red-900/30 border-red-700'
71 : status === 'success'
72 ? 'bg-green-900/30 border-green-700'
73 : 'bg-blue-900/30 border-blue-700'
74 }`}
75 >
76 {/* Header - always visible */}
77 <div
78 className={`flex items-center gap-2 px-4 py-3 ${hasContent ? 'cursor-pointer hover:bg-white/5' : ''}`}
79 onClick={hasContent ? handleToggle : undefined}
80 >
81 {hasContent && (
82 <button className="p-0.5 hover:bg-white/10 rounded transition-colors">
83 {isCollapsed ? (
84 <ChevronRight size={16} className="text-gray-400" />
85 ) : (
86 <ChevronDown size={16} className="text-gray-400" />
87 )}
88 </button>
89 )}
90 {status === 'error' ? (
91 <XCircle className="text-red-400" size={20} />
92 ) : status === 'success' ? (
93 <CheckCircle className="text-green-400" size={20} />
94 ) : (
95 <div className="w-5 h-5 border-2 border-blue-400 border-t-transparent rounded-full animate-spin" />
96 )}
97 <span
98 className={`font-medium ${
99 status === 'error'
100 ? 'text-red-300'
101 : status === 'success'
102 ? 'text-green-300'
103 : 'text-blue-300'
104 }`}
105 >
106 {status === 'error'
107 ? errorName
108 ? `Execution Failed · ${errorName}`
109 : 'Execution Failed'
110 : status === 'success'
111 ? 'Execution Complete'
112 : 'Executing...'}
113 </span>
114 </div>
115​
116 {/* Collapsible content */}
117 {!isCollapsed && hasContent && (
118 <div className="px-4 pb-4 space-y-3">
119 {logs && logs.length > 0 && (
120 <div>
121 <div className="flex items-center gap-2 text-sm text-gray-400 mb-1">
122 <Terminal size={14} />
123 Console Output
124 </div>
125 <div
126 className="bg-gray-950 text-gray-100 rounded p-3 text-sm font-mono max-h-32 overflow-y-auto"
127 style={{
128 scrollbarWidth: 'thin',
129 scrollbarColor: 'rgba(75, 85, 99, 0.5) transparent',
130 }}
131 >
132 {logs.map((log, i) => (
133 <div key={i} className="text-gray-300">
134 {log}
135 </div>
136 ))}
137 </div>
138 </div>
139 )}
140​
141 {(error || errorName) && (
142 <div className="bg-red-900/50 border border-red-700 rounded p-3 text-sm text-red-200 space-y-2">
143 {errorName && (
144 <div>
145 <span className="text-red-400/80 text-xs uppercase tracking-wide">
146 Name
147 </span>
148 <div className="font-mono text-red-100">{errorName}</div>
149 </div>
150 )}
151 {error && (
152 <div>
153 <span className="text-red-400/80 text-xs uppercase tracking-wide">
154 Message
155 </span>
156 <div className="font-mono text-red-100 whitespace-pre-wrap">
157 {error}
158 </div>
159 </div>
160 )}
161 {errorStack && (
162 <div>
163 <span className="text-red-400/80 text-xs uppercase tracking-wide">
164 Stack
165 </span>
166 <pre className="mt-1 font-mono text-[11px] text-red-200/90 whitespace-pre-wrap break-all max-h-48 overflow-y-auto">
167 {errorStack}
168 </pre>
169 </div>
170 )}
171 </div>
172 )}
173​
174 {result !== undefined && status === 'success' && (
175 <div>
176 <div className="text-sm text-gray-400 mb-1">Result:</div>
177 <pre
178 className="bg-gray-950 border border-gray-700 rounded p-3 text-sm text-gray-200 overflow-x-auto max-h-64 overflow-y-auto"
179 style={{
180 scrollbarWidth: 'thin',
181 scrollbarColor: 'rgba(75, 85, 99, 0.5) transparent',
182 }}
183 >
184 {typeof result === 'string'
185 ? result
186 : JSON.stringify(result, null, 2)}
187 </pre>
188 </div>
189 )}
190 </div>
191 )}
192 </div>
193 )