File size: 11,309 Bytes
6cdce85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use client';

import { useState } from 'react';
import {
    CheckCircle2,
    XCircle,
    Clock,
    ChevronDown,
    ChevronUp,
    Terminal,
    AlertTriangle,
    Copy,
    Check,
    Image as ImageIcon,
    Download,
    ZoomIn,
} from 'lucide-react';
import { clsx } from 'clsx';

export interface ExecutionResultData {
    success: boolean;
    output: string;
    error: string;
    executionTime: number;
    hasCircuitOutput?: boolean;
    images?: string[]; // Base64 encoded images
}

interface ExecutionResultProps {
    result: ExecutionResultData;
    isLoading?: boolean;
}

function ImageViewer({ images }: { images: string[] }) {
    const [selectedImage, setSelectedImage] = useState<number | null>(null);

    const handleDownload = (base64: string, index: number) => {
        const link = document.createElement('a');
        link.href = `data:image/png;base64,${base64}`;
        link.download = `quantum_output_${index + 1}.png`;
        link.click();
    };

    return (
        <div className="space-y-3">
            <div className="flex items-center gap-2 text-xs text-zinc-500 mb-2">
                <ImageIcon className="w-3.5 h-3.5" />
                <span>Generated Figures ({images.length})</span>
            </div>

            <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
                {images.map((base64, idx) => (
                    <div
                        key={idx}
                        className="relative group rounded-lg overflow-hidden border border-zinc-700/50 bg-zinc-900"
                    >
                        <img
                            src={`data:image/png;base64,${base64}`}
                            alt={`Output figure ${idx + 1}`}
                            className="w-full h-auto cursor-pointer hover:opacity-90 transition-opacity"
                            onClick={() => setSelectedImage(idx)}
                        />
                        <div className="absolute top-2 right-2 flex gap-1 opacity-0 group-hover:opacity-100 transition-opacity">
                            <button
                                onClick={() => setSelectedImage(idx)}
                                className="p-1.5 rounded bg-zinc-800/90 hover:bg-zinc-700 transition-colors"
                                title="View full size"
                            >
                                <ZoomIn className="w-3.5 h-3.5 text-zinc-300" />
                            </button>
                            <button
                                onClick={() => handleDownload(base64, idx)}
                                className="p-1.5 rounded bg-zinc-800/90 hover:bg-zinc-700 transition-colors"
                                title="Download image"
                            >
                                <Download className="w-3.5 h-3.5 text-zinc-300" />
                            </button>
                        </div>
                    </div>
                ))}
            </div>

            {/* Full-size image modal */}
            {selectedImage !== null && (
                <div
                    className="fixed inset-0 z-50 flex items-center justify-center bg-black/80 backdrop-blur-sm p-4"
                    onClick={() => setSelectedImage(null)}
                >
                    <div className="relative max-w-4xl max-h-[90vh] overflow-auto">
                        <img
                            src={`data:image/png;base64,${images[selectedImage]}`}
                            alt={`Output figure ${selectedImage + 1}`}
                            className="max-w-full h-auto rounded-lg"
                            onClick={(e) => e.stopPropagation()}
                        />
                        <div className="absolute top-2 right-2 flex gap-2">
                            <button
                                onClick={(e) => {
                                    e.stopPropagation();
                                    handleDownload(images[selectedImage], selectedImage);
                                }}
                                className="p-2 rounded-lg bg-zinc-800/90 hover:bg-zinc-700 transition-colors"
                                title="Download image"
                            >
                                <Download className="w-4 h-4 text-zinc-300" />
                            </button>
                            <button
                                onClick={() => setSelectedImage(null)}
                                className="p-2 rounded-lg bg-zinc-800/90 hover:bg-zinc-700 transition-colors"
                                title="Close"
                            >
                                <XCircle className="w-4 h-4 text-zinc-300" />
                            </button>
                        </div>
                    </div>
                </div>
            )}
        </div>
    );
}

export function ExecutionResult({ result, isLoading }: ExecutionResultProps) {
    const [isExpanded, setIsExpanded] = useState(true);
    const [copied, setCopied] = useState(false);

    const hasOutput = result.output.trim().length > 0;
    const hasError = result.error.trim().length > 0;
    const hasImages = result.images && result.images.length > 0;
    const outputToShow = hasError ? result.error : result.output;

    const handleCopy = async () => {
        await navigator.clipboard.writeText(outputToShow);
        setCopied(true);
        setTimeout(() => setCopied(false), 2000);
    };

    if (isLoading) {
        return (
            <div className="mt-3 rounded-lg border border-zinc-700/50 bg-zinc-900/50 overflow-hidden">
                <div className="flex items-center gap-2 px-3 py-2 bg-zinc-800/50 border-b border-zinc-700/50">
                    <div className="w-4 h-4 border-2 border-teal-500/30 border-t-teal-500 rounded-full animate-spin" />
                    <span className="text-xs font-medium text-zinc-400">Executing code...</span>
                </div>
                <div className="p-3">
                    <div className="flex items-center gap-2 text-zinc-500">
                        <Terminal className="w-4 h-4" />
                        <span className="text-sm">Running Python with Qiskit...</span>
                    </div>
                </div>
            </div>
        );
    }

    return (
        <div className={clsx(
            'mt-3 rounded-lg border overflow-hidden transition-all duration-200',
            result.success
                ? 'border-emerald-600/30 bg-emerald-950/20'
                : 'border-red-600/30 bg-red-950/20'
        )}>
            {/* Header */}
            <button
                onClick={() => setIsExpanded(!isExpanded)}
                className={clsx(
                    'w-full flex items-center justify-between px-3 py-2 transition-colors',
                    result.success
                        ? 'bg-emerald-900/30 hover:bg-emerald-900/40'
                        : 'bg-red-900/30 hover:bg-red-900/40'
                )}
            >
                <div className="flex items-center gap-2">
                    {result.success ? (
                        <CheckCircle2 className="w-4 h-4 text-emerald-400" />
                    ) : (
                        <XCircle className="w-4 h-4 text-red-400" />
                    )}
                    <span className={clsx(
                        'text-xs font-medium',
                        result.success ? 'text-emerald-300' : 'text-red-300'
                    )}>
                        {result.success ? 'Execution Successful' : 'Execution Failed'}
                    </span>

                    <span className="flex items-center gap-1 text-xs text-zinc-500 ml-2">
                        <Clock className="w-3 h-3" />
                        {result.executionTime}ms
                    </span>

                    {hasImages && (
                        <span className="flex items-center gap-1 text-xs text-teal-400 ml-2">
                            <ImageIcon className="w-3 h-3" />
                            {result.images?.length} figure{result.images?.length !== 1 ? 's' : ''}
                        </span>
                    )}
                </div>

                <div className="flex items-center gap-2">
                    {(hasOutput || hasError || hasImages) && (
                        <span className="text-xs text-zinc-500">
                            {isExpanded ? 'Hide' : 'Show'} output
                        </span>
                    )}
                    {isExpanded ? (
                        <ChevronUp className="w-4 h-4 text-zinc-500" />
                    ) : (
                        <ChevronDown className="w-4 h-4 text-zinc-500" />
                    )}
                </div>
            </button>

            {/* Output */}
            {isExpanded && (hasOutput || hasError || hasImages) && (
                <div className="relative">
                    {(hasOutput || hasError) && (
                        <div className="absolute right-2 top-2 z-10">
                            <button
                                onClick={handleCopy}
                                className="p-1.5 rounded bg-zinc-800/80 hover:bg-zinc-700 transition-colors"
                                title="Copy output"
                            >
                                {copied ? (
                                    <Check className="w-3.5 h-3.5 text-emerald-400" />
                                ) : (
                                    <Copy className="w-3.5 h-3.5 text-zinc-400" />
                                )}
                            </button>
                        </div>
                    )}

                    <div className={clsx(
                        'p-3 font-mono text-sm',
                        result.success ? 'bg-zinc-900/50' : 'bg-zinc-900/50'
                    )}>
                        {hasError && (
                            <div className="flex items-start gap-2 mb-3 text-red-400">
                                <AlertTriangle className="w-4 h-4 mt-0.5 flex-shrink-0" />
                                <pre className="whitespace-pre-wrap break-words text-red-300">{result.error}</pre>
                            </div>
                        )}

                        {hasOutput && (
                            <pre className={clsx(
                                'whitespace-pre-wrap break-words mb-3',
                                result.hasCircuitOutput ? 'text-teal-300' : 'text-zinc-300'
                            )}>
                                {result.output}
                            </pre>
                        )}

                        {!hasOutput && !hasError && !hasImages && result.success && (
                            <span className="text-zinc-500 italic">
                                Code executed successfully with no output
                            </span>
                        )}

                        {/* Display generated images */}
                        {hasImages && (
                            <ImageViewer images={result.images!} />
                        )}
                    </div>
                </div>
            )}
        </div>
    );
}