2025-03-01 09:12:32 +00:00
|
|
|
"use client";
|
|
|
|
|
2025-02-26 13:54:44 +00:00
|
|
|
import { cn } from "@/lib/utils";
|
2025-03-01 09:12:32 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
import { judge } from "@/app/actions/judge";
|
2025-02-26 13:54:44 +00:00
|
|
|
import { Button } from "@/components/ui/button";
|
2025-03-01 09:12:32 +00:00
|
|
|
import { LoaderCircleIcon, PlayIcon } from "lucide-react";
|
2025-03-03 06:20:25 +00:00
|
|
|
import { useCodeEditorStore } from "@/store/useCodeEditorStore";
|
2025-03-13 02:48:01 +00:00
|
|
|
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "./ui/tooltip";
|
2025-02-26 13:54:44 +00:00
|
|
|
|
|
|
|
interface RunCodeProps {
|
|
|
|
className?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function RunCode({
|
|
|
|
className,
|
|
|
|
...props
|
|
|
|
}: RunCodeProps) {
|
2025-03-03 06:20:25 +00:00
|
|
|
const { language, editor, setResult } = useCodeEditorStore();
|
2025-03-01 09:12:32 +00:00
|
|
|
const [isLoading, setIsLoading] = useState<boolean>(false);
|
|
|
|
|
|
|
|
const handleJudge = async () => {
|
|
|
|
if (!editor) return;
|
|
|
|
|
|
|
|
const code = editor.getValue() || "";
|
|
|
|
setIsLoading(true);
|
|
|
|
|
|
|
|
try {
|
2025-03-01 13:23:44 +00:00
|
|
|
const result = await judge(language, code);
|
|
|
|
setResult(result);
|
2025-03-01 09:12:32 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error("Error occurred while judging the code:");
|
|
|
|
console.error(error);
|
|
|
|
} finally {
|
|
|
|
setIsLoading(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-02-26 13:54:44 +00:00
|
|
|
return (
|
2025-03-13 02:48:01 +00:00
|
|
|
<TooltipProvider delayDuration={0}>
|
|
|
|
<Tooltip>
|
|
|
|
<TooltipTrigger asChild>
|
|
|
|
<Button
|
|
|
|
{...props}
|
|
|
|
variant="secondary"
|
|
|
|
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" />
|
|
|
|
)}
|
|
|
|
{isLoading ? "Running..." : "Run"}
|
|
|
|
</Button>
|
|
|
|
</TooltipTrigger>
|
|
|
|
<TooltipContent className="px-2 py-1 text-xs">Run Code</TooltipContent>
|
|
|
|
</Tooltip>
|
|
|
|
</TooltipProvider>
|
2025-02-26 13:54:44 +00:00
|
|
|
);
|
|
|
|
}
|