-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-output.tsx
More file actions
70 lines (65 loc) · 2.07 KB
/
code-output.tsx
File metadata and controls
70 lines (65 loc) · 2.07 KB
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
"use client";
import { useState } from "react";
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Play } from "lucide-react";
import { runCode } from "@/ai/flows/run-code-flow";
type CodeOutputProps = {
code: string;
language: string;
};
export default function CodeOutput({ code, language }: CodeOutputProps) {
const [output, setOutput] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(false);
const [isOpen, setIsOpen] = useState(false);
const handleExecute = async () => {
setIsLoading(true);
setError(null);
setOutput(null);
setIsOpen(true);
try {
const result = await runCode({ code, language });
setOutput(result.output);
setError(result.error);
} catch (e: any) {
setError(e.message || "An unexpected error occurred.");
} finally {
setIsLoading(false);
}
};
return (
<Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogTrigger asChild>
<Button onClick={handleExecute}>
<Play className="mr-2" />
Execute Code
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-2xl">
<DialogHeader>
<DialogTitle className="font-headline text-2xl">Code Output</DialogTitle>
</DialogHeader>
<div className="mt-4 bg-muted rounded-lg p-4 font-code text-sm max-h-[60vh] overflow-auto">
{isLoading ? (
<div className="flex items-center justify-center h-24">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary"></div>
</div>
) : (
<pre className="whitespace-pre-wrap">
{error && <code className="text-red-500">{error}</code>}
{output && <code>{output}</code>}
{!error && !output && <code>No output.</code>}
</pre>
)}
</div>
</DialogContent>
</Dialog>
);
}