mirror of
https://github.com/massbug/judge4c.git
synced 2025-05-18 15:26:33 +00:00
feat(components/run-code): add code execution with loading state
This commit is contained in:
parent
8069df5973
commit
04794a34c7
@ -1,6 +1,11 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
import { PlayIcon } from "lucide-react";
|
import { useState } from "react";
|
||||||
|
import { judge } from "@/app/actions/judge";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { LoaderCircleIcon, PlayIcon } from "lucide-react";
|
||||||
|
import { useCodeEditorState } from "@/store/useCodeEditor";
|
||||||
|
|
||||||
interface RunCodeProps {
|
interface RunCodeProps {
|
||||||
className?: string;
|
className?: string;
|
||||||
@ -10,14 +15,44 @@ export default function RunCode({
|
|||||||
className,
|
className,
|
||||||
...props
|
...props
|
||||||
}: RunCodeProps) {
|
}: RunCodeProps) {
|
||||||
|
const { language, editor } = useCodeEditorState();
|
||||||
|
const [isLoading, setIsLoading] = useState<boolean>(false);
|
||||||
|
|
||||||
|
const handleJudge = async () => {
|
||||||
|
if (!editor) return;
|
||||||
|
|
||||||
|
const code = editor.getValue() || "";
|
||||||
|
setIsLoading(true);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const judgeResult = await judge(language, code);
|
||||||
|
console.log(judgeResult);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error occurred while judging the code:");
|
||||||
|
console.error(error);
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Button
|
<Button
|
||||||
{...props}
|
{...props}
|
||||||
variant="secondary"
|
variant="secondary"
|
||||||
className={cn("h-8 px-3 py-1.5", className)}
|
className={cn("h-8 px-3 py-1.5", className)}
|
||||||
|
onClick={handleJudge}
|
||||||
|
disabled={isLoading}
|
||||||
>
|
>
|
||||||
|
{isLoading ? (
|
||||||
|
<LoaderCircleIcon
|
||||||
|
className="-ms-1 opacity-60 animate-spin"
|
||||||
|
size={16}
|
||||||
|
aria-hidden="true"
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
<PlayIcon className="-ms-1 opacity-60" size={16} aria-hidden="true" />
|
<PlayIcon className="-ms-1 opacity-60" size={16} aria-hidden="true" />
|
||||||
Run
|
)}
|
||||||
|
{isLoading ? "Running..." : "Run"}
|
||||||
</Button>
|
</Button>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user