|
| 1 | +"use client"; |
| 2 | +import { |
| 3 | + Card, |
| 4 | + CardContent, |
| 5 | + CardDescription, |
| 6 | + CardHeader, |
| 7 | + CardTitle, |
| 8 | +} from "@/components/ui/card"; |
| 9 | +import { Clock, Code2, Play } from "lucide-react"; |
| 10 | +import LanguageSelection from "./editor-view/language-selection"; |
| 11 | +import { Button } from "@/components/ui/button"; |
| 12 | +import { ExecutionResult, Language, Output } from "@/@types"; |
| 13 | +import { useState, useTransition } from "react"; |
| 14 | +import { executeCodeAction } from "../_actions/execute"; |
| 15 | +import { CodeEditor } from "./editor-view/code-editor"; |
| 16 | +import ResultSection from "./output-view/result-sections"; |
| 17 | + |
| 18 | +export default function CodeInput() { |
| 19 | + const [selectedLanguage, setSelectedLanguage] = |
| 20 | + useState<Language>("typescript"); |
| 21 | + const [code, setCode] = useState(""); |
| 22 | + const [executionResult, setExecutionResult] = |
| 23 | + useState<ExecutionResult | null>(null); |
| 24 | + const [isExecuting, startExecution] = useTransition(); |
| 25 | + |
| 26 | + const executeCode = async () => { |
| 27 | + if (!code.trim()) return; |
| 28 | + startExecution(async () => { |
| 29 | + setExecutionResult({ |
| 30 | + status: "running", |
| 31 | + results: { |
| 32 | + output: "", |
| 33 | + responseTime: 0, |
| 34 | + }, |
| 35 | + language: selectedLanguage, |
| 36 | + }); |
| 37 | + try { |
| 38 | + const data = await executeCodeAction({ |
| 39 | + language: selectedLanguage, |
| 40 | + code, |
| 41 | + }); |
| 42 | + setExecutionResult({ |
| 43 | + status: "success", |
| 44 | + results: { |
| 45 | + output: data.output, |
| 46 | + responseTime: data.responseTime, |
| 47 | + }, |
| 48 | + language: selectedLanguage, |
| 49 | + }); |
| 50 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 51 | + } catch (err: any) { |
| 52 | + const error = JSON.parse(err.message) as Output; |
| 53 | + setExecutionResult({ |
| 54 | + status: "error", |
| 55 | + results: { |
| 56 | + output: error.output, |
| 57 | + responseTime: error.responseTime, |
| 58 | + }, |
| 59 | + language: selectedLanguage, |
| 60 | + }); |
| 61 | + } |
| 62 | + }); |
| 63 | + }; |
| 64 | + return ( |
| 65 | + <> |
| 66 | + <Card className="h-fit bg-white/5 backdrop-blur-sm border border-white/10 text-white"> |
| 67 | + <CardHeader className="flex justify-between"> |
| 68 | + <div> |
| 69 | + <CardTitle className="flex items-center gap-2 text-white"> |
| 70 | + <Code2 className="w-6 h-6 text-purple-400" /> |
| 71 | + Code Editor |
| 72 | + </CardTitle> |
| 73 | + <CardDescription className="text-gray-300"> |
| 74 | + Write or upload your code with full syntax highlighting |
| 75 | + </CardDescription> |
| 76 | + </div> |
| 77 | + {/* Language Selection */} |
| 78 | + <LanguageSelection |
| 79 | + selectedLanguage={selectedLanguage} |
| 80 | + setSelectedLanguage={setSelectedLanguage} |
| 81 | + /> |
| 82 | + </CardHeader> |
| 83 | + <CardContent className="space-y-4"> |
| 84 | + <CodeEditor |
| 85 | + value={code} |
| 86 | + onChange={setCode} |
| 87 | + language={selectedLanguage} |
| 88 | + height="310px" |
| 89 | + /> |
| 90 | + |
| 91 | + <Button |
| 92 | + onClick={executeCode} |
| 93 | + disabled={isExecuting || !code.trim()} |
| 94 | + className="w-full bg-gradient-to-r from-purple-600 to-pink-600 text-white hover:from-purple-700 hover:to-pink-700 transition-all duration-300 transform hover:scale-105 shadow-lg" |
| 95 | + size="lg" |
| 96 | + > |
| 97 | + {isExecuting ? ( |
| 98 | + <> |
| 99 | + <Clock className="w-5 h-5 mr-2 animate-spin" /> |
| 100 | + Executing... |
| 101 | + </> |
| 102 | + ) : ( |
| 103 | + <> |
| 104 | + <Play className="w-5 h-5 mr-2" /> |
| 105 | + Execute Code |
| 106 | + </> |
| 107 | + )} |
| 108 | + </Button> |
| 109 | + </CardContent> |
| 110 | + </Card> |
| 111 | + <ResultSection optional={executionResult} isExecuting={isExecuting} /> |
| 112 | + </> |
| 113 | + ); |
| 114 | +} |
0 commit comments